Skip to content

Prettier PHP Plugin That Enforces WordPress Coding Standards

License

Notifications You must be signed in to change notification settings

lots0logs/prettier-plugin-php-wp

 
 

Repository files navigation

🚧 Work in Progress! 🚧

Prettier PHP

Prettier PHP Plugin

Travis AppVeyor Build Status npm version Codecov Coverage Status code style: prettier Gitter Follow+Prettier+on+Twitter

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
    )
);

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

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

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".
trailingComma "none" If set to "all", trailing commas will be added to multiline arrays.
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

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

Alternatively, install Run on Save and add the following section to your settings:

"emeraldwalk.runonsave": {
  "commands": [
    {
      "match": "\\.php$",
        "cmd": "prettier ${file} --write"
    }
  ]
}

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

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


Christian Zosel

Evilebot Tnawi

About

Prettier PHP Plugin That Enforces WordPress Coding Standards

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 67.3%
  • JavaScript 32.4%
  • Other 0.3%