An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# PHP CS Fixer: custom fixers

[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3691-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/main.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=main)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fkubawerlos%2Fphp-cs-fixer-custom-fixers%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/kubawerlos/php-cs-fixer-custom-fixers/main)
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](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.