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

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)


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
  • 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


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) {
} else if ($a2 == $b2) {
} else {
} else if ($c == $d) {
while ($c == $d) {
} else if ($e == $f) {
for ($n = 0; $n < $h; $n++) {
} else{
switch ($z) {
case 1:
case 2:
case 3:
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.


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.

Aki Asahara

CEO of Sider. Aki joined Fixstars in 2008 and served major clients such as the US Airforce, MIT, USC, Toyota, and Hitachi High-technologies. After his successful tenure, he was appointed CEO of US operations in 2012. He was appointed CEO of Sider in 2019. He holds a Ph.D. in Astrophysics from Kyoto University and is a Certified Scrum Master.

Leave a Reply

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