What Are Coding Standards And How Can We Use Them?
8 months ago

You may have heard of a bunch of terms thrown around like, static analysis, Rector, Easy Coding Standards and Phpstan. But what are they, what do they mean and how can we use them? 

Firstly, static analysis, refers to the process of analysing source code without actually executing it. simple enough right? It looks at code, but doesn't run it. 

What static analysis tools are available?

There are lots, but I use the following:

Rector: Instant Upgrade and Automated Refactoring of any PHP code
Easy Coding Standards: Use Coding Standard with 0-knowledge of PHP-CS-Fixer and PHP_CodeSniffer. ( this will fix our coding style)
Phpstan: PHPStan focuses on finding errors in your code without actually running it.

So why do we want to use these tools?

Using these tools automates the process of find problems in our code, we run these tools with a command in our terminal and it either fixes some problems automatically or will give us information as to where the problem is.

Let's have a look at each tool in more detail.


Rector

Rector is great at fixing or upgrading your code functionality automatically

install like so: 

composer require --dev rector/rector


run like so: 

vendor/bin/rector p --ansi

or if you want to see what changes it would make, but not actually make them: 

vendor/bin/rector p --dry-run --ansi

If you run this for the first time, it'll ask you if you want to create rector.php config file, you'll need one, say yes if you don't have one. 

Inside here we can configure what rector rules and sets should be applied, here's my general configuration:  (a set is just a bunch of rules together, as rule is some predefined instructions that define the transformation of a piece of code. (Don't worry you don't have to write them) 


Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/assets',
        __DIR__ . '/config',
        __DIR__ . '/public',
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])
    ->withSets([
        SetList::PHP_83,
        SetList::TYPE_DECLARATION,
        SetList::DEAD_CODE
    ]);

This will execute in all the directories in the`withPaths` array. 

You can check the `SetList` object to see all the available sets.  Commit each time after you run it so you can revert the changes if need by.


Easy Coding Standards

ECS is great at fixing your code style and how it looks. 

Install it like so: 

composer require --dev symplify/easy-coding-standard

run it like so:

vendor/bin/ecs check --ansi

or if you want to see what changes it would make, but not actually make them: 

vendor/bin/ecs check --fix --ansi

I'm getting dejavu. 

If you run this for the first time, it'll ask you if you want to create ecs.php config file, you'll need one, say yes if you don't have one. 

Let's have a look at my config file:

PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withPaths([
        __DIR__ . '/assets',
        __DIR__ . '/config',
        __DIR__ . '/public',
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])

    ->withPreparedSets(
         arrays: true,
        comments: true,
        docblocks: true,
        spaces: true,
        namespaces: true
     )
     ;

This will execute in all the directories in the`withPaths` array.


Phpstan

Phpstan is great at find errors and telling you where they are. 

Install it like so:

composer require --dev phpstan/phpstan

run it like so:

vendor/bin/phpstan analyze

Unfortunately, as of writing, phpstan doesn't have a way of generating it's configuration file, so we have to create this manually, `phpstan.dist.neon` (don't ask me why it's using .neon) 

Inside my configuration file: 

parameters:
    level: 6
    paths:
        - bin/
        - config/
        - public/
        - src/
        - tests/







This will execute in all the directories in the `paths` array on a level six, the maximum is level 8, I wouldn't recommend this level as it gets a bit ridiculous.

I find that using these 3 tools you get very good cross over and creates some beautiful code with little effort. 



Thanks to reddit user FrantisekHeca for giving me the idea to write this article, I hope you find it useful.