News about Tipps & Tricks from the EXWE Blog
Subscribe to articles about digitalization, software development and e-commerce
  Show all news (70)

Integrate phpstan into Bitbucket pipeline

Banner Tipps & Tricks

With this short article we would like to give you some helpful tips in the area of web development. If you have any further questions or need more information about custom & responsive web development, feel free to contact us. We are here to help you with all your challenges and now our tip regarding PHP code analyzer:

A static code analyzer helps to detect many issues before they kill your application.

We use phpstan for our PHP projects and manage to integrate it into the pipeline so that a push is only deployed if the analysis is successful.

For this to work you first need to configure phpstan itself for your PHP application. We assume that you are using composer, so you can easily add it as a dev-dependency:

composer require --dev phpstan/phpstan

Then you have to create a config to at least exclude the vendor files. To do this, create a phpstan.neon in your root directory and fill it with this, for example:

- *vendor/*

You can now test if your code is clean by running the following command from your root directory (assuming your vendor folder is there).

vendor/bin/phpstan analysis -l 0 -c .\phpstan.neon . --memory-limit=1G

Let's have a quick look at the parameters:

-l 0
This sets the strictness level, i.e. how strict the ruleset is that phpstan checks for. 0 is "least strict".
-c .\phpstan.neon
This flag tells phpstan where the config is located.
In which folder should phpstan start?

As your codebase gets bigger you may need to allocate more memory to phpstan, you can do that via this flag.

If all that works, we can move on to the bitbucket pipeline:

To do this, simply create a bitbucket-pipelines.yml file in your root folder and fill it with the following content:

image: php:7.2-cli

dev: // On which branch should the pipeline run?
- step: // The first step is to call phpstan
name: Run PHP analyzer        
caches: // This is a standard Bitbucket cache that will cache your composer modules.
- composer // you don't have to reload them every time you run the pipeline
- apt-get update -yqq
- apt-get install -y wget git zip unzip // we need git and unzip for the composer installs
- docker-php-ext-install pdo_mysql // If you use MySQL you probably need this php extension too,
// otherwise phpstan won't find the MySQL constants etc.
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
                                            // Composer itself has to be installed, too, of course.
- cd ./application/ // Depending on the framework ... in our case ZF3
- composer install // Install dependencies once 
- vendor/bin/phpstan analyze -l 0 -c phpstan.neon . --memory-limit=1G
// Last but not least the crucial call
- step:
... // This is where your deployment to the DEV / TEST / whatever environment would come in.

So if phpstan has something to complain about it will return a non-zero exit code and Bitbucket will break the pipeline.

The person who did the bad push will even get a mail and get to fix his errors quickly before anyone notices.

You can test the whole thing by pushing syntactically wrong code on the branch you configured the pipeline for and going to the pipelines overview in Bitbucket.

From there you can get to the individual pipeline jobs and can see the log by clicking on an individual job.

Beispiel Pipeline


from 09.12.19 um 16:53

Hello, my name is Meike. I take care of the EXWE back office and am responsible for our social media channels. All of our articles are meant to make your life easier and help you make decisions. Nevertheless, it can happen that something remains unclear, so: If you have questions about this article you can easily reach me at +49 231 93149827.

We also have the following interesting posts from our tech blog for you on this topic
Why your business needs a landing page

Everything you need to know for your landing page and how you can use it to target customers and direct them to your website

Bootstrap media queries cheat sheet for Bootstrap 3 & Bootstrap 4 & Bootstrap 5

Bootstrap media queries cheat sheet for Bootstrap 3 & Bootstrap 4 & Bootstrap 5 - tips and tricks about Bootstrap from our software developers.

How do I find the best software service provider for my business?

You need support in the area of software development and are looking for the right software service provider? Find the most important criteria here.

Schreibe EXWE auf WhatsApp