degraciamathieu/php-arguments-detector
Detect and analyze function/method arguments in PHP using a lightweight, reflection-based approach. Useful for tooling that needs to inspect call signatures, validate inputs, or generate metadata about parameters and defaults across codebases.
Installation Add the package via Composer:
composer require --dev degraciamathieu/php-arguments-detector
Register the provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
DeGraciaMathieu\ArgumentsDetector\ArgumentsDetectorServiceProvider::class,
],
Basic Usage Run the detector on your project:
php artisan arguments:detect
This will scan your codebase and report methods exceeding your configured argument limit.
First Use Case
.php-arguments-detector.php (auto-generated in project root):
return [
'max_arguments' => 4, // Default: 4
'exclude_paths' => [
'vendor/*',
'tests/*',
],
];
CI/CD Pipeline Add the detector to your CI pipeline (e.g., GitHub Actions) to fail builds on violations:
- name: Check method arguments
run: php artisan arguments:detect --fail-on-violations
Pre-Commit Hooks Use tools like PHP-CS-Fixer or custom scripts to run the detector before commits:
composer require --dev php-cs-fixer
composer cs-fix
php artisan arguments:detect --format=json > arguments_report.json
IDE Integration
--format=json) to highlight violations in your IDE (e.g., PHPStorm via custom inspections).{
"violations": [
{
"file": "app/Services/UserService.php",
"method": "createUser",
"line": 42,
"arguments": 5,
"max_allowed": 4
}
]
}
Refactoring Violations Use the output to identify methods needing refactoring (e.g., extract parameters into objects or classes):
// Before (5 args)
public function processOrder($userId, $productId, $quantity, $discountCode, $shippingAddress) { ... }
// After (1 arg)
public function processOrder(OrderRequest $request) { ... }
Configuring Exceptions
Exclude specific methods/classes from checks in .php-arguments-detector.php:
return [
'excluded_methods' => [
'App\Services\LegacyService::migrateData', // Legacy code
],
];
Custom Rules Extend the detector by creating a custom rule (e.g., for constructors):
// app/Rules/ConstructorArgumentsRule.php
namespace App\Rules;
use DeGraciaMathieu\ArgumentsDetector\Rules\AbstractRule;
class ConstructorArgumentsRule extends AbstractRule {
protected $maxArguments = 3;
public function check($method) {
return $method->isConstructor() && $method->getParameters()->count() > $this->maxArguments;
}
}
Register it in the config:
'custom_rules' => [
App\Rules\ConstructorArgumentsRule::class,
],
False Positives
'exclude_static_methods' => true,
__call or __callStatic to avoid noise.Performance
--parallel for faster scans (if supported in future versions).php artisan arguments:detect --format=json > report.json
git add report.json
Configuration Overrides
.php-arguments-detector.php file is auto-generated but may be overwritten. Commit it to version control to preserve settings.'max_arguments' => env('ARGUMENTS_DETECTOR_MAX', 4),
--verbose to debug skipped files or methods:
php artisan arguments:detect --verbose
--dry-run to see what would be checked without running the full analysis.Custom Formatters
Extend the DeGraciaMathieu\ArgumentsDetector\Formatters\FormatterInterface to create custom output formats (e.g., Slack notifications):
// app/Formatters/SlackFormatter.php
namespace App\Formatters;
use DeGraciaMathieu\ArgumentsDetector\Formatters\FormatterInterface;
class SlackFormatter implements FormatterInterface {
public function format(array $violations) {
$message = "🚨 Arguments Detector Violations:\n";
foreach ($violations as $violation) {
$message .= "- {$violation['file']}::{$violation['method']} (Line {$violation['line']})\n";
}
return $message;
}
}
Register it in config:
'formatter' => App\Formatters\SlackFormatter::class,
Hooks for Refactoring Integrate with tools like Rector to auto-fix violations:
composer require rector/rector
vendor/bin/rector process src --dry-run
Git Blame Integration
Combine with git blame to identify who introduced violations:
php artisan arguments:detect --format=json | jq -r '.violations[] | "git blame -L /\(.line),\(.line) \(.file)"' | xargs -I{} sh -c '{}'
How can I help you explore Laravel packages today?