kubawerlos / php-cs-fixer-custom-fixers
A set of custom fixers for PHP CS Fixer
Installs: 7 182 449
Dependents: 115
Suggesters: 0
Security: 0
Stars: 210
Watchers: 7
Forks: 19
Open Issues: 5
Requires
- php: ^7.4 || ^8.0
- ext-filter: *
- ext-tokenizer: *
- friendsofphp/php-cs-fixer: ^3.61.1
Requires (Dev)
- phpunit/phpunit: ^9.6.4 || ^10.5.29
- dev-main
- v3.22.0
- v3.21.0
- v3.20.0
- v3.19.2
- v3.19.1
- v3.19.0
- v3.18.0
- v3.17.1
- v3.17.0
- v3.16.2
- v3.16.1
- v3.16.0
- v3.15.0
- v3.14.0
- v3.13.0
- v3.12.0
- v3.11.3
- v3.11.2
- v3.11.1
- v3.11.0
- v3.10.2
- v3.10.1
- v3.10.0
- v3.9.0
- v3.8.1
- v3.8.0
- v3.7.2
- v3.7.1
- v3.7.0
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.0
- v1.17.1
- v1.17.0
- v1.16.2
- v1.16.1
- v1.16.0
- v1.15.1
- v1.15.0
- v1.14.0
- v1.13.0
- v1.12.1
- v1.12.0
- v1.11.0
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
This package is auto-updated.
Last update: 2024-10-12 11:16:41 UTC
README
PHP CS Fixer: custom fixers
A set of custom fixers for PHP CS Fixer.
Installation
PHP CS Fixer: custom fixers can be installed by running:
composer require --dev kubawerlos/php-cs-fixer-custom-fixers
Usage
In your PHP CS Fixer configuration register fixers and use them:
<?php return (new PhpCsFixer\Config()) + ->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers()) ->setRules([ '@PSR2' => true, 'array_syntax' => ['syntax' => 'short'], + PhpCsFixerCustomFixers\Fixer\NoLeadingSlashInGlobalNamespaceFixer::name() => true, + PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true, ]);
⚠️ When PHP CS Fixer is installed via php-cs-fixer/shim
package,
requiring bootstrap may be needed to load PhpCsFixerCustomFixers
classes:
require __DIR__ . '/vendor/kubawerlos/php-cs-fixer-custom-fixers/bootstrap.php';
Fixers
CommentSurroundedBySpacesFixer
Comments must be surrounded by spaces.
<?php -/*foo*/ +/* foo */
CommentedOutFunctionFixer
The configured functions must be commented out. Risky: when any of the configured functions have side effects or are overwritten. Configuration options:
functions
(array
): list of functions to comment out; defaults to['print_r', 'var_dump', 'var_export']
<?php -var_dump($x); +//var_dump($x);
ConstructorEmptyBracesFixer
Constructor's empty braces must be on a single line.
<?php class Foo { public function __construct( $param1, $param2 - ) { - } + ) {} }
DataProviderNameFixer
Data provider names must match the name of the test.
DEPRECATED: use php_unit_data_provider_name
instead.
Risky: when relying on name of data provider function.
Configuration options:
prefix
(string
): prefix that replaces "test"; defaults to'provide'
suffix
(string
): suffix to be added at the end"; defaults to'Cases'
<?php class FooTest extends TestCase { /** - * @dataProvider dataProvider + * @dataProvider provideSomethingCases */ public function testSomething($expected, $actual) {} - public function dataProvider() {} + public function provideSomethingCases() {} }
DataProviderReturnTypeFixer
The return type of PHPUnit data provider must be iterable
.
DEPRECATED: use php_unit_data_provider_return_type
instead.
Risky: when relying on signature of data provider.
<?php class FooTest extends TestCase { /** * @dataProvider provideSomethingCases */ public function testSomething($expected, $actual) {} - public function provideSomethingCases(): array {} + public function provideSomethingCases(): iterable {} }
DataProviderStaticFixer
Data providers must be static.
DEPRECATED: use php_unit_data_provider_static
instead.
Risky: when force
is set to true
.
Configuration options:
force
(bool
): whether to make static data providers having dynamic class calls; defaults tofalse
<?php class FooTest extends TestCase { /** * @dataProvider provideSomethingCases */ public function testSomething($expected, $actual) {} - public function provideSomethingCases() {} + public static function provideSomethingCases() {} }
DeclareAfterOpeningTagFixer
Declare statement for strict types must be placed on the same line, after the opening tag.
-<?php +<?php declare(strict_types=1); $foo; -declare(strict_types=1); $bar;
EmptyFunctionBodyFixer
Empty function body must be abbreviated as {}
and placed on the same line as the previous symbol, separated with a space.
<?php function foo( int $x -) -{ -} +) {}
InternalClassCasingFixer
Classes defined internally by an extension or the core must be referenced with the correct case.
DEPRECATED: use class_reference_name_casing
instead.
<?php -$foo = new STDClass(); +$foo = new stdClass();
IssetToArrayKeyExistsFixer
Function array_key_exists
must be used instead of isset
when possible.
Risky: when array is not defined, is multi-dimensional or behaviour is relying on the null value.
<?php -if (isset($array[$key])) { +if (array_key_exists($key, $array)) { echo $array[$key]; }
MultilineCommentOpeningClosingAloneFixer
Multiline comments or PHPDocs must contain an opening and closing line with no additional content.
<?php -/** Hello +/** + * Hello * World! */
MultilinePromotedPropertiesFixer
Promoted properties must be on separate lines. Configuration options:
keep_blank_lines
(bool
): whether to keep blank lines between properties; defaults tofalse
minimum_number_of_parameters
(int
): minimum number of parameters in the constructor to fix; defaults to1
<?php class Foo { - public function __construct(private array $a, private bool $b, private int $i) {} + public function __construct( + private array $a, + private bool $b, + private int $i + ) {} }
NoCommentedOutCodeFixer
There must be no commented out code.
<?php
-//var_dump($_POST);
print_r($_POST);
NoDoctrineMigrationsGeneratedCommentFixer
There must be no comments generated by Doctrine Migrations.
<?php namespace Migrations; use Doctrine\DBAL\Schema\Schema; -/** - * Auto-generated Migration: Please modify to your needs! - */ final class Version20180609123456 extends AbstractMigration { public function up(Schema $schema) { - // this up() migration is auto-generated, please modify it to your needs $this->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 totrue
<?php
$x = [
- "foo" => 1,
"bar" => 2,
"foo" => 3,
];
NoDuplicatedImportsFixer
There must be no duplicate use
statements.
<?php
namespace FooBar;
use Foo;
-use Foo;
use Bar;
NoImportFromGlobalNamespaceFixer
There must be no imports from the global namespace.
<?php namespace Foo; -use DateTime; class Bar { - public function __construct(DateTime $dateTime) {} + public function __construct(\DateTime $dateTime) {} }
NoLeadingSlashInGlobalNamespaceFixer
Classes in the global namespace cannot contain leading slashes.
<?php -$x = new \Foo(); +$x = new Foo(); namespace Bar; $y = new \Baz();
NoNullableBooleanTypeFixer
There must be no nullable boolean types. Risky: when the null is used.
<?php -function foo(?bool $bar) : ?bool +function foo(bool $bar) : bool { return $bar; }
NoPhpStormGeneratedCommentFixer
There must be no comments generated by PhpStorm.
<?php -/** - * Created by PhpStorm. - * User: root - * Date: 01.01.70 - * Time: 12:00 - */ namespace Foo;
NoReferenceInFunctionDefinitionFixer
There must be no parameters passed by reference in functions. Risky: when rely on reference.
<?php -function foo(&$x) {} +function foo($x) {}
NoSuperfluousConcatenationFixer
There must be no superfluous concatenation of literal strings. Configuration options:
allow_preventing_trailing_spaces
(bool
): whether to keep concatenation if it prevents having trailing spaces in string; defaults tofalse
keep_concatenation_for_different_quotes
(bool
): whether to keep concatenation if single-quoted and double-quoted would be concatenated; defaults tofalse
<?php -echo 'foo' . 'bar'; +echo 'foobar';
NoTrailingCommaInSinglelineFixer
An element list written on one line cannot contain a trailing comma.
<?php -$x = ['foo', 'bar', ]; +$x = ['foo', 'bar'];
NoUselessCommentFixer
There must be no useless comments.
<?php /** - * Class Foo * Class to do something */ class Foo { /** - * Get bar */ function getBar() {} }
NoUselessDirnameCallFixer
There must be no useless dirname
calls.
<?php -require dirname(__DIR__) . "/vendor/autoload.php"; +require __DIR__ . "/../vendor/autoload.php";
NoUselessDoctrineRepositoryCommentFixer
There can be no comments generated by Doctrine ORM.
<?php -/** - * FooRepository - * - * This class was generated by the Doctrine ORM. Add your own custom - * repository methods below. - */ class FooRepository extends EntityRepository {}
NoUselessParenthesisFixer
There must be no useless parentheses.
<?php -foo(($bar)); +foo($bar);
NoUselessStrlenFixer
Functions strlen
and mb_strlen
must not be compared to 0.
Risky: when the function strlen
is overridden.
<?php -$isEmpty = strlen($string) === 0; -$isNotEmpty = strlen($string) > 0; +$isEmpty = $string === ''; +$isNotEmpty = $string !== '';
NumericLiteralSeparatorFixer
Numeric literals must have configured separators.
DEPRECATED: use numeric_literal_separator
instead.
Configuration options:
binary
(bool
,null
): whether add, remove or ignore separators in binary numbers.; defaults tofalse
decimal
(bool
,null
): whether add, remove or ignore separators in decimal numbers.; defaults tofalse
float
(bool
,null
): whether add, remove or ignore separators in float numbers.; defaults tofalse
hexadecimal
(bool
,null
): whether add, remove or ignore separators in hexadecimal numbers.; defaults tofalse
octal
(bool
,null
): whether add, remove or ignore separators in octal numbers.; defaults tofalse
<?php -echo 0b01010100_01101000; // binary -echo 135_798_642; // decimal -echo 1_234.456_78e-4_321; // float -echo 0xAE_B0_42_FC; // hexadecimal -echo 0123_4567; // octal +echo 0b0101010001101000; // binary +echo 135798642; // decimal +echo 1234.45678e-4321; // float +echo 0xAEB042FC; // hexadecimal +echo 01234567; // octal
PhpUnitAssertArgumentsOrderFixer
PHPUnit assertions must have expected argument before actual one. Risky: when original PHPUnit methods are overwritten.
<?php class FooTest extends TestCase { public function testFoo() { - self::assertSame($value, 10); + self::assertSame(10, $value); } }
PhpUnitDedicatedAssertFixer
PHPUnit assertions like assertCount
and assertInstanceOf
must be used over assertEquals
/assertSame
.
Risky: when original PHPUnit methods are overwritten.
<?php class FooTest extends TestCase { public function testFoo() { - self::assertSame($size, count($elements)); - self::assertSame($className, get_class($object)); + self::assertCount($size, $elements); + self::assertInstanceOf($className, $object); } }
PhpUnitNoUselessReturnFixer
PHPUnit fail
, markTestIncomplete
and markTestSkipped
functions must not be directly followed by return
.
Risky: when original PHPUnit methods are overwritten.
<?php
class FooTest extends TestCase {
public function testFoo() {
$this->markTestSkipped();
- return;
}
}
PhpdocArrayStyleFixer
Generic array style should be used in PHPDoc.
DEPRECATED: use phpdoc_array_type
instead.
<?php /** - * @return int[] + * @return array<int> */ function foo() { return [1, 2]; }
PhpdocNoIncorrectVarAnnotationFixer
The @var
annotations must be used correctly in code.
<?php
-/** @var Foo $foo */
$bar = new Foo();
PhpdocNoSuperfluousParamFixer
There must be no superfluous parameters in PHPDoc.
<?php /** * @param bool $b - * @param int $i * @param string $s this is string - * @param string $s duplicated */ function foo($b, $s) {}
PhpdocOnlyAllowedAnnotationsFixer
Only the listed annotations are allowed in PHPDoc. Configuration options:
elements
(array
): list of annotations to keep in PHPDoc; defaults to[]
<?php /** * @author John Doe - * @package foo - * @subpackage bar * @version 1.0 */ function foo_bar() {}
PhpdocParamOrderFixer
Orders all @param
annotations in DocBlocks according to method signature.
DEPRECATED: use phpdoc_param_order
instead.
<?php /** + * @param int $a * @param int $b - * @param int $a * @param int $c */ function foo($a, $b, $c) {}
PhpdocParamTypeFixer
The @param
annotations must have a type.
<?php /** * @param string $foo - * @param $bar + * @param mixed $bar */ function a($foo, $bar) {}
PhpdocSelfAccessorFixer
In PHPDoc, the class or interface element self
must be used instead of the class name itself.
<?php class Foo { /** - * @var Foo + * @var self */ private $instance; }
PhpdocSingleLineVarFixer
The @var
annotation must be on a single line if it is the only content.
<?php class Foo { - /** - * @var string - */ + /** @var string */ private $name; }
PhpdocTypeListFixer
PHPDoc type list
must be used instead of array
without a key type.
DEPRECATED: use phpdoc_list_type
instead.
<?php /** - * @param array<string> + * @param list<string> */ function foo($x) {}
PhpdocTypesCommaSpacesFixer
PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace.
-<?php /** @var array<int,string> */ +<?php /** @var array<int, string> */
PhpdocTypesTrimFixer
PHPDoc types must be trimmed.
<?php /** - * @param null | string $x + * @param null|string $x */ function foo($x) {}
PhpdocVarAnnotationToAssertFixer
Converts @var
annotations to assert
calls when used in assignments.
<?php -/** @var string $x */ $x = getValue(); +assert(is_string($x));
PromotedConstructorPropertyFixer
Constructor properties must be promoted if possible. Configuration options:
promote_only_existing_properties
(bool
): whether to promote only properties that are defined in class; defaults tofalse
<?php class Foo { - private string $bar; - public function __construct(string $bar) { - $this->bar = $bar; + public function __construct(private string $bar) { } }
ReadonlyPromotedPropertiesFixer
Promoted properties must be declared as read-only. Risky: when property is written.
<?php class Foo { public function __construct( - public array $a, - public bool $b, + public readonly array $a, + public readonly bool $b, ) {} }
SingleSpaceAfterStatementFixer
Statements not followed by a semicolon must be followed by a single space. Configuration options:
allow_linebreak
(bool
): whether to allow statement followed by linebreak; defaults tofalse
<?php -$foo = new Foo(); -echo$foo->bar(); +$foo = new Foo(); +echo $foo->bar();
SingleSpaceBeforeStatementFixer
Statements not preceded by a line break must be preceded by a single space.
<?php -$foo =new Foo(); +$foo = new Foo();
StringableInterfaceFixer
A class that implements the __toString()
method must explicitly implement the Stringable
interface.
<?php -class Foo +class Foo implements \Stringable { public function __toString() { return "Foo"; } }
Contributing
Request a feature or report a bug by creating an issue.
Alternatively, fork the repository, commit your changes, and make sure everything is fine:
composer verify
and submit a pull request.