PHP_CodeSniffer support for PSR-1 and PSR-2 (In Progress)

3,586 views
Skip to first unread message

Greg Sherwood

unread,
Jun 6, 2012, 8:38:40 PM6/6/12
to php-st...@googlegroups.com
I've just pushed the first bit of PHP_CodeSniffer's PSR-1/2 support to the Github repo: https://github.com/squizlabs/PHP_CodeSniffer

Both standards are currently in ruleset.xml form and contain no custom sniffs. So they are easy to review, which you can do here:

By the look of things, a lot of code still needs to be written to enforce these standards. I don't think I can pull too much more from the existing sniff pool in PHP_CodeSniffer. You'll notice that PSR-1 is particularly bare.

This looks like it's going to be a big job so please let me know if you are able to help, either by writing whole new sniffs, testing what is already there, or finding an existing sniff that meets some of the standard.

The best way to get access to these new standards for testing is to clone the Github repo and run PHPCS directly from the checkout. This also wont impact any existing PHPCS install you have. To do this:
cd PHP_CodeSniffer
php scripts/phpcs --standard=PSR-1 /path/to/code
php scripts/phpcs --standard=PSR-2 /path/to/code

Note that there is currently a discussion around if camel case method names are allowed to contain two uppercase characters in a row: https://groups.google.com/d/topic/php-standards/rGopcf4WyyU/discussion

The PSR-1 standard in PHP_CodeSniffer is currently set to the strictest setting and will not allow two uppercase characters in a row. This is an easy change in the ruleset.xml file though, so I will make sure it reflects whatever the decision ultimately is.

Bryan Geraghty

unread,
Jun 6, 2012, 10:00:14 PM6/6/12
to php-st...@googlegroups.com
Go Squiz!

I've written my own custom sniffs for my framework, so I'll be happy to help in my "down" time.

Bryan

Hari K T

unread,
Jun 6, 2012, 10:04:52 PM6/6/12
to php-st...@googlegroups.com
Great man! 
I will try running tests :-) .

Hari K T
M: +91-9388758821 | W: http://harikt.com/blog




--
You received this message because you are subscribed to the Google Groups "PHP Standards Working Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/php-standards/-/SwB0_iq4AzwJ.

To post to this group, send email to php-st...@googlegroups.com.
To unsubscribe from this group, send email to php-standard...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/php-standards?hl=en.

Greg Sherwood

unread,
Jun 7, 2012, 12:38:05 AM6/7/12
to php-st...@googlegroups.com
I had to change the name of the standards because the class names would not match the directory names properly due to the dash not being allowed.

So now, please find the ruleset files here:
Both standards are currently in ruleset.xml form and contain no custom sniffs. So they are easy to review, which you can do here:
PSR-1: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR1/ruleset.xml
PSR-2: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml

And run them using:
php scripts/phpcs --standard=PSR1 /path/to/code
php scripts/phpcs --standard=PSR2 /path/to/code

Drak

unread,
Jun 7, 2012, 2:17:44 AM6/7/12
to php-st...@googlegroups.com
On 7 June 2012 06:23, Greg Sherwood <gshe...@gmail.com> wrote:
By the look of things, a lot of code still needs to be written to enforce these standards. I don't think I can pull too much more from the existing sniff pool in PHP_CodeSniffer. You'll notice that PSR-1 is particularly bare.

Actually not, Squiz has most of this already covered. In fact, I think it has all all of it covered between Squiz and Generic and PEAR (you can mix match rules). I did something a while back here


Regards,

Drak

Greg Sherwood

unread,
Jun 7, 2012, 8:13:52 AM6/7/12
to php-st...@googlegroups.com
On Thursday, 7 June 2012 16:17:44 UTC+10, Drak wrote:
On 7 June 2012 06:23, Greg Sherwood <gshe...@gmail.com> wrote:
By the look of things, a lot of code still needs to be written to enforce these standards. I don't think I can pull too much more from the existing sniff pool in PHP_CodeSniffer. You'll notice that PSR-1 is particularly bare.

