Bug #19348 [Fbk->WFx]: Missing function parameters in AbstractSniffUnitTest

From: Date: Mon, 26 Mar 2012 03:07:08 +0000
Subject: Bug #19348 [Fbk->WFx]: Missing function parameters in AbstractSniffUnitTest
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-8446@lists.php.net to get a copy of this message
Edit report at https://pear.php.net/bugs/bug.php?id=19348&edit=1 ID: 19348 Updated by: gsherwood@squiz.net Reported By: jnrbsn at gmail dot com Summary: Missing function parameters in AbstractSniffUnitTest -Status: Feedback +Status: Wont fix Type: Bug Package: PHP_CodeSniffer Operating System: Mac OS 10.7.3 "Lion" Package Version: 1.3.3 PHP Version: 5.3.8 Assigned To: squiz Roadmap Versions: New Comment: -Status: Feedback +Status: Wont fix Previous Comments: ------------------------------------------------------------------------ [2012-03-26 04:06:40] squiz Yeah, it's interesting that specifying a default value stops the error. It's obviously why I've missed the incorrect abstract method definition for so long. Reading the docs, I would have though it would have caused an error. You were right about breaking backwards compatibility in your first comment though. If I do fix the argument list in the abstract method, I will break all unit tests that are not using that argument, which is most. That's not a problem for the ones that PHPCS ships with, but it may be for other developers. I think it's best to leave it, unless a future PHP version ever enforces this specific case and throws errors. But 5.4 doesn't seem to, so I suspect it either isn't going to be an issue, or it will be easy to accept because developers will be updating various bits of software when PHP versions change. Thanks for bringing it to my attention. At least I know why it is working now. I'm going to close the bug report because of the potential B/C break that would occur if I fixed this. ------------------------------------------------------------------------ [2012-03-26 03:04:43] jnrbsn To answer your question, I'm running the tests using this command: phpunit PHP_CodeSniffer_AllTest /usr/lib/php/pear/tests/PHP_CodeSniffer/AllTests.php Sorry about the confusion. I just couldn't understand how you could not be getting the same error if you were testing it the same way. Again, sorry. But I figured out the difference. In all of the instances where you guys have used that parameter, you've specified a default value (like in the example you linked to), which works for me. I didn't realize this at first. I still think, ideally, it should be defined in the abstract signature for better visibility of this feature, but since there's an easy workaround (specifying a default value), I guess it's arguable whether or not this is a "bug" as I originally thought. ------------------------------------------------------------------------ [2012-03-26 02:48:32] jnrbsn To clarify, solution #1 would be a change to my code, and solutions #2 and #3 would be changes to PHP_CodeSniffer code. Also, another possible solution would be for me to make my own fork of the AbstractSniffUnitTest class. ------------------------------------------------------------------------ [2012-03-26 02:48:13] squiz Yes, I'm well aware of this issue. But I've never got any errors running the test suite and I have differing signatures in some, but not all, sniff unit tests. I asked how you were running them because I have no idea why I am not getting this error, and why I don't see it on my build systems. Here is an example file: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/G eneric/Tests/Commenting/FixmeUnitTest.php I used this extensively throughout the test suite, which is why I asked how you are running it. I have done this for quite a while on many different PHP versions, but never realised I forgot to change the abstract method. That's all my comment was asking and why I was asking it. I haven't had a chance to play around with the code and figure out what is wrong. Maybe it is a php.ini setting. Maybe it is the use of the default value for the argument. I really don't know. But it you can run the test suite as it stands right now, you're already running a lot of code that does what you want to do. ------------------------------------------------------------------------ [2012-03-26 02:12:32] jnrbsn Please forgive me, but are you sure you understand the error that I'm getting? The signature of a method that inherits from an abstract method must have the same number of arguments as its parent. From the "Class Abstraction" page in the PHP manual: "When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ." The signature of the method "AbstractSniffUnitTest::getErrorList()" looks likes this: protected abstract function getErrorList(); As you can see, it's not declared with any arguments. However, when "getErrorList()" is called by "generateFailureMessages()", it passes the basename of the test file as an argument. (On a side note, it took me a really long time to discover this "feature" because it doesn't seem to be documented anywhere, like in the signature of "getErrorList()" as one might expect.) There's nothing technically wrong with that part of it, because PHP allows you to pass more arguments than what's in the function signature (which are retrievable via "func_get_args()"). The problem is that I want my method (the one that inherits from the abstract one) to look like this: public function getErrorList($testFile) { /* my code here... */ } So that I can use the name of the test file to return a specific set of line numbers/errors for that specific file. This technique is obviously preferred over calling "func_get_args()". Does that make sense? So the solutions to this (or at least the ones I can think of) are as follows: 1. Use "func_get_args()" as I described above. 2. Change the signature of "AbstractSniffUnitTest::getErrorList()" (and "getWarningList()") to include the test file as an argument. This is problematic because it would break a lot of people's unit tests, because all of the sudden, the abstract method would define one argument, but child methods would define zero, breaking PHP's rules for abstract method inheritance and triggering the fatal error. The big benefit of this is that people would see that the name of test file is available to them in their tests, allowing for more robust tests. 3. Make change #2 but also make the methods non-abstract. The only downside with this is that it would break things if someone writing unit tests didn't override them. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://pear.php.net/bugs/bug.php?id=19348 -- Edit this bug report at https://pear.php.net/bugs/bug.php?id=19348&edit=1

« previous php.pear.bugs (#8446) next »