• h1_240789
  • 494.7KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-05-27 05:05
漂亮PHP插件 介绍 漂亮的是一个自以为是的代码格式化程序。 它通过解析代码并使用自己的规则(考虑最大行长)重新打印代码,从而实现一致的样式,并在必要时包装代码。 该插件为Prettier添加了对PHP语言的支持。 可以在生产中使用吗? 我们认为该插件在格式化纯PHP文件时是稳定的。 仍然将包含PHP和HTML混合文件的格式视为不稳定-有关详细信息,请参见。 如果要在生产环境中使用该插件,建议将其范围限制为纯PHP文件。 输入值 array_map ( function ( $ arg1 , $ arg2 ) use ( $ var1 , $ var2 ) { return $
<div align="center"> <img alt="Prettier" src=""> <img alt="PHP" height="180" hspace="25" vspace="15" src=""> </div> <h2 align="center">Prettier PHP Plugin</h2> <p align="center"> <a href="" rel='nofollow' onclick='return false;'> <img alt="GitHub Workflow Status" src=" CI?style=flat-square"> </a> <a href="" rel='nofollow' onclick='return false;'> <img alt="npm version" src=""> </a> <a href="" rel='nofollow' onclick='return false;'> <img alt="Codecov Coverage Status" src=""> </a> <!-- <a href="" rel='nofollow' onclick='return false;'> <img alt="monthly downloads" src=""> </a> --> <a href="#badge" rel='nofollow' onclick='return false;'> <img alt="code style: prettier" src=""> </a> <a href="" rel='nofollow' onclick='return false;'> <img alt="Gitter" src=""> </a> <a href="" rel='nofollow' onclick='return false;'> <img alt="Follow+Prettier+on+Twitter" src=""> </a> </p> ## 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: ```bash yarn add --dev prettier @prettier/plugin-php # or globally yarn global add prettier @prettier/plugin-php ``` npm: ```bash 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`, ```json { "scripts": { "prettier": "prettier" } } ``` and then run it via ```bash yarn run prettier path/to/file.php --write # or npm run prettier -- path/to/file.php --write ``` If you installed globally, run ```bash 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]( ```html <script src=""></script> <script src=""></script> ``` Then use Prettier with PHP, just like this: ```js prettier.format(YOUR_CODE, { plugins: prettierPlugins, parser: "php" }); ``` See this code in action [in this basic demo](,output). ### 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: ```js 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. <br> If set to `false`, no trailing commas are printed.