PHP Classes

PHP Function Arguments Detector: Detect complex functions with too many arguments

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: 44 All time: 10,815 This week: 560Up
Version License PHP version Categories
php-arguments-detect 1.0The PHP License5PHP 5, Tools
Description 

Author

This package can detect complex functions with too many arguments.

It can check the application classes automatically loaded using the vendor/autoload.php script.

The package can find application classes that take many arguments and report them in the command line console.

Innovation Award
PHP Programming Innovation award nominee
August 2022
Number 4
Complex PHP code is hard to maintain. In practice, this means that if you use a complex coding style, you spend a lot of time analyzing and improving your code every time you need to make changes.

There are several types of signs that you use a complex coding style. One of them is the way you declare your classes and functions.

For instance, if you create classes with functions with many parameters, that is a sign that you are trying to do many operations in those functions.

This package can help you to find which can be the complex class functions, so you can fix them, probably splitting them into several functions that do fewer operations.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 21 packages by
Country: France France
Age: ???
All time rank: 289780 in France France
Week rank: 18 Up2 in France France Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Example

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

use Symfony\Component\Console\Application;
use
DeGraciaMathieu\PhpArgsDetector\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

build packagist) packagist)

php arguments detector

> The ideal number of arguments for a function is zero. ~ Robert C. Martin

Keep control over the complexity of your methods by checking that they do not have too many arguments with this package.

Installation

Requires >= PHP 7.3

composer require degraciamathieu/php-arguments-detector --dev

Usage

vendor/bin/phpargsdetector inspect {folder}

Options

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

Examples

vendor/bin/phpargsdetector inspect app/Services/Saml/

+------------------------------------------+------------------+-----------+--------+
| Files                                    | Methods          | Arguments | Weight |
+------------------------------------------+------------------+-----------+--------+
| app/Services/Saml/SamlMessageFactory.php | __construct      | 2         | 2      |
| app/Services/Saml/SamlMessageFactory.php | makeSamlResponse | 2         | 68     |
| app/Services/Saml/SamlSecurity.php       | checkSignature   | 2         | 18     |
| app/Services/Saml/SamlIssuer.php         | find             | 1         | 3      |
| app/Services/Saml/SamlKeeper.php         | keep             | 1         | 1      |
| app/Services/Saml/SamlMessageFactory.php | addAttributes    | 1         | 26     |
| app/Services/Saml/SamlMessageFactory.php | sign             | 1         | 12     |
| app/Services/Saml/SamlResponder.php      | launch           | 1         | 10     |
| app/Services/Saml/SamlKeeper.php         | has              | 0         | 0      |
| app/Services/Saml/SamlKeeper.php         | retrieve         | 0         | 0      |
+------------------------------------------+------------------+-----------+--------+
Total of methods : 10
vendor/bin/phpargsdetector inspect app/ --limit=3 --min=2 --without-constructor

+-------------------------------------------------+---------+-----------+--------+
| Files                                           | Methods | Arguments | Weight |
+-------------------------------------------------+---------+-----------+--------+
| app/Http/Middleware/RedirectIfAuthenticated.php | handle  | 3         | 27     |
| app/Http/Controllers/IssuerController.php       | update  | 2         | 24     |
| app/Http/Controllers/RestrictionController.php  | update  | 2         | 28     |
+-------------------------------------------------+---------+-----------+--------+
Total of methods : 3
vendor/bin/phpargsdetector inspect app/ --limit=3 --sort-by-weight

+-------------------------------------------------+------------------+-----------+--------+
| Files                                           | Methods          | Arguments | Weight |
+-------------------------------------------------+------------------+-----------+--------+
| app/Services/Saml/SamlMessageFactory.php        | makeSamlResponse | 2         | 68     |
| app/Http/Controllers/RestrictionController.php  | update           | 2         | 28     |
| app/Http/Middleware/RedirectIfAuthenticated.php | handle           | 3         | 27     |
+-------------------------------------------------+------------------+-----------+--------+
Total of methods : 3

Weight

The weight is the number of arguments multiplied by the number of lines of the method.

The weight of the foo method is 10 : 2 arguments * 5 lines.

class Bar {
    public function foo($a, $b)
    {
        if ($a) {
           //
        }

        return $b;
    }
}

You can use it as a complexity indicator.


  Files folder image Files (13)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocker (1 directory)
Files folder imagesrc (3 files, 3 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file docker-compose.yml Data Auxiliary data
Accessible without login Plain text file phpargsdetector Example Example script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (13)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (13)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file build.yml Data Auxiliary data

  Files folder image Files (13)  /  docker  
File Role Description
Files folder imageapp (1 file)

  Files folder image Files (13)  /  docker  /  app  
File Role Description
  Accessible without login Plain text file Dockerfile Data Auxiliary data

  Files folder image Files (13)  /  src  
File Role Description
Files folder imageCommands (1 file)
Files folder imagePrinters (1 file)
Files folder imageVisitors (1 file)
  Plain text file Detector.php Class Class source
  Plain text file Method.php Class Class source
  Plain text file NodeMethodExplorer.php Class Class source

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

  Files folder image Files (13)  /  src  /  Printers  
File Role Description
  Plain text file Console.php Class Class source

  Files folder image Files (13)  /  src  /  Visitors  
File Role Description
  Plain text file FileVisitor.php Class Class source

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:44
This week:0
All time:10,815
This week:560Up