DEV Community

Cover image for Refactoring with PHPStan (From the office)
anastasionico
anastasionico

Posted on

Refactoring with PHPStan (From the office)

I am Nico, 

I am a senior engineer for a ticket broker based in London.

From the office! is a daily (almost) posting routine that I keep to journal about the tech things I have been doing in my day-to-day office life.

Packages & Composer

Let's start from the very beginning.

If you are a PHP developer, you got to know what Composer is.

Composer is THE package manager for the PHP language.

The concept is very easy. 

We, as developers, do not want to reinvent the wheel every time we need some feature.

For this reason, we create a little bit of code that performs a unique task.

Then, we 'download' these little bits of code into the project when we need them.

The majority of PHP projects right now are composed of many dozens if not hundreds of these bits of code, they are called packages.

PHPStan

My daily task at my new workplace can be summarized with a single word: refactoring.

That's what I have been brought in for and that is what I am doing on a daily basis.

To do that I leverage the use of several tools like PHPUnit, Symfony built-in feature and many, you guessed, packages.

One of them I have been using recently is PHPStan.

PHPStan is a PHP Static Analysis Tool. 

PHPStan finds errors in the code without running it. 

It catches whole categories of bugs even before any tests have been written write tests for the code.

Static Analysis Tool

As you read above PHPStan is a Static analysis tool.

Static analysis tools mean many different things, in this case, this is a tool that investigates source code and finds issues before they happen

It is very powerful because it does that without actually running the code and without the needs of adding Unit or integration tests for it.

It has an extension for many of the most popular frameworks and CMS such as Laravel, Symfony and Drupal.

And can be easily used inside your CI pipeline.

How does it work?

What PHPStan does is check your code depending on a group of rules it has set.

These rules are grouped by different levels (they go from level 1 as the lowest to level 9 as the highest.

This is good because you can start viewing and correcting errors of level one and once you have done it move a step forward, gradually reaching the higher level.

Installation

Installing this package into your project is very easy.

All you have to do is to cd into the root folder of your project, or wherever you keep your composer file and run the following command

composer require --dev phpstan/phpstan

To run the program you need to indicate what folder you want to be analyzed.

In my case, I keep my code inside the src/ folder and my tests inside tests/

Analyse

vendor/bin/phpstan analyse src tests

That's it!

There are many options that you might want to personalize.

Let's have a look at the major ones

--level|-l

Specifies the rule level to run.

--configuration|-c

Specifies the path to a configuration file. Relative paths are resolved based on the current working directory.

--memory-limit

Specifies the memory limit in the same format php.ini accepts.

--xdebug

PHPStan turns off XDebug if it's enabled to achieve better performance.

Configuration file

PHPStan uses a file called phpstan.neon for its configuration.

Don't get scared by the neon extension, the syntax looks a lot like YAML.

In this file, you can specify rules such as what are the paths of your code you want to be analyzed, what level and even whether you want to ignore certain errors or not.

Here is an example:

parameters:
    paths:
        - src
    ignoreErrors:
        - '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
        - '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
Enter fullscreen mode Exit fullscreen mode

Here is the official website and Docs for this package.

My advice is to give it a try even if you do not need to refactor the code right now.

Conclusion

I have been a web developer for almost a decade now.

Working with world-class businesses and award-winning marketing agencies situated in the heart of London.

Also, I write articles and tutorials on my blog and online communities and help businesses build their presence online.

Click here to read more than 100+ of my blog post

Top comments (0)