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
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token comment"># Download using curl</span> <span class="token function">curl</span> -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar <span class="token function">curl</span> -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar <span class="token comment"># Or download using wget</span> <span class="token function">wget</span> https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar <span class="token function">wget</span> https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar <span class="token comment"># Then test the downloaded PHARs</span> php phpcs.phar -h php phpcbf.phar -h |
- Installation through Composer, you can install as global (system-wide)
1 2 | composer global require <span class="token string">"squizlabs/php_codesniffer=*"</span> |
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:
1 2 | composer global config bin-dir --absolute |
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
1 2 | <span class="token function">vi</span> .zshrc |
Add this line:
1 2 | <span class="token function">export</span> PATH <span class="token operator">=</span> ~/.composer/vendor/bin/: <span class="token variable">$PATH</span> |
Then check with echo $PATH
to see if it has:
1 2 | .../home/nguyen.van.minhb/.composer/vendor/bin/:.... |
1.2. Use
To use, quite simply, use the command phpcs
to combine files and directories you want to check:
1 2 3 4 5 6 7 8 9 | <span class="token comment"># Check cú pháp với file</span> $ phpcs /path/to/code/myfile.php <span class="token comment"># Check cú pháp với thư mục</span> $ phpcs /path/to/code-directory <span class="token comment"># Check cú pháp với các tùy chọn config</span> $ phpcs --standard <span class="token operator">=</span> PSR2 /path/to/code-directory |
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.
1 2 3 4 5 | phpcs --config-set default_standard PSR2 // Kiểm tra việc setting config phpcs --config-show |
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:
1 2 3 4 5 6 7 8 | // Version 2 <span class="token function">cd</span> ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards <span class="token function">git</span> clone -b 0.2 <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> :wataridori/framgia-php-codesniffer.git Framgia // Version 3 <span class="token function">cd</span> ~/.composer/vendor/squizlabs/php_codesniffer/src/Standards <span class="token function">git</span> clone <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> :wataridori/framgia-php-codesniffer.git Framgia |
Use:
1 2 | phpcs <span class="token operator">--</span> standard <span class="token operator">=</span> Framgia <span class="token operator">/</span> path <span class="token operator">/</span> to <span class="token operator">/</span> your <span class="token operator">/</span> code |
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 @@)
1 2 3 | <span class="token function">cd</span> project_path subl <span class="token keyword">.</span> |
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
1 2 | $ <span class="token function">wget</span> https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer |
Still download but determine the version
1 2 | $ <span class="token function">wget</span> https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.1/php-cs-fixer.phar -O php-cs-fixer |
It is also downloading but using another command
1 2 3 | $ <span class="token function">curl</span> -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fix <span class="token operator">!</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token punctuation">(</span> https://images.viblo.asia/09a62f3a-64c5-4551-9fdc-a16e49fbfc71.gif <span class="token punctuation">)</span> er |
Then:
1 2 3 | $ <span class="token function">sudo</span> <span class="token function">chmod</span> a+x php-cs-fixer $ <span class="token function">sudo</span> <span class="token function">mv</span> php-cs-fixer /usr/local/bin/php-cs-fixer |
End, done, let’s test php-cs-fixer --version
Globally (Composer)
First, you also download it as a global
1 2 | $ composer global require friendsofphp/php-cs-fixer |
Add PATH as phpcs
1 2 | $ <span class="token function">export</span> PATH <span class="token operator">=</span> <span class="token string">" <span class="token variable">$PATH</span> : <span class="token variable">$HOME</span> /.composer/vendor/bin"</span> |
Accomplished!
2.2 Use
Not as different as phpcs
: v
1 2 3 4 | $ php php-cs-fixer.phar fix /path/to/dir $ php php-cs-fixer.phar fix /path/to/file $ php php-cs-fixer.phar fix /path/to/project --rules <span class="token operator">=</span> @PSR2 |
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)