Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Checking your code for PSR-2
Jul 28, 2015 @ 08:17:20

Rob Allen has posted a guide showing you how to make your code PSR-2 compliant with the help of some handy tools, both in and out of your editor/IDE.

Most of the projects that I work on follow the PSR-2 coding style guidelines. I prefer to ensure that my PRs pass before Travis or Jenkins tells me, so let's look at how to run PSR-2 checks locally.

He looks at three different methods - not the only ones out there but three quick to implement ones:

  • Using the PSR-2 sniffs for PHP_CodeSniffer
  • Automating the checks with Phing
  • Editor integration (he shows VIM and Sublime Text)

There's links to the tools mentioned here and screenshots/configuration information showing how to get it set up too.

tagged: psr2 code style check phpcodesniffer phing editor vim sublimetext

Link: http://akrabat.com/checking-your-code-for-psr-2/

This Programming Thing:
Creating Your Own Standard in PHPCS
May 12, 2015 @ 08:55:30

On the This Programming Thing blog there's a recent post showing you how to define your own "sniff" settings for the popular PHP_CodeSniffer tool. The codesniffer lets you define standards that need to be in place for all code in your application and notifies you of violations.

At Zimco, we’ve started working on standardizing our coding but we ran into a little problem while we tried to automate the process of making sure our code adhered to that standard. [...] I think we get into our own way of doing things and everything else is wrong. This code makes me feel irrationally angry (so angry I’m having a hard time not fixing it…). Ultimately, the best way to fix these kinds of formatting problems is to sit down and discuss what’s best and have everyone stick to the same set of standards.

They talk some about the place for PSR in coding standards (specifically PSR-2) and the fact that there's already "sniffs" provided to check against those rules. However, they point out that running this against a non-PSR-2 codebase can be a mess and show you how to customize your own standard to more match your current state. They use an XML configuration file to update the tab width setting to four spaces and then apply the PSR-2 standards. They also show how to exclude certain rules and mention a handy plugin you can use in Sublime Text to keep your code within standards.

tagged: standard phpcs phpcodesniffer sniff configuration xml psr2 update exclude

Link: http://www.thisprogrammingthing.com/2015/creating-your-own-standard-in-phpcs/

Squizlabs Blog:
PHP_CodeSniffer 2.0.0 released
Dec 05, 2014 @ 12:03:34

The Squizlabs blog has an announcement about the release of the latest major version of the popular PHP_CodeSniffer tool for PHP - CodeSniffer v2.0. Among the updates in this latest release is a major one - the automated fixing of issues the tool finds.

Nineteen months ago, I started work on a project to allow PHP_CodeSniffer to fix the problems that it finds. Doing this required a lot of changes to the core classes, a lot of iteration and refactoring of the fixing and testing code, and an enormous amount of time and testing across many PHP projects to ensure I am confident enough to release something that actually modifies code. I could keep writing unit tests forever, but I've finally got to a point where I am happy to release this first version of the PHP Code Beautifier and Fixer (PHPCBF), for when you just can't be bothered fixing coding standard errors yourself.

The fixes are made possible through the newly introduced "PHP Code Beautifier and Fixer" (PHPCBF) tool. When the CodeSniffer tool is run against your code the PHPCBF kicks in too and tells you which of the issues can be automatically fixed. Additionally, you can now add custom code to your custom sniffer rules to enable this auto-fix functionality yourself. He also includes a list of the other updates in the release including:

  • a new information report to show you how your code is written rather than if it conforms to a standard
  • the ability to set command line arguments in ruleset.xml files
  • the ability to create your own custom reporting classes and use them with PHP_CodeSniffer
  • support for running on HHVM

You can find out more information about this release in the PEAR or GitHub changelogs.

tagged: phpcodesniffer v2 release automated fix phpcbf

Link: https://www.squizlabs.com/php-codesniffer/2.0.0-released

The Nerdery:
Is my code compatible with PHP 5.4 or 5.5?
Nov 06, 2013 @ 11:15:40

On The Nerdery's blog today there's a new post that shows you a way to help test your code for PHP 5.4 and 5.5 - via PHP_CodeSniffer - as well as mentioning some of the changes that came with these versions.

Do you want to know if your code is compatible with that latest version of PHP? Do you need to upgrade your existing PHP website to a newer version of PHP? Do you want to see if your legacy PHP code is using any features that have been removed or deprecated in later versions of PHP? [...] ou need to migrate your existing code to ensure nothing will break when the upgrade occurs. How can you accomplish that?

They start with the list of differences (and backward incompatible changes) in the newer versions and point to the migration pages for each. Then they get into the use of PHP_CodeSniffer and some additional rules provided by Win Godden for checking the compatibility. With these added in, you can call the "phpcs" checking tool with the "PHPCompatibility" standard and check out the results. An example of what you might see on an older application is included in the post.

tagged: code compatible update versions phpcodesniffer standard phpcompatibility

Link: http://blog.nerdery.com/2013/11/code-compatible-php-5-4-5-5/

Apr 12, 2013 @ 10:46:26

On NetTuts.com today there's a post that talks about applying the PSR formatting to your application's code. If you haven't already read their introduction to the PSRs, it's highly suggested.

In a previous lesson here on Nettuts+, you learn about PSR; however, that article didn’t detail the process of integrating that coding style into your projects. Let’s fix that!

