Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork139
prettier/plugin-php
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
This plugin adds support for the PHP language to Prettier.
Note
This plugin uses PSR / PER as guidance when making formatting decisions, but does not aim to be fully PSR / PER compliant. The idea is to stay reasonably close to how Prettier for JS works.
We're considering the plugin to be stable when pure PHP files are formatted. Formatting of files that contain mixed PHP and HTML is still considered unstable - please seeopen issues with the tag "inline" for details.
If you want to use the plugin in production, we recommend limiting its scope to pure PHP files.
<?phparray_map(function($arg1,$arg2)use ($var1,$var2 ) {return$arg1+$arg2/($var+$var2);},array("complex"=>"code","with"=>function() {return"inconsistent";},"formatting"=>"is","hard" =>"to","maintain"=>true));
<?phparray_map(function ($arg1,$arg2)use ($var1,$var2) {return$arg1 +$arg2 / ($var +$var2); }, ["complex" =>"code","with" =>function () {return"inconsistent"; },"formatting" =>"is","hard" =>"to","maintain" =>true, ]);
You can give the plugin a try in ourplayground!
yarn:
yarn add --dev prettier @prettier/plugin-php# or globally (pending https://github.com/prettier/prettier/issues/15141)yarn global add prettier @prettier/plugin-phpnpm:
npm install --save-dev prettier @prettier/plugin-php# or globally (pending https://github.com/prettier/prettier/issues/15141)npm install --global prettier @prettier/plugin-phpCreate or modify yourprettier configuration file to activate the plugin:
{"plugins": ["@prettier/plugin-php"]}If you installed prettier as a local dependency, you can add prettier as a script in yourpackage.json,
{"scripts": {"prettier":"prettier" }}and then run it via
yarn run prettier path/to/file.php --write# ornpm run prettier -- path/to/file.php --writeIf you installed globally, run
prettier path/to/file.php --write
This package exposes astandalone.js that can be used alongside Prettier's ownstandalone.js to make the PHP plugin work in browsers without a compile step.
First, grab both standalone scripts from an npm CDN likeunpkg:
<scriptsrc="https://unpkg.com/prettier/standalone.js"></script><scriptsrc="https://unpkg.com/@prettier/plugin-php/standalone.js"></script>
Then use Prettier with PHP, just like this:
awaitprettier.format(YOUR_CODE,{plugins:prettierPlugins,parser:"php",});
See this code in actionin this basic demo.
Bundlers like webpack, Rollup or browserify automatically recognize how to handle the PHP plugin. Remember that even when using a bundler, you still have to use the standalone builds:
importprettierfrom"prettier/standalone";import*asprettierPluginPhpfrom"@prettier/plugin-php/standalone";awaitprettier.format(YOUR_CODE,{plugins:[prettierPluginPhp],parser:"php",});
Prettier for PHP supports the following options. We recommend that all users set thephpVersion option.
| Name | Default | Description |
|---|---|---|
phpVersion | "auto" * | Allows specifying the PHP version you're using. (See Notes Below) |
printWidth | 80 | Same as in Prettier (see prettier docs) |
tabWidth | 4 | Same as in Prettier (see prettier docs), The default is4 based on thePSR-2 coding standard. |
useTabs | false | Same as in Prettier (see prettier docs) |
singleQuote | false | If set to"true", strings that use double quotes but do not rely on the features they add, will be reformatted. Example:"foo" -> 'foo',"foo $bar" -> "foo $bar". |
trailingCommaPHP | true | If set totrue, trailing commas will be added wherever possible.If set to false, no trailing commas are printed. |
braceStyle | "per-cs" | If set to"per-cs", prettier will move open brace for code blocks (classes, functions and methods) onto new line.If set to "1tbs", prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
requirePragma | false | Same as in Prettier (see prettier docs) |
insertPragma | false | Same as in Prettier (see prettier docs) |
The default settingauto, attempts to auto-detect your supported php versions from acomposer.json with in thecurrent directory or any parent directory, the plugin will use a minimum supported php version from{"require":{"php":"..."}} to set the phpVersion. If not found or not able to be parsed, it will default to latestsupported PHP version.
You set thephpVersion tocomposer and this will only use thecomposer.json file to determine the phpversion, prettier will crash if the PHP cannot be determined.
You can also set thephpVersion to a specific version, such as7.4,8.0,8.1,8.2,8.3 or8.4.
Please Note: If the phpVersion is not set correctly for your environment, this plugin will produce code that couldbe incompatible with your PHP runtime. For example, if you are using PHP 7.4, but the plugin is set to PHP 8.3, it willproduce code that uses features not available in PHP 7.4.
A comment// prettier-ignore will exclude the next node in the abstract syntax tree from formatting.
For example:
matrix(1,0,0,0,1,0,0,0,1);// prettier-ignorematrix(1,0,0,0,1,0,0,0,1);
will be transformed to
matrix(1,0,0,0,1,0,0,0,1);// prettier-ignorematrix(1,0,0,0,1,0,0,0,1)
The officialprettier plugin for vscode supports plugins since Version 1.10.0. To enable it, install the extension and make sure the plugin is installedlocally (in your project folder).
Visual Studio Code may not recognise the document selectors provided by this plugin, and so you can add php to your document selectors by adding the following line to.vscode/settings.json:
"prettier.documentSelectors": ["**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}" ]
You may also need to declare php as the parser in your prettier config file:
{"parser":"php","plugins": ["@prettier/plugin-php"]}See (this issue)[#1730] for more discussion around potential VS Code solutions.
- Install prettier and plugin locally
yarn add -D prettier @prettier/plugin-php - Open Settings (File, Settings)
- Go to Plugins Section, Select Marketplace, Search for Prettier, Install Plugin, Restart IDE
- Open Settings, Search for Prettier, select Prettier in left settings navigation
- Check prettier package has auto-detected, should be something like
myproject/node_modules/prettier - Update Run for Files to include .php, eg:
{**/*,*}.{js,ts,jsx,tsx,php,json,scss,vue,md} - Tick the On Save button, if you want your files formatting updated on file save
- Clock OK to save settings
Note: Just pressing save does not reformat your current file unless the file has been modified in some way,alternatively you can use the Prettier shortcut Ctrl+Alt+Shift+P
Sublime Text support is available through Package Control and theJsPrettier plugin.
The officialprettier plugin for vim has built-in support for plugin-php since 1.0.
ThePrettier⁺ Extension has built-in support for plugin-php.
The linting plugin ALE has built-in support for prettier and its plugins. Just add prettier to yourlist of fixers. For example:
letg:ale_fixers={\'javascript': ['prettier'],\'json': ['prettier'],\'php': ['prettier'],\}
Alternatively, adding the following to.vimrc will define a custom command:PrettierPhp that runs the plugin while preserving the cursor position and run it on save.
" Prettier for PHPfunctionPrettierPhpCursor()let save_pos=getpos(".")%! prettier--stdin--parser=phpcallsetpos('.', save_pos)endfunction" define custom commandcommand PrettierPhpcallPrettierPhpCursor()" format on saveautocmdBufwritePre*.php PrettierPhp
Seedocs/recipes/php-cs-fixer for integration help, code can also be found inhttps://gist.github.com/Billz95/9d5fad3af728b88540fa831b73261733
If you're interested in contributing to the development of Prettier for PHP, you can follow theCONTRIBUTING guide from Prettier, as it all applies to this repository too.
To test it out on a PHP file:
- Clone this repository.
- Run
yarn. - Create a file called
test.php. - Run
yarn prettier test.phpto check the output.
![]() Christian Zosel | ![]() Evilebot Tnawi |
About
Prettier PHP Plugin
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.

