Created by the PHP-FIG, the PSR specification is a unified push to create standards across all PHP projects. The committee is formed from over 20 developers representing some of the most popular frameworks. Having a unified standard has been a much needed endeavor since PHP standards have been somewhat lacking in the past.
Sublime Text - User Settings
While some of the proposed standards tell you how to format code, we can configure some of the more basic options directly into Sublime. First off if you don't yet know how to modify your Sublime settings got to
Preferences -> Settings-User. You should always modify the "User" files in Sublime because the default files will be overwritten with each new update.
"Code MUST use 4 spaces for indenting, not tabs."
Since PSR states we must use spaces, we need to make sure that when we press the tab key it is translated to spaces. Additionally, since all of our tabs are now spaces setting the tab size to 4 will actually set the amount of spaces to 4.
"There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less."
The rulers options allows you to place lines in the editor at the 80 and 120 character marks. Word wrap will not impose a hard limit.
"All PHP files MUST use the Unix LF (linefeed) line ending."
If your company is using git and the developers have both Mac and Windows systems, you can save yourself a ton of headaches by making sure everyone is using Unix line endings. This prevents the file from changing line ending type and misleading git into thinking that every single line has changed between commits.
"All PHP files MUST end with a single blank line."
This one is pretty self explanatory.
"There MUST NOT be trailing whitespace at the end of non-blank lines."
This is another all or nothing option if your company is using git. Some editors will place spaces at the end of non-blank lines. PSR states that there should be no whitespace on these lines.
So with all these options placed into your
User - Settings file you have a good baseline for transitioning into using PSR. Before starting this make sure that you have all developers on board especially if you are using git. Additionally, I highly recommend reading the full documentation of the standards which you can find at the php-fig website. For easy copy-pasting, here is the final settings file.
In a future post I plan on delving into the PHP Code Sniffer plugin which can be used for automatically converting files to PSR standards.comments powered by Disqus