Standard Setup for PHP projects
Created at 2020-05-31 Updated at 2021-08-08 Category Coding Standards
Note: This is my standard setup from 2020, a newer version from 2021 is now available Standard setup for PHP projects 2021
The focus in setting up PHP Projects in PhpStorm, in particular a Laravel project.
Composer with PHP with Easy Coding Standard (ECS), PHPUnit, and PhpStan. Also, some simplify projects to allow the output to be displayed in PHPStorm.
Below are some other options, including psalm, Rector and PHP Mess Detector.
Example snippet of a composer.json
.editorconfig file, normally found in the root of a project.
root = true
Xdebug is used to debug PHP script, however due to how it analyses the script it makes PHP very slow, debugging should only enable it when debugging is required.
Add the following to the php.ini:
Note: PhpStorm will automatically enable xdebug, when the following is configure and debugging is enable, there is no need to leave xdebug enabled in php.ini. To temporarily disable xdebug comment out and the line. Using Laragon: Menu > PHP > Quick settings > xdebug, click to toggle on and off. This will be the same as manually adding a ; to the php.ini file:
TL;DR (parts copied from above PhpStorm Tutorials, full version below)
In the Settings/Preferences dialog Ctrl+Alt+S, select Languages & Frameworks | PHP.
Check the Xdebug installation associated with the selected PHP interpreter:
a. On the PHP page, choose the relevant PHP installation from the CLI Interpreter list and click the Browse button next to the field.
b. The CLI Interpreters dialog that opens shows the following:
i. The version of the selected PHP installation. (e.g. D:\laragon\bin\php\php-7.4.4-Win32-vc15-x64\php.exe)
ii. The name and version of the debugging engine associated with the selected PHP installation (Xdebug). (e.g. D:\laragon\bin\php\php-7.4.4-Win32-vc15-x64\ext\php_xdebug.dll)
iii Once set click OK.
Define the Xdebug behaviour. Click Debug under the PHP node (Languages & Frameworks | PHP | Debug.). On the Debug page that opens, specify the following settings in the Xdebug area:
- Check the Pre-configuration has been complete (Xdebug ins installed, Install browser toolbar, Enable listening for PHP Debug connections and Start debug session)
- Under the XDebug heading, Choose the Debug Port field (default is 9000). This must be exactly the same port number as specified in the php.ini file: xdebug.remote_port =9000
- To accept any incoming connections, select the Can accept external connections checkbox. (Default)
- Select the Force break at the first line when no path mapping is specified checkbox to have the debugger stop as soon as it reaches and opens a file that is not mapped to any file in the project on the Servers page. (Default)
- Click OK
Set a breakpoint in the file to be inspected.
Open public/index.php, wait for the file to open, then in to top right there will be a floating bar with a list of browsers. Click Chrome (Alt+F2 + Choose Chrome)
Chrome browser will launch and open the projects webpage. If the Debug icon isn’t already green, click the Debug icon for Xdebug helper extension. From the drop down list click Debug option, the icon will then display green.
Navigate the project’s website, to the page which uses the file that has the breakpoint set. As soon as the page is hit PhpStorm will open (it may be behind the browser).
In PhpStorm, accept the connection (this warning normally only displays the first hit and accepted once).
Debug information will display in the console, including the option to inspect variables and evaluate.
Youtube: Step Into Debugging with PhpStorm by Gary Hockin, 58 min video on Debugging.
PHPStorm has an inspection to set strict mode.
Strict types declaration setting can be found under Type compatibility in the Inspections pane of the
Or double shift search for Missing Strict types declaration tick the inspection.
To run the inspection against the whole project (code base): CTRL + ALT + SHIFT + I or double shift search for
run inspection by name. Search for missing strict type declaration, Enter for whole project. The inspection
will run, click on each file and click Add strict types declaration
Add the Laravel code standard for PHP code sniffer, static analysis for PHPStan and PHPStorm helper.
composer require --dev emielmolenaar/phpcs-laravel
Example script to run phpcs using the phpcs-laravel standard:
- phpDoc generation for Laravel Facades
- phpDocs for models
- PhpStorm Meta file
After updating composer, add the service provider to the
providers array in
To generate helper files, run the following:
php artisan ide-helper:generate
It is a good idea to add the generated files to .gitignore
echo /.phpstorm.meta.php >> .gitignore
In PhpStorm enable the Laravel plugin
Create batch files in the root of the project, which work in combination with the composer scripts.
Runs the composer script for ECS to check the code.
Runs the composer script for ECS to fix the code.
Runs the composer script for Rector for dry run. Adjust rector.yml as required.
Runs the composer script for Rector to fix the code (run dry run first!). Also adjust rector.yml as required.
Runs the composer script for PhpStan to check the code.
Runs the composer script for PhpUnit.
PHP Static Analysis Tool - discover bugs in your code without running it!
- Github: phpstan/phpstan
composer require phpstan/phpstan --dev
PHPStan needs a
phpstan.neon file an example can be found here
The above includes placeholders (buggy/mixed etc) for ignored files or rules. See the documentation for how to add.
Another PhpStan.neon example from LaraStan:
phpstan analyse src tests --level max --error-format checkstyle
Go to Preferences > Tools > External Tools and click + to add a new tool.
- Name: PHPStan (Can be any value)
- Description: PHPStan (Can be any value)
- Program: $ProjectFileDir$/vendor/bin/phpstan.bat (Path to
phpstan.batexecutable; On Windows path separators must be a
- Parameters: check $FilePathRelativeToProjectRoot$ (append –fix to auto-fix)
- Working directory: $ProjectFileDir$
Press Cmd/Ctrl + Shift + A (Find Action), search for phpstan, and then hit Enter. It will run phpstan for all files in the project.
You can also create a keyboard shortcut in Preferences > Keymap to run phpstan.
I have also tried setting up PHPStan as a watcher as follows:
- File type: PHP
- Scope: All Places
- Program: $ProjectFileDir$/vendor/bin/phpstan
- Arguments: analyse
- Working directory: $ProjectFileDir$
- Show console: On error
It runs PHPStan on every save, but only opens the console when PHPStan detects an error. Not as good as integrated analysis, but a good compromise, until Jetbrains can integrate it.
The easiest way to start using PHP CS Fixer and PHP_CodeSniffer with 0-knowledge
composer require symplify/easy-coding-standard --dev
Working example from KnpLabs/DoctrineBehaviors
Example from phpdocumentor\reflection-common\easy-coding-standard.neon
EasyCodingStandard can be used as an External Tool
External Tools and click
+ to add a new tool.
ecs(Can be any value)
easyCodingStandard(Can be any value)
ecsexecutable; On Windows path separators must be a
- Working directory:
A (Find Action), search for
ecs, and then hit Enter. It will run
ecs for the current file.
ecs on a directory, right click on a folder in the project browser go to external tools and select
You can also create a keyboard shortcut in Preferences > Keymap to run
composer require squizlabs/php_codesniffer --dev
Example from phpdocumentor\type-resolver
The PHP Unit Testing framework.
composer require phpunit/phpunit --dev
Example phpunit.xml.dist from Laravel Package Boilerplate, very useful for
echo /build/ >> .gitignore
Example phpunit.xml from livewire\livewire
phpunit.xml from KnpLabs/DoctrineBehaviors (minimalist)
Follow the link from phpmd.org to download the latest version and save the
phpmd.phar file in an accessible location.
Mess detection rules from nesbot\Carbon
1) Download and install the PHP Mess Detector scripts.
To check the PHP Mess Detector installation, switch to the installation directory and run the following command:
If the tool is available, you will get a message in the following format:
PHPMD version <version>
To have code checked against your own custom coding standard, create it. Store the rules and the ruleset.xml file that points to them in the rulesets root directory.
2) Register the local PHP Mess Detector script in PhpStorm:
a. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to Languages & Frameworks | PHP | Quality Tools.
b. On the Quality Tools page that opens, expand the Mess Detector area and click the Browse button next to the Configuration list.
c. In the PHP Mess Detector dialog that opens, specify the location of the
phpmd PHP Mess Detector executable in the PHP Mess Detector path field. Type the path manually or click the Browse button and select the relevant folder in the dialog that opens.
To check that the specified path to
phpmd ensures interaction between PhpStorm and PHP Mess Detector, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the
phpmd --version command. If validation passes successfully, PhpStorm displays the information on the detected PHP Mess Detector version.
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to Languages & Frameworks | PHP | Quality Tools.
On the Quality Tools page that opens, expand the Mess Detector area and click the … (Browse button) next to the Configuration list. The PHP Mess Detector dialog opens showing the list of all the configured PHP Mess Detector scripts in the left-hand pane, one of them is of the type Local and others are named after the PHP interpreters with which the scripts are associated. Click the Add button on the toolbar.
In the PHP Mess Detector by Remote Interpreter dialog that opens, choose the remote PHP interpreter to use the associated script from. If the list does not contain a relevant interpreter, click the Browse button and configure a remote interpreter in the CLI Interpreters dialog as described in Configuring Remote PHP Interpreters.
When you click OK, PhpStorm brings you back to the PHP Mess Detector dialog where the new PHP Mess Detector configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHP Mess Detector associated with it, and the advanced PHP Mess Detector options.
A static analysis tool for finding errors in PHP applications https://psalm.dev
Install via Composer:
composer require --dev vimeo/psalm
Add a config:
Then run Psalm:
Instant Upgrades and Instant Refactoring of any PHP 5.3+ code https://getrector.org
composer require rector/rector --dev
git clone <forked> https://github.com/rectorphp/demo.git
Example rector-ci.yaml from symplify / symplify
Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than
composer require phan/phan