Brief History of Tools Watching and Changing Your PHP Code

From coding standard tools, over static analysis to instant upgrade tools. This post is going to be a geeky history trip.

Which tool was first? How they build on shoulders of each other?

1. Coding Standard Tools

PHP_CodeSniffer

Tokens Checks Code

The first tool that made it to the mainstream of coding standard tools was created around 2007 by Greg Sherwood from Australia. At least by date of the oldest post I could found.

Greg has been maintaining the repository for last 11 years (at least). I don't know anyone else who would take care for his project for such a long time without stepping back - much respect!

PHP CS Fixer

Tokens Checks Code Modifies Code

Reporting errors are helpful, but fixing them for you is even more helpful. More people got a similar need and around Symfony 2.0 times and 2011 next tools were born - PHP CS Fixer.

The first version was created by Fabien Potentier, author and founder of Symfony, and it used mostly regular expressions.

The decision to fix everything by default was the huge jump in history of these tools. It was the first case of a tool that would be so bold to change your code for you. You had to believe it, you had to overcome the fear of modifying it the wrong way or even deleting. I mean, now we're used to it like to cars, but at one point of history, they were just riding bombs.


With PHP CS Fixer 1.0 release in 2014 and rising popularity of automated fixes was big motivation for PHP_CodeSniffer to add similar feature - code beautifier - to version 2.

It also helped with another issue. The development of PHP_CodeSniffer 2.8 and later almost froze to zero. I remember because I started working on EasyCodingStandard right between PHP_CodeSniffer 2.8 and 3.0 (depending on 3.0-dev), which took 14 uncertain months.

So PHP_CodeSniffer now holds Modifies Code as well.


Both use token_get_all() that basically parses code to strings. Do you want to know how they actually work?

EasyCodingStandard

Tokens Checks Code

I saw many projects that use both tools, yet very poorly - because split attention divides the focus in the same ratio. The mission of this tool is to help new generations to adopt coding standard with almost no effort. So in 2016 the EasyCodingStandard was born.

2. Static Analysis Tools

nikic/php-parser

AST

This is a package that is barely known for direct code analysis. But all following depend on it thanks to abstract syntax tree (known as AST; it's much simpler than it sounds).

It all started as a question on StackOverflow - Any decent PHP parser written in PHP? nikic answered himself with a php-parser less than a half year later.

I would not write this post and neither have my fuel for passion without this tool, so huge thank you, Nikita, for creating it and maintaining it.

PHPStan, Phan, Psalm

AST Checks Code

In my knowledge and according to Github, 0.1 tags, all have been published around 2015/2016.

All these tools use an AST analyzer - that's how they know you're calling $object->undefined(). Apart php-parser, there is Microsoft/tolerant-php-parser. His big advantage is that it can work with incomplete code. Thanks to such feature it's great for IDE autocompletion, e.g. phpactor for VIM.

The downloads speaks for themselves:

If you don't use any of these, give at least PHPStan a try. I've made minimalist intro just for you. The overhead is worth it to overcome since these tools will become more natural to use, like composer is now today.

Deprecation Detectors

AST Checks Code

Last group that is widely used mainly in general PHP community (not-framework-bounded):

It's useful to know what places are wrong, but it still only reports these changes.

3. Instant Upgrade Tools

umpirsky/Symfony-Upgrade-Fixer

Tokens Modifies Code

This tool was created in 2015 and it's revolutionary. Why? It wisely connects token analysis of PHP CS Fixer with deprecations in Symfony 2 to 3. Imagine it like PHP CS Fixer + sensiolabs-de/deprecation-detector working for you.

Isn't that amazing? You just sit, run this tool and send invoices. Genius! The features are limited due to Tokens, but still, I love this.

cakephp/upgrade

Modifies Code

It's completely natural, that frameworks that evolve fast need a tool that helps their users to migrate. CakePHP is jumping faster and faster recent years, they made their own tool. It's based on regular expressions.

silverstripe-upgrader

AST Modifies Code

SilverStripe is Australian CMS written in PHP. Its logic is very similar to Rector and I like it. It's also quite small, so the code is easy to understand. Be sure to check it!

Rector

AST Modifies Code

It's 2017 and Rector had to wait to be born. For what?

Well, it's built on php-parser and as it modifies the code and prints it back, it needed to keep spacing. That's one of AST drawbacks - it doesn't care about all that coding standards spacings.

They say "history repeats", but I never trusted that. Untill I saw that similar need Fabien had while making the PHP CS Fixer in 2012 - Optionally add nodes for whitespace. More people wanted AST-based coding standards:

php-parser 4.0-dev already had this feature, so Rector ran on it since the very start. It was not until February 2018 when it was finally released.

Last giant Rector builds on is the one for type analysis - PHPStan. Thanks to that, it doesn't reinvent the wheel and can focus on the refactoring part.


And that's a brief history of big-brother tools that watch your code and modify it for you.


Typo? Fix it, please  and join 49 people who build this website