Điểm qua prettier/plugin-php: 1.9k sao trên GitHub, viết chủ yếu bằng PHP, thuộc nhóm Developer Tools. Prettier PHP Plugin
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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.
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.
Create or modify your prettier configuration file to activate the plugin:
{
"plugins": ["@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:
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 * as prettierPluginPhp from "@prettier/plugin-php/standalone";
await prettier.format(YOUR_CODE, {
plugins: [prettierPluginPhp],
parser: "php",
});
Configuration
Prettier for PHP supports the following options. We recommend that all users set the phpVersion 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 is 4 based on the PSR-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 to true, 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)
* phpVersion Configuration Notes :
The default setting auto, attempts to auto-detect your supported php versions from a composer.json with in the
current 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 latest
supported PHP version.
You set the phpVersion to composer and this will only use the composer.json file to determine the php
version, prettier will crash if the PHP cannot be determined.
You can also set the phpVersion to a specific version, such as 7.4, 8.0, 8.1, 8.2, 8.3 or 8.4.
Please Note: If the phpVersion is not set correctly for your environment, this plugin will produce code that could
be incompatible with your PHP runtime. For example, if you are using PHP 7.4, but the plugin is set to PHP 8.3, it will
produce code that uses features not available in PHP 7.4.
Ignoring code
A comment // prettier-ignore will exclude the next node in the abstract syntax tree from formatting.
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).
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:
See (this issue)[https://github.com/prettier/plugin-php/issues/1730] for more discussion around potential VS Code solutions.
PhpStorm / IntelliJ / Jetbrains IDE
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
Sublime Text support is available through Package Control and the JsPrettier plugin.
Vim
The official prettier plugin for vim has built-in support for plugin-php since 1.0.
Nova
The Prettier⁺ Extension has built-in support for plugin-php.
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.
prettier/plugin-php có 1.9k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/prettier/plugin-php. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
prettier/plugin-php có những chủ đề gì?
GitHub topics của prettier/plugin-php: "autoformat", "beautifier", "formatter", "php", "php5", "php7", "prettier", "printer". TopGit xếp repo vào nhóm Developer Tools.
prettier/plugin-php còn đang phát triển không?
Commit gần nhất trên prettier/plugin-php là 12 ngày trước (theo timestamp GitHub). Repo có 145 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
prettier/plugin-php là gì?
prettier/plugin-php (prettier/plugin-php) là dự án PHP trên GitHub. Theo mô tả gốc: Prettier PHP Plugin
prettier/plugin-php so với các dự án Developer Tools khác thế nào?
prettier/plugin-php được TopGit xếp vào nhóm Developer Tools, với 1.9k sao GitHub và viết bằng PHP. Xem trang chủ đề Developer Tools trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
prettier/plugin-php viết bằng ngôn ngữ gì?
prettier/plugin-php chủ yếu viết bằng PHP. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao prettier/plugin-php được xếp vào nhóm Developer Tools?
TopGit xếp prettier/plugin-php vào nhóm Developer Tools dựa trên GitHub topics và mô tả của repo (gắn thẻ: "autoformat", "beautifier", "formatter"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
plugin-php có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về plugin-php.