Php cs fixer fixers

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A tool to automatically fix PHP Coding Standards issues

License

PHP-CS-Fixer/PHP-CS-Fixer

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

PHP Coding Standards Fixer

The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc., or other community driven ones like the Symfony one. You can also define your (team’s) style through configuration.

It can modernize your code (like converting the pow function to the ** operator on PHP 5.6) and (micro) optimize it.

If you are already using a linter to identify coding standards problems in your code, you know that fixing them by hand is tedious, especially on large projects. This tool does not only detect them, but also fixes them for you.

Note Each new PHP version requires a huge effort to support the new syntax. That’s why the latest PHP version might not be supported yet. If you need it, please, consider supporting the project in any convenient way, for example with code contribution or reviewing existing PRs. To run PHP CS Fixer on yet unsupported versions «at your own risk» — leverage the PHP_CS_FIXER_IGNORE_ENV.

The recommended way to install PHP CS Fixer is to use Composer in a dedicated composer.json file in your project, for example in the tools/php-cs-fixer directory:

mkdir -p tools/php-cs-fixer composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

For more details and other installation methods, see installation instructions.

Assuming you installed PHP CS Fixer as instructed above, you can run the following command to fix the files PHP files in the src directory:

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src

See usage, list of built-in rules, list of rule sets and configuration file documentation for more details.

If you need to apply code styles that are not supported by the tool, you can create custom rules.

Dedicated plugins exist for:

The PHP CS Fixer is maintained on GitHub at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer. Bug reports and ideas about new features are welcome there.

You can reach us at https://gitter.im/PHP-CS-Fixer/Lobby about the project, configuration, possible improvements, ideas and questions, please visit us!

The tool comes with quite a few built-in fixers, but everyone is more than welcome to contribute more of them.

About

A tool to automatically fix PHP Coding Standards issues

Источник

Usage¶

The fix command tries to fix as much coding standards problems as possible on a given file or files in a given directory and its subdirectories:

php php-cs-fixer.phar fix /path/to/dir php php-cs-fixer.phar fix /path/to/file 

By default —path-mode is set to override , which means, that if you specify the path to a file or a directory via command arguments, then the paths provided to a Finder in config file will be ignored. You can also use —path-mode=intersection , which will use the intersection of the paths from the config file and from the argument:

php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir 

The —format option for the output format. Supported formats are txt (default one), json , xml , checkstyle , junit and gitlab .

NOTE: the output for the following formats are generated in accordance with schemas

  • checkstyle follows the common “checkstyle” XML schema
  • gitlab follows the codeclimate JSON schema
  • json follows the own JSON schema
  • junit follows the JUnit XML schema from Jenkins
  • xml follows the own XML schema

The —quiet Do not output any message.

The —verbose option will show the applied rules. When using the txt format it will also display progress notifications.

NOTE: if there is an error like “errors reported during linting after fixing”, you can use this to be even more verbose for debugging purpose

The —rules option limits the rules to apply to the project:

php php-cs-fixer.phar fix /path/to/project --rules=@PSR12 

By default the PSR12 rules are used. If the —rules option is used rules from config files are ignored.

The —rules option lets you choose the exact rules to apply (the rule names must be separated by a comma):

php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type 

You can also exclude the rules you don’t want by placing a dash in front of the rule name, if this is more convenient, using -name_of_fixer :

php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type 

When using combinations of exact and exclude rules, applying exact rules along with above excluded results:

php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison 

Complete configuration for rules can be supplied using a json formatted string.

php php-cs-fixer.phar fix /path/to/project --rules='>' 

The —dry-run flag will run the fixer without making changes to your files.

The —diff flag can be used to let the fixer output all the changes it makes in udiff format.

The —allow-risky option (pass yes or no ) allows you to set whether risky rules may run. Default value is taken from config file. A rule is considered risky if it could change code behaviour. By default no risky rules are run.

The —stop-on-violation flag stops the execution upon first file that needs to be fixed.

The —show-progress option allows you to choose the way process progress is rendered:

  • none : disables progress output;
  • dots : same as estimating but using all terminal columns instead of default 80.

If the option is not provided, it defaults to dots unless a config file that disables output is used, in which case it defaults to none . This option has no effect if the verbosity of the command is less than verbose .

php php-cs-fixer.phar fix --verbose --show-progress=estimating 

The command can also read from standard input, in which case it won’t automatically fix anything:

cat foo.php | php php-cs-fixer.phar fix --diff - 

Finally, if you don’t need BC kept on CLI level, you might use PHP_CS_FIXER_FUTURE_MODE to start using options that would be default in next MAJOR release and to forbid using deprecated configuration:

PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer.phar fix -v --diff 

The —dry-run option displays the files that need to be fixed but without actually modifying them:

php php-cs-fixer.phar fix /path/to/code --dry-run 

By using —using-cache option with yes or no you can set if the caching mechanism should be used.

The list-files command¶

The list-files command will list all files which need fixing.

php php-cs-fixer.phar list-files 

The —config option can be used, like in the fix command, to tell from which path a config file should be loaded.

php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php 

The output is built in a form that its easy to use in combination with xargs command in a linux pipe. This can be useful e.g. in situations where the caching mechanism might not be available (CI, Docker) and distribute fixing across several processes might speedup the process.

Note: You need to pass the config to the fix command, in order to make it work with several files being passed by list-files .

php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v 
  • -n defines how many files a single subprocess process
  • -P defines how many subprocesses the shell is allowed to spawn for parallel processing (usually similar to the number of CPUs your system has)

Rule descriptions¶

Use the following command to quickly understand what a rule will do to your code:

php php-cs-fixer.phar describe align_multiline_comment 

To visualize all the rules that belong to a ruleset:

php php-cs-fixer.phar describe @PSR2 

Caching¶

The caching mechanism is enabled by default. This will speed up further runs by fixing only files that were modified since the last run. The tool will fix all files if the tool version has changed or the list of rules has changed. Cache is supported only for tool downloaded as phar file or installed via composer.

Cache can be disabled via —using-cache option or config file:

 $config = new PhpCsFixer\Config(); return $config->setUsingCache(false); 

Cache file can be specified via —cache-file option or config file:

 $config = new PhpCsFixer\Config(); return $config->setCacheFile(__DIR__.'/.php-cs-fixer.cache'); 

Using PHP CS Fixer on CI¶

Require friendsofphp/php-cs-fixer as a dev dependency:

./composer.phar require --dev friendsofphp/php-cs-fixer 

Then, add the following command to your CI:

IFS=' ' CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$") if ! echo "$" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "$"); else EXTRA_ARGS=''; fi vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no $

Where $COMMIT_RANGE is your range of commits, e.g. $TRAVIS_COMMIT_RANGE or HEAD~..HEAD .

Environment options¶

The PHP_CS_FIXER_IGNORE_ENV environment variable can be used to ignore any environment requirements. This includes requirements like missing PHP extensions, unsupported PHP versions or by using HHVM.

NOTE: Execution may be unstable when used.

PHP_CS_FIXER_IGNORE_ENV=1 php php-cs-fixer.phar fix /path/to/dir 

Exit code¶

Exit code of the fix command is built using following bit flags:

  • 0 — OK.
  • 1 — General error (or PHP minimal requirement not matched).
  • 4 — Some files have invalid syntax (only in dry-run mode).
  • 8 — Some files need fixing (only in dry-run mode).
  • 16 — Configuration error of the application.
  • 32 — Configuration error of a Fixer.
  • 64 — Exception raised within the application.

Источник

Читайте также:  Java перевод строки на новую строку
Оцените статью