Php cs fixer install

PHP CS Fixer

In addition to built-in coding assistance, IntelliJ IDEA provides checking the source code through integration with the PHP CS Fixer tool, which detects coding standards problems in your code.

To use PHP CS Fixer from IntelliJ IDEA instead of command line, you need to register it in IntelliJ IDEA and configure it as a IntelliJ IDEA code inspection. Once installed and enabled in IntelliJ IDEA, the tool is available in any opened PHP file, and no additional steps are required to launch it. The on-the-fly code check is activated upon every update in the file thus making it easy to get rid of discovered problems.

Errors and warnings reported by PHP CS Fixer on-the-fly are highlighted in the editor. When the tool is run in the batch mode, the errors and warnings are displayed in the Problems tool window. Each message has the PHP CS Fixer prefix to distinguish it from IntelliJ IDEA internal inspections.

Читайте также:  Php throw exception with error code

You can have predefined rules applied or define your own custom set of rules.

Install the PHP plugin

This functionality relies on the PHP plugin, which you need to install and enable.

The PHP plugin is available only in IntelliJ IDEA Ultimate.

  1. Press Ctrl+Alt+S to open the IDE settings and select Plugins .
  2. Open the Marketplace tab, find the PHP plugin, and click Install (restart the IDE if prompted).

Prerequisites

Prior to integrating PHP CS Fixer in IntelliJ IDEA, make sure the following prerequisites are met:

docker-compose exec for PHP interpreter

  • You are working with PHP CS Fixer version 2.8.0 and later.
  • The directory containing the PHP engine executable must be added to the system path . This allows code quality tool scripts execute calls to the system-wide PHP engine.
  • For Docker Compose-based remote interpreters, make sure to use docker-compose exec mode to avoid spawning additional containers.
    1. In the Settings dialog ( Ctrl+Alt+S ), go to Languages & Frameworks | PHP .
    2. On the PHP page that opens, click next to the CLI Interpreter list.
    3. In the CLI Interpreters dialog that opens, set the Lifecycle mode for the selected interpreter to Connect to existing container (‘docker-compose exec’) .

Install and configure PHP CS Fixer

Install PHP CS Fixer with Composer

Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.

When you install PHP CS Fixer with Composer, IntelliJ IDEA automatically downloads the necessary scripts, registers them in the IDE, and, optionally, enables and configures the corresponding code inspection.

  1. Inside composer.json , add the friendsofphp/php-cs-fixer dependency record to the require or require-dev section. Press Ctrl+Space to get code completion for the package name and version.
  2. Do one of the following:
    • Click the Install shortcut link on top of the editor panel.
    • If the Non-installed Composer packages inspection is enabled, IntelliJ IDEA will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

Click next to the package record in the composer.json editor gutter to jump to the corresponding Settings page and configure PHP CS Fixer manually .

Gutter icon for php-cs-fixer settings in composer.json

Reset PHP CS Fixer configuration

After PHP CS Fixer is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHP CS Fixer configuration.

Empty PHP CS Fixer path field

  1. In the Settings dialog ( Ctrl+Alt+S ), navigate to Languages & Frameworks | PHP | Quality Tools .
  2. On the Quality Tools page that opens, expand the PHP CS Fixer area and click next to the Configuration list.
  3. In the PHP CS Fixer dialog that opens, empty the PHP CS Fixer path field.
  4. Update the project Composer dependencies by clicking Update on top of the composer.json editor panel. See Update dependencies for details.

IntelliJ IDEA will perform the PHP CS Fixer configuration anew and thus apply the changes in composer.json .

Configure PHP CS Fixer in IntelliJ IDEA

When you install PHP CS Fixer with Composer, IntelliJ IDEA automatically detects PHP CS Fixer’s executable file in the vendor/bin folder and sets the PHP interpreter configured in the system path to run it.

In Settings ( Ctrl+Alt+S ) | Languages & Frameworks | PHP | Quality Tools | PHP CS Fixer , you can change the default PHP interpreter, set the path to a manually downloaded and installed PHP CS Fixer executable file, or add some options to be passed to PHP CS Fixer when running it in IntelliJ IDEA.

PHP CS Fixer settings

  • Configuration : in this field, you can change the default PHP interpreter and path to the PHP CS Fixer executable file.
    1. To only change the interpreter, select the required item from the list of local and remote PHP interpreters configured in your project. The PHP CS Fixer executable file ( php-cs-fixer.bat for Windows or php-cs-fixer for Linux and macOS) contains a path to a PHP engine in it. IntelliJ IDEA lets you overwrite this path and use the PHP interpreter of your choice. Learn more about configuring PHP interpreters in Configuring Remote PHP Interpreters or in Configuring Local PHP Interpreters.
    2. To change the path to the PHP CS Fixer executable file, or both the interpreter and the path, click next to the Configuration list to open the PHP CS Fixer dialog and edit the fields there as described on the PHP CS Fixer Dialog reference page.
  • Show ignored files : use this setting to exclude files from PHP CS Fixer validation inspection. For details, see the Quality Tools reference page.
  • Options : in this area, add the options to run PHP CS Fixer as a IntelliJ IDEA inspection with. Edit the fields there as described on the PHP CS Fixer reference page.

Enable PHP CS Fixer as an IntelliJ IDEA inspection

There are two ways to set up PHP CS Fixer as a IntelliJ IDEA inspection: automatically during PHP CS Fixer installation with Composer, or manually in IntelliJ IDEA’s inspections settings.

