adamwojs/php-cs-fixer-phpdoc-force-fqcn
PHP-CS-Fixer custom rule that forces fully qualified class names in PHPDoc annotations. Ensures consistent, unambiguous @param/@return/@var types by converting short names to FQCNs, improving readability and reducing namespace-related confusion.
Installation: Add the package to your project via Composer:
composer require --dev adamwojs/php-cs-fixer-phpdoc-force-fqcn
Register the Rule:
Add the rule to your .php-cs-fixer.dist.php config:
<?php
return PhpCsFixer\Config::create()
->registerCustomRule(
Adamwojs\PhpCsFixer\Rules\ForceFqcnInDocBlock::class
)
->setRules([
'@PHP-CS-Fixer:risky' => true,
'adamwojs_force_fqcn_in_docblock' => true,
]);
First Use Case:
Run the fixer on a file with partial FQCNs in docblocks (e.g., @param array $data instead of @param \ArrayObject $data):
./vendor/bin/php-cs-fixer fix src/Your/Class.php
The rule will auto-correct docblocks to use fully qualified class names (e.g., \ArrayObject).
CI/CD Pipeline:
- name: Run PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --rules=adamwojs_force_fqcn_in_docblock
--dry-run to preview changes before applying.Pre-Commit Hook: Use tools like PHP-CS-Fixer’s pre-commit hook to auto-fix docblocks before commits:
composer require --dev php-cs-fixer/php-cs-fixer
vendor/bin/php-cs-fixer fix --allow-risky=yes
Team Onboarding:
\Laravel\Foo\Bar instead of Bar)."Partial Application:
->setRules([
'adamwojs_force_fqcn_in_docblock' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('legacy')
),
Performance Overhead:
--parallel flag.False Positives:
@param mixed $data). Review changes manually for edge cases.@template TKey of array-key should not be modified.Namespace Conflicts:
User in \App\User and \Vendor\User), the rule may pick the wrong FQCN.Dynamic Classes:
@param string $className where $className is a variable).--dry-run --diff to see changes before applying:
./vendor/bin/php-cs-fixer fix --dry-run --diff
./vendor/bin/php-cs-fixer fix -v
Custom FQCN Mapping: Override default FQCN resolution by extending the rule:
use Adamwojs\PhpCsFixer\Rules\ForceFqcnInDocBlock;
class CustomForceFqcnRule extends ForceFqcnInDocBlock {
protected function getFqcn(string $shortClassName): ?string {
// Custom logic (e.g., alias mapping)
return match ($shortClassName) {
'User' => '\\App\\Models\\CustomUser',
default => parent::getFqcn($shortClassName),
};
}
}
Register it in .php-cs-fixer.dist.php:
->registerCustomRule(CustomForceFqcnRule::class)
Excluding Rules:
Disable the rule for specific docblock types (e.g., @var):
->setRules([
'adamwojs_force_fqcn_in_docblock' => [
'types' => ['param', 'return', 'throws'], // Exclude 'var'
],
]),
Laravel-Specific Use Cases:
@param \Illuminate\Contracts\Auth\Factory $auth)./**
* @param \Illuminate\Console\Command $command
* @return void
*/
Testing:
$this->assertStringContainsString('\Laravel\Foo\Bar', $docblock);
roave/security-advisories to ensure FQCNs align with dependency versions.IDE Support:
How can I help you explore Laravel packages today?