Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR2 with class declarations extending classes with namespaces in PHP Code Sniffer

I've hit a problem with PHPCS using the PSR2 standard. Have searched high and low but to my surprise I can't find anyone reporting the same issue.

Say I have a class declaration as follows:

<?php

class MyChildClass extends \SomeNameSpace\MyParentClass
{
}

Then I run it through PHPCS with:

bash-3.2$ phpcs -s  --standard=PSR2 test.php 

FILE: test.php
--------------------------------------------------------------------------------
FOUND 2 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 3 | ERROR | Expected 0 spaces between "SomeNameSpace" and comma; $1 found
   |       | (PSR2.Classes.ClassDeclaration.SpaceBeforeComma)
 3 | ERROR | Expected 1 space before "MyParentClass"; 13 found
   |       | (PSR2.Classes.ClassDeclaration.SpaceBeforeName)
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 4.00Mb

Also:

Bash-3.2$ phpcs --version
PHP_CodeSniffer version 1.3.6 (stable) by Squiz Pty Ltd. (http://www.squiz.net)

Has anyone come across this? Am I doing something wrong? Otherwise I'm going head first into the sniffer code - which doesn't feel right.

like image 345
oprimus Avatar asked Sep 20 '12 23:09

oprimus


1 Answers

The PSR-1 and PSR-2 standard inside the current release of PHP_CodeSniffer are not complete. I didn't ever mention them in the release notes so people obviously either just found them, or they are talking about the current dev version, where they are complete.

If you want to try out the complete version of PSR-2 inside PHP_CodeSniffer, you'll need to clone the git repo and use it directly:

git clone git://github.com/squizlabs/PHP_CodeSniffer.git
cd PHP_CodeSniffer
php scripts/phpcs --standard=PSR2 /path/to/code

Or you can wait for the official release, which I'm planning for sometime next week, assuming no major issues are reported.

If you run the latest dev version on the code you supplied, you'll get this:

2:PHP_CodeSniffer gsherwood$ php scripts/phpcs --standard=psr2 temp.php

FILE: /Users/gsherwood/Sites/Projects/PHP_CodeSniffer/temp.php
--------------------------------------------------------------------------------
FOUND 2 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
 3 | ERROR | Each class must be in a namespace of at least one level (a
   |       | top-level vendor name)
 5 | ERROR | Expected 1 blank line at end of file; 0 found
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 4.25Mb

Hope that helps.

like image 77
Greg Sherwood Avatar answered Sep 30 '22 02:09

Greg Sherwood