Skip to content

Latest commit

 

History

History
147 lines (92 loc) · 7.72 KB

README.md

File metadata and controls

147 lines (92 loc) · 7.72 KB

PHP Formatter

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.

GIF example of formatting your PHP code

Features

  • Format current selection only, or the whole file.
  • Trigger formatting with custom keybindings or actions.
  • Supports formatting on save.
  • 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.

Requirements

Requires installation of the PHP-CS-Fixer. For more info see their repo.

Installation Guide

  1. Download this extension by using any of the methods here.

  2. Install PHP-CS-Fixer using one of the methods here.

  3. Based on your installation method of PHP-CS-Fixer, use one of the following settings:


    With Composer

    Add Composer to your PATH environment variable. E.g. on Windows: %APPDATA%\Composer\vendor\bin. E.g. on Linux: /.composer/vendor/bin.

    Add "phpformatter.composer": true to your VSCode user settings.

    VSCode user settings result with Composer


    Manual

    Add PHP to your PATH environment variable. I.e. ensure that php -v works. Otherwise use "phpformatter.phpPath" = "/path/to/php/executable".

    Point the phpformatter.pharPath setting to where you put the php-cs-fixer file.

    VSCode user settings result with Manual

    Not working? Make sure the phpformatter.composer setting is set to false, which it is by default.


  4. 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 Awaiting VSCode feature to support this functionality.

  5. You're done! You might need to restart Visual Studio Code, however.

Extension Settings

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. Not supported right now. Awaiting an internal VSCode feature. Use the phpformatter.fix command for now instead.
  • 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 of phpformatter.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.

Extension commands

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.

phpformatter.fix

  • 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.

Known Issues

  • phpformatter.onSave Not supported right now. Awaiting an internal VSCode feature. Use the phpformatter.fix command for now instead.
  • 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 run setup_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.

Release Notes

0.1.2

Solved an issue where fixing a file would erase its contents.

0.1.1

New icon.

0.1.0

  • 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 of phpformatter.logging.
  • Added phpformatter.logging setting to replace phpformatter.enableFixerLogging.
  • Improved logging. Added more log messages and added two notifications.

0.0.7

  • 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.

0.0.6

Added icon and more meta info in package.json. As well as the first icon!

0.0.5

Added working support for multiple file extensions besides .php. See phpformatter.additionalExtensions setting for more info.

0.0.4

All paths now support spaces (' ').

0.0.3

Setting phpformatter.composer to true now actually overrides the use of pharPath.

0.0.2

  • 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.

0.0.1

Initial commit.