In this episode, we dive into using PHP CodeSniffer to keep your code clean and consistent with a coding style guide (we're focusing on PSR-2, but you can use any standard that fits your needs). We'll kick things off by talking about why coding standards matter—not just for big teams, but even if you're working solo. Consistency in code style can save you future headaches, especially when others jump into your project.
First, we'll get Composer set up (super easy if you haven't done it before) and use it to pull in PHP CodeSniffer as a dev dependency. After that, we create a quick sample PHP file in a new Controllers directory, deliberately breaking the standards in places (using tabs instead of spaces, messing up braces, etc.) so you can see CodeSniffer in action.
Next, you'll see how to run PHP CodeSniffer from the command line and check your code against the PSR-2 standard. You'll probably notice a handful of errors, most of which are simple indent or formatting issues. We show off editor config—a handy tool that helps maintain coding spaces/tabs and styles across different editors. You'll learn how to create a .editorconfig
file to enforce those rules automatically and see it bring down the error count instantly with just a quick reindent.
We'll keep going, step by step, fixing the last few issues (like namespaces, correct brace placement, etc.), and use CodeSniffer after each change until everything passes and your code is fully compliant. By the end of the video, you'll know how to set up PHP CodeSniffer, configure your editor, and enforce code standards with ease so your PHP code always looks sharp—whether it’s just you, or a full team working together.