Logo BrocksiNet

BrocksiNet

Try to clean the web

Configure PHPStan in PHPStorm to use PHP from Docker Container

In PHPStorm you can set up PHP > Quality Tools that will check your code as you code. This will help you write clean code. I suspect that most developers use a local configuration for these PHP > Quality Tools. But if you already have some Docker containers set up for your project, you should use the same PHP version and the same tool version, e.g. PHPStan, installed in this project via Composer. In this post I will guide you through the setup with many screenshots. Requirements: The quality tools you wanna use are already installed via Composer (if not check how to install them). Also docker is installed and working.

1.) With the command below you can check on your Terminal if the containers for the project you wanna set up are running (if not docker-compose up -d could help).

docker ps --format "{{.Names}} | {{.Image}} | {{.Status}}"

The output of the command should look similar like in the picture below.

Output from docker command that shows the running docker containers

2.) Open Settings > Plugins. Check if the needed plugins (Docker and PHP Docker) in PHPStorm are activated.

Screens shows needed plugins for docker and php CLI Interpreter configuration

3.) We should test if the docker connection is working. Navigate in Settings to Build, Execution, Deployment > Docker. On the right side of the window you should see a message with Connection successful.

Docker Connection is successful in PHPStorm Configuration

4.) Navigate in Settings to Languages & Frameworks > PHP. At the CLI Interpreter line you have to click on the three dots for adding an new CLI Interpreter. We want to add our docker container as new CLI Interpreter.

Screen shows the way in settings to add a new PHP CLI Interpreter

5.) In the new dialog window you should click on the plus icon in the upper left corner and select From Docker, Vagrant, VM, WSL, Remote ... as a new CLI Interpreter.

Select From Docker, Vagran, VM, WSL, Remote... as new CLI Interpreter

6.) Now we need to configure the new CLI interpreter. To do this, you should set a name, select the service running PHP, in our case app_server, then change the lifecycle to Connect to existing container and set the path to the PHP executable file for your project (/usr/local/bin/php). If you click the refresh icon next to the PHP executable path, you can check which PHP version was found on the Docker service/container.

Picture shows the CLI Interpreter configuration for PHP Docker Container.

7.) Let's navigate in Settings to Languages & Frameworks > PHP > Quality Tools. To creat a new configuration for PHPStan, that is using our new CLI Interpreter we created before, you should click again on the three dots next to the PHPStan current selected configuration.

Create a new configuration for PHPStan in Quality Tools settings.

8.) In the new dialog you click again on the plus icon (in the left upper corner) and then you select the new CLI Interpreter, we created before, in our case it is named app_server. After that you should update the path to PHPStan in our case it is vendor/bin/phpstan and then click on the validate button to see if PHPStan was found. You will see the PHPStan version at the bottom of this dialog. If everything is fine, just click ok.

Configure the PHPStan path inside the Docker container and validate that PHPStan is found.

9.) Before you leave the settings in Languages & Frameworks > PHP > Quality Tools, just make sure that the new Configuration is selected below PHPStan. If this is fine, you can just confirm it with ok and the settings will be closed.

Just double check that the new CLI Interpreter is selected below PHPStan Configuration.

10.) To validate that PHPStan is correct working just remove for example a return type from a PHP Class and look in the problems tab about the PHPStan Warning. The picture below shows you the problems of the current file and you also see the PHPStan validation.

Picture shows PHPStan validation in problems tab of PHPStorm.

11.) Optional: You can configure which inspections you wanna use. Just navigate to settings then Editor > Inspections > Open Quality tools in the Inspections list on the right side of the window.

Configure the PHP Quality tools inspections you want to use in PHPStorm settings.
Released - 10.09.2022

Comments


About the author

Bild Bjoern
Björn MeyerSoftware Engineer
Björn is interested in the details of web technologies. He started with Joomla, Drupal, Typo3, vBulletin, Fireworks and Photoshop. Today his focus is on PHP for example with Magento 2, Shopware 6 and Symfony. He also likes JavaScript with Angular, React, VueJs and TypeScript. In his free time, he seeks constant balance with swimming, running and yoga.