Actually not, Squiz has most of this already covered. In fact, I think it has all all of it covered between Squiz and Generic and PEAR (you can mix match rules). I did something a while back here

I've taken a good look through all the sniffs I've written in the past and I'm pretty confident that they wont fit the PSR standards in many cases. Some do, and I've used those where I can because I don't want to duplicate code. But many enforce different rules. I personally wrote the Squiz standard and use it every day at work (I work at Squiz, the company) and I know we don't enforce a lot of what is in the PSR standards. We enforce a heap more and some of the same things, but we don't have hard rules around things like namespaces, order of keywords, elseif, closures, intent of code etc. So sniffs for all those need to be written (and more) and I am certainly willing to do this. Maybe my definition of "a lot of code" is different to yours :)

I've put the enforceable parts of the PSR standards into their ruleset.xml files (linked previously) as comments and the sniffs that I think meet those points under each, where available. If you know of any sniffs that meet one of those points in the standard that no sniffs are defined for, please let me know what they are and I'll review them (even if they do slightly more than is required). Otherwise, I'll just go through them one by one. I've already written a sniff for basic camel case checking (minus all the rules about underscores) and the first PSR2-specific one for the requirement to have a blank line at the end of PHP files, so I'm getting there.

Greg Sherwood

unread,
Jun 7, 2012, 8:18:06 AM6/7/12
to php-st...@googlegroups.com
On Thursday, 7 June 2012 16:17:44 UTC+10, Drak wrote:
Sorry, I forgot to say thanks for posting this link. Sorry if I'm telling you something you already know, but this is the old coding standard definition and I have since changed PHP_CodeSniffer to use an XML-based ruleset format to define coding standards rather than the old class method. This provides a lot of flexibility around including specific sniffs (as in the past) but also the ability to change messages, mute errors, change settings for sniffs etc.

If you or anyone else if planning on helping out, a good doc to check out is the annotated ruleset.xml file. It will show the various options that the format provides, along with a few I've already had to use in the PSR1 and 2 ruleset files:

Drak

unread,
Jun 7, 2012, 8:40:22 AM6/7/12
to php-st...@googlegroups.com
On 7 June 2012 18:03, Greg Sherwood <gshe...@gmail.com> wrote:
On Thursday, 7 June 2012 16:17:44 UTC+10, Drak wrote:
Sorry, I forgot to say thanks for posting this link. Sorry if I'm telling you something you already know, but this is the old coding standard definition and I have since changed PHP_CodeSniffer to use an XML-based ruleset format to define coding standards rather than the old class method. This provides a lot of flexibility around including specific sniffs (as in the past) but also the ability to change messages, mute errors, change settings for sniffs etc.

Yes, it's here too. https://github.com/drak/phpcs-zikula/blob/master/Zikula/ruleset.xml  While this is not all the PSR list, it is already most of them.
 
Drak

Greg Sherwood

unread,
Jun 7, 2012, 6:11:56 PM6/7/12
to php-st...@googlegroups.com
On Thursday, 7 June 2012 22:40:22 UTC+10, Drak wrote:

Yes, it's here too. https://github.com/drak/phpcs-zikula/blob/master/Zikula/ruleset.xml  While this is not all the PSR list, it is already most of them.

Thanks again. Looks like we've come to the same conclusion about many of the sniffs. I've used a lot of these in the current PSR rulesets as well.

Paul Dragoonis

unread,
Jun 8, 2012, 5:21:11 AM6/8/12
to php-st...@googlegroups.com
Good job guys.

Does the PSR1 sniffer cover all PSR1 use cases? (is it ready)

- Paul.
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Standards Working Group" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/php-standards/-/jVtqsLX7bsUJ.

Greg Sherwood

