plugin-php

Prettier PHP Plugin

Github星跟踪图

WORK IN PROGRESS

Please note that this plugin is currently in alpha stage and still under active development. We encourage everyone to try it and give feedback, but we don't recommend it for production use yet.

Intro

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.

Input

array_map(function($arg1,$arg2) use ( $var1, $var2 ) {
    return $arg1+$arg2/($var+$var2);
}, array("complex"=>"code","with"=>"inconsistent","formatting"=>"is", "hard" => "to", "maintain"=>true));

Output

array_map(
    function ($arg1, $arg2) use ($var1, $var2) {
        return $arg1 + $arg2 / ($var + $var2);
    },
    array(
        "complex" => "code",
        "with" => "inconsistent",
        "formatting" => "is",
        "hard" => "to",
        "maintain" => true
    )
);

Playground

You can give the plugin a try in our playground!

Install

yarn:

yarn add --dev prettier @prettier/plugin-php
# or globally
yarn global add prettier @prettier/plugin-php

npm:

npm install --save-dev prettier @prettier/plugin-php
# or globally
npm install --global prettier @prettier/plugin-php

Use

With Node.js

If you installed prettier as a local dependency, you can add prettier as a script in your package.json,

{
  "scripts": {
    "prettier": "prettier"
  }
}

and then run it via

yarn run prettier path/to/file.php --write
# or
npm run prettier -- path/to/file.php --write

If you installed globally, run

prettier path/to/file.php --write

In the Browser

This package exposes a standalone.js that can be used alongside Prettier's own standalone.js to make the PHP plugin work in browsers without a compile step.

First, grab both standalone scripts from an npm CDN like unpkg:

<script src="https://unpkg.com/prettier/standalone.js"></script>
<script src="https://unpkg.com/@prettier/plugin-php/standalone.js"></script>

Then use Prettier with PHP, just like this:

prettier.format(YOUR_CODE, {
  plugins: prettierPlugins,
  parser: "php"
});

See this code in action in this basic demo.

With Bundlers

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:

import prettier from "prettier/standalone";
import phpPlugin from "@prettier/plugin-php/standalone";

prettier.format(YOUR_CODE, {
  plugins: [phpPlugin],
  parser: "php"
});

Configuration

Prettier for PHP supports the following options:, Name, Default, Description, ------------------, ---------, -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------, printWidth, 80, Same as in Prettier (see prettier docs), tabWidth, 4, Same as in Prettier (see prettier docs), 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, "none", If set to "all", trailing commas will be added wherever possible. If set to "php7.3", trailing commas will be added to multiline arrays, lists, uses and function calls. If set to "php7.2", trailing commas will be added to multiline arrays, lists and uses. If set to "php5", trailing commas will be added to multiline arrays and lists. if set to "none", no trailing commas., braceStyle, "psr-2", If set to "psr-2", 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), ## Editor integration

Atom

The official prettier plugin for atom supports plugins.

VScode

The official prettier plugin for vscode supports plugins since Version 1.10.0. To enable it, install the extension and make sure the plugin is installed locally (in your project folder). After restarting VScode the plugin should work as expected.

Sublime Text

Sublime Text support is available through Package Control and the JsPrettier plugin.

Vim

Regarding plugin support in the official plugin vim-prettier see this issue.

ALE

The linting plugin ALE has built-in support for prettier and its plugins. Just add prettier to your list of fixers. For example:

let g:ale_fixers={
  \'javascript': ['prettier'],
  \'json': ['prettier'],
  \'php': ['prettier'],
\}

Custom

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 PHP
function PrettierPhpCursor()
  let save_pos = getpos(".")
  %! prettier --stdin --parser=php
  call setpos('.', save_pos)
endfunction
" define custom command
command PrettierPhp call PrettierPhpCursor()
" format on save
autocmd BufwritePre *.php PrettierPhp

Integration for other tools

PHP-CS-Fixer

See docs/recipes/php-cs-fixer for integration help, code can also be found in https://gist.github.com/Billz95/9d5fad3af728b88540fa831b73261733

Contributing

If you're interested in contributing to the development of Prettier for PHP, you can follow the CONTRIBUTING 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.php to check the output.

Maintainers

主要指标

概览
名称与所有者prettier/plugin-php
主编程语言PHP
编程语言JavaScript (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2017-12-24 02:56:22
推送于2025-08-11 05:20:53
最后一次提交
发布数67
最新版本名称v0.24.0 (发布于 2025-07-16 10:05:18)
第一版名称v0.0.0 (发布于 2018-04-09 22:08:06)
用户参与
星数1.8k
关注者数25
派生数136
提交数1.4k
已启用问题?
问题数522
打开的问题数106
拉请求数1337
打开的拉请求数13
关闭的拉请求数560
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?