Enable the PHP CS Fixer inspection with Composer

You can include the information on the default and custom PHP CS Fixer rulesets inside the scripts section of composer.json . When you install or update project dependencies, the specified rulesets will be detected and the PHP CS Fixer validation inspection will be enabled automatically.

If no ruleset is specified in the scripts section of composer.json , IntelliJ IDEA will additionally check the project root to locate the ruleset with the .php_cs or .php_cs.dist default name. If the file is present, it will be automatically selected as the inspection’s Custom ruleset.

    In the scripts section of composer.json , add the php-cs-fixer PHP CS Fixer launch command into one of the leaf elements. Provide the —rules argument to denote the coding standard used. For example, adding the following record will set the coding standard to PSR1 :

Alternatively, provide the —config argument and the path to the ruleset file, which will set the coding standard to Custom :

After PHP CS Fixer is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHP CS Fixer configuration on the Languages & Frameworks | PHP |Quality Tools page of the Settings dialog ( Ctrl+Alt+S ) and update project dependencies.

Enable PHP CS Fixer validation in Inspections settings

  1. In the Settings dialog ( Ctrl+Alt+S ), click Inspections under Editor .
  2. On the Inspections page that opens, expand the PHP | Quality Tools node and select the checkbox next to PHP CS Fixer validation . Select PHP CS Fixer validation checkboxIf you have installed PHP CS Fixer with Composer but the corresponding inspection is currently disabled, IntelliJ IDEA highlights its record in composer.json . Press Alt+Enter and use the provided Enable inspection quick-fix to enable the inspection and open the Inspections page. Enable the PHP CS Fixer inspection
  3. On the right-hand pane of the page, configure how IntelliJ IDEA handles the PHP CS Fixer inspection output:
    1. Scope : choose the scope to limit the inspection application to.
    2. Severity : choose the severity degree for the inspection. The selected value determines how seriously the detected discrepancies will be treated by IntelliJ IDEA and presented in the inspection results.
    3. Highlighting in editor : choose how the issues detected by the inspection are highlighted in the editor.

You can also enable and disable the PHPStan validation inspection in the Settings dialog ( Ctrl+Alt+S ) | PHP | Quality Tools by using the ON/OFF toggle in the PHP CS Fixer section, but note that this action is applied only to the Project Default inspection profile.

Run PHP CS Fixer in the batch mode

PHP CS Fixer inspections list

  1. From the main menu, select Code | Inspect code .
  2. In the Specify Inspection Scope dialog that opens, select the inspection profile from the list, or click Configure to open the Inspections dialog and configure a new profile. You can also click Configure to check which fixes will be applied within the scope of the selected inspection profile, and make sure that the PHP CS Fixer validation inspection is enabled.
  3. View the inspection results in the Problems tool window. Errors and warnings reported by PHP CS Fixer are prefixed with PHP CS Fixer to distinguish them from IntelliJ IDEA internal inspections.

Fix issues detected by PHP CS Fixer

Fix issues by using a quick-fix

Upon detecting an issue, IntelliJ IDEA highlights it in accordance with the PHP CS Fixer inspection settings.

  1. Place the caret at the detected issue and click , or press Alt+Enter .
  2. Select PHP CS Fixer: fix the whole file from the popup menu. Note that this will fix all issues detected in the current file.

You can also detect issues in the batch mode, or fix them automatically with code reformatting.

Fix issues with code reformatting

You can fix issues detected by PHP CS Fixer by running the Reformat Code action.

Select external formatter

  1. Set PHP CS Fixer as an external formatter in the Quality Tools settings. To do so, in the Settings dialog ( Ctrl+Alt+S ) go to Languages & Frameworks |PHP | Quality Tools and select PHP CS Fixer in the External Formatters section.
  2. Apply Reformat Code to the required file or project scope:
    • For a code fragment or a file opened in the editor, select Code | Reformat Code from the main menu or press Ctrl+Alt+L .
    • For a selected file, directory, or group of files in the Project tool window, select Reformat Code from the context menu or press Ctrl+Alt+L .

PHP CS Fixer is also run as an external formatter with reformat code on save and reformat code before commit actions.

External formatters are no longer run during the Code Cleanup action.

Share a custom coding style with the team

  1. Put the root directory of your coding standard under the project root.
  2. Configure PHP CS Fixer as an IntelliJ IDEA inspection.
  3. Appoint your coding standard.
  4. Make sure that a project profile is selected at the top of the Inspections page. Such profiles are saved in a particular project’s .idea directory (for example, $PROJECT_DIR$/.idea/inspectionProfiles ). See Configure profiles for details.
  5. On the Version Control page of the Settings dialog, make sure that the .idea directory is put under version control.

Exclude files from PHP CS Fixer inspection

When waiting for PHP CS Fixer response exceeds the limit specified in the Tool process timeout field in the PHP CS Fixer dialog, IntelliJ IDEA suggests adding the file to the ignore list .

  1. In the Settings dialog ( Ctrl+Alt+S ), navigate to Languages & Frameworks | PHP | Quality Tools .
  2. On the Quality Tools page that opens, expand the PHP CS Fixer area and click the Show ignored files link.
    • To add a file, click and locate the desired file in the dialog that opens.
    • To delete a file from the list and have PHP CS Fixer process it again, select the file and click .
    • To remove all the files from the list, click .

Источник

Оцените статью