unread,
Jun 8, 2012, 6:12:06 AM6/8/12
to php-st...@googlegroups.com
Niether the PSR1 or PSR2 standards I have committed are complete. They are both very much still a work in progress. So this one isn't ready yet and nobody has sent me a ruleset for PSR1 that looks 100% complete, so I'm not aware of a properly tested complete standard at the moment.

On Friday, 8 June 2012 19:21:11 UTC+10, Paul Dragoonis wrote:
Good job guys.

Does the PSR1 sniffer cover all PSR1 use cases? (is it ready)

- Paul.

On Thu, Jun 7, 2012 at 11:11 PM, Greg Sherwood <gshe...@gmail.com> wrote:
> On Thursday, 7 June 2012 22:40:22 UTC+10, Drak wrote:
>>
>>
>> Yes, it's here
>> too. https://github.com/drak/phpcs-zikula/blob/master/Zikula/ruleset.xml  While
>> this is not all the PSR list, it is already most of them.
>
>
> Thanks again. Looks like we've come to the same conclusion about many of the
> sniffs. I've used a lot of these in the current PSR rulesets as well.
>
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Standards Working Group" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/php-standards/-/jVtqsLX7bsUJ.
>
> To post to this group, send email to php-st...@googlegroups.com.
> To unsubscribe from this group, send email to

Hari K T

unread,
Jun 11, 2012, 1:28:47 AM6/11/12
to php-st...@googlegroups.com
Hi Greg,

I recently saw the PHP CS by Klaus Silveira

https://github.com/klaussilveira/phpcs-psr

It has a ruleset

https://github.com/klaussilveira/phpcs-psr/blob/master/ruleset.xml

I have not tested with it . But I am not sure whether its fully correct without any bugs .

Thank you .
To view this discussion on the web visit https://groups.google.com/d/msg/php-standards/-/2Cz03dQ1hD0J.

To post to this group, send email to php-st...@googlegroups.com.
To unsubscribe from this group, send email to php-standard...@googlegroups.com.

Rafael Dohms

unread,
Jun 11, 2012, 9:07:24 AM6/11/12
to php-st...@googlegroups.com

On Monday, June 11, 2012 7:28:47 AM UTC+2, Hari K T wrote:
Hi Greg,

I recently saw the PHP CS by Klaus Silveira

https://github.com/klaussilveira/phpcs-psr


Is it me or do we have 2 different efforts on creating PSR-1/2 CS Sniffs?
If so, can we join the 2 projects and focus on having one solution and then pushing it to the official PEAR repo?
That will be the best way to consolidate this and make it go forward.
 

Kinn Julião

unread,
Jun 11, 2012, 12:33:36 PM6/11/12
to php-st...@googlegroups.com
I pulled the Greg's repo and it is works fine... PPI is running 100% under PSR1..
Just some details with camelCaps in getIP method... anyway, I think we can have a fix for this cases... it's better to see getIP instead of getIp.
It is not yet on PEAR, but since Greg is the maintainer of PHPCS, I think that he will do it asap :)

2012/6/11 Rafael Dohms <rdo...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "PHP Standards Working Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/php-standards/-/3si-Pgvf8L4J.

To post to this group, send email to php-st...@googlegroups.com.
To unsubscribe from this group, send email to php-standard...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/php-standards?hl=en.



--
---------------------------------------------------------
             Kinn Coelho Julião
            
                 kinncj.com.br
---------------------------------------------------------

Paul Dragoonis

unread,
Jun 11, 2012, 5:11:08 PM6/11/12
to php-st...@googlegroups.com
On Mon, Jun 11, 2012 at 5:33 PM, Kinn Julião <kin...@gmail.com> wrote:
I pulled the Greg's repo and it is works fine... PPI is running 100% under PSR1..
Just some details with camelCaps in getIP method... anyway, I think we can have a fix for this cases... it's better to see getIP instead of getIp.
It is not yet on PEAR, but since Greg is the maintainer of PHPCS, I think that he will do it asap :)

