In this video, we will see what code style guides are (specifically PSR-1 and PSR-2), why it is good to follow these standards, and how we can apply them automatically thanks to the PhpStorm IDE. While this could be the first thing to do when arriving at a project that does not follow these guidelines, it could be the second step after applying a correct namespace as we saw in the previous video.
Topics covered
- What it is and why it is important to follow the PSR-2 code style standard
- How to configure PhpStorm to automate code formatting following PSR2
- Inspection for traditional array syntax
- Important: The video does not mention some of the very interesting resources listed in the "Related Material" section of this post. I recommend checking them out :)
Video
Index
- 0:00 - Introduction
- 0:33 - Configuring PhpStorm with PSR-2 code style
PSR2 PhpStorm Code Style - 1:09 - Why it is good to follow the PSR-2 standard
- 2:40 - Applying style guides automatically
- 4:00 - Manual process altering overlooked details
- 4:58 - Error for not specifying the full namespace of a class. Why to specify this through "use" and not in each class
- 5:40 - Adding inspection for traditional array syntax
Array syntax inspection - 7:05 - Commit with style changes
Related Material
- PSR-2 Standard Specification for PHP code style from PHP FIG
- Documentation on PhpStorm code style settings, including the section to enforce new array syntax when applying the style guide
- PHP Coding Standards Fixer: Console tool to automate using command line the application of PSR-1 and PSR-2 rules
- PHP Code Sniffer: Tool to detect style rule violations automatically
- Git hooks for PHP projects: Library to orchestrate this type of process at the time of committing through hooks. Especially useful to avoid publishing such code in production. We will talk about this later :). This tool was created by Pablo Braulio based on what they use at Atrápalo regarding this
Upcoming videos
- SOLID - Single Responsibility Principle and Interface Segregation Principle (Intermediate Level)
- SOLID - Dependency Inversion Principle (Intermediate Level)
- Interview!
- DTOs / Anemic Domain Models vs Domain Models (Intermediate Level)
- Composition over Inheritance - A Turning Point (Intermediate Level)