Giter VIP home page Giter VIP logo

php-cs-fixer's Introduction

PHP CS Fixer (Prettier for PHP)

This extension requires almost zero configuration to format .php files. It uses v3.13.0 of cs.symfony.com by default but that's easily replaceable. As long as PHP 7+ is installed on your system and in your PATH, the extension should work out of the box.

demo

Extension Optional Settings

This extension contributes the following settings:

  • php-cs-fixer.toolPath: The path to the php-cs-fixer tool (default: "")
  • php-cs-fixer.useCache: Use a cache file when fixing files (--using-cache) (default: false)
  • php-cs-fixer.allowRisky: Determines whether risky rules are allowed (--allow-risky) (default: false)
  • php-cs-fixer.config: Path to a config file (--config) (default: "WorkspaceFolder/.php-cs-fixer.php" OR "WorkspaceFolder/.php-cs-fixer.dist.php")
  • php-cs-fixer.rules: Rules to use when fixing files (--rules) (default: "@PSR12,@PSR1,@PSR2,@Symfony,-yoda_style")
  • php-cs-fixer.fixOnSave: Runs fix command on save (default: true)

Adding PHP CS Fixer from a composer dependency

You can require PHP CS Fixer as composer dependency in your project and the extension will load it automatically for you.

If extension could not find/load the binary from /vendor/bin/php-cs-fixer, it would default to the binary included in extension.

Manually adding PHP CS Fixer binary file?

Download the required file version from above link and provide the file path to the extension.

Open settings.json file (Ctrl + Shift + P) and add the following setting:

"php-cs-fixer.toolPath": "path\\php-cs-fixer.phar",

On Windows:

"php-cs-fixer.toolPath": "C:\\Users\\username\\.vscode\\php-cs-fixer.phar",

Config File

This extension formats .php files based on specified rules. Commonly these rules are defined in a .php-cs-fixer.dist.php OR .php-cs-fixer.php config file inside your project root path. The extension would try to pick a config file with above filenames, if not found it uses default rules.

You can also define a global config file which can be used accross all projects.

Open settings.json file (Ctrl + Shift + P) and add the following setting:

"php-cs-fixer.config": "/full/config/file/path"

On Windows:

"php-cs-fixer.config": "C:\\Users\\username\\.vscode\\.php-cs-fixer.php"

Not sure which rules to add?

Try out following config rules. Try to remove a rule and observe the changes.

<?php

$config = new PhpCsFixer\Config();

return $config
    ->setRules([
        '@PSR12' => true,
        'new_with_braces' => false,
        'array_indentation' => true,
        'array_syntax' => ['syntax' => 'short'],
        'combine_consecutive_unsets' => true,
        'multiline_whitespace_before_semicolons' => true,
        'single_quote' => true,
        'blank_line_before_statement' => true,
        'braces' => [
            'allow_single_line_closure' => true,
        ],
        'concat_space' => ['spacing' => 'one'],
        'declare_equal_normalize' => true,
        'function_typehint_space' => true,
        'include' => true,
        'lowercase_cast' => true,
        'no_multiline_whitespace_around_double_arrow' => true,
        'no_spaces_around_offset' => true,
        'no_unused_imports' => true,
        'no_whitespace_before_comma_in_array' => true,
        'no_whitespace_in_blank_line' => true,
        'object_operator_without_whitespace' => true,
        'single_blank_line_before_namespace' => true,
        'ternary_operator_spaces' => true,
        'trailing_comma_in_multiline' => true,
        'trim_array_spaces' => true,
        'unary_operator_spaces' => true,
        'binary_operator_spaces' => true,
        'whitespace_after_comma_in_array' => true,
        'single_trait_insert_per_statement' => false,
    ])
    ->setLineEnding("\n");

You can find the complete rule set here

php-cs-fixer's People

Contributors

mansoorkhan96 avatar sanketgandhi876 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.