PHPCS and PHP-CS-Fixer

Tram Ho

If you want to encounter problems with code convention and struggling to find an effective tool to help you automatically check and fix those errors, then … go to Google to search for solutions. . The following article is also a suggestion =))

1. PHPCS PHP_CodeSniffer

I don’t know what kind of Vietnamese introduction, I’ll just take the Western prototype:

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

1.1. Setting

There are many ways to install PHPCS, you can use 2 main ways:

  • Download the file directly to run

  • Installation through Composer, you can install as global (system-wide)

Then make sure the bin folder of the composer is in your PATH . The default value of this directory is ~/.composer/vendor/bin/ , but you can check the value you need to use with the command:

For bash, the file is .bashrc in the home directory from ~/.bashrc , with zsh the file ~/.zshrc.

I use zsh so I’ll add the .zshrc file

Add this line:

Then check with echo $PATH to see if it has:

1.2. Use

To use, quite simply, use the command phpcs to combine files and directories you want to check:

Config

You have completely configured the standards you want to check the code you want, such as using tabs or spaces, checking which folders, ignore which files, and which standards. After setting PHPCS, by default it will use PEAR condition, this standard is outdated, you can change this by using Framgia or PRS2 standards.

How to change the configuration is NOT recommended , instead you should set up phpcs for each of your projects, using the file .phpcs.xml, phpcs.xml, .phpcs.xml.dist, phpcs.xml.dist .

Install Framgia PHP Standards

PHPCS’s check code standards are located in the ~/.composer/vendor/squizlabs/php_codesniffer/src/Standards . So if you want to add a standard check code, you just need to clone it to this folder:

Use:

Quite simple, huh ?

1.3. Integrating PHPCS into Sublime text 3

The next job is that you should integrate this syntax check directly into the editor to automatically detect errors, instead of column 1 circuit and run phpcs =))

You need to install 2 packages in Subime Text for this support

Sublime Linter (parent package)

SublimeLinter-phpcs (subpackages, small parts of Linter)

Restart Sublime Text always to enjoy this super new function: v (start with the new command @@)

3. Integrating PHPCS into PHPStorm

To integrate check phpcs and standards into PHPStorm, install it by following the link.

FIle => Setting => Editor => Inspections => PHP => PHP Code sniffer validation (Can be in Quality tools)

You then select the settings, display the error at the warning or error level and select Coding standard

2. PHPCS Fixer

This is a tool to help you fix code according to the standards (for example of PHPCS) you want

2.1. Setting

Locally

Super simple, download the file php-cs-fixer.phar and run it ?

Globally (manual)

First you download it

Still download but determine the version

It is also downloading but using another command

Then:

End, done, let’s test php-cs-fixer --version

Globally (Composer)

First, you also download it as a global

Add PATH as phpcs

Accomplished!

2.2 Use

Not as different as phpcs : v

2.3. Integration into Sublime Text 3

Package: PHP CS Fixer

Use

Once installed successfully, each time your file fails, use the following: Ctrl + Shift + P and Search PHP CS Fixer. You can even see its shortcut, press if you’re in the error file, it looks like a gourd. ? (this case is Ctrl + Alt + F )

OK, cool cool

2.4. Integration for PHP Storm

This I have not tried, you can refer to the following link https://www.jetbrains.com/help/phpstorm/using-php-cs-fixer.html

summary

  • The integrated phpcs and php-cs-fixer can only be installed and operated on editors if your machine has installed phpcs, php-cs-fixer in global form (because the nature integrated into the editor is also running commands manual only)
  • During the installation process, if there is an error, please read Sublime text 3’s console to fix it
  • PHPCS and PHP-CS-Fixer tell us that other languages ​​have coding standards and similar auto fixes. When coding in any language, do a search. ? (JS with Prettier must: 3)
Share the news now

Source : Viblo