Installation:
composer require sweetchuck/robo-phpmd
Add the trait to your RoboFile.php:
use Sweetchuck\Robo\PhpMessDetector\PhpmdTaskLoader;
First Use Case: Run a basic PHPMessDetector (PHPMD) analysis on your project:
public function phpmd()
{
return $this->taskPhpmdLintFiles()
->setPaths(['src/']);
}
Execute via CLI:
robo phpmd
Where to Look First:
PhpmdTaskLoader trait for available methods (e.g., setReportFormat, setRuleSetFileNames).custom-rules.xml) can be loaded via setRuleSetFileNames.Basic Linting:
$this->taskPhpmdLintFiles()
->setPaths(['src/', 'tests/'])
->setReportFormat('text'); // Outputs to console
Custom Rulesets:
Load a custom XML ruleset (e.g., rulesets/custom.xml):
$this->taskPhpmdLintFiles()
->setRuleSetFileNames(['rulesets/custom.xml']);
Excluding Paths: Skip specific files/directories:
$this->taskPhpmdLintFiles()
->setExcludePaths(['src/legacy/', 'vendor/']);
Integration with CI: Fail the build on violations (PHPMD returns non-zero exit code by default):
$this->taskPhpmdLintFiles()
->setFailOnViolation(true); // Explicitly enforce failure
HTML/JSON Reports: Generate reports for dashboards or tools:
$this->taskPhpmdLintFiles()
->setReportFormat('html')
->setReportFile('phpmd-report.html');
Parallel Execution: Use PHPMD’s parallel processing (if supported by your version):
$this->taskPhpmdLintFiles()
->setParallel(4); // Process 4 files at once
app/, config/, routes/):
$this->taskPhpmdLintFiles()
->setPaths([
'app/',
'config/',
'routes/',
'app/Providers/',
'app/Http/Controllers/'
]);
public function qualityCheck()
{
$this->taskExec('phpunit')
->then($this->taskPhpmdLintFiles()->setPaths(['src/']));
}
basePath() or appPath() for path resolution:
$this->taskPhpmdLintFiles()
->setPaths([base_path('app'), base_path('tests/Unit')]);
Outdated Package:
PhpmdTaskLoader if PHPMD’s CLI changes.RuleSet File Paths:
setRuleSetFileNames are relative to the working directory (not RoboFile.php).base_path() for reliability:
->setRuleSetFileNames([base_path('rulesets/custom.xml')])
Performance:
--exclude-paths to narrow scope.vendor/ or node_modules/ (irrelevant for code quality).Report Formats:
xml may require additional setup).html or json in a CI environment to ensure rendering works.Exit Codes:
2 on errors (e.g., invalid ruleset). Handle gracefully:
$this->taskPhpmdLintFiles()
->setFailOnViolation(true)
->run(); // Will throw exception on non-zero exit
$this->taskPhpmdLintFiles()
->setVerbose(true);
$this->taskPhpmdLintFiles()
->setFailOnViolation(false);
phpmd src/ text --exclude src/legacy/
Custom Rulesets:
Extend PHPMD’s rules by creating a ruleset.xml (see PHPMD docs).
Example structure:
<ruleset name="Custom Rules">
<rule ref="rulesets/codesize.xml">
<exclude name="ExcessiveClassComplexity"/>
</rule>
</ruleset>
Post-Processing Reports:
Parse PHPMD’s output (e.g., json) to integrate with tools like Slack or GitHub PR comments:
$report = $this->taskPhpmdLintFiles()
->setReportFormat('json')
->run();
// Parse $report->getMessage() for custom logic
Robo Task Extensions:
Override PhpmdTaskLoader to add custom methods (e.g., setCustomRule):
namespace App\Robo;
use Sweetchuck\Robo\PhpMessDetector\PhpmdTaskLoader as BaseLoader;
trait CustomPhpmdTaskLoader extends BaseLoader {
public function setCustomRule(string $rule) {
return $this->taskPhpmdLintFiles()->addArgument('--custom-rule');
}
}
Laravel Artisan Integration: Wrap PHPMD in an Artisan command for easier CLI access:
// app/Console/Commands/RunPhpmd.php
public function handle() {
$this->call('robo', ['phpmd']);
}
How can I help you explore Laravel packages today?