PHP Classes

How to Use a PHP Quality Assurance Tool to Improve Your PHP Code Using the PHP Smelly Code Detector: Analyze PHP code to evaluate quality factors

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 45 All time: 10,816 This week: 56Up
Version License PHP version Categories
php-smelly-code-dete 1.0.0The PHP License5PHP 5, Tools, Parsers
Description 

Author

This package can analyze PHP code to evaluate quality factors.

It provides a script that calls the package classes, analyzes the quality factors, and displays the results in a console terminal.

Currently, this package can analyze the following:

- The complexity of the code of functions

- The number of arguments of functions

- The number of lines of code

Innovation Award
PHP Programming Innovation award nominee
January 2023
Number 3
One way to evaluate the quality of your PHP code is to evaluate its complexity. Complex code is hard to maintain.

Complexity can be evaluated in terms of hints like the code inside the functions, the number of function arguments, and the number of lines of code.

This package provides a tool written in PHP that can evaluate all those complexity aspects.

This way, developers can have an idea of what code is too complex so they can improve the code to simplify it to lower the complexity grades that this tool provides.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Innovation award
Innovation award
Nominee: 16x

Winner: 2x

 

Example

#!/usr/bin/env php
<?php

use Symfony\Component\Console\Application;
use
DeGraciaMathieu\SmellyCodeDetector\Commands\InspectCommand;

$loaded = false;

foreach (array(
__DIR__ . '/../../autoload.php', __DIR__ . '/vendor/autoload.php') as $file) {
    if (
file_exists($file)) {
        require
$file;
       
$loaded = true;
        break;
    }
}

if (!
$loaded) {
    die(
       
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
       
'wget http://getcomposer.org/composer.phar' . PHP_EOL .
       
'php composer.phar install' . PHP_EOL
   
);
}

$application = new Application();

$command = $application->add(new InspectCommand());

$application->run();


Details

<p align="center"> <img src="https://github.com/DeGraciaMathieu/php-smelly-code-detector/blob/master/arts/robot.png" width="250"> </p>

testing phpstan Packagist Version Packagist PHP Version

php-smelly-code-detector

> "A code smell is a surface indication that usually corresponds to a deeper problem in the system." > ~ Martin Fowler

Code smell is a potentially problematic code indicator with the following formula : ($ccn + $arg) * $loc

  • ccn : cyclomatic complexity of the method
  • arg : number of method arguments
  • loc : number of lines in the method

A high smell value will often reveal that the method is too complex.

This complexity could be detrimental to the maintainability of the method, favoring the appearance of bugs in the future.

This indicator does not replace the expertise of a developer and must above all be considered as an alarm detecting "smelly" code.

Installation

Requires >= PHP 8.1

composer require degraciamathieu/php-smelly-code-detector --dev

Usage

vendor/bin/smellyphpcodedetector inspect {folder}

Options

| options | description | |-----------------------|-------------| | --min-smell= | Ignore methods with less than --min-smell | | --max-smell= | Ignore methods with more than --max-smell | | --limit= | Number of methods displayed. | | --without-constructor | Ignore method constructors from detection. | | --sort-by-smell | Sort the results by the smell of methods. |

Examples

$ php smellyphpcodedetector inspect app --sort-by-smell --limit=10
? PHP Smelly Code Detector ?
   81 [============================] < 1 sec
+-----------------------------------------------------------+---------------------------------+-------+
| Files                                                     | Methods                         | smell |
+-----------------------------------------------------------+---------------------------------+-------+
| app/Http/Controllers/Blog/AdminPostController.php         | update                          | 174   |
| app/Http/Controllers/Auth/NewPasswordController.php       | store                           | 87    |
| app/Console/Commands/FetchGoogleFonts.php                 | store                           | 78    |
| app/Http/Middleware/RedirectIfAuthenticated.php           | handle                          | 77    |
| app/Http/Controllers/User/ProfileController.php           | updateAvatar                    | 75    |
| app/Services/Community/CreatorRepository.php              | instantiateCreatorsFromResponse | 72    |
| app/Http/Controllers/Auth/PasswordResetLinkController.php | store                           | 51    |
| app/Http/Controllers/User/ProfileController.php           | updateInformations              | 51    |
| app/Http/Controllers/Auth/RegisteredUserController.php    | store                           | 50    |
| app/Http/Controllers/Blog/ShowPostController.php          | __invoke                        | 48    |
+-----------------------------------------------------------+---------------------------------+-------+
193 methods found.

  Files folder image Files (24)  
File Role Description
Files folder image.github (1 directory)
Files folder imagearts (1 file)
Files folder imagesrc (2 files, 5 directories)
Files folder imagetests (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file smellyphpcodedetector Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:45
This week:0
All time:10,816
This week:56Up