Thank you Kinn for making sure PPI2 is PSR1 complaint. There's only one warning which you mentioned as getIP(), but this (imo) is valid camelcase and I commonly have acronyms/abbreviations in uppercase, such as getDNS() or getID(), so we need to make sure the PSR sniffs support this form of camelcase.

Viva la PSR1 :-)

- Paul.

Greg Sherwood

unread,
Jun 11, 2012, 6:24:25 PM6/11/12
to php-st...@googlegroups.com
Hi Hari,

Klaus was kind enough to send me the ruleset.xml for review when I first opened this thread. I've done a review of that ruleset and the PSR1/2 standards and concluded that Klaus' ruleset does not fully check the PSR standards. It may enforce PSR0 (I haven't focused on that yet) but it definitely doesn't enforce all of PSR1 or 2 and some things may be incorrectly reported as well, due to the selection of sniffs.

It also doesn't allow the PSR standards to be checked independently, which is something I think should be available in PHPCS itself. 

Greg

Greg Sherwood

unread,
Jun 11, 2012, 6:25:56 PM6/11/12
to php-st...@googlegroups.com
Just a note that the PSR standards in the PHPCS repo are not complete yet. But thanks a lot for the feedback about your test run on the PPI code.

On Tuesday, 12 June 2012 02:33:36 UTC+10, Kinn Coelho Julião wrote:
I pulled the Greg's repo and it is works fine... PPI is running 100% under PSR1..
Just some details with camelCaps in getIP method... anyway, I think we can have a fix for this cases... it's better to see getIP instead of getIp.
It is not yet on PEAR, but since Greg is the maintainer of PHPCS, I think that he will do it asap :)

2012/6/11 Rafael Dohms <rdo...@gmail.com>

On Monday, June 11, 2012 7:28:47 AM UTC+2, Hari K T wrote:
Hi Greg,

I recently saw the PHP CS by Klaus Silveira

https://github.com/klaussilveira/phpcs-psr


Is it me or do we have 2 different efforts on creating PSR-1/2 CS Sniffs?
If so, can we join the 2 projects and focus on having one solution and then pushing it to the official PEAR repo?
That will be the best way to consolidate this and make it go forward.
 

--
You received this message because you are subscribed to the Google Groups "PHP Standards Working Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/php-standards/-/3si-Pgvf8L4J.

To post to this group, send email to php-st...@googlegroups.com.
To unsubscribe from this group, send email to php-standards+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/php-standards?hl=en.

Greg Sherwood

unread,
Jun 11, 2012, 6:30:29 PM6/11/12
to php-st...@googlegroups.com
Everything I am committing is going into the official PHPCS repo on github. There is no SVN repo in PEAR any more, but I still release PHPCS through PEAR as a package. I've already reviewed Klaus' ruleset and put the sniffs that I think are valid into the PSR1/2 rulesets, but there is more work to be done. I've recruited someone else at Squiz to help me write some sniffs in their spare time, so hopefully the process of getting the remaining points of the standard coded up should go a bit faster now.

Paul Dragoonis

unread,
Jun 11, 2012, 7:01:08 PM6/11/12
to php-st...@googlegroups.com
How many more rules need added to make sure the sniff is PSR1 compliant.

- Paul.
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Standards Working Group" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/php-standards/-/q9giIyybP-EJ.

Hari K T

unread,
Jun 11, 2012, 11:33:05 PM6/11/12
to php-st...@googlegroups.com
Thank you Greg for the faster response.

I / probably we :) ,  all are waiting for it :-) .

Thank you

--
You received this message because you are subscribed to the Google Groups "PHP Standards Working Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/php-standards/-/q9giIyybP-EJ.

Greg Sherwood

unread,
Jun 12, 2012, 4:08:59 AM6/12/12
to php-st...@googlegroups.com
If you take a look through the ruleset files, I've added comments for every point in PSR1/2 that I think can be enforced. For each, if there is no sniff listed under them and there is no comment saying that it has been implemented, it means it still needs to be done. So for each of these, either an existing sniff needs to be found that enforces this point correctly, or a new sniff needs to be written.

