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.
Illuminate\Contracts\Auth\Authenticatable vs. ambiguous User).php-cs-fixer CLI or IDE plugins (e.g., PHP-CS-Fixer for VSCode).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Rule Overrides | May conflict with existing .php-cs-fixer.dist.php configs. |
Audit existing config; use rules: ['@PHP-CS-Fixer:risky' => true] cautiously. |
| False Positives | FQCN enforcement might break legacy DocBlocks (e.g., * @var User). |
Test against a subset of PRs first; allowlist exceptions via config. |
| Toolchain Dependency | Requires php-cs-fixer (v2.18+ recommended). |
Pin version in composer.json; containerize CI steps for reproducibility. |
| IDE Misconfiguration | Some IDEs may not auto-fix FQCNs, causing manual work. | Enforce via CI (block PRs if rule fails) and document IDE setup in CONTRIBUTING.md. |
php-cs-fixer already used in the project? If not, what’s the migration path?# composer.json
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"adamwojs/php-cs-fixer-phpdoc-force-fqcn": "^1.0"
}
.php-cs-fixer.dist.php):
return (new PhpCsFixer\Config())
->setRules([
'@PHP-CS-Fixer:risky' => true,
'phpdoc_align' => ['align' => 'vertical'],
'phpdoc_no_empty_return' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_alphabetically' => true],
'phpdoc_var_without_type' => true,
// Custom rule (add after v2.18+)
'adamwojs_phpdoc_force_fqcn' => true,
]);
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->exclude('bootstrap/cache')
->exclude('storage')
)
app/, config/, routes/, and tests/ directories.--dry-run to estimate effort before full enforcement.php-cs-fixer fix --dry-run --diff to identify affected files.User → App\Models\User).- name: PHP-CS-Fixer (FQCN)
run: php-cs-fixer fix --rules=adamwojs_phpdoc_force_fqcn --dry-run --diff
README.md).Auth::class vs. Illuminate\Auth\AuthManager).autoload_files include the correct namespace mappings.php-cs-fixer is already integrated (or plan a parallel migration).composer.json and update config.main with CI warnings enabled.Illuminate\Support\Facades\*)..php-cs-fixer.dist.php with comments.'adamwojs_phpdoc_force_fqcn' => [
'ignore_annotations' => ['@mixin', '@template'],
'ignore_paths' => ['vendor/'],
],
php-cs-fixer version to avoid breaking changes (e.g., ^3.0).CONTRIBUTING.md:
## DocBlock FQCN Enforcement
This project enforces FQCNs in DocBlocks for type safety. Run:
```bash
composer require --dev adamwojs/php-cs-fixer-phpdoc-force-fqcn
php-cs-fixer fix
use statements or FQCN spelling.php-cs-fixer dependencies in CI to reduce flakiness:
- uses: actions/cache@v3
with:
path: vendor/bin/php-cs-fixer
key: ${{ runner.os }
How can I help you explore Laravel packages today?