Priti's Technical Corner

October 21, 2016

phpmd for code review


Hello Friends,

I use phpmd for code reviews. It is quick and easy to setup. Provides me first hand information about sub optimal code, over complicated experssion,Unused parameters, methods, properties and suggestions for refactoring class and functions.

Install phpmd

Add in your composer.json of your project

“require-dev”: {

“phpmd/phpmd” : “@stable”

}

Command prompt >phpmd /path/to/directory/for/php/class text

You may get following error sometimes

‘phpmd’ is not recognized as  internal or external command

on command prompt run

> composer global require phpmd/phpmd

Mandatory arguments:

1) A php source code filename or directory. Can be a comma-separated string

2) A report format (It can be Text, Html, XML)

3) A ruleset filename or a comma-separated string of ruleset filenames

Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments 

  • minimumpriority: rule priority threshold; rules with lower priority than this will not be used
  • reportfile: send report output to a file; default to STDOUT
  • suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml
  • exclude: comma-separated string of patterns that are used to ignore directories
  • strict: also report those nodes with a @SuppressWarnings annotation

Rule set for PHPMD

To dump phpmd output in a file

On command prompt

phpmd C:\wamp64\www\myproject\models text –reportfile anyfilename.txt  unusedcode, controversial, codesize, cleancode

The interesting thing is you can define the rules as per your projects. An easy CLI tool to keep code neat and clean.

Happy Coding !

Advertisements

March 21, 2013

PHP Array Index and Constant

Filed under: PHP, PHP 5.3 Certification, PHP Array — Tags: — pritisolanki @ 5:30 am

I asked following question in my training to explain some point.

Consider following

define(‘Message’,’name’);

$arr[1]=’Hello’;
$arr[‘Message’]=’World’;
$arr[‘name’]=”Jhon”;

echo $arr[1].$arr[Message];

Answers Received :

Hello World
Hello name
Hello Jhon

One question and multiple answers ! The right answer is “Hello Jhon” . Does it tickle your brain? why ! .

Please notice there is no single quote around string index – “Message” in $arr[Message] . And, “Message” is a defined constant which is initialized to “name” and $arr[‘name’] is Jhon :).

Above example explains why we should always provide single quotes to string index.

 

 

 

Older Posts »

Create a free website or blog at WordPress.com.

%d bloggers like this: