Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the php-cs-fixer coding formatting tool

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the php-cs-fixer coding formatting tool, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Php-cs-fixer-PHP coding formatting tool

Php-cs-fixer is a code formatting tool. The standards for formatting are PSR-1, PSR-2, and some symfony standards. This tool also comes from the same door as symfony, twig and other excellent PHP libraries.

Installation and update

You need to use PHP version 5.3.6 or later.

You can download the packaged phar package: php-cs-fixer.phar directly.

Or download it through wget (the following are all used on OSX and Linux):

Wget http://get.sensiolabs.org/php-cs-fixer.phar-O php-cs-fixer

Or download it through curl:

Curl http://get.sensiolabs.org/php-cs-fixer.phar-o php-cs-fixer

After the download is completed, the executable permission is given, and then move to the bin directory:

Sudo chmod axix php-cs-fixersudo mv php-cs-fixer / usr/local/bin/php-cs-fixer

This allows you to use the php-cs-fixer command directly from anywhere.

You can also use Composer to install:

Composer global require fabpot/php-cs-fixer

If you are a Mac user, homebrew user and have already tap homebrew/php, you can also directly:

Brew install php-cs-fixer

Or:

Brew install homebrew/php/php-cs-fixer

If you need to update later:

Php-cs-fixer self-update

If you installed it through homebrew:

Brew upgrade php-cs-fixer

If you don't put the execution file in the bin directory or if you need to use php php-cs-fixer.phar instead of php-cs-fixer in Windows.

Usage

The usage is also very simple. The most basic command parameter is fix. When executed directly, the code will be formatted as much as possible according to the default standard:

# format directory can omit the directory php-cs-fixer fix / path/to/dir# format file php-cs-fixer.phar fix / path/to/file if it is the current directory

The-- verbose option is used to show the applied rules. The default is txt format.

-- the level option controls the level of rules that need to be used:

Php-cs-fixer fix / path/to/project-level=psr0php-cs-fixer fix / path/to/project-level=psr1php-cs-fixer fix / path/to/project-level=psr2php-cs-fixer fix / path/to/project-level=symfony

By default, all options for PSR-2 as well as some additional options (mainly related to symfony) are executed. There are also some "contribution level" options, which you can add selectively through-- fixers,-- multiple conditions of fixers should be separated by commas:

Php-cs-fixer fix / path/to/dir-- fixers=linefeed,short_tag,indentation

You can also use-name_of_fixer to set which options are disabled by blacklisting if necessary. If both-fixers and-name_of_fixer are set, the former has a higher priority.

Using the-- dry-run and-- diff commands at the same time shows the summary that needs to be modified, but not actually.

You can also see what will be modified in the following ways, but do not actually change the file:

Cat foo.php | php-cs-fixer fix-- diff-Custom configuration

The config option can be used to set the selected directories and files for analysis and formatting, but this option can only set some common and known items, such as symfony:

# For the Symfony 2.3 + branchphp-cs-fixer fix / path/to/sf23-- config=sf23

There are options available:

Default default configuration

Magento magento project

Sf23 symfony's project

More often, we can customize formatting options and search directories and files through configuration files. Custom configuration is achieved by adding a .php _ cs file to the root of the project.

The setting itself is the PHP code, and finally you can return an instance of Symfony\ CS\ ConfigInterface. You can set formatting options, levels, files, and directories.

Here is a simple example:

And two PHP code tags

Braces [PSR-2]

All statement blocks must be contained in curly braces, and the position and indentation are standard.

Class_definition [PSR-2]

There can be only one space between the class, trait, interfaces keyword and name

Elseif [PSR-2]

Use elseif instead of elseif

Eof_ending [PSR-2]

The file must end with a blank line

Function_call_space [PSR-2]

When calling functions and methods, there can be no spaces between function and method names and parameter extensions

Function_declaration [PSR-2]

The use of spaces in function declaration needs to conform to PSR-2

Indentation [PSR-2]

The code must be indented with four spaces instead of tabs

Line_after_namespace [PSR-2]

There must be a blank line after the namespace declaration

Linefeed [PSR-2]

All PHP files can only end with LF (Unix)

Lowercase_constants [PSR-2]

PHP constants true, false, and null must be lowercase

Lowercase_keywords [PSR-2]

PHP keywords must all be lowercase

Method_argument_space [PSR-2]

When a method is declared and called, there must be no space before the comma between parameters, and there must be a space after the comma

Multiple_use [PSR-2]

Only one element can be declared per use

Parenthesis [PSR-2]

There can be no spaces on both sides of the parentheses

Php_closing_tag [PSR-2]

Pure PHP files must omit the? > tag

Single_line_after_imports [PSR-2]

Each use declaration has a separate line, with a blank line after the use statement block

Trailing_spaces [PSR-2]

Delete extra spaces after non-blank lines

Visibility [PSR-2]

Each property and method must specify whether the scope is public, protected, or private,abstract and final must be before the scope keyword, and static must be after the scope

