PHP development in a devcontainer with preinstalled code quality tools
Stop fighting code quality issues. Set up a consistent PHP development environment instantly! This step-by-step guide shows you how to configure a VSCode Devcontainer with preinstalled quality and analysis tools.
This article is part of the Coding using a devcontainer series:
- PHP development in a devcontainer with preinstalled code quality tools
- Install a PHP Docker environment in a matter of seconds
- Docker - Python devcontainer
- Docker - Easy setup of Python under Windows
- Docker - Even easier setup of Python
- One Docker Image for Production and Devcontainers - The Clean Way
- Level Up Your Presentations with Quarto, reveal.js, Decktape, Docker and DevContainers
Changelog

This long, step-by-step article builds a VSCode devcontainer for PHP with a full set of preinstalled code-quality tools: PHP-CS-Fixer and PHPCBF/PHPCS for auto-formatting on save, SonarLint and Intelephense for real-time bug detection, and Rector for automated codebase upgrades and refactoring suggestions — all configured once in devcontainer.json and Dockerfile so any team member gets the identical environment.
Let's imagine one of the following situations:
-
You are working with several people on the same PHP project and you notice that one person or another does not comply with your quality criteria when it comes to the layout of the code. You like to use four spaces for indentation, you want the brace that marks the start of a function to be on the line, you don't want to see any more useless spaces at the end of the line, ... and damned! you notice that some people don't care about this.
-
You are working alone on a project and don't want to lose time to configure your VSCode experience. You wish to start very quickly with a lot of tools already installed in VSCode so you can put your focus to the code, not the editor.
The ultimate solution: using a devcontainer in VSCode.
By using a devcontainer, you (and your team colleague) will use a preinstalled environment and everyone will have exactly the same. You'll save yourself a lot of time by not having to configure your system, and you'll be able to start coding straight away, supported by a range of quality analysis tools.
What a devcontainer does for you
Here is a PHP file as I saved it, with my worst habits on display — random indentation, trailing spaces (shown as dots), braces wherever they landed:
Now the same file, after a single CTRL+S inside the devcontainer. I changed nothing; I only saved:

