GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ashraf789/PHP-static-code-analysis: Static code analysis by PHPCS, PHPMD and PHP PHAN.

BranchesTags
Open more actions menu

Latest commit

 

History

35 Commits
35 Commits

Folders and files

NameName
Last commit message
Last commit date
.phan
.phan
 
 
app
app
 
 
bootstrap
bootstrap
 
 
config
config
 
 
database
database
 
 
git-hooks
git-hooks
 
 
public
public
 
 
resources
resources
 
 
routes
routes
 
 
storage
storage
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.env.example
.env.example
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.styleci.yml
.styleci.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
analysis.txt
analysis.txt
 
 
artisan
artisan
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
phpcs.xml
phpcs.xml
 
 
phpmd-ruleset.xml
phpmd-ruleset.xml
 
 
phpmd.xml
phpmd.xml
 
 
phpunit.xml
phpunit.xml
 
 
script.txt
script.txt
 
 
server.php
server.php
 
 
test.html
test.html
 
 
webpack.mix.js
webpack.mix.js
 
 

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

About

Static code analysis by PHPCS, PHPMD and PHP PHAN.

Topics

analysiscomposergithookslaravelphanphp-phanphpcsphpmdpsr-2static-code-analysis

Resources

Readme
Apache-2.0 license
Activity

Stars

5 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.