Be lazy
Be lazy and use Tools, I heard this earlier and totally agree.
They save you time.
Easy coding standard
https://github.com/easy-coding-standard/easy-coding-standard
This tool helps you to keep your code consistent.
The best thing is to get one of the predefined sets and use it in your project, If you want to exclude or add some rules then it is easy to do in your ecs.php file.
The code below uses PRS-12 and excludes Yoda Style (which I personally don't like :) ) and adds some useful Rules e.g. remove unused imports from files.
<?php
declare(strict_types=1);
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
return ECSConfig::configure()
->withPaths([
__DIR__ . '/config',
__DIR__ . '/public',
__DIR__ . '/src',
])
->withRules([
NoUnusedImportsFixer::class,
OrderedImportsFixer::class,
SingleLineAfterImportsFixer::class,
])
->withSkip([
YodaStyleFixer::class,
])
->withPhpCsFixerSets(
psr12: true,
);
You can add as a script to the composer and add to the pipeline to ensure that all members of the team will keep code with standards, that are set in the project.
PHPStan
https://phpstan.org/user-guide/getting-started
This tool helps you to avoid some bugs or make the code more clean.
The command is run by
vendor/bin/phpstan analyse [options] [<paths>...]
You can improve your code step by step because we start from the default 0 level and we can validate the project to max level (9)
I recommend using phpstan.neon.dist in your repository (default configuration).
If you want you can override it by phpstan.neon and add their own rules (e.g. change level).
includes:
- phpstan.neon.dist
parameters:
level: 4
Baseline and ignoring errors, exclude paths
You can start using it in legacy code because you can
ignore specific errors, and generate a baseline, which will ignore all errors, that you now have in the project. You can fix it later, but at the beginning concentrate only on new code in the project. You can exclude also some directories or classes.
Nice feature in the config
I like an option to set a clickable link in the code editor after running phpstan
e.g. for PHPStorm (for local)
parameters:
editorUrl: 'vscode://file/%%file%%:%%line%%'
For more look at the documentation, there are still some interesting things.
Top comments (0)