Personally, I don't like these limitations in PHPUnit and the like. IMHO,
they should never be a reason for changing good code. And sometimes,
methods have to be final. I don't know about the specific case, though.
Anyway, some time ago I came across [1], which allows mocking final methods
and classes. IIRC, it does that by removing the `final` keywords from the
tokenized PHP code. I don't know how well it works, nor if it could degrade
performance, but if it doesn't we could bring it in via composer.
[1] -
https://github.com/dg/bypass-finals
Il giorno martedì 27 agosto 2019, Aryeh Gregor <ayg(a)aryeh.name> ha scritto:
I see that in some classes, like WANObjectCache, most
methods are declared
final. Why is this? Is it an attempt to optimize?
The problem is that PHPUnit mocks can't touch final methods. Any ->method()
calls that try to do anything to them silently do nothing. This makes
writing tests harder.
If we really want these methods to be marked final for some reason, the
workaround for PHP is to make an interface that has all the desired
methods, have the class implement the interface, and make type hints all
refer to the interface instead of the class. But if there's no good reason
to declare the methods final to begin with, it's simplest to just drop it.
_______________________________________________
Wikitech-l mailing list
Wikitech-l(a)lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l
--
https://meta.wikimedia.org/wiki/User:Daimona_Eaytoy
"Daimona" is not my real name -- he/him