The ruleset files are here:
> php-standards+unsubscribe@googlegroups.com.

Rafael Dohms

unread,
Jun 20, 2012, 7:59:08 AM6/20/12
to php-st...@googlegroups.com
 Greg,

Any status update on the CS Scripts?
If it still needs work, would be cool to get a heading so we can incentivate people to finalize it.

Thanks!

Greg Sherwood

unread,
Jun 20, 2012, 6:47:11 PM6/20/12
to php-st...@googlegroups.com
Still working on them when I get time, although I had to move onto some generic PHPCS Windows bugs this week so haven't progressed too far. Keep an eye on the ruleset.xml files for progress. I'm currently working my way through PSR-2 and updating the ruleset.xml file with comments about what is done. Last change was to implement all the class/interface definition rules. Still a fair bit to go though.

https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml

On the plus side, this version now installs, so you can either run it from the git clone or:
pear uninstall php_codesniffer
pear install package.xml

If you do that, please make sure you uninstall phpcs before upgrading from PEAR or re-installing from a new git clone. Use the same 2 commands as above to do this.

Greg Sherwood

unread,
Aug 7, 2012, 2:43:26 AM8/7/12
to php...@googlegroups.com, php-st...@googlegroups.com
It's been 2 months since I started (sorry, work has been really busy) but I've finally pushed the last bits of the PSR-2 standard to the PHP_CodeSniffer Github repo.


But PSR-2 includes the entire PSR-1 standard, and I haven't completed PSR-1 yet. So if you check your code using the PSR-2 standard (--standard=PSR2) you will only be checking against the specific additions that PSR-2 provides. So technically, it is not complete.

But the reason I'm providing the update is because all those extra checks that are now coded into PHPCS for the PSR-2 standard are at a point where I think they should work. So now is a great time to test if you are able to.

See below for how to get started.

Thanks to all those who have already been testing and submitting bug reports. You can continue to send me pull requests or report issues here: https://pear.php.net/bugs/report.php?package=PHP_CodeSniffer


On Thursday, 7 June 2012 10:38:40 UTC+10, Greg Sherwood wrote:

The best way to get access to the new standards for testing is to clone the Github repo and run PHPCS directly from the checkout. This also wont impact any existing PHPCS install you have. To do this:
cd PHP_CodeSniffer
php scripts/phpcs --standard=PSR2 /path/to/code

Hari K T

unread,
Aug 7, 2012, 4:39:14 AM8/7/12
to php...@googlegroups.com
Hey Greg ,

Good work on it , we are testing it on https://github.com/auraphp for a while .

The only thing I don't love is the bug track via PEAR . Its horrible man ... Please consider to make it on github issue itself .

Thanks

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To post to this group, send email to php...@googlegroups.com.
To unsubscribe from this group, send email to php-fig+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/php-fig/-/p5pGU5NmkeEJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Greg Sherwood

unread,
Aug 7, 2012, 6:08:24 PM8/7/12
to php...@googlegroups.com
On Tuesday, 7 August 2012 18:39:14 UTC+10, Hari K T wrote:
The only thing I don't love is the bug track via PEAR . Its horrible man ... Please consider to make it on github issue itself .


It's a PEAR project, so I use the PEAR bug tracker. It works fine for me so I'm not planning on changing to anything else right now.

If you really don't like it, feel free to email me directly (many people do) or submit pull requests, even if they just contain an additional failing unit test. Whatever works for you is fine. 

Greg Sherwood

unread,
Aug 17, 2012, 1:24:32 AM8/17/12
to php...@googlegroups.com, php-st...@googlegroups.com
I've now completed both the PSR-1 and PSR-2 standards and I'd like to release them shortly. If anyone has time to test them against your code, I'd appreciate it.

Easiest way to do it:
cd PHP_CodeSniffer
php scripts/phpcs --standard=PSR1 /path/to/code
php scripts/phpcs --standard=PSR2 /path/to/code

