In any project, it is worth ensuring that the code is consistent and maintainable—especially when more than one person is working on it. In this article, I will present the configuration I use in my own PHP projects: PSR-2 and PSR-12, consistent line endings via .gitattributes, automatic formatting with PHP CS Fixer, and code analysis using PHPStan.
1. PHP coding standard
PSR-2 and PSR-12 define standards for writing PHP code. Among other things, they specify conventions for indentation, braces, spaces, and blank lines, as well as the declaration of classes and methods.
The primary purpose of these standards is to ensure consistency in PHP code, regardless of who wrote it. They do not alter the code’s functionality; instead, they dictate its appearance, thereby improving readability.
PSR-12 extends and updates the rules established by PSR-2, notably to accommodate newer versions of PHP.
https://www.php-fig.org/psr/psr-2
https://www.php-fig.org/psr/psr-12
2. Consistent line endings
The .gitattributes file allows you to specify how Git should handle files in the repository. One of its uses is to enforce consistent line endings.
This is particularly important when people using different operating systems, such as Windows and Linux – are working on the same project. Without this configuration, Git might treat a change in line endings alone as a modification of the entire file, leading to unnecessary differences in commits.
.gitattributes
* text=auto eol=lf
*.conf text eol=lf
*.html text eol=lf
*.ini text eol=lf
*.js text eol=lf
*.json text eol=lf
*.md text eol=lf
*.php text eol=lf
*.sh text eol=lf
*.yaml text eol=lf
*.yml text eol=lf
bin/console text eol=lf
*.ico binary
*.png binary3. PHP CS Fixer
PHP CS Fixer is a tool for automatically formatting code according to established standards.
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer
Packages:composer require friendsofphp/php-cs-fixer
.php-cs-fixer.dist.php
<?php
$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude('var')
->notPath([
'config/bundles.php',
'config/reference.php',
])
;
return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'phpdoc_to_comment' => ['ignored_tags' => ['var']],
])
->setFinder($finder)
;Make sure to run it before every commit, or integrate it with your IDE.
Command:
./vendor/bin/php-cs-fixer fix src/
4. PHPStan
A static code analysis tool (which analyzes code without executing it). It detects errors by examining source files. The higher the level, the greater the precision; in my opinion, working at level 6 is the minimum requirement.
Among other things, it can detect incorrect types, missing values, issues with method calls, and other situations that could lead to runtime errors.
Packages:composer require phpstan/phpstan-symfony
composer require phpstan/phpstan-doctrine
composer require phpstan/extension-installer
The configuration file will appear automatically, but it is worth modifying it for Symfony projects, as shown below.
phpstan.dist.neon
parameters:
level: 6
paths:
- bin/
- config/
- public/
- src/
- tests/
excludePaths:
- config/reference.php
- tests/bootstrap.php
- tests/Support/_generated
symfony:
containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml
doctrine:
allowNullablePropertyForRequiredField: true
objectManagerLoader: tests/object-manager.php
scanDirectories:
- var/cache/dev/Symfony/Config
scanFiles:
- vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php
treatPhpDocTypesAsCertain: false
Additionally, if you want a more thorough Doctrine test, copy the following file to the tests directory:
tests\object-manager.php
<?php
use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
require __DIR__.'/../vendor/autoload.php';
(new Dotenv())->bootEnv(__DIR__.'/../.env');
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();Command:
./vendor/bin/phpstan analyse --level=6

Leave a Reply