Nothing was installed on my machine to make that happen — no PHP, no Composer, no formatter binary. And the colleague who clones the repository gets the exact same behavior, without being asked to configure anything.
Why it works
- The Docker image carries the tools. PHP-CS-Fixer, PHPCS/PHPCBF and Composer are downloaded when the image is built, so they exist inside the container and nowhere else.
devcontainer.jsoncarries the editor. It lists the VSCode extensions to install and the settings to apply — including "format this file on save, with that binary, using that config file".- VSCode attaches itself to the container. Your editor keeps running on your machine, but everything it executes happens inside the container. That's why a path like
/usr/local/bin/php-cs-fixer.pharis perfectly valid even though it doesn't exist on your disk.
You just want the ready-made environment?
Then skip this whole tutorial and go to Install a PHP Docker environment in a matter of seconds: same result, three commands, no explanations.
Get the files
This article is written in the form of a step-by-step tutorial. If you don't want to take the time to create the configuration files yourself and download them directly, please run the commands below in a Linux console. You'll then get all the files and every time the tutorial below tells you to create a file, you'll already have it.
Download the very latest version
The php_devcontainer repository will evolve over time. If you wish to download the latest version and not the one frozen for this article, please use the following commands and not the ones above:
1. Create the project
Let's start immediately a new project by creating a temporary directory by running mkdir /tmp/devcontainer_php && cd $_.
Since we'll create a few files, please run code . to start Visual Studio Code (aka VSCode).
Create a new file called index.php with the PHP code below:
<?php
function sayHello( ) { $text="Hello World!"; return $text; }
echo sayHello();This script defines a simple function called sayHello that creates a string saying 'Hello World!' and returns it, then immediately calls that function and outputs the result to the screen.
That's the file shown at the beginning of this article, in its before state: VSCode saved it exactly as it was, with a layout that was just disgusting, extra whitespace included.
We can run that script in a browser by running docker run -d -p 80:80 -u $(id -u):$(id -g) -v .:/var/www/html php:8.2-apache (read The easiest way to run a PHP script / website post if a refresh is needed).
As you can expect, the script is running fine:
Stop reading here if ...
... you're one of those people who thinks The script works, doesn't it? So why all the noise?, please turn off your computer and promise never to touch a single line of code again. Even if the code work ... it stinks.
In this article we'll learn how to configure Visual Studio Code to come with preinstalled extensions and configured for your targeting version of PHP (would be 8.2 but you can change this very easily).
The technique we'll use is called devcontainer.
A devcontainer is a pre-configured, isolated development environment running in a Docker container, offering consistent tools and settings across different machines.
2. Install first the ms-azuretools.vscode-docker VSCode extension
In VSCode, please press CTRL+SHIFT+X to open the Extensions pane and search for the Microsoft Docker extension called ms-azuretools.vscode-docker and if you don't have it yet, please install and enable it.
3. Create the devcontainer.json file
The next step is to create a folder called .devcontainer and, there, a file called devcontainer.json with the content below.
In the rest of this article, we'll come back to this file.
{ "name": "php_devcontainer", "build": { "dockerfile": "Dockerfile", "args": { "PHP_VERSION": "8.2", "COMPOSER_VERSION": "2.5.5", "PHPCSFIXER_INSTALL": "true", "PHPCSFIXER_VERSION": "3.46.0", "PHPCBF_INSTALL": "true", "PHPCBF_VERSION": "3.7.2" } }, "workspaceMount": "source=${localWorkspaceFolder},target=/var/www/html,type=bind", "workspaceFolder": "/var/www/html", "remoteUser": "docker", "customizations": { "vscode": { "settings": { "[php]": { "editor.formatOnSave": true, "editor.defaultFormatter": "junstyle.php-cs-fixer" }, "editor.codeActionsOnSave": { "source.fixAll": true }, "editor.formatOnSave": true, "editor.renderWhitespace": "all", "files.autoSave": "onFocusChange", "files.eol": "\n", "files.insertFinalNewline": true, "files.trimTrailingWhitespace": true, "intelephense.environment.phpVersion": "8.2", "intelephense.telemetry.enabled": false, "php-cs-fixer.config": "/var/www/html/.config/.php-cs-fixer.php", "php-cs-fixer.executablePath": "/usr/local/bin/php-cs-fixer.phar", "php-cs-fixer.onsave": true, "php-cs-fixer.rules": "@PSR12", "php.validate.executablePath": "/usr/local/bin/php", "phpsab.executablePathCBF": "/usr/local/bin/phpcbf.phar", "phpsab.executablePathCS": "/usr/local/bin/phpcs.phar", "phpsab.fixerEnable": true, "phpsab.snifferShowSources": true, "phpsab.standard": "/var/www/html/.config/phpcs.xml", "telemetry.telemetryLevel": "off", "terminal.integrated.profiles.linux": { "bash": { "path": "/bin/bash", "icon": "terminal-bash" } }, "terminal.integrated.defaultProfile.linux": "bash" }, "extensions": [ "bmewburn.vscode-intelephense-client", "junstyle.php-cs-fixer", "ms-azuretools.vscode-docker", "ValeryanM.vscode-phpsab", "zobo.php-intellisense" ] } }, "postCreateCommand": "composer require rector/rector --dev"}This is a development container configuration for PHP 8.2 that sets up a Docker-based development environment with code formatting and linting tools. It configures VS Code to automatically format PHP code using PHP-CS-Fixer and PHP CodeSniffer, installs necessary PHP development extensions, and runs Composer to add the Rector tool after the container starts.
VS Code gives such a container a random name like serene_ganguly. To force a stable name and group it in Docker Desktop, see Devcontainer - Name Your Container and Group It in Docker Desktop.
4. Create the Dockerfile file
Please continue and create a second file called Dockerfile with this content:
ARG PHP_VERSION=8.2
ARG COMPOSER_HOME="/var/cache/composer"ARG COMPOSER_VERSION=2.5.5
ARG PHPCSFIXER_INSTALL=trueARG PHPCSFIXER_VERSION=3.46.0
ARG PHPCBF_INSTALL=trueARG PHPCBF_VERSION=3.7.2
ARG TIMEZONE="Europe/Brussels"
ARG OS_USERID=1000ARG OS_USERNAME="docker"
FROM composer:${COMPOSER_VERSION} AS composer_base
FROM php:${PHP_VERSION}-fpm
ARG TIMEZONEENV TZ=${TIMEZONE}
ARG PHPCSFIXER_INSTALLARG PHPCSFIXER_VERSION
RUN set -e -x \ && if [ "$PHPCSFIXER_INSTALL" = "true" ] ; then \ printf "\e[0;105m%s\e[0;0m\n" "Install php-cs-fixer.phar ${PHPCSFIXER_VERSION}" \ && curl -L https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/download/v${PHPCSFIXER_VERSION}/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer.phar \ && chmod +x /usr/local/bin/php-cs-fixer.phar ; \ fi
ARG PHPCBF_INSTALLARG PHPCBF_VERSION
RUN set -e -x \ && if [ "$PHPCBF_INSTALL" = "true" ] ; then \ printf "\e[0;105m%s\e[0;0m\n" "Install phpcbf.phar ${PHPCBF_VERSION}" \ && curl -L https://github.com/squizlabs/PHP_CodeSniffer/releases/download/${PHPCBF_VERSION}/phpcbf.phar -o /usr/local/bin/phpcbf.phar \ && chmod +x /usr/local/bin/phpcbf.phar \ && curl -L https://github.com/squizlabs/PHP_CodeSniffer/releases/download/${PHPCBF_VERSION}/phpcs.phar -o /usr/local/bin/phpcs.phar \ && chmod +x /usr/local/bin/phpcs.phar ; \ fi
ARG COMPOSER_HOMEENV COMPOSER_HOME=${COMPOSER_HOME}
RUN set -e -x \ && mkdir -p "${COMPOSER_HOME}/cache/files" \ && mkdir -p "${COMPOSER_HOME}/cache/vcs" \ && chmod -R 777 "${COMPOSER_HOME}"
COPY --from=composer_base /usr/bin/composer /usr/bin/composer
ARG OS_USERIDARG OS_USERNAME
RUN set -e -x \ && useradd --password '' -G www-data,root -u ${OS_USERID} -l -d "/home/${OS_USERNAME}" "${OS_USERNAME}" \ && mkdir -p "/home/${OS_USERNAME}" \ && chown -R "${OS_USERNAME}":"${OS_USERNAME}" "/home/${OS_USERNAME}"
RUN set -e -x \ && printf "\e[0;105m%s\e[0;0m\n" "Install Linux binaries" \ && apt-get update -yqq \ && apt-get install -y --no-install-recommends curl libzip-dev zip unzip \ && docker-php-ext-install zip \ && docker-php-source delete \ && apt-get clean \ && rm -rf /tmp/* /var/list/apt/*
WORKDIR /var/www/htmlThis Dockerfile creates a PHP 8.2 development image with code quality tools (PHP-CS-Fixer and PHPCBF), Composer for dependency management, and a non-root user for running the application. It sets up directories, installs necessary Linux packages, and configures environment variables like timezone and Composer cache location.
5. Reopen in the container
So, we've now three files: a disgusting index.php, .devcontainer/devcontainer.json and .devcontainer/Dockerfile.
What we will do now is to jump in a Docker container. Take a look at the bottom left of your screen:

You'll see something like, in my case, WSL: Ubuntu-20.04. It can be different on your computer depending on your operating system. What VSCode says here is I'm coding on my machine.
Click on that status or, as an alternative way, press CTRL+SHIFT+P to open the Command Palette and then search for Reopen in Container and press Enter.

Visual Studio Code will do a lot of stuff and, the first time, it can take a few minutes.

6. Working in the container
Now the magic happens: please reopen the so badly formatted index.php file and save the file without any changes. Just press CTRL+S and tadaaa 🎉🎉🎉 — you get the reformatted file shown at the beginning of this article.
How is this possible?
This is because we taught VSCode to use a specific formatter for our PHP file and we told him to format the file each time it's saved. And do you know where we did it? In our .devcontainer/devcontainer.json and .devcontainer/Dockerfile files, of course!
This is why we're working in a Devcontainer! We've done the configuration once and for all, so all we have to do is reuse it, whatever the PHP project.
The tools (pick the ones you need)
The five sections below are independent from each other: each one covers a tool that is already installed in the container, what it catches, and where it was declared. Read the ones that matter to you and skip the rest — the environment works with all of them enabled anyway.
The first tool we've used here is PHP-CS-Fixer
When we save a file in VSCode, we have configured a formatter which will adapt the layout of the file according to the rules we have determined. For example, the number of spaces for indentation or the fact that braces must be aligned (and many other things). That tool is PHP-CS-Fixer.
But let's understand when PHP-CS-Fixer was installed...
Please reopen your .devcontainer/devcontainer.json file and look at the highlighted lines below:
{ // ... "build": { "dockerfile": "Dockerfile" }, // ... "customizations": { "vscode": { "settings": { // highlight-next-line "[php]": { // highlight-next-line "editor.formatOnSave": true, // highlight-next-line "editor.defaultFormatter": "junstyle.php-cs-fixer" }, // ... // highlight-next-line "files.autoSave": "onFocusChange", // ... // highlight-next-line "php-cs-fixer.config": "/var/www/html/.config/.php-cs-fixer.php", // highlight-next-line "php-cs-fixer.executablePath": "/usr/local/bin/php-cs-fixer.phar", // highlight-next-line "php-cs-fixer.onsave": true, // highlight-next-line "php-cs-fixer.rules": "@PSR12", //... }, "extensions": [ // ... // highlight-next-line "junstyle.php-cs-fixer", ] } }}This configuration file sets up a development environment with Docker and Visual Studio Code customizations. It enables automatic code formatting for PHP files using the php-cs-fixer tool, which will run whenever you save a file and follows the PSR12 coding standard. The configuration also specifies where the formatter tool is located and which VSCode extension to use.
So, we're asking VSCode to, for PHP files, run a formatter when the file is saved and the tool to use for the formatting is junstyle.php-cs-fixer. Look directly at the end, in the extensions section, we instruct VSCode to install that extension.
We also instruct VSCode to automatically save the file as soon as the focus changes i.e. for instance, if we select another file, if we click elsewhere (no more in the editor), ...
Finally, junstyle.php-cs-fixer requires some settings that we need to initialize like f.i. php-cs-fixer.executablePath.
Right now, you can think: Wait a minute, I don't have the /usr/local/bin/php-cs-fixer.phar file on my computer. And you are probably right but think again: are you coding on your computer or are you inside a Docker container?
By running VSCode in a container, you're no more on your machine.
And now, you need to take a look at the third file we've created: Dockerfile.
Please reopen the .devcontainer/Dockerfile file and look at the highlighted lines below:
# highlight-next-lineARG PHP_VERSION=8.2
# ...
# highlight-next-lineARG PHPCSFIXER_INSTALL=true# highlight-next-lineARG PHPCSFIXER_VERSION=3.46.0
# ...
# highlight-next-lineFROM php:${PHP_VERSION}-fpm
# ...
# highlight-next-lineARG PHPCSFIXER_INSTALL# highlight-next-lineARG PHPCSFIXER_VERSION
# highlight-next-lineRUN set -e -x \ # highlight-next-line && if [ "$PHPCSFIXER_INSTALL" = "true" ] ; then \ # highlight-next-line printf "\e[0;105m%s\e[0;0m\n" "Install php-cs-fixer.phar ${PHPCSFIXER_VERSION}" \ # highlight-next-line && curl -L https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/download/v${PHPCSFIXER_VERSION}/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer.phar \ # highlight-next-line && chmod +x /usr/local/bin/php-cs-fixer.phar ; \ # highlight-next-line fi
# ...This Dockerfile snippet sets up a PHP-FPM container and optionally installs PHP-CS-Fixer (a code formatting tool). It defines configuration arguments at the top, pulls a PHP base image, then conditionally downloads and installs the php-cs-fixer.phar executable if the install flag is enabled.
That file will instruct VSCode to download a php:8.2-fpm Docker image and to install PHP-CS-Fixer (since PHPCSFIXER_INSTALL has been set to true).
The version 3.46.0 of the PHP-CS-Fixer executable will be downloaded and stored, in the Docker image, as usr/local/bin/php-cs-fixer.phar. This is why, in our devcontainer.json we can write "php-cs-fixer.executablePath": "/usr/local/bin/php-cs-fixer.phar",, because we've already downloaded and installed the executable.
Need to check?
Still in VSCode, open a terminal by pressing CTRL+´ and, in the prompt, type ls -l /usr/local/bin. As you can see, php-cs-fixer.phar is well there. It was downloaded and installed by VSCode when you jumped into the container.

PHP-CS-FIXER - Configuration file
The .devcontainer/devcontainer.json file also contains this line: "php-cs-fixer.config": "/var/www/html/.config/.php-cs-fixer.php".
So, we can have our own configuration file for PHP-CS-Fixer (with our own rules) and that file has to be created in a folder called .config and should be named .php-cs-fixer.php.
Let's try and this time we'll make sure all our PHP files will have a header block with our copyright or license information or anything else.
In VSCode, please create the .config folder and there the .php-cs-fixer.php file with this content:
<?php
$finder = PhpCsFixer\Finder::create() ->in('.') ->exclude([ '.config', '.devcontainer', '.git', 'node_modules', 'vendor' ]);
$header = file_get_contents(__DIR__ . '/licenseHeader.txt');
$config = new PhpCsFixer\Config();
return $config->setRules( [ '@PSR12' => true, 'header_comment' => [ 'header' => rtrim($header, "\r\n"), 'location' => 'after_declare_strict', 'comment_type' => 'PHPDoc', ], ]) ->setCacheFile('/tmp/.php-cs-fixer.cache') ->setIndent(' ') ->setLineEnding("\n") ->setFinder($finder);This file configures PHP CS Fixer, a tool that automatically formats PHP code to follow style standards. It sets up which files to scan (everything except common directories like vendor and node_modules), loads a license header text file, then applies PSR12 coding standards along with a custom license header comment at the top of each PHP file.
Please also create a second file called licenseHeader.txt file with the content you wish, f.i.:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>.
This is a standard GPL (GNU General Public License) header that appears at the top of open-source software files. It explains that the code is free to use, modify, and redistribute under specific legal terms, but comes with no warranties or guarantees.
Now, please reopen your index.php file and just press CTRL+S to save the file again and tadaaa...

As you can see, when formatting the file, PHP-CS-Fixer will also, now, injects our header.
Lots of options are explained here
Please go to https://mlocati.github.io/php-cs-fixer-configurator or https://github.com/PHP-CS-Fixer/PHP-CS-Fixer to learn more about the php-cs-fixer configuration file options.
PHP-CS-FIXER - Fix all files at once
One step further: is it possible to run PHP-CS-Fixer not just on the file being edited in VSCode but on all files in your repository. Yes, of course.
In VSCode, please press CTRL+´ (or click on the View menu then Terminal) to open a terminal. Make sure you're in the /var/www/html folder (run cd /var/www/html if needed) and run the following command:
This will run PHP-CS-Fixer on all your codebase (.). If you want to exclude some folders, open the .config/.php-cs-fixer.php file and update the exclude array.
The second set of tools is PHPCBF and PHPCS
Our Dockerfile container was configured to download and install PHP_CodeSniffer tools called phpcbf and phpcs.
phpcbf is a Code beautifier tool: just like PHP-CS-Fixer, phpcbf will make some minor changes to your files accordingly to enabled rules.
For instance, please update your index.php script with this content:
<?php
function sayHello($firstname){ if($firstname == "") { $text = "Hello World!"; }else{ $text = "Hello " . $firstname; }
return $text;}
echo sayHello();This code defines a function that greets someone by name, or says 'Hello World!' if no name is provided. It then calls that function and prints the result.
What's wrong with this syntax? Almost nothing but ... the standard (PSR12) ask to put one space after the if keyword and one space before and after else.
PHP-CS-Fixer will be called when you save the file
Did you've noticed? You have copied/pasted the script below where there were no spaces before and after the else keyword. But as soon as you save the file, boum, the file is refactored by PHP-CS-Fixer and, among other things, spaces have been added before and after else but not for if. This is because, actually, there is not ONE MAGIC TOOL for everything. Sometimes PHP-CS-Fixer can't make some changes but PHPCBF well or the opposite.
Like for PHP-CS-Fixer, we already have installed PHPCBF and PHPCS in our container. See your .devcontainer/devcontainer.json file:
{ // .. "customizations": { "vscode": { "settings": { // ... // highlight-next-line "phpsab.executablePathCBF": "/usr/local/bin/phpcbf.phar", // highlight-next-line "phpsab.executablePathCS": "/usr/local/bin/phpcs.phar", // highlight-next-line "phpsab.fixerEnable": true, // highlight-next-line "phpsab.snifferShowSources": true, // highlight-next-line "phpsab.standard": "/var/www/html/.config/phpcs.xml", // ... }, "extensions": [ // ... // highlight-next-line "ValeryanM.vscode-phpsab", // ... ] } }}This is a VS Code configuration snippet that sets up PHP code quality tools. It configures the phpsab extension (which provides PHP CodeSniffer and PHP Code Beautifier integration) with paths to the executable scripts, enables automatic fixing, and points to a custom coding standard ruleset.
And the two executables /usr/local/bin/phpcbf.phar and /usr/local/bin/phpcs.phar have been downloaded thanks to your Dockerfile. Just take a look if needed.
PHPCS and PHPCBF - Configuration file
In VSCode, please create in the .config folder a file called phpcs.xml file with this content:
<?xml version="1.0"?><ruleset name="MyRules"> <arg name="colors" /> <arg name="extensions" value="php" /> <arg value="s" /> <arg value="p" /> <rule ref="PSR12" /></ruleset>This is a PHP CodeSniffer configuration file that sets up code style rules for a project. It enables colored output, restricts checking to PHP files, configures verbosity and progress reporting, and applies the PSR12 coding standard which is a widely-used set of PHP formatting guidelines.
Now, since we have defined our coding standard (PSR12 here), just display the index.php script again:
Open a terminal by pressing CTRL+´ (or by clicking on the View menu then Terminal) and run this command:
And you'll get this output:
FILE: /var/www/html/index.php
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | WARNING | [ ] A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do
| | both. The first symbol is defined on line 18 and the first side effect is on line 29. (PSR1.Files.SideEffects.FoundWithSymbols)
20 | ERROR | [x] Expected 1 space(s) after IF keyword; 0 found (Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
There is one warning and one error concerning coding convention violation and the nice thing is phpcbf can fix some.
Now, run almost the same command but no more phpcs (to detect violations) but phpcbf (to fix them -the ones that can be fixed automatically-).
You'll get this:
PHPCBF RESULT SUMMARY ---------------------------------------------------------------------- FILE FIXED REMAINING ---------------------------------------------------------------------- /var/www/html/index.php 1 1 ---------------------------------------------------------------------- A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE ----------------------------------------------------------------------
And, indeed, if you look at your PHP script, now, there is a space after the if keyword.
PHPCS and PHPCBF - All files at once
You've probably already understood the syntax. Just use the . to scan the entire codebase:
- To fix violations, run
/usr/local/bin/phpcbf.phar --standard=/var/www/html/.config/phpcs.xml . - To scan remaining violations, run
/usr/local/bin/phpcs.phar --standard=/var/www/html/.config/phpcs.xml ..
The third tool is SonarLint
Here too, SonarLint was already installed in our Docker container thanks our .devcontainer/devcontainer.json file:
{ // ... "customizations": { "vscode": { "extensions": [ // ... // highlight-next-line "sonarsource.sonarlint-vscode", // ... ] } }}This is a configuration object that specifies Visual Studio Code extensions to install, including SonarLint which is a code quality analysis tool. It's part of a larger settings file that customizes the development environment.
It's only a VSCode extension
SonarLint doesn't need an external tool; it's a stand-alone VSCode extension so no need to foresee something in our Dockerfile.
So, please reopen the index.php file you have, and now pay attention to the echo sayHello() line:
<?php
function sayHello($firstname){ if ($firstname == "") { $text = "Hello World!"; } else { $text = "Hello " . $firstname; }
return $text;}
echo sayHello();This code defines a function that greets someone by name if provided, otherwise greets the world generically. It then calls that function and prints the result to the screen.

Move the mouse cursor on the sayHello() variable and get extra information from SonarLint as a popup. You can, too, press CTRL+SHIFT+M to show the Problems pane (or menu View then Problems) to get the list of every SonarLint problems encountered in the current file and, too, in any file you'll open from now.

One solution would be to update the prototype of sayHello like this: function sayHello($firstname = "").
SonarLint prevents bugs!
Unlike the tools we've seen here above, it's no longer about formatting, but about alerting the developer to potential bugs and fatal errors as they write their code.
The fourth tool is Intelephense
Next to SonarLint, there is also Intelephense who will also detect a certain number of potential bugs.
In the previous example; when the prototype was function sayHello($firstname), Intelephense was also alerting the developer about the missing parameter.
<?php
function sayHello($firstname){ if($firstname == "") { $text = "Hello World!"; } else { $text = "Hello " . $firstname; }
return $text;}
echo sayHello();This code defines a function that takes a person's first name and returns a personalized greeting. If no name is provided (empty string), it returns a generic 'Hello World!' message; otherwise, it returns 'Hello' followed by the provided name. The function is then called and its result is printed to the screen.

We need several tools
This example illustrates this point: to date, in February 2024, we still have to juggle with several extensions and tools to achieve clean, bug-free code.
The best for last, the fifth tool is Rector
Rector is a tremendous application to scan and automatically upgrade your codebase to a given version of PHP and this means, too, to inspect how you're coding.
Perhaps you're still using a way of programming that's worthy of PHP 5.4 but unworthy of a modern developer who's up to date with the latest developments.
Rector is my private coach
I really LOVE Rector since he'll help me to learn new features of PHP. When I run it on any of my codebase, I can see where I can improve my code by refactoring some part and do better.
I REALLY LOVE RECTOR.
This time, we'll need to install Rector for our project and for this, we'll follow the official documentation: https://github.com/rectorphp/rector?tab=readme-ov-file#install but ... it's already done in our .devcontainer/devcontainer.json file.
If you reopen it, take a look at the postCreateCommand node:
{ // ... "customizations": { // ... }, // highlight-next-line "postCreateCommand": "composer require rector/rector --dev"}This is a configuration object (likely for a development container or similar setup tool) that specifies a command to run after the container or environment is created. The command installs Rector, a PHP refactoring tool, as a development dependency using Composer.
Rector will be added to the composer.json file (and the file will be created if not yet present).
`Composer` has been installed in our `Dockerfile`
If you're thinking Yes, but I haven't installed composer..., well, it's wrong. We installed it in our container. See again your .devcontainer/Dockerfile if needed.
Rector - configuration file
Skip this chapter if you have already downloaded the configuration files
The second step in the installation guide asks us to run the command below. When prompted, please answer yes to create your rector.php configuration file.
Finally, open the rector.php configuration file and update it like this:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
return RectorConfig::configure() ->withSkip( [ '.config', '.devcontainer', '.git', 'node_modules','vendor' ] ) ->withPhpSets(php82: true) ->withPreparedSets(codeQuality: true, deadCode: true, typeDeclarations: true);This is a Rector configuration file that sets up automated code modernization rules for a PHP project. It tells Rector which directories to skip during analysis, targets PHP 8.2 as the language version, and enables three preset rule sets to improve code quality, remove dead code, and add proper type declarations.
Tell to Rector which PHP version you use
Rector will suggest you changes but to do this, he needs to know which version of PHP you're running. Indeed, Rector won't suggest PHP 8.2 syntax f.i. if you're still running on an older version (see PHP Version Features).
On my side, I prefer to put all configuration files in a .config folder
You've noticed that Rector has created his configuration file in the root directory of your project. I prefer to move it to the .config folder.
Run Rector
To illustrate what Rector can do, please edit the index.php with this content, once again:
<?php
function sayHello($firstname = ""){ if($firstname == "") { $text = "Hello World!"; } else { $text = "Hello " . $firstname; }
return $text;}
echo sayHello();This function takes an optional first name and returns a personalized greeting. If no name is provided, it returns 'Hello World!'; otherwise it returns 'Hello' followed by the provided name. The code then calls the function with no arguments and prints the result.
In a terminal, run vendor/bin/rector process index.php --dry-run --config .config/rector.php as we did before.

Take a look at the image above. In red, your current code and, yes, we knew that, didn't we, the code quality is poor.
Indeed, one concept in clean code approach is to avoid using the if ... else ... conditional statement. Most probably, we don't need else (see https://phpmd.org/rules/cleancode.html#elseexpression).
Rector suggests removing completely the if .. else ... expression and to use a ternary expression. And, also, to directly return the value instead of using a dummy $text variable.
And that's correct, we can write return $firstname == "" ? "Hello World!" : "Hello " . $firstname; to get the exact same thing.
And if you think it's over, you're wrong. Rector now looks at our function parameter and understands it's a string so he'll suggest adding a type hint and, too, to add a return type for the same reason.
Since we've analyzed the suggestions of Rector and we agree with, please rerun the same command but, this time, without, the --dry-run flag.
Rector has updated our index.php file! Now, our function is just one line and no more seven. Nice improvement!
Our new, improved, code is now:
<?php
function sayHello(string $firstname = ""): string{ return $firstname == "" ? "Hello World!" : "Hello " . $firstname;}
echo sayHello();This code defines a function that greets someone by name, or says 'Hello World!' if no name is provided. It then calls that function and prints the result to the screen.
Rector is absolutely brilliantly powerful. Learn more about it by reading carefully his documentation site: https://getrector.com/documentation/.
The programmer has the last word, but for how much longer?
We can do one more improvement by extracting the Hello prefix. Rector has not yet seen this, but for how long?
<?php
function sayHello(string $firstname = ""): string{ return "Hello " . ($firstname == "" ? "World!" : $firstname);}
echo sayHello();This code defines a function that greets someone by name, defaulting to 'World' if no name is provided. When called without arguments, it outputs 'Hello World!'
Rector - All files at once
Back in your terminal and run the command below to scan your entire codebase.
Dry-run means no changes are made, just displayed
When running Rector using vendor/bin/rector process . --dry-run --config .config/rector.php, no files will be rewritten. Rector will only suggest changes.
If you allow Rector to make changes to your files, run vendor/bin/rector process . --config .config/rector.php instead but, here, you need to be sure it's OK.
Code versioning using f.i. GitHub
If you're using a code versioning system, you can push your actual codebase to GitHub, create a new branch like for instance refactoring then run Rector safely on your disk. If something should be broken, you can always retrieve your source before any changes.
And we can add more tools
There are still several code analysis tools for PHP:
You can certainly also add them to your container.
Extensions
Some extensions will be automatically installed:
- bmewburn.vscode-intelephense-client - PHP code intelligence for Visual Studio Code,
- junstyle.php-cs-fixer - PHP CS Fixer extension for VS Code, php formatter, php code beautify tool, format html,
- ms-azuretools.vscode-docker - Makes it easy to create, manage, and debug containerized applications,
- sonarsource.sonarlint-vscode - Linter to detect & fix coding issues locally in JS/TS, Python, PHP, Java, C, C++, C#, Go, IaC. Use with SonarQube & SonarCloud for optimal team performance.,
- streetsidesoftware.code-spell-checker - Spelling checker for source code,
- usernamehw.errorlens - Improve highlighting of errors, warnings and other language diagnostics,
- ValeryanM.vscode-phpsab - PHP Sniffer & Beautifier for Visual Studio Code and
- zobo.php-intellisense - Advanced Autocompletion and Refactoring support for PHP
Conclusion
Remember the colleague from the beginning of this article, the one who doesn't indent the way you do? There's nothing left to ask them. The rules aren't in a wiki page or in a code-review comment anymore, they're in two versioned files — devcontainer.json and Dockerfile — and they apply the moment the project is opened, on every machine, identically.
That's really the shift: the environment stops being something each developer builds, and becomes something the repository provides.
If you'd rather run these tools on demand instead of baking them into an image, see jakzal/phpqa. And to make sure none of this development tooling ever ends up in your production image, read One Docker Image for Production and Devcontainers - The Clean Way.
This article is part of the Coding using a devcontainer series:
- PHP development in a devcontainer with preinstalled code quality tools
- Install a PHP Docker environment in a matter of seconds
- Docker - Python devcontainer
- Docker - Easy setup of Python under Windows
- Docker - Even easier setup of Python
- One Docker Image for Production and Devcontainers - The Clean Way
- Level Up Your Presentations with Quarto, reveal.js, Decktape, Docker and DevContainers
Loading comments…





