Every company has a complete set of code standards, but standards are standards, how to effectively let everyone abide by, it is necessary to tool assistance and real-time reminder.

For example, everyone in the front-end VUE will use ESLint to constrain our code. Any extra space will tell you something is wrong, and NPM run dev or NPM run Watch will tell you something is not conforming to the specification.

There are similar tools in Laravel development, which are recommended in this article:

phpcs

Before we start using PHPCS, let’s briefly review Laravel’s code specification standards

Laravel code specification

Laravel follows the PSR-2 coding standard and the PSR-4 autoloading standard.

Explanation from Laravel: https://laravel.com/

The implications of several code specifications

In fact, many web sites have already posted the description of the PSR-2 coding specification, which is recommended as follows:

https://laravel-china.org/docs/psr/psr-2-coding-style-guide/1606​

But in my actual use, in addition to being able to follow the above mentioned specifications, there is one important thing they did not mention.

File and class annotations

It mainly contains the following content blocks: file description, PHP version number, and the five elements (category, package, Author, license, link) in order, and the layout of these five elements should be aligned

Methods the annotation

Contains: method description, blank line, parameter block (type, parameter name, meaning — this needs to be aligned), blank line, and finally

The return type.

Install PHPCS

Before using PHPCS, you still need to know what it is, right?

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

From: https://github.com/squizlabs/PHP_CodeSniffer

There are two main tools: PHPCS and PHPCBF (more on that later). PHPCS mainly defines a series of code specification standards for PHP, JavaScript and CSS files. For example, the PHP PSR-2 standard used by Laravel, which we will use, can detect the code that does not conform to the code specification defined by us and issue warnings and errors. Of course, we can also set the error level.

There are two main ways to use PHPCS:

1, in the local development process, real-time detection of our code, so that when the code is submitted to the version library, it has met the specification standards;

2. Check the submitted code on the server. If it does not meet the standard, it will not enter the code base and call back for modification.

Let’s start by talking about how to install PHPCS tools in different ways.

composer

For those of you who write Laravel code, you should be familiar with using Composer. But the main difference is whether to “global installation” or “project installation”.

I personally recommend “global installation”, where you can directly fill in the global installation PHPCS executable path on each IDE. But if your repository is “Git,” I recommend “Project Installation,” and you’ll see why.

Note: after using this method of “global installation”, I found that “VSCode” could not be associated every time, the reason is pending.

​pear

To install the pear

Start installing the configuration,

Select 1 (Change the Installation Base);

Input: / usr/local/pear

Then select 4 (change the Binaries directory),

Input: / usr/local/bin

Start installing PHP_CodeSniffer

On MacOS:

Installation results in Centos Linux:

This method is more effective, but also in line with the multi-system try, such as I can be installed and used under the “Mac” and “Linux” at the same time.

Note: I haven’t tried it on Windows yet.

brew

It certainly worked on “Mac” systems!

Of course, according to the official website documents, there are other methods, you are welcome to try:

Specific reference: the “Installation” section of https://github.com/squizlabs/PHP_CodeSniffer.

The use of PHPCS

Whether it’s local or server, once we’ve installed it, we’re ready to use it. The most intuitive and easiest way is to use the command line, such as:

But given that we’re writing code in an IDE and want to see it in real time, let’s try a few ides to see how it works.

Install the VSCode plug-in

On the plug-in interface, search for PHPCS and install it.

Reference: https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs

Configure the plug-in

Since the project uses PHPCS of the system, the executable path and self-defined writing style need to be configured in user Setting

At this time we go to see our code interface, whether there is a PHPCS prompt:

Install the PhpStorm plugin

Just look at the picture, no need to do too much explanation.

Basically, PHPCS plug-in is ready to use.

Version checking specification

We want the “pre-commit” to be checked before the team project code commits to the repository

Generate code files that do not conform to the “PSR-2” standard. Both SVN and Git can obtain the code files of the submitted version library in “pre-commit”, and then use “PHPCS” to check whether each file conforms to the specification.

svn

Because each SVN has a corresponding hooks folder on the server, you can verify the code specification during “pre-commit” and directly load the file.



This is what we want to see, whether it’s the IDE’s real-time notification, or the inspection feedback when submitting code, that tells us where the format is not conforming to the specification.

git

Here the main reference WickedReports/PHPCS – pre – commit – hook writing of https://github.com/WickedReports/phpcs-pre-commit-hook:


The main errors are:

Line 60: Output function

Line and 17:

Git commit

Then we can run the same local method and return the result:

Git commit

Perfect!

Note: The difference between “SVN” and “git” is that SVN is installed on the server for “pre-commit” detection, while “git” is installed locally in this project, which is also mentioned above. If you use Git as a version library, it is recommended that you install the tool in “Composer” project.

conclusion


In this paper, the local Mac system and server Linux system as the installation end, VSCode and PHPStorm two mainstream IDE as the use end, SVN and Git as the version library as an example, a relatively complete and systematic description of the use of “PHPCS”, I hope to help you!

One final question: If you use Docker and Git, how do you do this first-class program checking?

This article is reprinted from Ye Meishu. If you need to reprint, please apply to the original author. The original title is “Recommend a PHPCS plug-in specification for Laravel code (specification from local code writing to version control)”.

Recommended reading

May 2018 Book List (bonus at the end)

A list of new books for April 2018

Asynchronous books the most complete Python book list

A list of essential algorithms books for programmers

The first Python neural network programming book

​​

Long press the QR code, you can follow us yo

I share IT articles with you every day.


If you reply “follow” in the background of “Asynchronous books”, you can get 2000 online video courses for free.