Static analysis always watch over your code
Static analysis always watch over your code

Before coding, we all want to produce clean, smart and efficient code. During coding, we keep the eye on the target, and when we’re done, the result is usually not what we expected. There is always room for improvement.

Improvement may come in various flavors: corner cutting, PHP specific gotcha, migration or even old habits that tends to stay. This is always frustrating to realize that code was written too fast, and could be really better.

What is Static Analysis?

Static Code Analysis is the analyze of code without execution. PHP does quite a lot by itself, mentioning fatal errors overtime we forget a; or }.

But static analysis reads the code, and point out any situations that looks suspicious. Then, we can change it before it turns into a bug.

What are the direct advantages of static analysis ?

Fixes before bugs

Sometimes, we know about this error-prone piece of code, and we even tell everyone about it. But working under pressure, or even simply well focused on another aspect of the code, and we forget about it. When the bug bites, it’s too late. Static analysis is able to keep an eye on those situations, so we can come back to any forgotten code.

Dead code hunt

Adding more code is always easy. Spotting those parts of the code that are never used is always harder. It means taking variables, functions or classes, and checking in the whole code where they are mentioned, and how they are used. The rewards are important: less code, less review, less maintenance and a lot of simplifications.

Consistency

Especially when working on a team, each developer has its own personality that shows in the code. But to work as a team means also sharing some common rules and standards. Indeed, great projects display consistency across the whole code-set.

Preparing for the future

As a platform, PHP evolves regularly. It adds new features that imply dropping old habits (like array() versus []), or simply do the same things in other ways. Static analysis may review code and apply versions related sets of recommendations for preparing to PHP 7 or maintaining older version compatibility. This means reducing the gap between the current situation and the next version.

An eye over your shoulder

Finally, static code analysis is like a friend, constantly looking over your shoulder. Once in a while, I even thinking to myself: ‘I can’t leave that here, exakat will find it anyway. It know that the engine will not find everything, but knowing that something will review my code later makes me more aware of what I’m coding.

The best is may be that static code analysis will never shame me in front of my peers and keep all of it a secret for us both. Forever.