Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.1

Bug #19695 PSR-1 Namespace sniff - false positive for PHP5.2 style namespacing
Submitted: 2012-11-07 13:56 UTC
From: zygous Assigned: squiz
Status: Bogus Package: PHP_CodeSniffer (version SVN)
PHP Version: 5_4 SVN-2012-11-07 OS: Linux
Roadmaps: (Not assigned)    
Subscription  


 [2012-11-07 13:56 UTC] zygous (Richard Turner)
Description: ------------ Given a class written for PHP 5.2, with a name that incorporates a namespace, PHPCS complains that "each class must be in a namespace of at least one level". E.g. class Vendor_Mvc_Controller_Index is, according to PSR-0, equivalent to \Vendor\Mvc\Controller\Index in PHP5.3+ code, so PHPCS shouldn't complain about namespacing.

Comments

 [2012-11-08 02:44 UTC] squiz (Greg Sherwood)
-Status: Open +Status: Feedback -Assigned To: +Assigned To: squiz
The reason you are seeing that error is because you are running PHPCS using a version of PHP that supports namespaces. PSR-1 states that if code is written for 5.3 and after then is MUST use namespaces. But there is nothing that will actually tell me what version of PHP your code is supposed to support. The only thing I can check is the PHP version. So my problem when doing this sniff was that I have to throw an error (the use of MUST means error, not warning) if you are using old-style namespacing for code meant to run under PHP 5.3+. If I see old-style namespacing and I don't throw an error, I could be hiding a really important error message and that would probably be a bigger mistake. If you know that you'll always be running PHP_CodeSniffer using PHP 5.3+, you can write your own standard that mutes this particular error message. Or you can run PHP_CodeSniffer using the lowest PHP version that your code supports (seems like PHP 5.2 in this case). If you want to create your own standard, create a file somewhere called (for example) mystandard.xml and make the contents: <?xml version="1.0"?> <ruleset name="MyStandard"> <description>My custom coding standard.</description> <rule ref="PSR2"/> <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace"> <severity>0</severity> </rule> </ruleset> Then you can run PHP_CodeSniffer like this: phpcs --standard=/path/to/mystandard.xml /path/to/code
 [2012-11-08 17:19 UTC] zygous (Richard Turner)
Yep, that all makes sense. It's a shame there's no PHPDoc tag to specify a minimum required PHP version that PHPCS could check. Having to maintain a custom set of sniffs would be a bit of a nuisance. I guess this report can be resolved as not-a-bug then.
 [2012-11-09 04:18 UTC] squiz (Greg Sherwood)
-Status: Feedback +Status: Bogus
Yeah, it would be good if the PSR standards said something about documenting the min PHP version. PEAR has a requirement for this inside the file comment on each PHP file, but documentation is not covered at all by the PSRs. Hopefully this will change one day, but maybe PHP5.2 support will be dropped before then anyway. THe PSRs seems to move a little slowly. Wish I had a Not a Bug status, but I only have Bogus. Please don't be offended.
 [2012-11-09 13:31 UTC] zygous (Richard Turner)
Would it be worth honouring the PEAR comment if that's present? From what I've read of the PSRs I don't think that would be in contravention, would it? Any means necessary to identify the target PHP version... We have a lot of code written before PHP 5.3 that we're now running on 5.3 servers. It all works, so there's no reason to change namespacing, and I believe it's PSR compliant in all other regards. It wasn't written for 5.3, so needn't use 'real' namespaces. I know the mailing list is fed-up of discussing. PSRs 1 and 2, but perhaps iI should raise this on the list... I'm not offended by the bogus status :-) Oh, and good job on the sniffs!
 [2012-11-12 03:21 UTC] squiz (Greg Sherwood)
Yes, the mailing list for the PHP FIG seems like it doesn't like receiving mail, which is a little odd given they have released a common PHP coding standard to the community. A great thing, but you're obviously going to get feedback. Still, I'd ask there because it isn't the sort of call I could make as I'm not a voting member. If the authors of the standard decide to relax things and just say that both methods of namespacing are fine, then it's just a little bit of code to remove.
 [2013-01-07 16:58 UTC] njh (Nicholas Humfrey)
Just a note that this is a problem for me too. My library (EasyRdf) is still being used by people running PHP 5.2 and people using old style name-spacing on PHP 5.3. Would it be possible to work something into the test that says if class contains underscores but the path matches it, then it is ok? Something like: implode(DIRECTORY_SEPARATOR, explode('_', 'Foo_Bar')).'.php' == __FILE__.
 [2013-01-08 01:42 UTC] squiz (Greg Sherwood)
The problem is that if you have recently bumped your PHP requirement to 5.3 and you still use 5.2-style class names, the sniff would not report any issues. Similarly, if you just decided to start a new project and not use namespaces, the sniff would still not report errors. Without some way of knowing exactly what version of PHP your project requires, the sniff can't ever really know what you should be using. The version of PHP used to run PHPCS is the closest I have to a version number without the PSR's defining some sort of requirements file or file comment line (like PEAR does).