All Projects → prettier → Plugin Php

prettier / Plugin Php

Licence: mit
Prettier PHP Plugin

Projects that are alternatives of or similar to Plugin Php

Prettier
Prettier is an opinionated code formatter.
Stars: ✭ 41,411 (+3231.54%)
Mutual labels:  prettier, printer, formatter
pre-commit-hooks
Standardizing our code quality tooling 🛠️
Stars: ✭ 44 (-96.46%)
Mutual labels:  formatter, prettier
Typeset
自动修正中文、英文、代码混合排版中的全半角、空格等问题
Stars: ✭ 63 (-94.93%)
Mutual labels:  beautifier, formatter
prettier-eslint-atom
DEPRECATED IN FAVOR OF prettier-atom + ESLint integration
Stars: ✭ 64 (-94.85%)
Mutual labels:  formatter, prettier
plugin-pug
Prettier Pug Plugin
Stars: ✭ 163 (-86.89%)
Mutual labels:  formatter, prettier
stylus-supremacy
A Node.js script that helps formatting Stylus files.
Stars: ✭ 86 (-93.08%)
Mutual labels:  formatter, beautifier
prettier-check
Check that all files match prettier code style.
Stars: ✭ 54 (-95.66%)
Mutual labels:  formatter, prettier
Prettier Plugin Csharp
Prettier C# Plugin
Stars: ✭ 224 (-81.98%)
Mutual labels:  prettier, formatter
Prettier Eslint
Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code ✨
Stars: ✭ 3,435 (+176.35%)
Mutual labels:  prettier, formatter
Latexindent.pl
Perl script to add indentation (leading horizontal space) to LaTeX files. It can modify line breaks before, during and after code blocks; it can perform text wrapping and paragraph line break removal. It can also perform string-based and regex-based substitutions/replacements. The script is customisable through its YAML interface.
Stars: ✭ 415 (-66.61%)
Mutual labels:  beautifier, formatter
Exfmt
🌸 An opinionated Elixir source code formatter
Stars: ✭ 445 (-64.2%)
Mutual labels:  printer, formatter
dart-more
More Dart — Literally.
Stars: ✭ 81 (-93.48%)
Mutual labels:  formatter, printer
Typescript Express Starter
🚀 TypeScript Express Starter
Stars: ✭ 238 (-80.85%)
Mutual labels:  prettier, formatter
vscode-liquid
💧Liquid language support for VS Code
Stars: ✭ 137 (-88.98%)
Mutual labels:  formatter, beautifier
Prettyhtml
💅 The formatter for the modern web https://prettyhtml.netlify.com/
Stars: ✭ 241 (-80.61%)
Mutual labels:  prettier, formatter
csharpier
CSharpier is an opinionated code formatter for c#.
Stars: ✭ 337 (-72.89%)
Mutual labels:  formatter, prettier
Unibeautify
One Beautifier to rule them all, One Beautifier to clean them, One Beautifier to bring them all and in the darkness sheen them
Stars: ✭ 466 (-62.51%)
Mutual labels:  beautifier, formatter
Spotless
Keep your code spotless
Stars: ✭ 2,285 (+83.83%)
Mutual labels:  prettier, formatter
Poetic
Automatically install and maintain ESLint, Prettier, EditorConfig and Airbnb rules for JavaScript, TypeScript and React.
Stars: ✭ 165 (-86.73%)
Mutual labels:  prettier, formatter
StyLua
An opinionated Lua code formatter
Stars: ✭ 479 (-61.46%)
Mutual labels:  formatter, printer
Prettier PHP

Prettier PHP Plugin

GitHub Workflow Status npm version Codecov Coverage Status code style: prettier Gitter Follow+Prettier+on+Twitter

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.

Can this be used in production?

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 see open 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.

Input

<?php
array_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));

Output

<?php

array_map(
    function ($arg1, $arg2) use ($var1, $var2) {
        return $arg1 + $arg2 / ($var + $var2);
    },
    [
        "complex" => "code",
        "with" => function () {
            return "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. We recommend that all users set the phpVersion option.

Name Default Description
phpVersion "5.4" Allows specifying the PHP version you're using. If you're using PHP 7.1 or later, setting this option will make use of modern language features in the printed output. If you're using PHP 5.3 or lower, you'll have to set this option or Prettier will generate incompatible code.
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 true If set to true, trailing commas will be added wherever possible.
If set to false, no trailing commas are printed.
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)

Ignoring code

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-ignore
matrix(
  1, 0, 0,
  0, 1, 0,
  0, 0, 1
);

will be transformed to

matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

// prettier-ignore
matrix(
  1, 0, 0,
  0, 1, 0,
  0, 0, 1
)

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

Christian Zosel Evilebot Tnawi
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].