Array_element_no_space_before_comma [symfony]

In an array declaration, there can be no spaces before the comma

Array_element_white_space_after_comma [symfony]

In an array declaration, there must be a person space after the comma

Blankline_after_open_tag [symfony]

The same line of the PHP opening tag cannot have code, and there must be a blank line below it.

Concat_without_spaces [symfony]

There can be no extra spaces on the left and right sides of the dot connector.

Double_arrow_multiline_whitespaces [symfony]

There cannot be multiple blank lines at both ends of the = > operator

Duplicate_semicolon [symfony]

Delete duplicate semicolons

Empty_return [symfony]

If the return statement does not return anything, just write return (without return null)

Extra_empty_lines [symfony]

Delete extra blank lines

Function_typehint_space [symfony]

Fixed missing spaces between function parameters and type hints

Include [symfony]

A space is required between the include and the file path, and the file path does not need to be enclosed in parentheses

Join_function [symfony]

Replace the implode function with join

List_commas [symfony]

Remove extra commas from list statements

Method_argument_default_value [symfony]

A parameter with a default value in a function parameter cannot precede a parameter without a default value.

Multiline_array_trailing_comma [symfony]

The last element of the multiline array should also have a comma

Namespace_no_leading_whitespace [symfony]

There should be no spaces before the namespace

New_with_braces [symfony]

New instances should be followed by parentheses when using new

No_blank_lines_after_class_opening [symfony]

There should be no blank lines after the class starts the tag

No_empty_lines_after_phpdocs [symfony]

There should be no blank lines below the start element of the PHP document block

Object_operator [symfony]

There should be no spaces at both ends of T_OBJECT_OPERATOR (- >)

Operators_spaces [symfony]

At least one space at both ends of the binary operator

Phpdoc_indent [symfony]

Phpdoc should remain indented

Phpdoc_inline_tag [symfony]

Fixed the phpdoc inline label format so that the tag and subsequent content are always on one line

Phpdoc_no_access [symfony]

@ access should not appear in phpdoc

Phpdoc_no_empty_return [symfony]

@ return void and @ return null should not appear in phpdoc

Phpdoc_no_package [symfony]

@ package and @ subpackage should not appear in phpdoc

Phpdoc_params [symfony]

Phpdoc tags such as @ param, @ throws, @ return, @ var, and @ type should be vertically aligned

Phpdoc_scalar [symfony]

Phpdoc scalar type declarations should use int instead of integer,bool instead of boolean,float rather than real or double

Phpdoc_separation [symfony]

Attributes with the same comments in phpdoc should be put together, and there should be a blank line division between different attributes.

Phpdoc_short_description [symfony]

The brief description of phpdoc should be.,! Or? End

Phpdoc_to_comment [symfony]

Document blocks should be structured elements

Phpdoc_trim [symfony]

Except for the beginning and end of the document block, both the beginning and the end of the phpdoc should have content.

Phpdoc_type_to_var [symfony]

@ type needs to be replaced by @ var

Phpdoc_types [symfony]

Case should be used correctly in phpdoc

Phpdoc_var_without_name [symfony]

Variable names should not be included in @ var and @ type comments

Pre_increment [symfony]

The usage of + I or-I should not be used.

Print_to_echo [symfony]

If possible, use echo instead of print statement

Remove_leading_slash_use [symfony]

Delete the blank line before use

Remove_lines_between_uses [symfony]

Delete a blank line in a use statement block

Return [symfony]

There should be a blank line before return

Self_accessor [symfony]

Use self instead of the class name in the current class

Short_bool_cast [symfony]

You should not try two exclamation marks before bool type data

Single_array_no_trailing_comma [symfony]

There should be no spaces after the last element of the PHP single-line array

Single_blank_line_before_namespace [symfony]

There should be a blank line before the namespace declaration

Single_quote [symfony]

Simple strings should use single quotation marks instead of double quotation marks

Spaces_after_semicolon [symfony]

Fix the space after the semicolon

Spaces_before_semicolon [symfony]

Writing with only single-line spaces and semicolons is prohibited

Spaces_cast [symfony]

There should be a space between the variable and the modifier

Standardize_not_equal [symfony]

Use instead of! =

Ternary_spaces [symfony]

Standardization of spaces between ternary operators

Trim_array_spaces [symfony]

The array needs to be formatted to be similar to function / method parameters, with no blank lines up and down

Unalign_double_arrow [symfony]

No, it = >

Unalign_equals [symfony]

No, its equal sign.

Unary_operators_spaces [symfony]

Unary operators and operands need to be adjacent

Unneeded_control_parentheses [symfony]

Delete extra parentheses in the control structure statement

Unused_use [symfony]

Delete unused use statements

Whitespacy_lines [symfony]

Delete extra spaces in a blank line

In addition to the above options, there are some user-contributed options, which will not be covered here.

Thank you for reading this article carefully. I hope the article "how to use php-cs-fixer coding formatting tools" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report