https://github.com/kubawerlos/php-cs-fixer-custom-fixers
A set of custom fixers for PHP CS Fixer
https://github.com/kubawerlos/php-cs-fixer-custom-fixers
code-standards code-style php php-cs-fixer static-analysis
Last synced: about 1 year ago
JSON representation
A set of custom fixers for PHP CS Fixer
- Host: GitHub
- URL: https://github.com/kubawerlos/php-cs-fixer-custom-fixers
- Owner: kubawerlos
- License: mit
- Created: 2018-05-21T20:06:07.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2025-05-09T10:44:46.000Z (about 1 year ago)
- Last Synced: 2025-05-14T10:59:09.390Z (about 1 year ago)
- Topics: code-standards, code-style, php, php-cs-fixer, static-analysis
- Language: PHP
- Homepage:
- Size: 2.45 MB
- Stars: 226
- Watchers: 6
- Forks: 20
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP CS Fixer: custom fixers
[](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[](https://php.net)
[](LICENSE)

[](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
[](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=main)
[](https://dashboard.stryker-mutator.io/reports/github.com/kubawerlos/php-cs-fixer-custom-fixers/main)
[](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
A set of custom fixers for [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).
## Installation
PHP CS Fixer: custom fixers can be installed by running:
```bash
composer require --dev kubawerlos/php-cs-fixer-custom-fixers
```
## Usage
In your PHP CS Fixer configuration register fixers and use them:
```diff
registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
+ PhpCsFixerCustomFixers\Fixer\NoLeadingSlashInGlobalNamespaceFixer::name() => true,
+ PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true,
]);
```
:warning: When PHP CS Fixer is installed via [`php-cs-fixer/shim`](https://github.com/PHP-CS-Fixer/shim) package,
requiring bootstrap may be needed to load `PhpCsFixerCustomFixers` classes:
```php
require __DIR__ . '/vendor/kubawerlos/php-cs-fixer-custom-fixers/bootstrap.php';
```
## Fixers
#### ClassConstantUsageFixer
Class constant must be used instead of a copy of string.
```diff
$value) {
- $product *= $elements[$key];
+ $product *= $value;
}
```
#### InternalClassCasingFixer
Classes defined internally by an extension or the core must be referenced with the correct case.
DEPRECATED: use `class_reference_name_casing` instead.
```diff
addSql("UPDATE t1 SET col1 = col1 + 1");
}
public function down(Schema $schema)
{
- // this down() migration is auto-generated, please modify it to your needs
$this->addSql("UPDATE t1 SET col1 = col1 - 1");
}
}
```
#### NoDuplicatedArrayKeyFixer
There must be no duplicate array keys.
Configuration options:
- `ignore_expressions` (`bool`): whether to keep duplicated expressions (as they might return different values) or not; defaults to `true`
```diff
1,
"bar" => 2,
"foo" => 3,
];
```
#### NoDuplicatedImportsFixer
There must be no duplicate `use` statements.
```diff
0;
+$isEmpty = $string === '';
+$isNotEmpty = $string !== '';
```
#### NoUselessWriteVisibilityFixer
There must be no useless write visibility.
```diff
markTestSkipped();
- return;
}
}
```
#### PhpUnitRequiresConstraintFixer
Assertions and attributes for PHP and PHPUnit versions must have explicit version constraint and space after comparison operator.
```diff
= 8.1
*/
public function testFoo() {}
/**
- * @requires PHP <8.3
+ * @requires PHP < 8.3
*/
public function testBar() {}
- #[\PHPUnit\Framework\Attributes\RequiresPhpunit('12.0')]
+ #[\PHPUnit\Framework\Attributes\RequiresPhpunit('>= 12.0')]
public function testBaz() {}
}
```
#### PhpdocArrayStyleFixer
Generic array style should be used in PHPDoc.
DEPRECATED: use `phpdoc_array_type` instead.
```diff
*/
function foo() { return [1, 2]; }
```
#### PhpdocNoIncorrectVarAnnotationFixer
The `@var` annotations must be used correctly in code.
```diff
+ * @param list
*/
function foo($x) {}
```
#### PhpdocTypesCommaSpacesFixer
PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace.
```diff
- */
+ */
```
#### PhpdocTypesTrimFixer
PHPDoc types must be trimmed.
```diff
bar = $bar;
+ public function __construct(private string $bar) {
}
}
```
#### ReadonlyPromotedPropertiesFixer
Promoted properties must be declared as read-only.
*Risky: when property is written.*
```diff
bar();
+$foo = new Foo();
+echo $foo->bar();
```
#### SingleSpaceBeforeStatementFixer
Statements not preceded by a line break must be preceded by a single space.
```diff
'v1',
- 'option 2 or 3' => 'v23',
+ 'option 1' => 'v1',
+ 'option 2 or 3' => 'v23',
];
```
## Contributing
Request a feature or report a bug by creating an [issue](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/issues).
Alternatively, fork the repository, commit your changes, and make sure everything is fine:
```bash
composer verify
```
and submit a pull request.