Custom Fixers
Naming Convention Fixers
Section titled “Naming Convention Fixers”AbstractNameFixer
Section titled “AbstractNameFixer”Enforces that abstract classes follow the Abstract* naming pattern.
// ❌ Beforeabstract class BaseRepository {}abstract class RepositoryBase {}
// ✅ Afterabstract class AbstractRepository {}Rule Key: Architecture/abstract_name_fixer
InterfaceNameFixer
Section titled “InterfaceNameFixer”Enforces that interfaces follow the *Interface naming pattern.
// ❌ Beforeinterface Repository {}interface IRepository {}
// ✅ Afterinterface RepositoryInterface {}Rule Key: Architecture/interface_name_fixer
TraitNameFixer
Section titled “TraitNameFixer”Enforces that traits follow standard trait naming conventions.
// ❌ Beforetrait TimestampsTrait {}
// ✅ Aftertrait Timestamps {}Rule Key: Architecture/trait_name_fixer
ExceptionNameFixer
Section titled “ExceptionNameFixer”Enforces that exceptions follow the *Exception naming pattern.
// ❌ Beforeclass InvalidInput {}class ValidationError {}
// ✅ Afterclass InvalidInputException {}class ValidationException {}Rule Key: Architecture/exception_name_fixer
VariableCaseFixer
Section titled “VariableCaseFixer”Enforces camelCase naming for variables.
// ❌ Before$user_name = 'John';$UserEmail = 'john@example.com';
// ✅ After$userName = 'John';$userEmail = 'john@example.com';Rule Key: Architecture/variable_case_fixer
Import Fixers
Section titled “Import Fixers”ImportFqcnInNewFixer
Section titled “ImportFqcnInNewFixer”Automatically imports fully qualified class names in new expressions.
// ❌ Before$user = new \App\Models\User();
// ✅ Afteruse App\Models\User;
$user = new User();Rule Key: Architecture/import_fqcn_in_new_fixer
ImportFqcnInAttributeFixer
Section titled “ImportFqcnInAttributeFixer”Automatically imports fully qualified class names in attributes.
// ❌ Before#[\App\Attributes\Cached(ttl: 3600)]class UserRepository {}
// ✅ Afteruse App\Attributes\Cached;
#[Cached(ttl: 3600)]class UserRepository {}Rule Key: Architecture/import_fqcn_in_attribute_fixer
ImportFqcnInStaticCallFixer
Section titled “ImportFqcnInStaticCallFixer”Automatically imports fully qualified class names in static method calls.
// ❌ Before$value = \App\Services\Cache::get('key');
// ✅ Afteruse App\Services\Cache;
$value = Cache::get('key');Rule Key: Architecture/import_fqcn_in_static_call_fixer
ImportFqcnInPropertyFixer
Section titled “ImportFqcnInPropertyFixer”Automatically imports fully qualified class names in property type declarations.
// ❌ Beforeclass UserController{ private \App\Services\UserService $userService;}
// ✅ Afteruse App\Services\UserService;
class UserController{ private UserService $userService;}Rule Key: Architecture/import_fqcn_in_property_fixer
Code Quality Fixers
Section titled “Code Quality Fixers”FinalReadonlyClassFixer
Section titled “FinalReadonlyClassFixer”Automatically adds final modifier to readonly classes.
// ❌ Beforereadonly class User {}
// ✅ Afterfinal readonly class User {}Rule Key: Architecture/final_readonly_class_fixer
RedundantReadonlyPropertyFixer
Section titled “RedundantReadonlyPropertyFixer”Removes redundant readonly modifiers from properties in readonly classes.
// ❌ Beforereadonly class User{ public readonly string $name;}
// ✅ Afterreadonly class User{ public string $name;}Rule Key: Architecture/redundant_readonly_property_fixer
DuplicateDocBlockAfterAttributesFixer
Section titled “DuplicateDocBlockAfterAttributesFixer”Removes duplicate PHPDoc blocks that appear after PHP attributes.
// ❌ Before#[Route('/users')]/** * @param string $id */public function show(string $id) {}
// ✅ After#[Route('/users')]public function show(string $id) {}Rule Key: Architecture/duplicate_docblock_after_attributes_fixer
PsalmImmutableOnReadonlyClassFixer
Section titled “PsalmImmutableOnReadonlyClassFixer”Adds @psalm-immutable annotation to readonly classes.
// ❌ Beforereadonly class User {}
// ✅ After/** * @psalm-immutable */readonly class User {}Rule Key: Architecture/psalm_immutable_on_readonly_class_fixer
Documentation Fixers
Section titled “Documentation Fixers”AuthorTagFixer
Section titled “AuthorTagFixer”Enforces consistent @author tag format in PHPDoc blocks.
// ❌ Before/** * @author John Doe */
// ✅ After/** * @author Brian Faust <brian@cline.sh> */Rule Key: Architecture/author_tag_fixer
VersionTagFixer
Section titled “VersionTagFixer”Enforces consistent @version tag format in PHPDoc blocks.
// ❌ Before/** * @version 1.0 */
// ✅ After/** * @version 1.0.0 */Rule Key: Architecture/version_tag_fixer
NamespaceFixer
Section titled “NamespaceFixer”Enforces consistent namespace declarations.
Rule Key: Architecture/namespace_fixer
Formatting Fixers
Section titled “Formatting Fixers”NewArgumentNewlineFixer
Section titled “NewArgumentNewlineFixer”Enforces newlines for constructor arguments in new expressions.
// ❌ Before$user = new User('John', 'Doe', 'john@example.com', 25);
// ✅ After$user = new User( 'John', 'Doe', 'john@example.com', 25,);Rule Key: Architecture/new_argument_newline_fixer
Enabling Custom Fixers
Section titled “Enabling Custom Fixers”All custom fixers are automatically registered when using ConfigurationFactory::createFromPreset(). To enable specific fixers:
use Cline\CodingStandard\PhpCsFixer\ConfigurationFactory;use Cline\CodingStandard\PhpCsFixer\Preset\Standard;
return ConfigurationFactory::createFromPreset( new Standard(), [ 'Architecture/abstract_name_fixer' => true, 'Architecture/interface_name_fixer' => true, 'Architecture/exception_name_fixer' => true, ]);Some fixers are disabled by default in the Standard preset:
// Commented out in Standard preset (disabled by default):// 'Architecture/abstract_name_fixer' => true,// 'Architecture/exception_name_fixer' => true,// 'Architecture/interface_name_fixer' => true,// 'Architecture/trait_name_fixer' => true,// 'Architecture/version_tag_fixer' => true,// 'Architecture/final_readonly_class_fixer' => true,Enable them explicitly if needed:
return ConfigurationFactory::createFromPreset( new Standard(), [ 'Architecture/abstract_name_fixer' => true, 'Architecture/exception_name_fixer' => true, ]);