After installing as an extension with Visual Studio Code, this extension automatically formats your PHP code, in accordance with PSR-0, PSR-1, PSR-2 or Symfony style conventions.
- Includes auto-save functionality.
- Supports different PHP-CS-Fixer installation methods, i.e. Composer vs manual installation.
- Supports level and fixers.
- Can be configured to support other file extensions than PHP as well, i.e. ".inc" files.
Requires installation of the PHP-CS-Fixer. For more info see their repo.
- Download this extension by using any of the methods here.
- Install PHP-CS-Fixer using one of the methods here.
- Based on your installation method of PHP-CS-Fixer, use one of the following settings:
- [Composer] If you installed PHP-CS-Fixer using Composer, then do the following:
1. Make sure you add Composer to your PATH environment variable. On Windows that is
%APPDATA%\Composer\vendor\bin
. For Linux that is/.composer/vendor/bin
. 2. Add this to your user settings"phpformatter.composer": true
. - [Manual] If you installed the php-cs-fixer file manually, then do the following:
1. Point the
phpformatter.pharPath
setting to where you put the php-cs-fixer file, e.g."phpformatter.pharPath" = "C:/php-cs-fixer.phar"
or"phpformatter.pharPath" = "/usr/local/bin/php-cs-fixer"
. 2. Make sure PHP is part of your PATH environment variable (test this by typingphp -v
into your terminal). If you don't want PHP in your path variable, then add this setting"phpformatter.phpPath" = "C:\xampp\php.exe"
and point it to the relevant location. 3. Make sure thephpformatter.composer
setting is set tofalse
, which it is by default. - To format a file with a custom keybinding or action, see Extension Commands. To format a file on save, add this to your user settings:
"phpformatter.onSave" = true
. - You're done! You might need to restart Visual Studio Code, however.
This extension contributes the following settings:
phpformatter.pharPath
: Should point to php-cs-fixer.phar file, if you have installed this manually (without Composer). Should include .phar extension.phpformatter.phpPath
: If the pharPath is set, and you are not using Composer, this should point to the php.exe file.phpformatter.composer
: Whether the php-cs-fixer library has been installed using Composer. If true, the extension will override pharPath and assume you have added Composer to your PATH.phpformatter.onSave
: Whether files should be fixed on save.phpformatter.level
: Fixer level to use when fixing a file, e.g. psr0, psr1, psr2, symfony (More info).phpformatter.fixers
: Fixers to use when fixing a file, e.g. strict, short_array_syntax (More info).phpformatter.additionalExtensions
: Which additional file extensions, besides PHP, should be fixed as well. E.g. inc, without the leading dot. For this to work you'll also have to configure your VSCode files.associations settings (More info).phpformatter.enableFixerLogging
: Deprecated in favor ofphpformatter.logging
.phpformatter.logging
: If true, the extension will log all sorts of (debug) info to the console. Useful for troubleshooting.phpformatter.notifications
: If true, the extension will show notifications.phpformatter.useTempFiles
: If true, a temp file will be used for the formatter to fix. After formatting the temp file, the contents will be copied back to the original file. This circumvents a lot of issues the original way had. Therefore, this method will be used by default.
The extension currently contributes just one command. Your Visual Studio Code environment can be configured to trigger this command with a custom keybinding or other action.
- Fixes the current file or selection, if there is any.
- Does not save the file after fixing.
- Requires
phpformatter.useTempFiles
to be turned on.
To set this up. Go to File -> Preferences -> Keyboard Shortcuts
and add the following to keybindings.json
:
{"key": "alt+shift+f", "command": "phpformatter.fix", "when": "editorFocus"}
After saving the file you should be able to format files using the keybinding alt+shift+f
.
- If you add Composer to your PATH, make sure to restart ALL of your Visual Studio Code instances afterwards. Visual Studio Code only reads out PATH variables during startup.
- If the setting
phpformatter
.useTempFiles is of, you will lose your undo history after saving the file. - If you are on Windows, using xampp, and get the error
PHP Warning: PHP Startup: Unable to load dynamic library
, try going to you xampp directory and runsetup_xampp.bat
. After that, restart Visual Studio Code and try again.
Note: Most issues stem from incorrect installation of the PHP-CS-Fixer, see their repo for more info.
- Added
phpformatter.useTempFiles
setting, which is on by default. This fixes a whole ranges of issues the old method had, and opens up the road other features as well. - Added
phpformatter.fix
command. This introduces the ability to fix a file or selection by registering a custom keybinding. See Extension Commands for more info. - Added the ability to fix only the current selection. This requires
phpformatter.useTempFiles
to be turned on. - Deprecated
phpformatter.enableFixerLogging
in favor ofphpformatter.logging
. - Added
phpformatter.logging
setting to replacephpformatter.enableFixerLogging
. - Improved logging. Added more log messages and added two notifications.
- Added
phpformatter.notifications
setting. - The extension will now show a notification when the required settings are not set.
- The notification has a button that opens up a browser with the installation guide on the Github page.
- Added installation guide and known issue to the readme.
Added icon and more meta info in package.json. As well as the first icon!
Added working support for multiple file extensions besides .php. See phpformatter.additionalExtensions
setting for more info.
All paths now support spaces (' '
).
Setting phpformatter.composer
to true
now actually overrides the use of pharPath.
- Code improvements.
- Fixed functionality.
- Improved code readability.
- Added setting to enable fixer logging. Logs all fixer results (including errors) to the console.
- Settings are now read every time the fix function is called. Previously settings were only read during activation.
Initial commit.