keeping a watchdog in the PHP repository(using PHPMD, PHP_CodeSniffer)

  • Post author:
  • Post last modified:2020-11-25
  • Post category:Code Review
  • Reading time:4 mins read

These days more people are trying to follow the coding rules. The coding style is also illustrated in details in PHP: The Right Way.

Regarding PHP, our codes are all verified by the service called SideCI and when it finds code style violation, it comments (bark out) in PullRequest.
(※This is our service.)

This service makes PHP_CodeSniffer and PHP Mess Detector(PHPMD) verify the repository.

Moreover, this article is inspired by an introductory article about HoundCI

Advantages of having SideCI

  1. It always detects the codes which don’t follow the code style before master.
  2. No need to install the tool for the entire member.
  3. By deciding the code style and following it, no need to discuss over and over.
  4. It does not break relationship of the team(important)

Image

Suppose we have this source code. Obviously there is a method which is not indented correctly.

PHP_CodeSniffer will find such code and SideCI will point out in the comment.

Sample Code

public function display1() {
}
public function display() {
image of GitHub PullRequest

How to use SIdeCI

You can use it by following below steps.

  • Access SideCI and Sign Up with GitHub Auth
  • https://www.sideci.com/
  • Select the repository of which you want to verify
  • Enable PHP_CodeSniffer and PHPMD and Save
  • Initial settings are done!
  • Open a new PullRequest in GitHub
  • The execution results of PHP_CodeSniffer and PHPMD will be commented by SideCI after a few seconds to a few minutes.
  • commenting will be done by the account which has signed up in SideCI first.
  • Customize!
  • Here, you can set such as the coding style and rules to verify

Customize!

What coding style to use?

First, you need to select the coding style. If you don’t select any in SideCI, it selects automatically as per the frame work. The supported frameworks are CakePHP, FuelPHP, CodeIgniter and Symfony2 (as of the creation of this article). PSR1 will be selected for others but you can specify PSR2 by sideci.yml.
When you are using the framework, it’s better to use the coding style which is used in framework side. For example, CakePHP is using PSR2. (Therefore, the style for CakePHP is most likely PSR2.)

What is PHPMD?

PHP_CodeSniffer is a popular coding style verification tool, but do you know what actually PHP Mess Detector(PHPMD) is?
This is a tool that is developed to be something similar to PMD of Java. It verifies the complexity of methods. It can also verify if the class name is the camel case style.
For example, the following code will be identified as complexity-high. (It is already hard to read with the multiple nested codes!)

/**
* CyclomaticComplexity
*/
// Cyclomatic Complexity = 12
class Foo {
public function example() {
if ($a == $b) {
if ($a1 == $b1) {
fiddle();
} else if ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
} else if ($c == $d) {
while ($c == $d) {
fiddle();
}
} else if ($e == $f) {
for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else{
switch ($z) {
case 1:
fiddle();
break;
case 2:
fiddle();
break;
case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
image of GitHub comment

Aside from the complexity, there are many rules which you can select, hence it’s better to describe the rules which you want to have in sideci.ymlfile.(Details are available in the SideCI website!

Disadvantages of having SideCI

1.It barks(comments) often in the beginning.

It may bark for the things which you were not aware of and it may bother you.

However, if you start following the rules, it will stop. Moreover, by using the editor that has lint tool settings, it reduces the barks largely.

If it still bothers you, you can change the settings of coding style.

Lets’try!

You can use SideCI for free right now!
Please try it!

More articles about PHP (PHPMD, PHP_CodeSniffer)


For more information about Sider, please go to our website.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.