Skip to content

ashraf789/PHP-static-code-analysis

Repository files navigation

PHP static code analysis by PHPCS, PHPMD and PHP Phan

PHPCS

install by composer

composer require --dev squizlabs/php_codesniffer
Now create a controller by artisan like php artisan make:controller TestController And modify controller like bellow.

namespace AppHttpControllers;

use IlluminateHttpRequest;

class testController extends Controller
{
    private function testTest(){
    }
}

Run PPHCS on terminal

./vendor/bin/phpcs app/Http/Controllers/TestController.php

Some error will display on terminal also you will get a message on bottom of report that some error could be fixed automatically To automatically fix those error run this command.


./vendor/bin/phpcbf app/Http/Controllers/TestController.php


Automatically some error will be fixed. we can make a configure file to make it more simple make a phpcs.xml file on top of root directory and write this code


<?xml version="1.0"?>
<ruleset name="PSR2">    
<description>The PSR2 coding standard.</description>    
<rule ref="PSR2"/>     
<file>app/</file>     
<exclude-pattern>vendor</exclude-pattern>    
<exclude-pattern>resources</exclude-pattern>    
<exclude-pattern>database/</exclude-pattern>    
<exclude-pattern>storage/</exclude-pattern>    
<exclude-pattern>node_modules/</exclude-pattern>
<exclude-pattern>public</exclude-pattern>
</ruleset>

Now we can check our all app/ directory code by just a simple command

./vendor/bin/phpcs

Every time we write any new code we have to run this command again and again. We can make it more simpler like we could add a rule on git pre hook to run this before any new commit.

Git pre hook add:

create folder name git-hooks and inside the folder create a file name pre-hooks then add this code on your git-hooks file
https://github.com/ashraf789/PHP-static-code-analysis/blob/master/git-hooks/pre-commit

Now we have to move git-hooks file inside .git/hooks/ directory to move there we will use composer.
Add this below code on your composer.json file inside the scripts.

"post-update-cmd": [
            "cp git-hooks/pre-commit .git/hooks/pre-commit",
            "chmod a+x .git/hooks/pre-commit"
        ],
        "post-install-cmd": [
            "cp git-hooks/pre-commit .git/hooks/pre-commit",
            "chmod a+x .git/hooks/pre-commit"
        ]

Note: please use git bash if you get any error like bellow

> cp git-hooks/pre-commit .git/hooks/pre-commit
'cp' is not recognized as an internal or external command,
operable program or batch file.
Script cp git-hooks/pre-commit .git/hooks/pre-commit handling the post-install-cmd event returned with error code 1

Look at my composer.json file
https://github.com/ashraf789/PHP-static-code-analysis/blob/master/composer.json

Now go to termianl and run composer up
That's it

PHPMD

install by composer

composer require --dev phpmd/phpmd

Run PPHMD on terminal


vendor/bin/phpmd app html cleancode,codesize,controversial,design,naming,unusedcode > phpmd.html

Command Analysis

  1. vendor/bin/phpmd -> phpmd location
  2. app -> analysable code directory
  3. html -> output formate[text is also fine]
  4. cleancode,codesize,controversial,design,naming,unusedcode -> rule to analysis code
  5. "> phpmd.html" -> save output on phpmd.html file

We can save all rules on a xml file. See the bellow file.


https://github.com/ashraf789/PHP-static-code-analysis/blob/master/phpmd.xml


Now we can run PPHMD on terminal like bellow


vendor/bin/phpmd app html phpmd.xml > phpmd.html

PHAN

install by composer composer require --dev "phan/phan:2.x"

The Phan is depend on php-ast so before run project install php-ast.
Please read this documentation to install php-ast
https://github.com/nikic/php-ast

Before run PHAN we need to create a PHAN configure file. Create a .phan directory on your project root directory and inside directory make a config.xml file. Give a look on my config file https://github.com/ashraf789/PHP-static-code-analysis/blob/master/.phan/config.php


Now run phan on your project

./vendor/bin/phan