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   View files Example   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 45 This week: 1All time: 10,742 This week: 560Up
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  
Name: DeGraciaMathieu <contact>
Classes: 16 packages by
Country: France France
Age: ???
All time rank: 297081 in France France
Week rank: 106 Up5 in France France Up
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

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  
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

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (2 files)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file phpstan.yml Data Auxiliary data
  Accessible without login Plain text file testing.yml Data Auxiliary data

  Files folder image Files  /  arts  
File Role Description
  Accessible without login Image file robot.png Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageCommands (1 file)
Files folder imageEnums (1 file)
Files folder imageMetrics (1 file)
Files folder imagePrinters (1 file)
Files folder imageVisitors (3 files, 1 directory)
  Plain text file FileParser.php Class Class source
  Plain text file VisitorBag.php Class Class source

  Files folder image Files  /  src  /  Commands  
File Role Description
  Plain text file InspectCommand.php Class Class source

  Files folder image Files  /  src  /  Enums  
File Role Description
  Accessible without login Plain text file Metric.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Metrics  
File Role Description
  Plain text file MethodMetric.php Class Class source

  Files folder image Files  /  src  /  Printers  
File Role Description
  Plain text file InspectCommandPrinter.php Class Class source

  Files folder image Files  /  src  /  Visitors  
File Role Description
Files folder imageAbstracts (1 file)
  Plain text file ArgumentVisitor.php Class Class source
  Plain text file CyclomaticComplexityVisitor.php Class Class source
  Plain text file WeightVisitor.php Class Class source

  Files folder image Files  /  src  /  Visitors  /  Abstracts  
File Role Description
  Plain text file Visitor.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageUnit (2 files, 3 directories)

  Files folder image Files  /  tests  /  Unit  
File Role Description
Files folder imageCommands (1 file)
Files folder imageMetrics (1 file)
Files folder imageStubs (1 file)
  Plain text file FileParserTest.php Class Class source
  Plain text file VisitorBagTest.php Class Class source

  Files folder image Files  /  tests  /  Unit  /  Commands  
File Role Description
  Plain text file InspectCommandTest.php Class Class source

  Files folder image Files  /  tests  /  Unit  /  Metrics  
File Role Description
  Plain text file MethodMetricTest.php Class Class source

  Files folder image Files  /  tests  /  Unit  /  Stubs  
File Role Description
  Plain text file Foo.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:45
This week:1
All time:10,742
This week:560Up