Thanks all

Andreas Möller

unread,
Aug 17, 2012, 2:04:03 AM8/17/12
to php...@googlegroups.com, php-st...@googlegroups.com
> I've now completed both the PSR-1 and PSR-2 standards and I'd like to release them shortly. If anyone has time to test them against your code, I'd appreciate it.

Awesome! Will do.


Best regards,

Andreas

Hari K T

unread,
Aug 17, 2012, 4:12:40 AM8/17/12
to php...@googlegroups.com
Wow cool man! .

I will run it on @auraphp code base and let you know if any as usual :) .

Probably I quick and lazy question is there a way I can set these standards as default and run both standards without running PSR1 , PSR2 like PSR ?

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To post to this group, send email to php...@googlegroups.com.
To unsubscribe from this group, send email to php-fig+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/php-fig/-/_-U4j9LC3wMJ.

Greg Sherwood

unread,
Aug 19, 2012, 6:30:02 PM8/19/12
to php...@googlegroups.com
On Friday, 17 August 2012 18:12:40 UTC+10, Hari K T wrote:
Wow cool man! .

I will run it on @auraphp code base and let you know if any as usual :) .

Thanks. Always appreciate your feedback.
 

Probably I quick and lazy question is there a way I can set these standards as default and run both standards without running PSR1 , PSR2 like PSR ?

Sorry, I should have been more clear about that. PSR-2 includes the whole of PSR-1 already, so if you run the PSR2 standard, you will also be checking your code against PSR1 as well. If you have the very latest code from the github repo, you can see the list of sniffs by running php scripts/phpcs --standard=PSR2 -e (38 sniffs) and compare that to php scripts/phpcs --standard=PSR1 -e (7 sniffs).

If you'd like to *just* check PSR-1, you can use the PSR1 standard by itself. That would be for those projects that find PSR-2 a bit hard to adapt to. 

Evandro Oliveira

unread,
Nov 26, 2012, 3:23:14 PM11/26/12
to php...@googlegroups.com, php-st...@googlegroups.com
Since PSR-2 requires PSR-1, run both seems redundant to me.

Maybe Call PSR-2 can invoke lower PSR ones, or we can import PSR-0/1 rules to PSR-2 ruleset.

justin

unread,
Nov 26, 2012, 3:26:36 PM11/26/12
to php...@googlegroups.com, php-st...@googlegroups.com


On Mon, Nov 26, 2012 at 12:23 PM, Evandro Oliveira <evandr...@gmail.com> wrote:
Since PSR-2 requires PSR-1, run both seems redundant to me.

Maybe Call PSR-2 can invoke lower PSR ones, or we can import PSR-0/1 rules to PSR-2 ruleset.

It does:


Evandro Oliveira

unread,
Nov 29, 2012, 10:01:02 AM11/29/12
to php...@googlegroups.com, php-st...@googlegroups.com
So, run it

php scripts/phpcs --standard=PSR1 /path/to/code
php scripts/phpcs --standard=PSR2 /path/to/code

like suggested above is really redundant. The last line does all dirty work.

Greg Sherwood

unread,
Nov 29, 2012, 10:10:09 PM11/29/12
to php...@googlegroups.com, php-st...@googlegroups.com
On Friday, 30 November 2012 02:01:02 UTC+11, Evandro Oliveira wrote:
So, run it

php scripts/phpcs --standard=PSR1 /path/to/code
php scripts/phpcs --standard=PSR2 /path/to/code

like suggested above is really redundant. The last line does all dirty work.

That's right. I only mentioned both lines in my original posts because I was asking for testers of both the PSR2 and PSR1 standards. Someone people use PSR2 and some use PSR1, so I provided both lines for those able to help me get things right. At the time, the standards were incomplete.

Now that it has been released, you only need to use the standard that your code follows. So PSR2 for you, but some others will want to stick to PSR1 only.

Greg
Reply all
Reply to author
Forward
0 new messages