EasyCodingStandard and PHPStan meet 3 Symfony E-Commerce Projects

Found a typo? Edit me
This post is deprecated since December 2018. Its knowledge is old and should not be used.

All 3 projects changed massively during last year, so these numbers are way outdated.

This post was updated at December 2018 with fresh know-how.
What is new?

Updated with EasyCodingStandard 5, Neon to YAML migration and checkers to services migration.

In the last post, we looked at the static analysis of 3 Symfony E-Commerce projects.

Lines of code, Duplicated code, Cyclomatic complexity or Method length. These metrics are very rarely used in practise (even though there is a sniff for that).

Today, I am going to show you how you can check them with tools that can help you keep your code better on daily basis - EasyCodingStandard and PHPStan.

Try It Yourself

I've updated the repository on Github with both tools, so you can verify all the results in this post locally.

(Tip: logos lead to projects' landing pages. Be sure to explore.)

Why These Tools?

When a project uses coding standards - moreover CI-checkers - it is very easy to contribute to it. I don't have to be afraid a PR gets rejected or at least postponed due to a wrong bracket position (yea, that happens).

Second, PHPStan is the best tool when it comes to passing type validations (arrays of objects, like Type[]), incorrect namespaces, calling non-existing methods and more.

To get the idea how it improves your code in practice, just check this commit in ApiGen.

Coding Style Violations with EasyCodingStandard


PSR-2 is the most spread coding standard in PHP, described in PHP-FIG guide. Both PHP_CodeSniffer and PHP CS Fixer have a set of ~30 rules, so why not to combine them and use them on our projects?

Shopsys Spryker Sylius
0 errors 9 723 errors 133 errors

What Would Be the Takeaways?

If you’re used to PSR2 style, you probably use PHPStorm to check it and contributing to Sylius would mean no extra work for you. Sylius' ruleset contains just handful of checkers though.

Spryker has own package just for coding-standard build on PHP_CodeSnifer, but they don't comply with some rules of PHP CS Fixer. This might be issue if you're in the Symfony world and you use that tool in your projects.

Shopsys has its own coding-standard package build on both tools - PHP CS Fixer and PHP_CodeSniffer - compatible with PSR-2. I'd say that's the best solution, because you have the least responsibility as a contributor. CI tools will handle the code style for you.

The Four Cleaners

This is another small and easy-to-understand set I use to keep the code of open-source packages clean. In Sylius it removed 500 lines of unused code just few days ago as of writing this article.

The full set looks like this:

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();

    // use short array []
        ->call('configure', [['syntax' => 'short']]);

    // drop dead code

    // drop dead use namespaces

    // and sort them A → Z

And results for our 3 projects?

Shopsys Spryker Sylius
39 errors 97 errors 7 errors

Code Violations with PHPStan

There are 8 levels in PHPStan to this day - from 0 to 7. Level 0 being the starter and 7 the Mission Impossible for most projects.

If you never used this tools before, you probably don’t know how strict PHPStan is. Doctrine2 has now level 1 and successfully passes it, thanks to Majkl578. In order to get the idea of what had to be done, see the PR.

Do you want to add PHPStan to your project? Read this short intro.

To have an idea about real numbers, I picked results for lvl 0 and lvl 7:

PHPStan levels \ Projects Shopsys Spryker Sylius
0 1 406 errors 7 905 errors 1 404 errors
7 1 413 errors 12 977 errors 3 715 errors

As you can see, both Shopsys and Sylius are doing great. Sylius is just falling bit behind in higher levels.

As we have seen in the previous article, not all the projects are of the same size. It might not be fair to Spryker to count only PHPStan violations as its codebase is considerably larger. If we count violations relative to the size of the project (measured in lines of code) the graph changes significantly:

Results for lvl 0 and lvl 7:

Relative to LOC Shopsys Spryker Sylius
0 0.014 0.021 0.012
7 0.014 0.035 0.033

This point of view shows that, on the less strict levels of analysis, Sylius and Shopsys are ahead of Spryker in terms of PHPStan violations. During the second level of analysis this changes and number of problems found notably rises, only Shopsys stands out with less than a half of the issues of both other frameworks. This trend does not change even while moving to the higher levels of scrutiny from mr. PHPStan.

Shopsys has long way to passing level 7 with 0 errors, but it has still some time till the release to improve this.

Happy coding!

Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.