They were coded by Dave, your colleague developer.

The classes are full of formatting errors, poor indentation, and weird one letter variables.

There are so many dependencies you gotta scroll down for minutes to escape the bloated constructor.

PHP code quality tools to check and improve your code

It’s free, every week, in your inbox.

However, since you are such a respectful human being, you know its not a good solution.

Teaching instead of blaming always gives better results.

a good mentor is better than any quality tool

Then, you decide to introduce to your team some code quality tools.

It can help your colleagues detect defects in the codebase and teach them some key concepts.

Dont forget however that the advice and the data they can provide wont be appropriate everywhere.

Article image

As often, it depends largely on the context: is your codebase large?

Is there a good reason the cyclomatic complexity is high for some function?

The last thing before diving in: tools presented in this article analyze or format your code.

crap metric

I wont speak about testing.

My personal preference is to use the composers global package installation usingcgrto avoid dependency problems on the global scope.

you’re able to as well use thePHARformat, in most cases.

PHP Insight first screen

Most of the time you just need to pass the codebases path as an argument and voila!

I describe this process for every tools in this article.

I advise you to call the tools from the main folder of your project.

PHP Insight second screen

Every example assume that your codebase is in the foldersrc.

With the pluginneomakeyou can plug easily PHPMD, PHPSTAN, and PHPCS to Vim.

It will display in the gutter warnings and errors.

example dephpend output

you might even create your own makers to use every PHP code quality tools you want.

As a reference, you’re able to consult myneomake config file.

They will format your code properly and gives you precious advice.

phpcf output

A great example ofParkinsons Law of Triviality.

Personally I dont have any preferences regarding code formatting.

By default,PSR-1andPSR-2rules are used but you’re free to define your own formatting rules.

you’ve got the option to as well create anhtmlorxmloutput by replacing thetextoption in the command line above.

Do you want to output the errors in a file?

you’re free to then refine your result by using the–minimumpriorityoption for example.

In short: PHPMD is a great tool I really encourage you to use.

It will detect a lot of potential problems in your code and will save you hours of debugging.

Your boss will be so happy he will increase your salary by 200%.

PHPStan (PHP Static Analysis Tool)

PHPStan is another tool to have in your toolbox.

Output errors like a compiled language would display during compilation.

Its a good complement to PHPMD.

The minimum islevel 0, the maximumlevel 7.

PHPUnit and the CRAP metric

This article is not about the unit test.

I assume you know that unit testing your code is far more important than anything present in this article.

PHPUnit can as well display very interesting information: theCRAPmetric.

The more the CRAP index is high, the more your code will be considered as crappy.

You wont even notice till your boss yells at you.

you could open theindex.htmlin there and opt for dashboard link to finally contemplate the CRAP indicator.

This is an entirely different topic I will keep for another article.

They can help you with seeing the big picture.

They can be a great help for refactoring.

PhpLoc

PhpLoc is a very good tool to get an idea of the size of a project.

PHPCPD (PHP Copy past detector)

PHPCPD will scan your codebase and output the code duplicated.

Do you want to verify if your wonderful project is not mutating into a complex Big Ball of Mud?

dePHPend can help you grandly on that matter.

it’s possible for you to as well output plain text or UML for example.

This is a pretty interesting approach.

A very complex class that is often modified has indeed a high chance to introduce bugs.

PhpCodeFixer

Deprecated functions are bad.

They can create very weird bugs difficult to debug.

This tool can help you detect them in your shiny system.

It will outputa lotof metrics about your project.

Now keep in mind that metrics are not necessarily the absolute truth, it really depends on your project.

I wont explain everything this tool can output here, maybe in a future article?

Do we really need these tools to check our PHP code?

My experience showed me thatsoftware entropyis a real thing.

The more you will modify your tool, the more the tool has chances to break.

Your app will inevitably become more complex.

These PHP code quality tools can definitely help you in that matter.

Do you use other tools than the ones described here?

Do you use them differently?

Dont hesitate to help the community by sharing your experience.

you could read the piecehere.

Also tagged with