Skip to content

Custom Fixers

Enforces that abstract classes follow the Abstract* naming pattern.

// ❌ Before
abstract class BaseRepository {}
abstract class RepositoryBase {}
// ✅ After
abstract class AbstractRepository {}

Rule Key: Architecture/abstract_name_fixer

Enforces that interfaces follow the *Interface naming pattern.

// ❌ Before
interface Repository {}
interface IRepository {}
// ✅ After
interface RepositoryInterface {}

Rule Key: Architecture/interface_name_fixer

Enforces that traits follow standard trait naming conventions.

// ❌ Before
trait TimestampsTrait {}
// ✅ After
trait Timestamps {}

Rule Key: Architecture/trait_name_fixer

Enforces that exceptions follow the *Exception naming pattern.

// ❌ Before
class InvalidInput {}
class ValidationError {}
// ✅ After
class InvalidInputException {}
class ValidationException {}

Rule Key: Architecture/exception_name_fixer

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

Automatically imports fully qualified class names in new expressions.

// ❌ Before
$user = new \App\Models\User();
// ✅ After
use App\Models\User;
$user = new User();

Rule Key: Architecture/import_fqcn_in_new_fixer

Automatically imports fully qualified class names in attributes.

// ❌ Before
#[\App\Attributes\Cached(ttl: 3600)]
class UserRepository {}
// ✅ After
use App\Attributes\Cached;
#[Cached(ttl: 3600)]
class UserRepository {}

Rule Key: Architecture/import_fqcn_in_attribute_fixer

Automatically imports fully qualified class names in static method calls.

// ❌ Before
$value = \App\Services\Cache::get('key');
// ✅ After
use App\Services\Cache;
$value = Cache::get('key');

Rule Key: Architecture/import_fqcn_in_static_call_fixer

Automatically imports fully qualified class names in property type declarations.

// ❌ Before
class UserController
{
private \App\Services\UserService $userService;
}
// ✅ After
use App\Services\UserService;
class UserController
{
private UserService $userService;
}

Rule Key: Architecture/import_fqcn_in_property_fixer

Automatically adds final modifier to readonly classes.

// ❌ Before
readonly class User {}
// ✅ After
final readonly class User {}

Rule Key: Architecture/final_readonly_class_fixer

Removes redundant readonly modifiers from properties in readonly classes.

// ❌ Before
readonly class User
{
public readonly string $name;
}
// ✅ After
readonly class User
{
public string $name;
}

Rule Key: Architecture/redundant_readonly_property_fixer

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

Adds @psalm-immutable annotation to readonly classes.

// ❌ Before
readonly class User {}
// ✅ After
/**
* @psalm-immutable
*/
readonly class User {}

Rule Key: Architecture/psalm_immutable_on_readonly_class_fixer

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

Enforces consistent @version tag format in PHPDoc blocks.

// ❌ Before
/**
* @version 1.0
*/
// ✅ After
/**
* @version 1.0.0
*/

Rule Key: Architecture/version_tag_fixer

Enforces consistent namespace declarations.

Rule Key: Architecture/namespace_fixer

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

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,
]
);