Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP_CodeSniffer, PHPMD or PHP Depend
PHP offers several powerful code analysis tools to help maintain code quality and enforce coding standards. Three essential tools − PHP_CodeSniffer (phpcs), PHPMD, and PHP Depend (pdepend) − each serve different purposes in code quality assessment.
PHP_CodeSniffer (phpcs)
PHP_CodeSniffer tokenizes PHP, JavaScript, and CSS files to detect violations against predefined coding standards. It ensures code consistency and helps prevent common semantic errors.
Installation: Install via Composer:
composer global require "squizlabs/php_codesniffer=*"
Example Usage
Here's how to check a PHP file against PSR−12 coding standard ?
<?php
// sample.php - File with coding standard violations
class myClass{
public function getData( $param ){
if($param==null)return false;
$result=$this->process($param);
return $result;
}
}
?>
Command to run PHP_CodeSniffer ?
phpcs --standard=PSR12 sample.php
FILE: sample.php FOUND 5 ERRORS AFFECTING 4 LINES 2 | ERROR | Class name "myClass" is not in PascalCase format 3 | ERROR | Expected 0 spaces after opening parenthesis; 1 found 4 | ERROR | Expected 1 space after IF keyword; 0 found 4 | ERROR | Expected 1 space before closing parenthesis; 0 found
PHPMD (PHP Mess Detector)
PHPMD scans PHP source code to identify potential problems including possible bugs, suboptimal code, and overcomplicated expressions.
Installation: Install via Composer:
composer global require phpmd/phpmd
Example Usage
Sample PHP code with potential issues ?
<?php
class UserManager {
public function processUserData($userData) {
// Unused variable
$tempData = array();
// Overly complex condition
if (isset($userData['name']) && !empty($userData['name']) &&
isset($userData['email']) && !empty($userData['email']) &&
isset($userData['age']) && is_numeric($userData['age']) &&
$userData['age'] > 0 && $userData['age'] < 120) {
return true;
}
return false;
}
}
?>
Command to run PHPMD ?
phpmd UserManager.php text cleancode,unusedcode
UserManager.php:5 Avoid unused local variables such as '$tempData'. UserManager.php:7 The method processUserData() has a Cyclomatic Complexity of 4.
PHP Depend (pdepend)
PHP Depend generates comprehensive software metrics from codebases to measure project quality and identify areas requiring refactoring.
Installation: Install via Composer:
composer global require pdepend/pdepend
Example Usage
Command to generate dependency analysis ?
pdepend --jdepend-xml=report.xml --overview-pyramid=pyramid.svg /path/to/source
Sample metrics output ?
Analyzing Dependencies... Classes: 25 Methods: 127 Lines of Code: 2,341 Cyclomatic Complexity: 8.5 Maintainability Index: 85.3
Comparison
| Tool | Primary Purpose | Output Format |
|---|---|---|
| PHP_CodeSniffer | Coding standards compliance | Violation reports |
| PHPMD | Code quality issues detection | Problem identification |
| PHP Depend | Software metrics generation | Metrics and visualizations |
Conclusion
These three tools complement each other perfectly − use PHP_CodeSniffer for coding standards, PHPMD for code quality issues, and PHP Depend for architectural insights and metrics.
