Disclaimer: this tutorial assumes that you have Composer installed. If you don't, click here to get started.
In this tutorial, we are going to learn how to install Php CS Fixer as a local dependency and have different configurations for different projects on our computer. In JavaScript userland, local dependencies are preferred over global dependencies except in very rare cases and I think this should be the same in Php userland.
To start, uninstall Php CS fixer if you have it installed globally. composer global remove friendsofphp/php-cs-fixer will do that easily. Skip this step if you don't have Php CS Fixer installed. By uninstalling Php CS Fixer globally, you would have to install it in every project you want to use it in. One of the advantages of this is that you can have different versions of the tool in different projects without conflict. Another advantage is that Project A and Project B can use different style guides or fixes without conflict.
Next, head over to Php CS Configurator. It’s a cool tool to generate a .php_cs config file for the fixes you want Php CS Fixer to apply to your .php files. It comes with a demo of each fix to describe what the fix does and its possible options. The base config I use for my personal projects is located here and tweaked depending on the framework I'm working with.
After configuring to your heart's content, download the generated .php-cs file and place it in the root of the project you want the fixes applied to. Then go ahead and install PHP CS Fixer in the said project using composer require friendsofphp/php-cs-fixer --dev. Note that Php CS Fixer should always be installed as a development dependency as it is not needed in production.
PhpStorm users should read, read the following guidelines while VS Code users should skip to the next paragraph.
Launch PhpStorm and open
Settings. Go toTools > External tools. Click on the+button or pressCtrl + N(Windows) orCmd + N(Mac) to create a new external tool. You should see a dialog screen like the image below.

Give the tool a name and description that will be easily memorable to you.
Fill the program input with the following text
$ProjectFileDir$\vendor\bin\php-cs-fixer.bat.$ProjectFileDir$is a PhpStorm macro that instructs PhpStorm to use the project directory of the currently opened file. In essence, we are telling PhpStorm to always look for thephp-cs-fixerexecutable in thevendor/binfolder of the currently opened project.Fill the argument input with the following text
fix –verbose --config=$ProjectFileDir$\.php_cs --path-mode=intersection "$FileDir$/$FileName$". We see two new macros.$FileDir$/$FileName$which together provide the filepath of the file to be fixed. Theconfigdirective tellsphp-cs-fixerwhere to look for the configuration file it will use as its ruleset. This current setting tells PhpStorm to look for a.php_csfile in the root of the project we have open.Fill the working directory input with
$ProjectFileDir$.If you want PhpStorm’s console to open each time you run this tool, keep the
Open console for tool outputcheckbox checked. If not, uncheck it. To see a list of all PhpStorm macros and their description, click any of theInsert Macrosbutton in the dialog box.Click
OKto complete the setup.Head over to
Keymapin theSettingsdialog. Search for the name you gave the external tool you just created. Double click on it and you should see an options popup. Click onAdd Keyboard Shortcutthen type a key combo (I useAlt+F) and clickOK. ClickOKagain to close theSettingsdialog.Open a
.phpfile and test run the external tool you just created using the keyboard shortcut configured for it. Notice any changes in the file?
VS Code users should follow the guidelines below
Install the php cs fixer extension. By default, the extension installs PHP CS Fixer in its directory located at its
$extensionPath. On Windows, this isC:\Users\yourUserName\.vscode\extensions\junstyle.php-cs-fixer-{version}. According to the docs,$extensionPathis the absolute file path of the directory containing an extension.If for whatever reason you don't want to use the php-cs-fixer downloaded by the extension, but the one installed locally in your project, you can change the
php-cs-fixer.executablePathsetting to${workspaceRoot}\\vendor\\bin\\php-cs-fixer.bat. Even though VS Code has deprecated${workspaceRoot}for the${workspaceFolder}variable name as documented here, the variable still works with the extension.The extension will automatically pick up the
.php_csfile located in the root of your project courtesy itsphp-cs-fixer.configwhich is set to.php_csby default.Open a
.phpfile, open the VS Code command palette usingF1. Use the commandphp-cs-fixer: fix this fileto fix your file. If the extension is not working, open VS Code developer tools viaHelp > Toggle Developer Tools. Click onConsoleto see what the error is and troubleshoot.
If you experience any challenges, let me know in the comments. If you find this helpful, also drop a like, reaction or comment. Enjoy!
Top comments (1)
Great tutorial! Thanks for this. I had some issues where I was getting PHP General Error. I checked in developer tools in VSCODE (Help > Toggle Developer Tools) and checked the console to reveal my tests/ directory didn't exist. After creating manually it fixed my issue. Thought I'd post here in case anyone else had this issue.
I was using a standard developer template:
<?php
$finder = PhpCsFixer\Finder::create()
->in(DIR . '/src')
->in(DIR . '/tests')
;
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@PHP71Migration' => true,
'binary_operator_spaces' => array(
'align_equals' => false,
'align_double_arrow' => false,
),
'@Symfony:risky' => true,
'@PHP71Migration:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'linebreak_after_opening_tag' => true,
'mb_str_functions' => true,
'no_php4_constructor' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'concat_space' => ['spacing' => 'one'],
'trailing_comma_in_multiline_array' => true,
'yoda_style' => false
])
->setFinder($finder)
->setCacheFile(DIR.'/.php_cs.cache');