They briefly recap the main two PSRs (PSR-1 and PSR-2, but no mention of PSR-3 the logging interface) and show code examples of them being applied. They also point to the PHP_CodeSniffer tool that you can use to keep your code in the correct structure. Instructions are included to install it specifically for the Sublime Text 2 editor via package control. It's just a command-line tool, though, so it could be integrated with just about any other editor/IDE out there too.

tagged: psr psr1 psr2 apply autoload formatting phpcodesniffer

Link: http://net.tutsplus.com/tutorials/tools-and-tips/psr-duh

Sebastian Marek's Blog:
PHP 5.4 Compatibility Coding Standard for PHP_CodeSniffer
Mar 02, 2012 @ 10:52:32

In the wake of the official release of PHP 5.4 Sebastian Marek has made a quick post to his blog about bringing PHP_CodeSniffer rules help bring his code up to date with this latest version.

So with PHP 5.3 upgrade underway (and PHP 5.4 out of the door now!) I thought it’s time to prepare for PHP 5.4 and make sure we’re compatible. So by looking at Wim Godden’s PHP53Compatibility code sniffs I have created a base for PHP 5.4 sniffs that we want to use to make sure we’re compatible.

Sniffs included in set are:

  • PHP54Compatibility_Sniffs_PHP_BreakContinueVarSyntaxSniff
  • PHP54Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff

You can grab this custom set of sniffs either from his github repository or from his personal PEAR channel if you'd rather install it that way (alpha channel).

tagged: phpcodesniffer compatibility coding standard upgrade


Michelangelo van Dam's Blog:
Quality Assurance on PHP projects - PHP_CodeSniffer
Jul 18, 2011 @ 09:06:48

Michelangelo van Dam has posted the next part in his "QA in PHP development" series to his blog, a look at PHP_CodeSniffer, a tool that can be used to keep the coding standards of your application in line.

PHP_CodeSniffer is probably the most convenient tool out there to analyze your source code and to verify it complies to company policies. Although it's debatable why source code should follow strict guidelines, it's only a matter of time before you discover yourself that it pays off to have a code base that appears to be written by one developer.

He talks about how it works to keep every brace in the right place and format the same through out all of your files. He also mentions that its standards are flexible and can be modified as you might see fit. He looks at the installation process (both manual and through the PEAR installer) and how to use it to analyze your codebase. He's even included a short screencast in the post so you can see the results first-hand.

tagged: quality assurance project code phpcodesniffer standards tutorial


Christian Weiske's Blog:
How to integrate PHP_CodeSniffer with Git repositories?
May 27, 2011 @ 11:16:48

Christian Weiske has a problem he hopes you can help with - he's trying to get the PHP_Codesniffer tool integrated into his git workflow (well, the workflow of his team) as an automatic process that runs on commit. Unfortunately he's having some issues.

At work, we used a SVN server and enforced our project coding standard with a pre-commit hook on the server that ran PHP_CodeSniffer. Whenever a developer tried to commit some code that does not match the standard, he got it rejected. [...] The only way to enforce the standard is a pre-receive hook on our central Git repository server that all devs push to. Just installing the SVN hook on it isn't the solution, though.

Because of how git handles commits (possibly multiple in one push) the usual methods won't work. Other tricky things like file renaming and allowing for legacy code check-ins are also needed. He's posted the question on StackOverflow too, but no one's come up with a good answer yet (at the time of this post).

tagged: git phpcodesniffer codesniffer commit hook stackoverflow


Volker Dusch's Blog:
Please ship your own coding standard as part of your project
Mar 14, 2011 @ 11:32:47

Volker Dusch has a suggestion for all of the PHP projects (or, really Open Source projects in general) that can help keep things cleaner in your codebase and make for simpler times when merging contributions - including your coding standard along with the rest of your project.

Let me elaborate on [an important] point: Contribution. Most developers i know care about producing good code, especially then they are contributing to an open source project! Those people will respect your coding standard, naming scheme and every thing else that they can check for before sending you all patch/pull request. So try to make that part easy.

He talks about doing things the hard way - reformatting everything by hand each time someone contributes - or the easier way of enforcing the coding standard as a part of the contribution flow. He mentions PHP_CodeSniffer and the PHP Mess Detector as a part of a Jenkins installation (easily built from this handy project).

tagged: coding standard phpcodesniffer phpmessdetector jenkins contribution


Mayflower Blog:
Creating coding standards for PHP_CodeSniffer
Feb 25, 2011 @ 13:33:07

On the Mayflower blog today there's a new tutorial posted about creating coding standard "sniffs" for the PHP_CodeSniffer tool. A "sniff" is what defines the rules for your coding standards to follow (like "curly braces after function definitions should be on the next line" kinds of things).

In some cases the pre-installed coding standards like PEAR or Zend might not be sufficient for our current project or we want to deviate. This is the moment when we want to be able to create a custom one that fits our special needs. In this article I want to share my first experiences with you about how to create a custom coding standard for PHP_CodeSniffer.

They get into the details of what a "sniff" is and shows where they belong in the current structure of your PEAR install. There's an example of how to run the command line tool and how to create your own structure for your own custom sniffs. Their first example sniff checks to ensure that the first letter of a class is in uppercase.

tagged: coding standard phpcodesniffer tutorial introduction