repo
stringlengths
7
63
file_url
stringlengths
81
284
file_path
stringlengths
5
200
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:02:33
2026-01-05 05:24:06
truncated
bool
2 classes
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NotOperatorWithSpaceFixerTest.php
tests/Fixer/Operator/NotOperatorWithSpaceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NotOperatorWithSpaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NotOperatorWithSpaceFixer> * * @author Javier Spagnoletti <phansys@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NotOperatorWithSpaceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php $i = 0; $i++; ++$i; $foo = ! false || ( ! true);', '<?php $i = 0; $i++; ++$i; $foo = !false || (!true);', ]; yield [ '<?php $i = 0; $i--; --$i; $foo = ! false || ($i && ! true);', '<?php $i = 0; $i--; --$i; $foo = !false || ($i && !true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! /* some comment */true);', '<?php $i = 0; $i--; $foo = !false || ($i && !/* some comment */true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! true);', '<?php $i = 0; $i--; $foo = !false || ($i && ! true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! true);', '<?php $i = 0; $i--; $foo = !false || ($i && ! true);', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/UnaryOperatorSpacesFixerTest.php
tests/Fixer/Operator/UnaryOperatorSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer> * * @author Gregor Harlan <gharlan@web.de> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class UnaryOperatorSpacesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php $a= 1;$a# ++# ;#', ]; yield [ '<?php $a++;', '<?php $a ++;', ]; yield [ '<?php $a--;', '<?php $a --;', ]; yield [ '<?php ++$a;', '<?php ++ $a;', ]; yield [ '<?php --$a;', '<?php -- $a;', ]; yield [ '<?php $a = !$b;', '<?php $a = ! $b;', ]; yield [ '<?php $a = !!$b;', '<?php $a = ! ! $b;', ]; yield [ '<?php $a = ~$b;', '<?php $a = ~ $b;', ]; yield [ '<?php $a = &$b;', '<?php $a = & $b;', ]; yield [ '<?php $a=&$b;', ]; yield [ '<?php $a * -$b;', '<?php $a * - $b;', ]; yield [ '<?php $a *-$b;', '<?php $a *- $b;', ]; yield [ '<?php $a*-$b;', ]; yield [ '<?php function &foo(){}', '<?php function & foo(){}', ]; yield [ '<?php function &foo(){}', '<?php function & foo(){}', ]; yield [ '<?php function foo(&$a, array &$b, Bar &$c) {}', '<?php function foo(& $a, array & $b, Bar & $c) {}', ]; yield [ '<?php function foo($a, ...$b) {}', '<?php function foo($a, ... $b) {}', ]; yield [ '<?php function foo(&...$a) {}', '<?php function foo(& ... $a) {}', ]; yield [ '<?php function foo(array ...$a) {}', ]; yield [ '<?php foo(...$a);', '<?php foo(... $a);', ]; yield [ '<?php foo($a, ...$b);', '<?php foo($a, ... $b);', ]; yield [ '<?php function foo($a, ...$b) { return (--$a) * ($b++);}', '<?php function foo($a, ... $b) { return (-- $a) * ($b ++);}', ['only_dec_inc' => false], ]; yield [ '<?php function foo($a, ... $b) { return (--$a) * ($b++);}', '<?php function foo($a, ... $b) { return (-- $a) * ($b ++);}', ['only_dec_inc' => true], ]; yield [ '<?php static fn(Token $t): bool => 8 === ($t->flags & 8);', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/OperatorLinebreakFixerTest.php
tests/Fixer/Operator/OperatorLinebreakFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @covers \PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer * * @internal * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer> * * @author Kuba Werłos <werlos@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OperatorLinebreakFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { foreach (self::pairs() as $key => $value) { yield \sprintf('%s when position is "beginning"', $key) => $value; yield \sprintf('%s when position is "end"', $key) => [ $value[1], $value[0], ['position' => 'end'], ]; } yield 'ignore add operator when only booleans enabled' => [ '<?php return $foo + $bar; ', null, ['only_booleans' => true], ]; yield 'handle operator when on separate line when position is "beginning"' => [ '<?php return $foo || $bar; ', '<?php return $foo || $bar; ', ]; yield 'handle operator when on separate line when position is "end"' => [ '<?php return $foo || $bar; ', '<?php return $foo || $bar; ', ['position' => 'end'], ]; yield 'handle Elvis operator with space inside' => [ '<?php return $foo ?: $bar; ', '<?php return $foo ? : $bar; ', ]; yield 'handle Elvis operator with space inside when position is "end"' => [ '<?php return $foo ?: $bar; ', '<?php return $foo ? : $bar; ', ['position' => 'end'], ]; yield 'handle Elvis operator with comment inside' => [ '<?php return $foo/* Lorem ipsum */ ?: $bar; ', '<?php return $foo ?/* Lorem ipsum */: $bar; ', ]; yield 'handle Elvis operators with comment inside when position is "end"' => [ '<?php return $foo ?: /* Lorem ipsum */$bar; ', '<?php return $foo ?/* Lorem ipsum */: $bar; ', ['position' => 'end'], ]; yield 'assign by reference' => [ '<?php $a = $b; $c =& $d; ', '<?php $a = $b; $c =& $d; ', ]; yield 'passing by reference' => [ '<?php function foo( &$a, &$b, int &$c, \Bar\Baz &$d ) {};', null, ['position' => 'end'], ]; yield 'multiple switches' => [ '<?php switch ($foo) { case 1: break; case 2: break; } switch($bar) { case 1: break; case 2: break; }', ]; yield 'return type' => [ '<?php function foo() : bool {};', ]; yield 'go to' => ['<?php prepare_value: $objectsPool[$value] = [$id = \count($objectsPool)]; ']; yield 'alternative syntax' => [ '<?php if (true): echo 1; else: echo 2; endif; while (true): echo "na"; endwhile; ', null, ['position' => 'beginning'], ]; yield 'nullable type when position is "end"' => [ '<?php function foo( ?int $x, ?int $y, ?int $z ) {};', null, ['position' => 'end'], ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield 'handle ?-> operator' => [ '<?php $foo ?-> $bar; ', '<?php $foo ?-> $bar; ', ]; } /** * @dataProvider provideFix85Cases * * @requires PHP 8.5 */ public function testFix85(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: string}> */ public static function provideFix85Cases(): iterable { yield 'pipe operator' => [ <<<'PHP' <?php $numberOfAdmins = getUsers() |> (fn ($list) => array_filter($list, isAdmin(...))) |> count(...); PHP, <<<'PHP' <?php $numberOfAdmins = getUsers() |> (fn ($list) => array_filter($list, isAdmin(...))) |> count(...); PHP, ]; } /** * @return iterable<array{0: string, 1: null|string, 2?: array<string, mixed>}> */ private static function pairs(): iterable { yield 'handle equal sign' => [ '<?php $foo = $bar; ', '<?php $foo = $bar; ', ]; yield 'handle add operator' => [ '<?php return $foo + $bar; ', '<?php return $foo + $bar; ', ['only_booleans' => false], ]; yield 'handle uppercase operator' => [ '<?php return $foo AND $bar; ', '<?php return $foo AND $bar; ', ]; yield 'handle concatenation operator' => [ '<?php return $foo .$bar; ', '<?php return $foo. $bar; ', ]; yield 'handle ternary operator' => [ '<?php return $foo ? $bar : $baz; ', '<?php return $foo ? $bar : $baz; ', ]; yield 'handle multiple operators' => [ '<?php return $foo || $bar || $baz; ', '<?php return $foo || $bar || $baz; ', ]; yield 'handle multiple operators with nested' => [ '<?php return $foo || $bar || ($bar2 || $bar3) || $baz; ', '<?php return $foo || $bar || ($bar2 || $bar3) || $baz; ', ]; yield 'handle operator when no whitespace is before' => [ '<?php function foo() { return $a ||$b; } ', '<?php function foo() { return $a|| $b; } ', ]; yield 'handle operator with one-line comments' => [ '<?php function getNewCuyamaTotal() { return 562 // Population + 2150 // Ft. above sea level + 1951; // Established } ', '<?php function getNewCuyamaTotal() { return 562 + // Population 2150 + // Ft. above sea level 1951; // Established } ', ]; yield 'handle operator with PHPDoc comments' => [ '<?php function getNewCuyamaTotal() { return 562 /** Population */ + 2150 /** Ft. above sea level */ + 1951; /** Established */ } ', '<?php function getNewCuyamaTotal() { return 562 + /** Population */ 2150 + /** Ft. above sea level */ 1951; /** Established */ } ', ]; yield 'handle operator with multiple comments next to each other' => [ '<?php function foo() { return isThisTheRealLife() // First comment // Second comment // Third comment || isThisJustFantasy(); } ', '<?php function foo() { return isThisTheRealLife() || // First comment // Second comment // Third comment isThisJustFantasy(); } ', ]; yield 'handle nested operators' => [ '<?php function foo() { return $a && ( $b || $c ) && $d; } ', '<?php function foo() { return $a && ( $b || $c ) && $d; } ', ]; yield 'handle Elvis operator' => [ '<?php return $foo ?: $bar; ', '<?php return $foo ?: $bar; ', ]; yield 'handle ternary operator inside of switch' => [ '<?php switch ($foo) { case 1: return $isOK ? 1 : -1; case ( $a ? 2 : 3 ) : return 23; case $b[ $a ? 4 : 5 ] : return 45; } ', '<?php switch ($foo) { case 1: return $isOK ? 1 : -1; case ( $a ? 2 : 3 ) : return 23; case $b[ $a ? 4 : 5 ] : return 45; } ', ]; yield 'handle ternary operator with switch inside' => [ '<?php $a ? array_map( function () { switch (true) { case 1: return true; } }, [1, 2, 3] ) : false; ', '<?php $a ? array_map( function () { switch (true) { case 1: return true; } }, [1, 2, 3] ) : false; ', ]; $operators = [ '+', '-', '*', '/', '%', '**', // Arithmetic '+=', '-=', '*=', '/=', '%=', '**=', // Arithmetic assignment '=', // Assignment '&', '|', '^', '<<', '>>', // Bitwise '&=', '|=', '^=', '<<=', '>>=', // Bitwise assignment '==', '===', '!=', '<>', '!==', '<', '>', '<=', '>=', // Comparison 'and', 'or', 'xor', '&&', '||', // Logical '.', '.=', // String '->', // Object '::', // Scope Resolution ]; $operators[] = '??'; $operators[] = '<=>'; foreach ($operators as $operator) { yield \sprintf('handle %s operator', $operator) => [ \sprintf('<?php $foo %s $bar; ', $operator), \sprintf('<?php $foo %s $bar; ', $operator), ]; } yield 'handle => operator' => [ '<?php [$foo => $bar]; ', '<?php [$foo => $bar]; ', ]; yield 'handle & operator with constant' => [ '<?php \Foo::bar & $baz; ', '<?php \Foo::bar & $baz; ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/AssignNullCoalescingToCoalesceEqualFixerTest.php
tests/Fixer/Operator/AssignNullCoalescingToCoalesceEqualFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AbstractShortOperatorFixer * @covers \PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class AssignNullCoalescingToCoalesceEqualFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple' => [ '<?php $a ??= 1;', '<?php $a = $a ?? 1;', ]; yield 'minimal' => [ '<?php $a ??= 1;', '<?php $a=$a??1;', ]; yield 'simple array' => [ '<?php $a[1] ??= 1;', '<?php $a[1] = $a[1] ?? 1;', ]; yield 'simple array [0]' => [ '<?php $a[1][0] ??= 1;', '<?php $a[1][0] = $a[1][0] ?? 1;', ]; yield 'simple array ([0])' => [ '<?php $a[1][0] ??= 1;', '<?php $a[1][0] = ($a[1][0]) ?? 1;', ]; yield 'simple array, comment' => [ '<?php $a[1] /* 1 */ ??= /* 2 */ /* 3 */ /* 4 */ /* 5 */ 1;', '<?php $a[1]/* 1 */ = /* 2 */ $a[1/* 3 */] /* 4 */ ??/* 5 */ 1;', ]; yield 'simple in call' => [ '<?php a(1, $a ??= 1);', '<?php a(1, $a = $a ?? 1);', ]; yield [ '<?php \A\B::$foo ??= 1;', '<?php \A\B::$foo = \A\B::$foo ?? 1;', ]; yield 'same' => [ '<?php $a ??= 1;', '<?php $a = ($a) ?? 1;', ]; yield 'object' => [ '<?php $a->b ??= 1;', '<?php $a->b = $a->b ?? 1;', ]; yield 'object II' => [ '<?php $a->b[0]->{1} ??= 1;', '<?php $a->b[0]->{1} = $a->b[0]->{1} ?? 1;', ]; yield 'simple, before ;' => [ '<?php ; $a ??= 1;', '<?php ; $a = $a ?? 1;', ]; yield 'simple, before {' => [ '<?php { $a ??= 1; }', '<?php { $a = $a ?? 1; }', ]; yield 'simple, before }' => [ '<?php if ($a){} $a ??= 1;', '<?php if ($a){} $a = $a ?? 1;', ]; yield 'in call' => [ '<?php foo($a ??= 1);', '<?php foo($a = $a ?? 1);', ]; yield 'in call followed by end tag and ternary' => [ '<?php foo( $a ??= 1 ) ?><?php $b = $b ? $c : $d ?>', '<?php foo( $a = $a ?? 1 ) ?><?php $b = $b ? $c : $d ?>', ]; yield 'simple, before ) I' => [ '<?php if ($a) $a ??= 1;', '<?php if ($a) $a = $a ?? 1;', ]; yield 'simple, before ) II' => [ '<?php if ($a) $a ??= 1; foreach ($d as $i) $a ??= 1; while (foo()) $a ??= 1; ', '<?php if ($a) $a = $a ?? 1; foreach ($d as $i) $a = $a ?? 1; while (foo()) $a = $a ?? 1; ', ]; yield 'simple, end' => [ '<?php $a ??= 1 ?>', '<?php $a = $a ?? 1 ?>', ]; yield 'simple, 10x' => [ '<?php'.str_repeat(' $a ??= 1;', 10), '<?php'.str_repeat(' $a = $a ?? 1;', 10), ]; yield 'simple, multi line' => [ '<?php $a ??= '.' '.' 1;', '<?php $a = $a ?? 1;', ]; yield 'dynamic var' => [ '<?php ${beers::$ale} ??= 1;', '<?php ${beers::$ale} = ${beers::$ale} ?? 1;', ]; yield [ '<?php $argv ??= $_SERVER[\'argv\'] ?? [];', '<?php $argv = $argv ?? $_SERVER[\'argv\'] ?? [];', ]; yield 'do not fix' => [ '<?php $a = 1 + $a ?? $b; $b + $a = $a ?? 1; $b = $a ?? 1; $b = $a ?? $b; $d = $a + $c ; $c ?? $c; $a = ($a ?? $b) && $c; // just to be sure $a = (string) $a ?? 1; $a = 1 ?? $a; ', ]; yield 'do not fix because of precedence 1' => [ '<?php $a = $a ?? $b ? $c : $d;', ]; yield 'do not fix because of precedence 2' => [ '<?php $a = $a ?? $b ? $c : $d ?>', ]; yield ['<?php $a[1][0] = $a ?? $a[1][0];']; yield 'switch case & default' => [ '<?php switch(foo()) { case 1: $a ??= 1; break; default: $b ??= 1; break; } ', '<?php switch(foo()) { case 1: $a = $a ?? 1; break; default: $b = $b ?? 1; break; } ', ]; yield 'operator precedence' => [ '<?php $x = $z ? $b : $a = $a ?? 123;', ]; yield 'alternative syntax' => [ '<?php foreach([1, 2, 3] as $i): $a ??= 1; endforeach;', '<?php foreach([1, 2, 3] as $i): $a = $a ?? 1; endforeach;', ]; yield 'assign and return' => [ '<?php class Foo { private $test; public function bar($i) { return $this->test ??= $i; } }', '<?php class Foo { private $test; public function bar($i) { return $this->test = $this->test ?? $i; } }', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: string}> */ public static function provideFixPre80Cases(): iterable { yield 'mixed array' => [ '<?php $a[1] ??= 1; $a{2} ??= 1; $a{2}[$f] ??= 1; ', '<?php $a[1] = $a[1] ?? 1; $a{2} = $a{2} ?? 1; $a{2}[$f] = $a{2}[$f] ?? 1; ', ]; yield 'same II' => [ '<?php $a[1] ??= 1;', '<?php $a[1] = $a{1} ?? 1;', ]; yield 'same III' => [ '<?php $a[1] ??= 1;', '<?php $a[1] = (($a{1})) ?? 1;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/LogicalOperatorsFixerTest.php
tests/Fixer/Operator/LogicalOperatorsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\LogicalOperatorsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\LogicalOperatorsFixer> * * @author Haralan Dobrev <hkdobrev@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LogicalOperatorsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php if ($a == "foo" && $b == "bar") {}', '<?php if ($a == "foo" and $b == "bar") {}', ]; yield [ '<?php if ($a == "foo" || $b == "bar") {}', '<?php if ($a == "foo" or $b == "bar") {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" and ($b == "bar" or $c == "baz")) {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" and ($b == "bar" || $c == "baz")) {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" && ($b == "bar" or $c == "baz")) {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" AND ($b == "bar" OR $c == "baz")) {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" and ($b == "bar" OR $c == "baz")) {}', ]; yield [ '<?php if ($a == "foo" && ($b == "bar" || $c == "baz")) {}', '<?php if ($a == "foo" AND ($b == "bar" or $c == "baz")) {}', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NewWithBracesFixerTest.php
tests/Fixer/Operator/NewWithBracesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NewWithBracesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NewWithBracesFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\NewWithBracesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NewWithBracesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixNamedWithDefaultConfigurationCases */ public function testFixNamedWithDefaultConfiguration(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixNamedWithDefaultConfigurationCases(): iterable { yield ['<?php $x = new X(foo(/**/));']; yield ['<?php $xyz = new X(new Y(new Z(/**/ foo())));']; yield ['<?php $self = new self(a);']; yield [ '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}', ]; yield [ '<?php $static = new self(new \SplFileInfo(__FILE__));', ]; yield [ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', ]; yield [ '<?php $x = new X();', '<?php $x = new X;', ]; yield [ '<?php $y = new Y() ;', '<?php $y = new Y ;', ]; yield [ '<?php $x = new Z() /**/;//', '<?php $x = new Z /**/;//', ]; yield [ '<?php $foo = new $foo();', '<?php $foo = new $foo;', ]; yield [ '<?php $bar1 = new $foo[0]->bar(); $bar2 = new $foo[0][1]->bar(); ', ]; yield [ '<?php $xyz = new X(new Y(new Z()));', '<?php $xyz = new X(new Y(new Z));', ]; yield [ '<?php $foo = (new $bar())->foo;', '<?php $foo = (new $bar)->foo;', ]; yield [ '<?php $foo = (new $bar((new Foo())->bar))->foo;', '<?php $foo = (new $bar((new Foo)->bar))->foo;', ]; yield [ '<?php $self = new self();', '<?php $self = new self;', ]; yield [ '<?php $static = new static();', '<?php $static = new static;', ]; yield [ '<?php $a = array( "key" => new DateTime(), );', '<?php $a = array( "key" => new DateTime, );', ]; yield [ '<?php $a = array( "key" => new DateTime() );', '<?php $a = array( "key" => new DateTime );', ]; yield [ '<?php $a = new $b[$c]();', '<?php $a = new $b[$c];', ]; yield [ '<?php $a = new $b[$c][0]();', '<?php $a = new $b[$c][0];', ]; yield [ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();', '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];', ]; yield [ '<?php $a = new $b[\'class\']();', '<?php $a = new $b[\'class\'];', ]; yield [ '<?php $a = new $b[\'class\'] ($foo[\'bar\']);', ]; yield [ '<?php $a = new $b[\'class\'] () ;', ]; yield [ '<?php $a = new $b[$c] ($hello[$world]) ;', ]; yield [ "<?php \$a = new \$b['class']()\r\n\t ;", "<?php \$a = new \$b['class']\r\n\t ;", ]; yield [ '<?php $a = $b ? new DateTime() : $b;', '<?php $a = $b ? new DateTime : $b;', ]; yield [ '<?php new self::$adapters[$name]["adapter"]();', '<?php new self::$adapters[$name]["adapter"];', ]; yield [ '<?php $a = new \Exception()?> <?php echo 1;', '<?php $a = new \Exception?> <?php echo 1;', ]; yield [ '<?php $b = new \StdClass() /**/?>', '<?php $b = new \StdClass /**/?>', ]; yield [ '<?php $a = new Foo() instanceof Foo;', '<?php $a = new Foo instanceof Foo;', ]; yield [ '<?php $a = new Foo() + 1; $a = new Foo() - 1; $a = new Foo() * 1; $a = new Foo() / 1; $a = new Foo() % 1; ', '<?php $a = new Foo + 1; $a = new Foo - 1; $a = new Foo * 1; $a = new Foo / 1; $a = new Foo % 1; ', ]; yield [ '<?php $a = new Foo() & 1; $a = new Foo() | 1; $a = new Foo() ^ 1; $a = new Foo() << 1; $a = new Foo() >> 1; ', '<?php $a = new Foo & 1; $a = new Foo | 1; $a = new Foo ^ 1; $a = new Foo << 1; $a = new Foo >> 1; ', ]; yield [ '<?php $a = new Foo() and 1; $a = new Foo() or 1; $a = new Foo() xor 1; $a = new Foo() && 1; $a = new Foo() || 1; ', '<?php $a = new Foo and 1; $a = new Foo or 1; $a = new Foo xor 1; $a = new Foo && 1; $a = new Foo || 1; ', ]; yield [ '<?php if (new DateTime() > $this->startDate) {} if (new DateTime() >= $this->startDate) {} if (new DateTime() < $this->startDate) {} if (new DateTime() <= $this->startDate) {} if (new DateTime() == $this->startDate) {} if (new DateTime() != $this->startDate) {} if (new DateTime() <> $this->startDate) {} if (new DateTime() === $this->startDate) {} if (new DateTime() !== $this->startDate) {} ', '<?php if (new DateTime > $this->startDate) {} if (new DateTime >= $this->startDate) {} if (new DateTime < $this->startDate) {} if (new DateTime <= $this->startDate) {} if (new DateTime == $this->startDate) {} if (new DateTime != $this->startDate) {} if (new DateTime <> $this->startDate) {} if (new DateTime === $this->startDate) {} if (new DateTime !== $this->startDate) {} ', ]; yield [ '<?php $a = new \stdClass() ? $b : $c;', '<?php $a = new \stdClass ? $b : $c;', ]; yield [ '<?php foreach (new Collection() as $x) {}', '<?php foreach (new Collection as $x) {}', ]; yield [ '<?php $a = [(string) new Foo() => 1];', '<?php $a = [(string) new Foo => 1];', ]; yield [ '<?php $a = [ "key" => new DateTime(), ];', '<?php $a = [ "key" => new DateTime, ];', ]; yield [ '<?php $a = [ "key" => new DateTime() ];', '<?php $a = [ "key" => new DateTime ];', ]; yield [ '<?php $a = new Foo() ** 1; ', '<?php $a = new Foo ** 1; ', ]; yield [ '<?php $a = new Foo() <=> 1; ', '<?php $a = new Foo <=> 1; ', ]; yield [ "<?php \$a = new \$b['class']/* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class'] /* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class']()/* */;", "<?php \$a = new \$b['class']/* */;", ]; yield [ "<?php \$a = new \$b['class']() /* */;", "<?php \$a = new \$b['class'] /* */;", ]; } /** * @dataProvider provideFixNamedWithoutBracesCases */ public function testFixNamedWithoutBraces(string $expected, ?string $input = null): void { $this->fixer->configure(['named_class' => false]); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixNamedWithoutBracesCases(): iterable { yield ['<?php $x = new X(foo(/**/));']; yield ['<?php $xyz = new X(new Y(new Z(/**/ foo())));']; yield ['<?php $self = new self(a);']; yield [ '<?php $bar1 = new $foo->bar["baz"];', '<?php $bar1 = new $foo->bar["baz"]();', ]; yield [ '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}', ]; yield [ '<?php $static = new self(new \SplFileInfo(__FILE__));', ]; yield [ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ /**/ /**/ /**//**//**/ /**//**//**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', ]; yield [ '<?php $x = new X;', '<?php $x = new X();', ]; yield [ '<?php $y = new Y ;', '<?php $y = new Y() ;', ]; yield [ '<?php $x = new Z /**/;//', '<?php $x = new Z() /**/;//', ]; yield [ '<?php $foo = new $foo;', '<?php $foo = new $foo();', ]; yield [ '<?php $xyz = new X(new Y(new Z));', '<?php $xyz = new X(new Y(new Z()));', ]; yield [ '<?php $foo = (new $bar)->foo;', '<?php $foo = (new $bar())->foo;', ]; yield [ '<?php $foo = (new $bar((new Foo)->bar))->foo;', '<?php $foo = (new $bar((new Foo())->bar))->foo;', ]; yield [ '<?php $self = new self;', '<?php $self = new self();', ]; yield [ '<?php $static = new static;', '<?php $static = new static();', ]; yield [ '<?php $a = array( "key" => new DateTime, );', '<?php $a = array( "key" => new DateTime(), );', ]; yield [ '<?php $a = array( "key" => new DateTime );', '<?php $a = array( "key" => new DateTime() );', ]; yield [ '<?php $a = new $b[$c];', '<?php $a = new $b[$c]();', ]; yield [ '<?php $a = new $b[$c][0];', '<?php $a = new $b[$c][0]();', ]; yield [ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];', '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();', ]; yield [ '<?php $a = new $b[\'class\'];', '<?php $a = new $b[\'class\']();', ]; yield [ '<?php $a = new $b[\'class\'] ($foo[\'bar\']);', ]; yield [ '<?php $a = new $b[\'class\'] ;', '<?php $a = new $b[\'class\'] () ;', ]; yield [ '<?php $a = new $b[$c] ($hello[$world]) ;', ]; yield [ "<?php \$a = new \$b['class']\r\n\t ;", "<?php \$a = new \$b['class']()\r\n\t ;", ]; yield [ '<?php $a = $b ? new DateTime : $b;', '<?php $a = $b ? new DateTime() : $b;', ]; yield [ '<?php new self::$adapters[$name]["adapter"];', '<?php new self::$adapters[$name]["adapter"]();', ]; yield [ '<?php $a = new \Exception?> <?php echo 1;', '<?php $a = new \Exception()?> <?php echo 1;', ]; yield [ '<?php $b = new \StdClass /**/?>', '<?php $b = new \StdClass() /**/?>', ]; yield [ '<?php $a = new Foo instanceof Foo;', '<?php $a = new Foo() instanceof Foo;', ]; yield [ '<?php $a = new Foo + 1; $a = new Foo - 1; $a = new Foo * 1; $a = new Foo / 1; $a = new Foo % 1; ', '<?php $a = new Foo() + 1; $a = new Foo() - 1; $a = new Foo() * 1; $a = new Foo() / 1; $a = new Foo() % 1; ', ]; yield [ '<?php $a = new Foo & 1; $a = new Foo | 1; $a = new Foo ^ 1; $a = new Foo << 1; $a = new Foo >> 1; ', '<?php $a = new Foo() & 1; $a = new Foo() | 1; $a = new Foo() ^ 1; $a = new Foo() << 1; $a = new Foo() >> 1; ', ]; yield [ '<?php $a = new Foo and 1; $a = new Foo or 1; $a = new Foo xor 1; $a = new Foo && 1; $a = new Foo || 1; ', '<?php $a = new Foo() and 1; $a = new Foo() or 1; $a = new Foo() xor 1; $a = new Foo() && 1; $a = new Foo() || 1; ', ]; yield [ '<?php if (new DateTime > $this->startDate) {} if (new DateTime >= $this->startDate) {} if (new DateTime < $this->startDate) {} if (new DateTime <= $this->startDate) {} if (new DateTime == $this->startDate) {} if (new DateTime != $this->startDate) {} if (new DateTime <> $this->startDate) {} if (new DateTime === $this->startDate) {} if (new DateTime !== $this->startDate) {} ', '<?php if (new DateTime() > $this->startDate) {} if (new DateTime() >= $this->startDate) {} if (new DateTime() < $this->startDate) {} if (new DateTime() <= $this->startDate) {} if (new DateTime() == $this->startDate) {} if (new DateTime() != $this->startDate) {} if (new DateTime() <> $this->startDate) {} if (new DateTime() === $this->startDate) {} if (new DateTime() !== $this->startDate) {} ', ]; yield [ '<?php $a = new \stdClass ? $b : $c;', '<?php $a = new \stdClass() ? $b : $c;', ]; yield [ '<?php foreach (new Collection as $x) {}', '<?php foreach (new Collection() as $x) {}', ]; yield [ '<?php $a = [(string) new Foo => 1];', '<?php $a = [(string) new Foo() => 1];', ]; yield [ '<?php $a = [ "key" => new DateTime, ];', '<?php $a = [ "key" => new DateTime(), ];', ]; yield [ '<?php $a = [ "key" => new DateTime ];', '<?php $a = [ "key" => new DateTime() ];', ]; yield [ '<?php $a = new Foo ** 1; ', '<?php $a = new Foo() ** 1; ', ]; yield [ '<?php $a = new Foo <=> 1; ', '<?php $a = new Foo() <=> 1; ', ]; yield [ "<?php \$a = new \$b['class']/* */\r\n\t ;", "<?php \$a = new \$b['class']/* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class'] /* */\r\n\t ;", "<?php \$a = new \$b['class'] /* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class']/* */;", "<?php \$a = new \$b['class']()/* */;", ]; yield [ "<?php \$a = new \$b['class'] /* */;", "<?php \$a = new \$b['class']() /* */;", ]; } /** * @dataProvider provideFixAnonymousWithDefaultConfigurationCases */ public function testFixAnonymousWithDefaultConfiguration(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixAnonymousWithDefaultConfigurationCases(): iterable { yield ['<?php $a = new class($a) {use SomeTrait;};']; yield ['<?php $a = new class(foo(/**/)) implements Foo{};']; yield ['<?php $a = new class($c["d"]) /**/ extends Bar1{};']; yield ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};']; yield ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};']; yield ['<?php $a = new class( $h /**/) {}?>']; yield [ '<?php $a = new Foo() <=> 1; ', '<?php $a = new Foo <=> 1; ', ]; yield [ '<?php $a = new class() {use SomeTrait;}; $a = new class() implements Foo{}; $a = new class() /**/ extends Bar1{}; $a = new class() extends Bar2 implements Foo{}; $a = new class() extends Bar3 implements Foo, Foo2{}; $a = new class() {}?> ', '<?php $a = new class {use SomeTrait;}; $a = new class implements Foo{}; $a = new class /**/ extends Bar1{}; $a = new class extends Bar2 implements Foo{}; $a = new class extends Bar3 implements Foo, Foo2{}; $a = new class {}?> ', ]; yield [ '<?php class A { public function B() { $static = new static(new class(){}); } } ', '<?php class A { public function B() { $static = new static(new class{}); } } ', ]; } /** * @dataProvider provideFixAnonymousWithoutBracesCases */ public function testFixAnonymousWithoutBraces(string $expected, ?string $input = null): void { $this->fixer->configure(['anonymous_class' => false]); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixAnonymousWithoutBracesCases(): iterable { yield ['<?php $a = new class($a) {use SomeTrait;};']; yield ['<?php $a = new class(foo(/**/)) implements Foo{};']; yield ['<?php $a = new class($c["d"]) /**/ extends Bar1{};']; yield ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};']; yield ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};']; yield ['<?php $a = new class( $h /**/) {}?>']; yield [ '<?php $a = new class {use SomeTrait;}; $a = new class implements Foo{}; $a = new class /**/ extends Bar1{}; $a = new class extends Bar2 implements Foo{}; $a = new class extends Bar3 implements Foo, Foo2{}; $a = new class {}?> ', '<?php $a = new class() {use SomeTrait;}; $a = new class() implements Foo{}; $a = new class() /**/ extends Bar1{}; $a = new class() extends Bar2 implements Foo{}; $a = new class() extends Bar3 implements Foo, Foo2{}; $a = new class ( ) {}?> ', ]; yield [ '<?php class A { public function B() { $static = new static(new class{}); } } ', '<?php class A { public function B() { $static = new static(new class(){}); } } ', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php $a = new $b{$c}();', '<?php $a = new $b{$c};', ]; yield [ '<?php $a = new $b{$c}{0}{1}() ?>', '<?php $a = new $b{$c}{0}{1} ?>', ]; yield [ '<?php $a = new $b{$c}[1]{0}[2]();', '<?php $a = new $b{$c}[1]{0}[2];', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php $a = new (foo());', ]; yield [ '<?php class Bar { public function __construct(int $a = null) { echo $a; } }; $foo = "B"; $a = new ($foo."ar");', ]; yield [ '<?php $bar1 = new $foo[0]?->bar(); $bar2 = new $foo[0][1]?->bar(); ', ]; yield [ '<?php $a = new #[Internal] class(){}; ', '<?php $a = new #[Internal] class{}; ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php function test( $foo = new A(), $baz = new C(x: 2), ) { } class Test { public function __construct( public $prop = new Foo(), ) {} } static $x = new Foo(); const C = new Foo(); function test2($param = new Foo()) {} ', '<?php function test( $foo = new A, $baz = new C(x: 2), ) { } class Test { public function __construct( public $prop = new Foo, ) {} } static $x = new Foo; const C = new Foo; function test2($param = new Foo) {} ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * @author Gregor Harlan <gharlan@web.de> * @author Carlos Cirello <carlos.cirello.nl@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class BinaryOperatorSpacesFixerTest extends AbstractFixerTestCase { /** * @param array<string, mixed> $config * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $config, string $exceptionExpression): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($exceptionExpression); $this->fixer->configure($config); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'invalid option' => [ ['foo' => true], '/^\[binary_operator_spaces\] Invalid configuration: The option "foo" does not exist\. Defined options are: "default", "operators"\.$/', ]; yield 'wrong config type for operators' => [ ['operators' => true], '/^\[binary_operator_spaces\] Invalid configuration: The option "operators" with value true is expected to be of type "array", but is of type "(bool|boolean)"\.$/', ]; yield 'wrong config type for operators key' => [ ['operators' => [123 => 1]], '/^\[binary_operator_spaces\] Invalid configuration: Unexpected "operators" key, expected any of ".*", got "integer#123"\.$/', ]; yield 'wrong config type for operators value' => [ ['operators' => ['+' => 'abc']], '/^\[binary_operator_spaces\] Invalid configuration: Unexpected value for operator "\+", expected any of ".*", got "string#abc"\.$/', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ "<?php function myFunction() { \t\$foo = 1; \t\$looooongVar = 2; \t\$middleVar = 1; }", "<?php function myFunction() { \t\$foo= \t1; \t\$looooongVar\t = 2; \t\$middleVar\t= 1; }", ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ "<?php class A{ public function myFunction() { \t \$foo = 1; \t \$looooongVar = 2; \t \$middleVar = 1; } }", "<?php class A{ public function myFunction() { \t \$foo = 1; \t \$looooongVar = 2; \t \$middleVar = 1; } }", ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]], ]; yield [ '<?php $this->a = $this->b = 1 ;', '<?php $this->a = $this->b = 1 ;', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $this->newName = $this->path = $this->randomName = $this->remoteFile = $this->tmpContent = null;', '<?php $this->newName = $this->path = $this->randomName = $this->remoteFile = $this->tmpContent = null;', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a// = 1; ', '<?php $a// = 1; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::SINGLE_SPACE]], ]; yield [ '<?php $a = 1; $b = 2; ', '<?php $a = 1; $b=2; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::AT_LEAST_SINGLE_SPACE]], ]; yield [ '<?php $var = [1 => 2]; foreach ([ 1 => 2, 2 => 3, ] as $k => $v) { $var[] = [$i => $bar]; }', '<?php $var = [1=>2]; foreach ([ 1=> 2, 2 =>3, ] as $k => $v) { $var[] = [$i => $bar]; }', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a = array( 1 => 2, 4 => 5, 5 => 2, 6 => 5, 7 => 8, 9 => 10, 11 => 1222, );', '<?php $a = array( 1=>2, 4=>5, 5=>2, 6 => 5, 7=>8, 9=>10, 11=>1222, );', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a = array(1 => 2, 4 => 5);', '<?php $a = array(1=>2, 4 => 5);', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a = array(1 => 2, 4 => 5 && $b, 5 => 5 && $b, 6 => 5 && $b, 7 => 5 && $b);', '<?php $a = array(1 => 2, 4 => 5&&$b, 5 => 5 && $b, 6 => 5&& $b, 7 => 5 &&$b);', ['operators' => ['&&' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php [1 => "foo"]; [2 => "foo"]; [3 => "foo"]; ', '<?php [1 => "foo"]; [2 =>"foo"]; [3=>"foo"]; ', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield [ '<?php [1 => "foo"]; [2 => "foo"]; [3 => "foo"]; ', '<?php [1 => "foo"]; [2 =>"foo"]; [3=>"foo"]; ', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a += 1;', '<?php $a+=1;', ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield [ '<?php $a += 1;', '<?php $a+=1;', ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a+=1;', null, ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN]], ]; yield [ '<?php $ade = $b !== $a; $b = $b !== $a; $c = $b !== $a; ', '<?php $ade = $b!== $a; $b = $b!== $a; $c = $b!==$a; ', ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield [ '<?php $aab = $b !== $e; $b = $b !== $c; $c = $b !== $d; ', '<?php $aab = $b !==$e; $b = $b !==$c; $c = $b !==$d; ', ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $aaa*= 11; $b *= 21; $c *=31; $d = $e and $f; $d = $g or $h; ', '<?php $aaa*= 11; $b *= 21; $c*=31; $d = $e and $f; $d = $g or $h; ', [ 'operators' => [ 'and' => BinaryOperatorSpacesFixer::SINGLE_SPACE, '*=' => BinaryOperatorSpacesFixer::ALIGN, 'or' => null, ], ], ]; yield [ '<?php $abc = $b !== $a; $b = $b !== $a; $c = $b !== $a; ', '<?php $abc = $b !== $a; $b = $b !== $a; $c = $b !== $a; ', ['operators' => ['!==' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a = [ 1 => 2, 2 => 3, ];', '<?php $a = [ 1=>2, 2 => 3, ];', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php [1 => "foo", 2 => "foo"]; ', '<?php [1 => "foo", 2 => "foo"]; ', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php [1 => "foo"]; $i += 1; ', '<?php [1 => "foo"]; $i+= 1; ', ['operators' => ['+=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a = 1 + 2; $b = array( 13 =>3, 4 => 3, 5=>2, );', null, ['default' => null], ]; yield [ '<?php $a = 1 + 2; $b = array( $øøø => $ø0ø0ø, $ø4 => $ø1ø1ø, $ø5 => $ø2ø2ø, ); $a = 12 + 1; $a = 13 + 41; ', '<?php $a = 1 + 2; $b = array( $øøø =>$ø0ø0ø, $ø4 => $ø1ø1ø, $ø5=>$ø2ø2ø, ); $a = 12 + 1; $a = 13+41; ', ['default' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL], ]; yield 'do not align with nor touch strings' => [ '<?php \putenv("{$name}= {$value}"); $b = $c + 1; $b = $c - 1; ', '<?php \putenv("{$name}= {$value}"); $b =$c+1; $b =$c - 1; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield 'do not align with declare' => [ '<?php declare(ticks=1); $a = 1; $b = 1; ', '<?php declare(ticks=1); $a = 1; $b = 1; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield 'do not align with multibyte character in array key' => [ '<?php $map = [ "ø" => "oe", ]; ', null, ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield 'align correctly with multibyte characters in array key' => [ '<?php $inflect_male = array( "aitė\b" => "øasø", "ytė\b" => "øisø", "iūtė\b" => "øiusø", "utė\b" => array( "aitė\b" => "øas", "ytė\b" => "øis", "iūtė\b" => $øøius, "utė\b" => "us", ), );', '<?php $inflect_male = array( "aitė\b" => "øasø", "ytė\b" => "øisø", "iūtė\b" => "øiusø", "utė\b" => array( "aitė\b" => "øas", "ytė\b" => "øis", "iūtė\b" => $øøius, "utė\b" => "us", ), );', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield [ '<?php $foo = 1+$bar; ', '<?php $foo = 1 + $bar; ', [ 'default' => BinaryOperatorSpacesFixer::NO_SPACE, 'operators' => ['=' => BinaryOperatorSpacesFixer::SINGLE_SPACE], ], ]; yield [ '<?php $foo = 1 + $bar|$a; ', '<?php $foo = 1 + $bar | $a; ', [ 'default' => null, 'operators' => [ '=' => BinaryOperatorSpacesFixer::SINGLE_SPACE, '|' => BinaryOperatorSpacesFixer::NO_SPACE, ], ], ]; yield [ '<?php $foo = $d # | # $a| // foo $b# |$d; ', '<?php $foo = $d # | # $a | // foo $b# | $d; ', [ 'operators' => ['|' => BinaryOperatorSpacesFixer::NO_SPACE], ], ]; yield [ '<?php declare(strict_types=1); $a = 1; echo 1 <=> 1; echo 1 <=> 2; echo 2 <=> 1; echo 2 <=> 1; $a = $a ?? $b; $a = $ab ?? $b; $a = $ac ?? $b; $a = $ad ?? $b; $a = $ae ?? $b; ', '<?php declare(strict_types=1); $a = 1; echo 1<=>1; echo 1 <=>2; echo 2<=> 1; echo 2 <=> 1; $a = $a ?? $b; $a = $ab ?? $b; $a = $ac ?? $b; $a = $ad ?? $b; $a = $ae?? $b; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE, '??' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield 'align array destructuring' => [ '<?php $c = [$d] = $e[1]; function A(){}[$a] = $a[$c]; $b = 1; ', '<?php $c = [$d] = $e[1]; function A(){}[$a] = $a[$c]; $b = 1; ', ['operators' => ['=' => BinaryOperatorSpacesFixer::ALIGN]], ]; yield 'align array destructuring with assignments' => [ '<?php $d = [ "a" => $a, "b" => $b, "c" => $c ] = $array; ', '<?php $d = [ "a"=>$a, "b" => $b, "c" => $c ] = $array; ', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield 'multiple exceptions catch, default config' => [ '<?php try {} catch (A | B $e) {}', ]; yield 'multiple exceptions catch, no space config' => [ '<?php try {} catch (A | B $e) {}', null, ['operators' => ['|' => BinaryOperatorSpacesFixer::NO_SPACE]], ]; yield [ '<?php $a = fn() => null; $b = fn() => null; ', '<?php $a = fn() => null; $b = fn() => null; ', ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]], ]; yield [ '<?php $a ??= 1;', '<?php $a??=1;', ['operators' => ['??=' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE]], ]; yield [ <<<'PHP' <?php $array1 = [ '0カキ' => 1, '123カキ' => 2, 'カキ' => 3, '鍵' => 4, ]; $array2 = [ 'あいうえお' => 1, '10かきくけ' => 2, ]; PHP, <<<'PHP' <?php $array1 = [ '0カキ' => 1, '123カキ' => 2, 'カキ' => 3, '鍵' => 4, ]; $array2 = [ 'あいうえお' => 1, '10かきくけ' => 2, ]; PHP, ['default' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE], ]; yield [ '<?php $a + /** */ $b;', '<?php $a + /** */ $b;', ]; yield [ '<?php '.' $a + $b + $d; ;', '<?php '.' $a +$b + $d; ;', ]; yield [ '<?php $a /***/ + $b /***/ + $d; ;', '<?php $a /***/+ $b /***/ +$d; ;', ]; yield [ '<?php $a + $b;', '<?php $a+$b;', ]; yield [ '<?php 1 + $b;', '<?php 1+$b;', ]; yield [ '<?php 0.2 + $b;', '<?php 0.2+$b;', ]; yield [ '<?php $a[1] + $b;', '<?php $a[1]+$b;', ]; yield [ '<?php FOO + $b;', '<?php FOO+$b;', ]; yield [ '<?php foo() + $b;', '<?php foo()+$b;', ]; yield [ '<?php ${"foo"} + $b;', '<?php ${"foo"}+$b;', ]; yield [ '<?php $a & $b;', '<?php $a&$b;', ]; yield [ '<?php $a &= $b;', '<?php $a&=$b;', ]; yield [ '<?php $a &= $b;', '<?php $a &=$b;', ]; yield [ '<?php $a &= $b;', '<?php $a&= $b;', ]; yield [ '<?php $a &= $b;', '<?php $a &= $b;', ]; yield [ '<?php $a &= $b;', ]; yield [ '<?php $a &= $b;', '<?php $a &=$b;', ]; yield [ '<?php (1) and 2;', '<?php (1)and 2;', ]; yield [ '<?php 1 or ($b - $c);', '<?php 1 or($b-$c);', ]; yield [ '<?php "a" xor (2);', '<?php "a"xor(2);', ]; yield [ '<?php $a * -$b;', '<?php $a*-$b;', ]; yield [ '<?php $a = -2 / +5;', '<?php $a=-2/+5;', ]; yield [ '<?php $a = &$b;', '<?php $a=&$b;', ]; yield [ '<?php $a++ + $b;', '<?php $a+++$b;', ]; yield [ '<?php __LINE__ - 1;', '<?php __LINE__-1;', ]; yield [ '<?php `echo 1` + 1;', '<?php `echo 1`+1;', ]; yield [ '<?php function foo(&$a, array &$b, Bar &$c) {}', ]; yield [ '<?php $a = 1 // || 2; ', ]; yield [ '<?php $a = 2;', ]; yield [ '<?php declare(ticks=1);', ]; yield [ '<?php declare(ticks = 1);', ]; yield [ '<?php $a = 1;declare(ticks = 1);$b = 1;', '<?php $a=1;declare(ticks = 1);$b=1;', ]; yield [ '<?php $a = array("b" => "c", );', '<?php $a = array("b"=>"c", );', ]; yield [ '<?php $a = array("b" => "c", );', '<?php $a = array("b" =>"c", );', ]; yield [ '<?php $a = array("b" => "c", );', '<?php $a = array("b"=> "c", );', ]; yield [ '<?php [1, 2] + [3, 4];', '<?php [1, 2]+[3, 4];', ]; yield [ '<?php [1, 2] + [3, 4];', '<?php [1, 2] + [3, 4];', ]; yield [ '<?php [1, 2] + // '.' [3, 4];', '<?php [1, 2] + // '.' [3, 4];', ]; yield [ '<?php $a = $b + $c;$a = $b + $c;$a = $b + $c;$a = $b + $c;$a = $b + $c;$a = $b + $c;$a = $b + $c;$a = $b + $c;', '<?php $a=$b+$c;$a=$b+$c;$a=$b+$c;$a=$b+$c;$a=$b+$c;$a=$b+$c;$a=$b+$c;$a=$b+$c;', ]; yield [ '<?php $c = $a + $b; ', ]; yield ['<a href="test-<?=$path?>-<?=$id?>.html">Test</a>']; yield 'reference in functions declarations' => [ '<?php function a(string &$x) { return $x + 1; }; $b = function (string &$x) { return $x + 1; }; $c = fn (string &$x) => $x + 1; ', ]; yield [ '<?php $a = "c"?>', '<?php $a="c"?>', ]; yield [ '<?php $a = "c";', '<?php $a ="c";', ]; yield [ '<?php $a = "c";', '<?php $a= "c";', ]; yield [ '<?php $d = $c + $a/**/ + // $b;', '<?php $d = $c+$a/**/+ // $b;', ]; yield [ '<?php $a = 1; $bbbb = \' $cccccccc = 3; \';', '<?php $a = 1; $bbbb = \' $cccccccc = 3; \';', ]; yield [ '<?php $ccc = 1; $bb = 1; $a = 1; /* Others alignments */ $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; $a[$b] = 1; $ab[$bc] = 1; $abc[$bcd] = 1; if ($a = 1) { $ccc = 1; $bb = 1; $a = 1; } function a($a = 1, $b = 2, $c = 3) { $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; } function b( $a = 1, $bbb = 2, $c = 3 ) { $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; } while (false) { $aa = 2; $a[$b] = array(); } for ($i = 0; $i < 10; $i++) { $aa = 2; $a[$b] = array(12); }', '<?php $ccc = 1; $bb = 1; $a = 1; /* Others alignments */ $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; $a[$b] = 1; $ab[$bc] = 1; $abc[$bcd] = 1; if ($a = 1) { $ccc = 1; $bb = 1; $a = 1; } function a($a = 1, $b = 2, $c = 3) { $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; } function b( $a = 1, $bbb = 2, $c = 3 ) { $a[$b = 1] = 1; $ab[$bc = 1] = 1; $abc[$bcd = 1] = 1; } while (false) { $aa = 2; $a[$b] = array(); } for ($i = 0; $i < 10; $i++) { $aa = 2; $a[$b] = array(12); }', ]; yield [ '<?php $data = [ "foo" => "Bar", "main" => array( [ "baz" => "Test", "bazaa" => $a->{"Test"}, "bazaa" => $a["Test"], "bazaaaa" => b("Test"), ] ), "bar" => array(), ];', '<?php $data = [ "foo" => "Bar", "main" => array( [ "baz" => "Test", "bazaa" => $a->{"Test"}, "bazaa" => $a["Test"], "bazaaaa" => b("Test"), ] ), "bar" => array(), ];', ]; yield [ '<?php $data = [ "foo" => "Bar", "main" => [array("baz" => "Test")], "bar" => array(), ]; $data = array( "foo" => "Bar", "main" => array("baz" => "Test"), "bar" => array(), ); $var = []; foreach ($foo as $i => $bar) { $var[] = /* Comment */ [$i => $bar]; }', '<?php $data = [ "foo" => "Bar", "main" => [array("baz" => "Test")], "bar" => array(), ]; $data = array( "foo" => "Bar", "main" => array("baz" => "Test"), "bar" => array(), ); $var = []; foreach ($foo as $i => $bar) { $var[] = /* Comment */ [$i => $bar]; }', ]; yield [ '<?php $data = [ "foo" => "Bar", "main" => [array("baz" => "Test")], "bar" => array(), ];', '<?php $data = [ "foo" => "Bar", "main" => [array("baz" => "Test")], "bar" => array(), ];', ]; yield [ '<?php $data = array( "foo" => "Bar", "main" => array("baz" => "Test"), "bar" => array(), );', '<?php $data = array( "foo" => "Bar", "main" => array("baz" => "Test"), "bar" => array(), );', ]; yield [ '<?php $data = array( "foo" => "Bar", "main" => array(array("baz" => "Test")), "bar" => array(), );', '<?php $data = array( "foo" => "Bar", "main" => array(array("baz" => "Test")), "bar" => array(), );', ]; yield [ '<?php $var = []; foreach ($foo as $i => $bar) { $var[] = /* Comment */ [$i => $bar]; }', '<?php $var = []; foreach ($foo as $i => $bar) { $var[] = /* Comment */ [$i => $bar]; }', ]; yield [ '<?php $var = []; foreach ($foo as $i => $bar) { $var[] = [$i => $bar]; }', ]; yield [ '<?php $var = []; foreach ([1 => 2] as $k => $v) { $var[] = [$i => $bar]; }', ]; yield [ '<?php $var = []; foreach (fncCall() as $k => $v){ $var[] = [$i => $bar]; }', ]; yield [ '<?php $var = []; foreach ($foo as $bar) { $var[] = [ $i => $bar, $iaaa => $bar, ]; }', '<?php $var = []; foreach ($foo as $bar) { $var[] = [ $i => $bar, $iaaa => $bar, ]; }', ]; yield [ '<?php $data = [ "foo" => "Bar", "main" => [["baz" => "Test", "bar" => "Test2"]], "bar" => [], ];', '<?php $data = [ "foo" => "Bar", "main" => [["baz" => "Test", "bar" => "Test2"]], "bar" => [], ];', ]; yield [ '<?php $a = [ 0 => 1, 10 /*Comment*/ => [ 1 => 2, 22 => 3, ], 100 => [ 1 => 2, 22 => 3, ] ];', '<?php $a = [ 0 => 1, 10 /*Comment*/ => [ 1 => 2, 22 => 3, ], 100 => [ 1 => 2, 22 => 3, ] ];', ]; yield [ '<?php $a = array( 0 => 1, 10 => array( 1 => 2, 22 => 3, ), 100 => array( 1 => 2, 22 => 3, ) );', '<?php $a = array( 0 => 1, 10 => array( 1 => 2, 22 => 3, ), 100 => array( 1 => 2, 22 => 3, ) );', ]; yield [ '<?php $arr = array( $a => 1, $bbbb => \' $cccccccc = 3; \', );', '<?php $arr = array( $a => 1, $bbbb => \' $cccccccc = 3; \', );', ]; yield [ '<?php $arr = [ $a => 1, $bbbb => \' $cccccccc = 3; \', ];', '<?php $arr = [ $a => 1, $bbbb => \' $cccccccc = 3; \', ];', ]; yield [ '<?php foreach($arr as $k => $v){ $arr = array($k => 1, $a => 1, $bbbb => \' $cccccccc = 3; \', ); }', '<?php foreach($arr as $k => $v){ $arr = array($k => 1, $a => 1, $bbbb => \' $cccccccc = 3; \', ); }', ]; yield [ '<?php $a = array( 10 => 11, 20 => 22, 30 => 33, 40 => 44, );', '<?php $a = array( 10 => 11, 20 => 22, 30=>33, 40 => 44, );', ]; yield [ '<?php return array( " " => "", "\t" => "", "\n" => "", "\r" => "", "\0" => "", "\x0B" => "", );', '<?php return array( " " => "", "\t" => "", "\n" => "", "\r" => "", "\0" => "", "\x0B" => "", );', ]; yield [ '<?php return $this->grabAttribsBeforeToken( $tokens, $index, $tokenAttribsMap, array( "abstract" => null, "final" => null, "visibility" => new Token(array(T_PUBLIC, "public")), "static" => null, ) );', '<?php return $this->grabAttribsBeforeToken( $tokens, $index, $tokenAttribsMap, array( "abstract" => null, "final" => null, "visibility" => new Token(array(T_PUBLIC, "public")), "static" => null, ) );', ]; yield [ '<?php return array( self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"), self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"), );', '<?php return array( self::STATUS_UNKNOWN_0 => array("symbol" => "?", "description" => "unknown"), self::STATUS_INVALID_0 => array("symbol" => "III", "description" => "invalid file syntax, file ignored"), );', ]; yield [ '<?php $array = array( "bazab" => b(array( 1 => 2, 5 => [ 6 => 7, 8 => 9, ], 3 => 4, 10 => 11, )), );', '<?php $array = array( "bazab" => b(array( 1 => 2, 5 => [
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/TernaryToNullCoalescingFixerTest.php
tests/Fixer/Operator/TernaryToNullCoalescingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\TernaryToNullCoalescingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\TernaryToNullCoalescingFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class TernaryToNullCoalescingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield // Do not fix cases. ['<?php $x = isset($a) ? $a[1] : null;']; yield ['<?php $x = isset($a) and $a ? $a : "";']; yield ['<?php $x = "isset($a) ? $a : null";']; yield ['<?php $x = isset($a) ? $$a : null;']; yield ['<?php $x = isset($this) ? $this : null;']; yield ['<?php $x = isset($A) ? $a : null;']; // different case yield ['<?php $x = isset($a) ? "$a" : null;']; yield ['<?php $x = isset($a) ?: false;']; yield ['<?php $x = $y ?? isset($a) ? $a : null;']; yield ['<?php $x = isset($a) ?: $b;']; yield ['<?php $x = isset($a, $b) ? $a : null;']; yield ['<?php $x = $a && isset($b) ? $b : null;']; yield ['<?php $x = $a & isset($b) ? $b : null;']; yield ['<?php $x = ! isset($a) ? $a : null;']; yield ['<?php $x = false === isset($a) ? $a : 2;']; yield ['<?php $x = 4 * isset($a) ? $a : 2;']; yield ['<?php $x = "4" . isset($a) ? $a : 2;']; yield ['<?php $x = 3 ** isset($a) ? $a : 2;']; yield ['<?php $x = 1 | isset($a) ? $a : 2;']; yield ['<?php $x = (array) isset($a) ? $a : 2;']; yield ['<?php $x = isset($a[++$i]) ? $a[++$i] : null;']; yield ['<?php $x = function(){isset($a[yield]) ? $a[yield] : null;};']; yield ['<?php $x = isset($a[foo()]) ? $a[foo()] : null;']; yield ['<?php $x = isset($a[$callback()]) ? $a[$callback()] : null;']; yield ['<?php $y = isset($a) ? 2**3 : 3**2;']; yield ['<?php $x = function(){isset($a[yield from $a]) ? $a[yield from $a] : null;};']; // Fix cases. yield 'Common fix case (I).' => [ '<?php $x = $a ?? null;', '<?php $x = isset($a) ? $a : null;', ]; yield 'Common fix case (II).' => [ '<?php $x = $a[0] ?? 1;', '<?php $x = isset($a[0]) ? $a[0] : 1;', ]; yield 'Minimal number of tokens case.' => [ '<?php $x=$a??null?>', '<?php $x=isset($a)?$a:null?>', ]; yield [ '<?php $x = $a ?? 1; $y = isset($b) ? "b" : 2; $x = $c ?? 3;', '<?php $x = isset($a) ? $a : 1; $y = isset($b) ? "b" : 2; $x = isset($c) ? $c : 3;', ]; yield [ '<?php $x = $a[ $b[ "c" ]] ?? null;', '<?php $x = isset ( $a[$b["c"]]) ?$a[ $b[ "c" ]] : null;', ]; yield [ '<?php $x = $a ?? $b[func(1, true)];', '<?php $x = isset($a) ? $a : $b[func(1, true)];', ]; yield [ '<?php $x = $a ?? ($b ?? "");', '<?php $x = isset($a) ? $a : (isset($b) ? $b : "");', ]; yield [ '<?php $x = ($a ?? isset($b)) ? $b : "";', '<?php $x = (isset($a) ? $a : isset($b)) ? $b : "";', ]; yield [ '<?php $x = $obj->a ?? null;', '<?php $x = isset($obj->a) ? $obj->a : null;', ]; yield [ '<?php $x = $obj->a->b ?? null;', '<?php $x = isset($obj->a->b) ? $obj->a->b : null;', ]; yield [ '<?php $x = $obj->$a ?? null;', '<?php $x = isset($obj->$a) ? $obj->$a : null;', ]; yield [ '<?php $x = $obj->a->$b ?? null;', '<?php $x = isset($obj->a->$b) ? $obj->a->$b : null;', ]; yield [ '<?php $x = $obj->a[3] ?? null;', '<?php $x = isset($obj->a[3]) ? $obj->a[3] : null;', ]; yield [ '<?php $x = $obj->a[\'foo\'] ?? null;', '<?php $x = isset($obj->a[\'foo\']) ? $obj->a[\'foo\'] : null;', ]; yield [ '<?php $x = $obj->a[$b] ?? null;', '<?php $x = isset($obj->a[$b]) ? $obj->a[$b] : null;', ]; yield [ '<?php $x = $obj->a[$b][\'foo\'] ?? null;', '<?php $x = isset($obj->a[$b][\'foo\']) ? $obj->a[$b][\'foo\'] : null;', ]; yield [ '<?php $x = $obj->a[$b[\'foo\']] ?? null;', '<?php $x = isset($obj->a[$b[\'foo\']]) ? $obj->a[$b[\'foo\']] : null;', ]; yield [ '<?php $x = $a[$obj->b] ?? null;', '<?php $x = isset($a[$obj->b]) ? $a[$obj->b] : null;', ]; yield [ '<?php $x = Foo::A[$b] ?? null;', '<?php $x = isset(Foo::A[$b]) ? Foo::A[$b] : null;', ]; yield [ '<?php $x = $a[Foo::B] ?? null;', '<?php $x = isset($a[Foo::B]) ? $a[Foo::B] : null;', ]; yield [ '<?php $x = ( // c1 // c2 // c3 $a // c4 ?? // c5 null /* c6 */ ) # c7 ;', '<?php $x = ( // c1 isset($a) // c2 ? // c3 $a // c4 : // c5 null /* c6 */ ) # c7 ;', ]; yield [ '<?php $x = $THIS ?? null;', '<?php $x = isset($THIS) ? $THIS : null;', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixPre80Cases(): iterable { yield ['<?php $x = $a ? $a : isset($b) ? $b : isset($c) ? $c : "";']; yield [ '<?php $x = $a ?? isset($b) ? $b : isset($c) ? $c : "";', '<?php $x = isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : "";', ]; yield [ '<?php $x = /*a1*//*a2*/ /*b*/ $a /*c*/ ?? /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";', '<?php $x = isset($a) /*a1*//*a2*/ ? /*b*/ $a /*c*/ : /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NoSpaceAroundDoubleColonFixerTest.php
tests/Fixer/Operator/NoSpaceAroundDoubleColonFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @covers \PhpCsFixer\Fixer\Operator\NoSpaceAroundDoubleColonFixer * * @internal * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NoSpaceAroundDoubleColonFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoSpaceAroundDoubleColonFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php echo self::$a;', '<?php echo self :: $a;', ]; yield [ '<?php echo static::$a;', '<?php echo static ::$a;', ]; yield [ '<?php echo F\B::class; echo A\B:: /**/ c; echo C\B/**/::c; ', '<?php echo F\B:: class; echo A\B :: /**/ c; echo C\B/**/:: c; ', ]; yield [ '<?php namespace { class Foo { public const a = 1; } echo Foo::a; // Fix echo "\n".Place\Bar::$a."\n"; // Fix } namespace Somewhere\Over\The\Rainbow { class Bar { public static $a = "BAR-A:: "; public function v(?string $z = "zzz"): void { echo "\n".self::$a.$z; // Fix echo "\n".static::class; // Fix echo "\n".static # do ... :: # ... not ... $a.$z; // ... fix } } $bar = new Bar(); $bar->v(); } # ; echo A :: B; // ; echo A :: B; /* ; echo A :: B; */ ', '<?php namespace { class Foo { public const a = 1; } echo Foo:: a; // Fix echo "\n".Place\Bar :: $a."\n"; // Fix } namespace Somewhere\Over\The\Rainbow { class Bar { public static $a = "BAR-A:: "; public function v(?string $z = "zzz"): void { echo "\n".self :: $a.$z; // Fix echo "\n".static :: class; // Fix echo "\n".static # do ... :: # ... not ... $a.$z; // ... fix } } $bar = new Bar(); $bar->v(); } # ; echo A :: B; // ; echo A :: B; /* ; echo A :: B; */ ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/LongToShorthandOperatorFixerTest.php
tests/Fixer/Operator/LongToShorthandOperatorFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AbstractShortOperatorFixer * @covers \PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\LongToShorthandOperatorFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LongToShorthandOperatorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple I' => [ '<?php $a += 123;', '<?php $a = $a + 123;', ]; yield 'simple II' => [ '<?php $b[0] *= 789;', '<?php $b[0] = ($b[0]) * 789;', ]; yield 'simple III' => [ '<?php ($b *= 789);', '<?php ($b = $b * 789);', ]; yield 'simple IV' => [ '<?php foo($c /= 1234, 1);', '<?php foo($c = $c / 1234, 1);', ]; yield 'simple V' => [ '<?php foo(1, $x *= 1235, 1);', '<?php foo(1, $x = $x * 1235, 1);', ]; yield 'simple II\' array' => [ '<?php $aa[1] %= 963;', '<?php $aa[1] = $aa[1] % 963;', ]; yield 'simple III array' => [ '<?php $a[1][2] -= 852;', '<?php $a[1][2] = $a[1][2] - 852;', ]; yield 'simple IV array' => [ '<?php {$a[0][1][122] ^= $a;}', '<?php {$a[0][1][122] = $a[0][1][122] ^ $a;}', ]; yield [ '<?php $xa .= $b;', '<?php $xa = $xa . $b;', ]; $constants = ['"foo"', "'foo'", '1', '1.1']; foreach ($constants as $i => $constant) { yield 'c #'.$i => [ \sprintf('<?php $fa .= %s;', $constant), \sprintf('<?php $fa = $fa . %s;', $constant), ]; yield 'c reverse #'.$i => [ \sprintf('<?php $ga *= %s ;', $constant), \sprintf('<?php $ga = %s * $ga;', $constant), ]; } foreach (['-', '/', '.', '%'] as $nonCommutativeKind) { yield \sprintf('non commutative kind "%s"', $nonCommutativeKind) => [ \sprintf('<?php $nck = 5 %s $nck;', $nonCommutativeKind), ]; } foreach (['*' => '*=', '|' => '|=', '&' => '&=', '^' => '^='] as $operator => $shortHand) { yield \sprintf('commutative operator "%s".', $operator) => [ \sprintf('<?php $a3 %s "456" ;', $shortHand), \sprintf('<?php $a3 = "456" %s $a3;', $operator), ]; } // array index yield 'simple I array' => [ '<?php $ai[1] += 566;', '<?php $ai[1] = $ai[1] + 566;', ]; yield 'simple II array' => [ '<?php $p[1] += 789;', '<?php $p[1] = $p[1] + 789;', ]; // minimal and multiple yield 'minimal' => [ '<?php $a += 1;', '<?php $a=$a+1;', ]; yield 'minimal, multiple' => [ '<?php $a += 1;$a += 1;$a += 1;$a += 1;', '<?php $a=$a+1;$a=$a+1;$a=$a+1;$a=$a+1;', ]; // test simple with all operators $operators = \Closure::bind(static fn (): array => LongToShorthandOperatorFixer::OPERATORS, null, LongToShorthandOperatorFixer::class)(); foreach ($operators as $operator => $info) { $shortHand = $info[1]; yield \sprintf('Simple test with operator "%s" var/var.', $operator) => [ \sprintf('<?php $a1 %s $b;', $shortHand), \sprintf('<?php $a1 = $a1 %s $b;', $operator), ]; yield \sprintf('Simple test with operator "%s" var/const.', $operator) => [ \sprintf('<?php $a2 %s 1;', $shortHand), \sprintf('<?php $a2 = $a2 %s 1;', $operator), ]; } // odds and ends yield [ '<?php $a4 += ++$b;', '<?php $a4 = $a4 + ++$b;', ]; yield [ '<?php $a5 .= '.' <<<EOD EOD ;', '<?php $a5 = $a5 . <<<EOD EOD ;', ]; yield [ '<?php $a6 .= '.' <<<\'EOD\' EOD ?>', '<?php $a6 = $a6 . <<<\'EOD\' EOD ?>', ]; yield [ '<?php $t += 1; $t1 -= 1; $t2 *= 1; $t3 /= 1; $t4 .= /* */ 1;', '<?php $t = ((($t))) + 1; $t1 = ($t1) - 1; $t2 = $t2 * 1; $t3 = ($t3) / 1; $t4 = ($t4) /* */ . 1;', ]; // before assignment var yield 'minus itself' => [ '<?php ;$a -= $a;', '<?php ;$a = $a - $a;', ]; yield 'after not needed block' => [ '<?php {echo 1;} $a &= $a;', '<?php {echo 1;} $a = $a & $a;', ]; yield 'after if' => [ '<?php if($z){echo 2;} $a |= $a;', '<?php if($z){echo 2;} $a = $a | $a;', ]; yield 'fn minus itself' => [ '<?php foo(1, $an -= $an);', '<?php foo(1, $an = $an - $an);', ]; yield 'simple, before ) I' => [ '<?php if ($a) $a .= "X"?>', '<?php if ($a) $a = $a . "X"?>', ]; yield [ '<?php $a1 /= +$b1; $a2 /= -$b2; ', '<?php $a1 = $a1 / +$b1; $a2 = $a2 / -$b2; ', ]; // do not fix yield 'do not fix various' => ['<?php $a = ${foo} . 1; $a = ${foo}++ + 1; $a = $a[1] * 1; $a = $a(1 + 2) . 1; $a = $a[1][2] . 1; $a = $a[1][2][3][foo()][$a++][1+$a][${"foo"}][99] . 1; $a = ${foo}++ . 1; $a = ($a /* */ /* */ /* */ /* */ + 1 /* */ ) + 1; $a = 1 . 1 + foo(); $a = 1 . foo() + 1; $a = 1 . foo(); $a = 1 . foo(1, ++$a); $a = foo() . 1; $a = foo(1, ++$a) . 1; $a = $a[1] * 1; $a[1] = $a[0] * 1; $a = $a(1 + 2) . 1; foo($b, ${foo} + 1); foo($a + 1); $a++ + 2; 2 + $a++; $a = 7 . (int) $a; $a = (int) $a . 7; (int) $a = 7 . (int) $a; (int) $a = (int) $a . 7; $a = 1 . $a + foo(); $a = $a instanceof \Foo & $b; $a = $a + $b instanceof \Foo; $a = $d / $a + $b; $d + $a = $a - $e; $a = $a >= $b; $a[1] = $a[1] instanceof \Foo & $b; ']; yield ['<?php $a = 123 + $a + $c ?>']; yield ['<?php $a = $a + 123 + $c ?>']; // do not fix; not assignment yield ['<?php ($a + 123);']; yield ['<?php while(true){$a + 123;}']; yield ['<?php $a + 123;']; yield ['<?php ; $a + 123;']; // do not fix; precedence yield [ '<?php $a = 1; $b = 3; $a = $a + $b ? 1 : 2; var_dump($a); $a = 1; $b = 3; $a += $b ? 1 : 2; var_dump($a); //--------------------- $a = 2; $b = null; $a = $a + $b ?? 3; var_dump($a); $a = 2; $b = null; $a += $b ?? 3; var_dump($a); //--------------------- $a = 3; $b = null; $a = $a + $b === null ? 3 : 1; var_dump($a); $a = 3; $b = null; $a += $b === null ? 3 : 1; var_dump($a); //--------------------- $a = $a & $a ^ true; $a = $a ^ true & $a; $a = 1 . $a + foo(); //--------------------- $a = 1; $b = false; $z = true; $a = $a + $b || $z; var_dump($a); ', ]; yield ['<?php {echo 1;} $a = new class{} & $a;']; // reverse yield 'simple I reverse' => [ '<?php $a *= 9988 ?>', '<?php $a = 9988 * $a ?>', ]; yield 'simple V, comments, reverse' => [ '<?php foo(1, /*1*/$x /*2*/*= /*3*/123/*4*//*5*//*6*/, 1);', '<?php foo(1, /*1*/$x/*2*/=/*3*/123/*4*/*/*5*/$x/*6*/, 1);', ]; yield 'simple VI, `)`, reverse' => [ '<?php foo(1, $x *= 123);', '<?php foo(1, $x=123*$x);', ]; yield [ '<?php $a99 .= // foo <<<EOD EOD ;', '<?php $a99 = $a99 . // foo <<<EOD EOD ;', ]; yield [ '<?php $a00 .= // foo2 <<<\'EOD\' EOD ;', '<?php $a00 = $a00 . // foo2 <<<\'EOD\' EOD ;', ]; yield 'do bother with to much mess' => [ '<?php $a = 1 + $a + 2 + $a; $a = $a + 1 + $a + 2; ', ]; yield [ '<?php $r[1] = [&$r[1]]; $r[1] = [$r[1],&$r[1]]; ', ]; yield 'switch case & default' => [ '<?php switch(foo()) { case \'X\': $pX -= 789; break; default: $pY -= $b5; } ', '<?php switch(foo()) { case \'X\': $pX = $pX - 789; break; default: $pY = $pY - $b5; } ', ]; yield 'operator precedence' => [ '<?php $x = $z ? $b : $a = $a + 123;', ]; yield 'alternative syntax' => [ '<?php foreach([1, 2, 3] as $i): $a += $i; endforeach;', '<?php foreach([1, 2, 3] as $i): $a = $a + $i; endforeach;', ]; yield 'assign and return' => [ '<?php class Foo { private int $test = 1; public function bar(int $i): int { return $this->test += $i; } }', '<?php class Foo { private int $test = 1; public function bar(int $i): int { return $this->test = $this->test + $i; } }', ]; } /** * @requires PHP <8.0 * * @dataProvider provideFixPre80Cases */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php $a = $a[1]{2} . 1; $a = $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99] . 1; $a = 1 . $a[1]{2}; $a = 1 . $a[1]{2}[3][foo()][$a++][1+$a][${"foo"}][99];', ]; yield 'simple I\' array' => [ '<?php $a[1] += 963;', '<?php $a[1] = $a{1} + 963;', ]; yield 'simple II array' => [ '<?php $a[1]{1} += 852;', '<?php $a[1]{1} = $a[1]{1} + 852;', ]; yield 'simple III array' => [ '<?php $a{7} += 742;', '<?php $a{7} = $a[7] + 742;', ]; yield 'simple IV array' => [ '<?php {$a[0]{1}[1] ^= $azz;} ?>', '<?php {$a[0]{1}[1] = $a[0][1]{1} ^ $azz;} ?>', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NewExpressionParenthesesFixerTest.php
tests/Fixer/Operator/NewExpressionParenthesesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NewExpressionParenthesesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NewExpressionParenthesesFixer> * * @author Valentin Udaltsov <udaltsov.valentin@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\NewExpressionParenthesesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NewExpressionParenthesesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases * * @requires PHP 8.4 */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: non-empty-string, 1?: ?non-empty-string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { // anonymous class yield ['<?php (new class {});']; yield ['<?php ((new class {}))->bar;']; yield [ '<?php new class {}->bar;', '<?php (new class {})->bar;', ]; yield [ '<?php /**/ new /**/ class /**/ {} /**/ ->bar;', '<?php ( /**/ new /**/ class /**/ {} /**/ )->bar;', ]; yield [ '<?php new class ($x, (1 + 2) / 3) {}->bar;', '<?php (new class ($x, (1 + 2) / 3) {})->bar;', ]; yield [ '<?php new class (new class {}) {}->bar;', '<?php (new class (new class {}) {})->bar;', ]; yield [ '<?php new class ($x, (1 + 2) / 3) extends stdClass implements Throwable {}->bar;', '<?php (new class ($x, (1 + 2) / 3) extends stdClass implements Throwable {})->bar;', ]; yield [ '<?php new class { public function __construct() { new class {}->bar; } }->bar;', '<?php (new class { public function __construct() { (new class {})->bar; } })->bar;', ]; yield ['<?php (new class {})->bar;', null, ['use_parentheses' => true]]; yield [ '<?php (new class {})->bar;', '<?php new class {}->bar;', ['use_parentheses' => true], ]; // regular class name yield ['<?php (new stdClass());']; yield ['<?php ((new stdClass()));']; yield [ '<?php new stdClass()->bar;', '<?php (new stdClass())->bar;', ]; yield [ '<?php new \stdClass()->bar;', '<?php (new \stdClass())->bar;', ]; yield [ '<?php new namespace\Foo()->bar;', '<?php (new namespace\Foo())->bar;', ]; yield [ '<?php new Name\Space\Foo()->bar;', '<?php (new Name\Space\Foo())->bar;', ]; yield [ '<?php new \Name\Space\Foo()->bar;', '<?php (new \Name\Space\Foo())->bar;', ]; yield [ '<?php /**/ new /**/ \Name\Space\Foo /**/ () /**/ ->bar;', '<?php ( /**/ new /**/ \Name\Space\Foo /**/ () /**/ )->bar;', ]; yield [ '<?php new Foo($x, (1 + 2) / 3)->bar;', '<?php (new Foo($x, (1 + 2) / 3))->bar;', ]; yield [ '<?php new Foo(new Baz()->m())->bar;', '<?php (new Foo((new Baz())->m()))->bar;', ]; yield ['<?php (new Foo)->bar;']; yield ['<?php (new Foo())->bar;', null, ['use_parentheses' => true]]; yield [ '<?php (new Foo())->bar;', '<?php new Foo()->bar;', ['use_parentheses' => true], ]; // $variable class name yield [ '<?php new $class()->bar;', '<?php (new $class())->bar;', ]; yield [ '<?php /**/ new /**/ $class /**/ ( /**/ ) /**/ ->bar;', '<?php ( /**/ new /**/ $class /**/ ( /**/ ) /**/ )->bar;', ]; yield [ '<?php new $$var()->bar;', '<?php (new $$var())->bar;', ]; yield [ '<?php new ${"var"}()->bar;', '<?php (new ${"var"}())->bar;', ]; yield [ '<?php new $obj->prop()->bar;', '<?php (new $obj->prop())->bar;', ]; yield [ '<?php new $obj?->prop()->bar;', '<?php (new $obj?->prop())->bar;', ]; yield [ '<?php new $obj::$prop()->bar;', '<?php (new $obj::$prop())->bar;', ]; yield [ '<?php new SomeClass::$prop()->bar;', '<?php (new SomeClass::$prop())->bar;', ]; yield [ '<?php new $obj["x"]()->bar;', '<?php (new $obj["x"]())->bar;', ]; yield [ '<?php new $obj[1][2]()->bar;', '<?php (new $obj[1][2]())->bar;', ]; yield ['<?php (new $class)->bar;', null, ['use_parentheses' => true]]; yield [ '<?php (new $class())->bar;', '<?php new $class()->bar;', ['use_parentheses' => true], ]; // (expression) class name yield ['<?php (new (foo()));']; yield ['<?php ((new (foo())));']; yield [ '<?php new (foo())()->bar;', '<?php (new (foo())())->bar;', ]; yield [ '<?php /**/ new /**/ ( /**/ foo() /**/ ) /**/ ( /**/ ) /**/ ->bar;', '<?php ( /**/ new /**/ ( /**/ foo() /**/ ) /**/ ( /**/ ) /**/ )->bar;', ]; yield ['<?php (new (foo()))->bar;']; yield ['<?php (new (foo()))->bar;', null, ['use_parentheses' => true]]; yield [ '<?php (new (foo())())->bar;', '<?php new (foo())()->bar;', ['use_parentheses' => true], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/TernaryOperatorSpacesFixerTest.php
tests/Fixer/Operator/TernaryOperatorSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class TernaryOperatorSpacesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'handle goto labels 1' => [ '<?php beginning: echo $guard ? 1 : 2;', '<?php beginning: echo $guard?1:2;', ]; yield 'handle goto labels 2' => [ '<?php function A(){} beginning: echo $guard ? 1 : 2;', '<?php function A(){} beginning: echo $guard?1:2;', ]; yield 'handle goto labels 3' => [ '<?php ; beginning: echo $guard ? 1 : 2;', '<?php ; beginning: echo $guard?1:2;', ]; yield 'handle goto labels 4' => [ '<?php { beginning: echo $guard ? 1 : 2;}', '<?php { beginning: echo $guard?1:2;}', ]; yield 'handle instanceof static' => [ '<?php $a instanceof static ? $b : $c;', '<?php $a instanceof static?$b:$c;', ]; yield [ '<?php $a = $a ? 1 : 0;', '<?php $a = $a ? 1 : 0;', ]; yield [ '<?php $a = $a ? # : $b;', ]; yield [ '<?php $a = $a# ? '.' # 1 : 0;', ]; yield [ '<?php $val = (1===1) ? true : false;', '<?php $val = (1===1)?true:false;', ]; yield [ '<?php $val = 1===1 ? true : false;', '<?php $val = 1===1?true:false;', ]; yield [ '<?php $a = $b ? 2 : ($bc ? 2 : 3); $a = $bc ? 2 : 3;', '<?php $a = $b ? 2 : ($bc?2:3); $a = $bc?2:3;', ]; yield [ '<?php $config = $config ?: new Config();', '<?php $config = $config ? : new Config();', ]; yield [ '<?php $a = $b ? ( $c + 1 ) : ( $d + 1 );', ]; yield [ '<?php $a = $b ? $c : $d;', '<?php $a = $b ?$c :$d;', ]; yield [ '<?php $a = $b // ? $c /**/ : $d;', '<?php $a = $b // ?$c /**/ :$d;', ]; yield [ '<?php $a = ($b ? $c : ($d ? $e : $f ) );', ]; yield [ '<?php $a = ($b ? ($c1 ? $c2 : ($c3a ?: $c3b)) : ($d1 ? $d2 : $d3) );', '<?php $a = ($b ? ($c1?$c2:($c3a? :$c3b)) : ($d1?$d2:$d3) );', ]; yield [ '<?php $foo = $isBar ? 1 : 2; switch ($foo) { case 1: return 3; case 2: return 4; } ', '<?php $foo = $isBar? 1 : 2; switch ($foo) { case 1: return 3; case 2: return 4; } ', ]; yield [ '<?php return $isBar ? array_sum(array_map(function ($x) { switch ($x) { case 1: return $y ? 2 : 3; case 4: return 5; } }, [1, 2, 3])) : 128; ', '<?php return $isBar?array_sum(array_map(function ($x) { switch ($x) { case 1: return $y? 2 : 3; case 4: return 5; } }, [1, 2, 3])):128; ', ]; yield [ '<?php declare(ticks=1):enddeclare; for ($i = 0; $i < 100; $i++): echo "."; endfor; foreach ($foo as $bar): $i++; endforeach; if ($x === 1): echo "One"; elseif ($x === 2): echo "Two"; else: echo "Three"; endif; switch (true): default: return 0; endswitch; while ($i > 10): $i--; endwhile; /* ternary operator to make the file a candidate for fixing */ true ? 1 : 0; ', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string}> */ public static function provideFix80Cases(): iterable { yield 'nullable types in constructor property promotion' => [ '<?php class Foo { public function __construct( private ?string $foo = null, protected ?string $bar = null, public ?string $xyz = null, ) { /* ternary operator to make the file a candidate for fixing */ true ? 1 : 0; } }', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFix81Cases(): iterable { yield [ <<<'PHP' <?php enum TaskType: int { public function foo(bool $value): string { return $value ? 'foo' : 'bar'; } } PHP, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NotOperatorWithSuccessorSpaceFixerTest.php
tests/Fixer/Operator/NotOperatorWithSuccessorSpaceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer> * * @author Javier Spagnoletti <phansys@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NotOperatorWithSuccessorSpaceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php $i = 0; $i++; $foo = ! false || (! true || ! ! false && (2 === (7 -5)));', '<?php $i = 0; $i++; $foo = !false || (!true || !!false && (2 === (7 -5)));', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! true);', '<?php $i = 0; $i--; $foo = !false || ($i && !true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! /* some comment */true);', '<?php $i = 0; $i--; $foo = !false || ($i && !/* some comment */true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! true);', '<?php $i = 0; $i--; $foo = !false || ($i && ! true);', ]; yield [ '<?php $i = 0; $i--; $foo = ! false || ($i && ! /* some comment */ true);', '<?php $i = 0; $i--; $foo = !false || ($i && ! /* some comment */ true);', ]; yield 'comment case' => [ '<?php $a=# ! # $b; ', '<?php $a=# ! # $b; ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/ConcatSpaceFixerTest.php
tests/Fixer/Operator/ConcatSpaceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\ConcatSpaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\ConcatSpaceFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\ConcatSpaceFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ConcatSpaceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidConfigurationCases * * @param array<string, mixed> $configuration */ public function testInvalidConfiguration(array $configuration, string $exceptionExpression): void { self::expectException(InvalidFixerConfigurationException::class); self::expectExceptionMessageMatches($exceptionExpression); $this->fixer->configure($configuration); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'missing key' => [ ['a' => 1], '#^\[concat_space\] Invalid configuration: The option "a" does not exist\. Defined options are: "spacing"\.$#', ]; yield 'invalid value' => [ ['spacing' => 'tabs'], '#^\[concat_space\] Invalid configuration: The option "spacing" with value "tabs" is invalid\. Accepted values are: "one", "none"\.$#', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input, array $configuration): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, null|string, _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php $foo = "a".\'b\'."c"."d".$e.($f + 1);', '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);', ['spacing' => 'none'], ]; yield [ '<?php $foo = 1 ."foo";', '<?php $foo = 1 . "foo";', ['spacing' => 'none'], ]; yield [ '<?php $foo = "foo". 1;', '<?php $foo = "foo" . 1;', ['spacing' => 'none'], ]; yield [ '<?php $foo = "a". "b";', '<?php $foo = "a" . "b";', ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" ."baz";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" //test ."baz";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" /* test */ ."baz";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" // ."baz";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" // ."baz"// ."cex"/**/ ."dev"/** */ ."baz" // ."cex" /**/ ."ewer23" '.' ."dev" /** */ ;', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" // ."baz" /**/ ."something";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" ."baz". // "something";', null, ['spacing' => 'none'], ]; yield [ '<?php $a = "foobar" ."baz". /** */ "something";', null, ['spacing' => 'none'], ]; yield [ "<?php \$longString = '*' .'*****' .'*****' .'*****' // Comment about next line .'*****' // Other comment .'*****'; ", "<?php \$longString = '*' . '*****' . '*****' . '*****' // Comment about next line . '*****' // Other comment . '*****'; ", ['spacing' => 'none'], ]; yield [ '<?php $a = // $c . /**/ $d # . $e /** */ . $f . // $z; ', '<?php $a = // $c . /**/ $d # . $e /** */ . $f . // $z; ', ['spacing' => 'one'], ]; yield [ '<?php $foo = "a" . \'b\' . "c" . "d" . $e . ($f + 1);', '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);', ['spacing' => 'one'], ]; yield [ '<?php $foo = "a" . "b";', '<?php $foo = "a". "b";', ['spacing' => 'one'], ]; yield [ '<?php $a = "foobar" . "baz";', '<?php $a = "foobar" ."baz";', ['spacing' => 'one'], ]; yield [ '<?php echo $a . $b; echo $d . $e . // $f; echo $a . $b?> <?php echo $c; ', '<?php echo $a.$b; echo $d . $e . // $f; echo $a . $b?> <?php echo $c; ', ['spacing' => 'one'], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/StandardizeNotEqualsFixerTest.php
tests/Fixer/Operator/StandardizeNotEqualsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\StandardizeNotEqualsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\StandardizeNotEqualsFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class StandardizeNotEqualsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield ['<?php $a = ($b != $c);']; yield ['<?php $a = ($b != $c);', '<?php $a = ($b <> $c);']; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/StandardizeIncrementFixerTest.php
tests/Fixer/Operator/StandardizeIncrementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer * @covers \PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer> * * @author ntzm * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class StandardizeIncrementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php ++$i;', '<?php $i += 1;', ]; yield [ '<?php ++$i;', '<?php $i+=1;', ]; yield [ '<?php for ($i = 0; $i < $n; ++$i) {};', '<?php for ($i = 0; $i < $n; $i += 1) {};', ]; yield [ '<?php ++$foo->bar;', '<?php $foo->bar += 1;', ]; yield [ '<?php ++$foo->$bar;', '<?php $foo->$bar += 1;', ]; yield [ '<?php ++$foo->$$$bar;', '<?php $foo->$$$bar += 1;', ]; yield [ '<?php ++$foo["bar"];', '<?php $foo["bar"] += 1;', ]; yield [ '<?php ++$foo[baz()];', '<?php $foo[baz()] += 1;', ]; yield [ '<?php ++$foo[$bar->baz];', '<?php $foo[$bar->baz] += 1;', ]; yield [ '<?php ++$foo[$bar];', '<?php $foo[$bar] += 1;', ]; yield [ '<?php ++$foo[Bar::BAZ];', '<?php $foo[Bar::BAZ] += 1;', ]; yield [ '<?php echo $foo[++$i];', '<?php echo $foo[$i += 1];', ]; yield [ '<?php echo ++$foo[$bar[$baz]];', '<?php echo $foo[$bar[$baz]] += 1;', ]; yield [ '<?php ++$$foo;', '<?php $$foo += 1;', ]; yield [ '<?php ++$$$$foo;', '<?php $$$$foo += 1;', ]; yield [ '<?php ++${$foo};', '<?php ${$foo} += 1;', ]; yield [ '<?php ++$$${$foo};', '<?php $$${$foo} += 1;', ]; yield [ '<?php ++$a[$b];', '<?php $a[$b] += 1;', ]; yield [ '<?php ++$a[++$b];', '<?php $a[$b += 1] += 1;', ]; yield [ '<?php foo(++$a);', '<?php foo($a += 1);', ]; yield [ '<?php foo(++$a, $bar);', '<?php foo($a += 1, $bar);', ]; yield [ '<?php ++$foo->{++$a};', '<?php $foo->{$a += 1} += 1;', ]; yield [ '<?php ++${++$a};', '<?php ${$a += 1} += 1;', ]; yield [ '<?php ++$i ?>', '<?php $i += 1 ?>', ]; yield [ '<?php $a = $b ? ++$c : ++$d;', '<?php $a = $b ? $c += 1 : $d += 1;', ]; yield [ '<?php ++$a->{++$b}[++$c];', '<?php $a->{$b += 1}[$c += 1] += 1;', ]; yield [ '<?php (++$i);', '<?php ($i += 1);', ]; yield [ '<?php (((++$i)));', '<?php ((($i += 1)));', ]; yield [ '<?php ++$a->b->$c;', '<?php $a->b->$c += 1;', ]; yield [ '<?php ++$i/* foo */;', '<?php $i +=/* foo */1;', ]; yield [ '<?php ++$i/* foo *//* bar */;', '<?php $i /* foo */ += /* bar */1;', ]; yield [ '<?php ++$i/** foo *//** bar */;', '<?php $i /** foo */ += /** bar */1;', ]; yield [ "<?php ++\$i// foo\n;", "<?php \$i += // foo\n1;", ]; yield [ '<?php --$i;', '<?php $i -= 1;', ]; yield [ '<?php --$i;', '<?php $i-=1;', ]; yield [ '<?php for ($i = 0; $i < $n; --$i) {};', '<?php for ($i = 0; $i < $n; $i -= 1) {};', ]; yield [ '<?php --$foo->bar;', '<?php $foo->bar -= 1;', ]; yield [ '<?php --$foo->$bar;', '<?php $foo->$bar -= 1;', ]; yield [ '<?php --$foo->$$$bar;', '<?php $foo->$$$bar -= 1;', ]; yield [ '<?php --$foo["bar"];', '<?php $foo["bar"] -= 1;', ]; yield [ '<?php --$foo[baz()];', '<?php $foo[baz()] -= 1;', ]; yield [ '<?php --$foo[$bar->baz];', '<?php $foo[$bar->baz] -= 1;', ]; yield [ '<?php --$foo[$bar];', '<?php $foo[$bar] -= 1;', ]; yield [ '<?php --$foo[Bar::BAZ];', '<?php $foo[Bar::BAZ] -= 1;', ]; yield [ '<?php echo $foo[--$i];', '<?php echo $foo[$i -= 1];', ]; yield [ '<?php echo --$foo->{$bar};', '<?php echo $foo->{$bar} -= 1;', ]; yield [ '<?php echo --$foo->{$bar->{$baz}};', '<?php echo $foo->{$bar->{$baz}} -= 1;', ]; yield [ '<?php echo --$foo[$bar[$baz]];', '<?php echo $foo[$bar[$baz]] -= 1;', ]; yield [ '<?php --$$foo;', '<?php $$foo -= 1;', ]; yield [ '<?php --$$$$foo;', '<?php $$$$foo -= 1;', ]; yield [ '<?php --${$foo};', '<?php ${$foo} -= 1;', ]; yield [ '<?php --$$${$foo};', '<?php $$${$foo} -= 1;', ]; yield [ '<?php --$a[$b];', '<?php $a[$b] -= 1;', ]; yield [ '<?php --$a[--$b];', '<?php $a[$b -= 1] -= 1;', ]; yield [ '<?php foo(--$a);', '<?php foo($a -= 1);', ]; yield [ '<?php foo(--$a, $bar);', '<?php foo($a -= 1, $bar);', ]; yield [ '<?php --$foo->{--$a};', '<?php $foo->{$a -= 1} -= 1;', ]; yield [ '<?php --${--$a};', '<?php ${$a -= 1} -= 1;', ]; yield [ '<?php --$i ?>', '<?php $i -= 1 ?>', ]; yield [ '<?php $a = $b ? --$c : --$d;', '<?php $a = $b ? $c -= 1 : $d -= 1;', ]; yield [ '<?php --$a->{--$b}[--$c];', '<?php $a->{$b -= 1}[$c -= 1] -= 1;', ]; yield [ '<?php (--$i);', '<?php ($i -= 1);', ]; yield [ '<?php (((--$i)));', '<?php ((($i -= 1)));', ]; yield [ '<?php --$a->b->$c;', '<?php $a->b->$c -= 1;', ]; yield [ '<?php --$i/* foo */;', '<?php $i -=/* foo */1;', ]; yield [ '<?php --$i/* foo *//* bar */;', '<?php $i /* foo */ -= /* bar */1;', ]; yield [ '<?php --$i/** foo *//** bar */;', '<?php $i /** foo */ -= /** bar */1;', ]; yield [ "<?php --\$i// foo\n;", "<?php \$i -= // foo\n1;", ]; yield [ '<?php $i + 1;', ]; yield [ '<?php $i - 1;', ]; yield [ '<?php $i = 1;', ]; yield [ '<?php $i = -1;', ]; yield [ '<?php $i += 1.0;', ]; yield [ '<?php $i += "1";', ]; yield [ '<?php $i -= 1.0;', ]; yield [ '<?php $i -= "1";', ]; yield [ '<?php $i += 1 * 2;', ]; yield [ '<?php $i += 1 ** 2;', ]; yield [ '<?php $i += 1 / 2;', ]; yield [ '<?php $i += 1 + 2;', ]; yield [ '<?php $i += 1 - 2;', ]; yield [ '<?php $i += 1 % 2;', ]; yield [ '<?php $i += 1 ?: 2;', ]; yield [ '<?php $i += 1 & 2;', ]; yield [ '<?php $i += 1 ^ 2;', ]; yield [ '<?php $i += 1 >> 2;', ]; yield [ '<?php $i += 1 << 2;', ]; yield [ '<?php $i += 1 && true;', ]; yield [ '<?php $i += 1 || true;', ]; yield [ '<?php $i += 1 and true;', ]; yield [ '<?php $i += 1 or true;', ]; yield [ '<?php $i += 1 xor true;', ]; yield [ '<?php $i += 1 === 2;', ]; yield [ '<?php $i += 1 == 2;', ]; yield [ '<?php $i += 1 !== 2;', ]; yield [ '<?php $i += 1 != 2;', ]; yield [ '<?php $i += 1 < 2;', ]; yield [ '<?php $i += 1 > 2;', ]; yield [ '<?php $i += 1 <= 2;', ]; yield [ '<?php $i += 1 >= 2;', ]; yield [ '<?php $i += 1 <> 2;', ]; yield [ '<?php $i -= 1 * 2;', ]; yield [ '<?php $i -= 1 ** 2;', ]; yield [ '<?php $i -= 1 / 2;', ]; yield [ '<?php $i -= 1 + 2;', ]; yield [ '<?php $i -= 1 - 2;', ]; yield [ '<?php $i -= 1 % 2;', ]; yield [ '<?php $i -= 1 ?: 2;', ]; yield [ '<?php $i -= 1 & 2;', ]; yield [ '<?php $i -= 1 ^ 2;', ]; yield [ '<?php $i -= 1 >> 2;', ]; yield [ '<?php $i -= 1 << 2;', ]; yield [ '<?php $i -= 1 && true;', ]; yield [ '<?php $i -= 1 || true;', ]; yield [ '<?php $i -= 1 and true;', ]; yield [ '<?php $i -= 1 or true;', ]; yield [ '<?php $i -= 1 xor true;', ]; yield [ '<?php $i -= 1 === 2;', ]; yield [ '<?php $i -= 1 == 2;', ]; yield [ '<?php $i -= 1 !== 2;', ]; yield [ '<?php $i -= 1 != 2;', ]; yield [ '<?php $i -= 1 < 2;', ]; yield [ '<?php $i -= 1 > 2;', ]; yield [ '<?php $i -= 1 <= 2;', ]; yield [ '<?php $i -= 1 >= 2;', ]; yield [ '<?php $i -= 1 <> 2;', ]; yield [ '<?php #1 #2 ++$i#3 #4 #5 #6 #7 ;#8 #9', '<?php #1 #2 $i#3 #4 +=#5 #6 1#7 ;#8 #9', ]; yield [ '<?php $a -= ($a -= ($a -= (--$a)));', '<?php $a -= ($a -= ($a -= ($a -= 1)));', ]; yield [ '<?php --$a[foo($d,foo($c))];', '<?php $a[foo($d,foo($c))] -= 1;', ]; yield [ '<?php $i *= 1; ++$i;', '<?php $i *= 1; $i += 1;', ]; yield [ '<?php ++A::$b;', '<?php A::$b += 1;', ]; yield [ '<?php ++\A::$b;', '<?php \A::$b += 1;', ]; yield [ '<?php ++\A\B\C::$d;', '<?php \A\B\C::$d += 1;', ]; yield [ '<?php ++$a::$b;', '<?php $a::$b += 1;', ]; yield [ '<?php ++$a::$b->$c;', '<?php $a::$b->$c += 1;', ]; yield [ '<?php class Foo { public static function bar() { ++self::$v1; ++static::$v2; } }', '<?php class Foo { public static function bar() { self::$v1 += 1; static::$v2 += 1; } }', ]; yield [ '<?php $i -= 1 ?? 2;', ]; yield [ '<?php $i += 1 ?? 2;', ]; yield [ '<?php $i -= 1 <=> 2;', ]; yield [ '<?php $i += 1 <=> 2;', ]; yield [ '<?php ++$a::$b::$c;', '<?php $a::$b::$c += 1;', ]; yield [ '<?php ++$a->$b::$c;', '<?php $a->$b::$c += 1;', ]; yield [ '<?php ++$a::${$b}::$c;', '<?php $a::${$b}::$c += 1;', ]; yield [ '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;', '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h += 1;', ]; yield [ '<?php $i += 1_0;', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php echo ++$foo->{$bar};', '<?php echo $foo->{$bar} += 1;', ]; yield [ '<?php echo ++$foo->{$bar->{$baz}};', '<?php echo $foo->{$bar->{$baz}} += 1;', ]; yield [ '<?php ++$a{$b};', '<?php $a{$b} += 1;', ]; yield [ '<?php --$a{$b};', '<?php $a{$b} -= 1;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NoUselessConcatOperatorFixerTest.php
tests/Fixer/Operator/NoUselessConcatOperatorFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NoUselessConcatOperatorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NoUselessConcatOperatorFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\NoUselessConcatOperatorFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoUselessConcatOperatorFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = ['juggle_simple_strings' => true]): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { $templateExpected = '<?php $b = %s;'; $templateInput = '<?php $b = %s.%s;'; $cases = [ 'single . single' => ["'a'", "'b'", "'ab'"], 'single . double' => ["'a'", '"b"', '"ab"'], 'double . single' => ['"b\n"', "'a'", '"b\na"'], 'double . double' => ['"b"', '"b"', '"bb"'], 'encapsed . encapsed' => ['"{$a}"', '"{$b}"', '"{$a}{$b}"'], 'encapsed space. encapsed' => ['"{$a1} "', '"{$b2}"', '"{$a1} {$b2}"'], 'encapsed . space encapsed' => ['"{$a}"', '" {$b}"', '"{$a} {$b}"'], 'encapsed space. space encapsed' => ['"{$a} "', '" {$b}"', '"{$a} {$b}"'], 'encapsed . single' => ['"{$a}"', "'Z'", '"{$a}Z"'], 'single . encapsed' => ["'Y'", '"{$a}"', '"Y{$a}"'], 'encapsed spaced. single' => ['"{$a} "', "'G'", '"{$a} G"'], 'single . space encapsed' => ["'V'", '" {$a}"', '"V {$a}"'], 'encapsed . double' => ['"{$a} "', '"XX"', '"{$a} XX"'], 'double . encapsed' => ['"XCV"', '"{$a}"', '"XCV{$a}"'], 'encapsed spaced . double' => ['"{$a} V "', '"PO"', '"{$a} V PO"'], 'double . space encapsed' => ['"DSA"', '" XX {$a}"', '"DSA XX {$a}"'], ]; foreach ($cases as $label => $case) { yield $label => [ \sprintf($templateExpected, $case[2]), \sprintf($templateInput, $case[0], $case[1]), ]; } yield 'encapsed followed by simple double quoted 1' => [ '<?php echo "Hello, {$fruit}s.";', '<?php echo \'Hello,\' . " {$fruit}s.";', ]; yield 'encapsed followed by simple double quoted 2' => [ '<?php echo "Hello.He drank some juice made of {$fruit}s.Bye $user!" /*1*//*2*/ /*3*//*4*/;', '<?php echo \'Hello.\' /*1*/ . /*2*/ "He drank some juice made of {$fruit}s."/*3*/./*4*/"Bye $user!";', ]; yield [ '<?php $string = "foobar"; echo "char @ -4 \"[$string[-2]]\"!"; ', '<?php $string = "foobar"; echo "char @ -4 \"[$string[-2]"."]\"!"; ', ]; yield 'double quote concat double quote + comment' => [ '<?php $fi = "lk" /* 1 *//* 2 */ ;', '<?php $fi = "l" /* 1 */ . /* 2 */ "k";', ]; yield 'empty concat empty' => [ '<?php $aT = "";', '<?php $aT = ""."";', ]; yield 'multiple fixes' => [ '<?php $f0 = "abc | defg";', '<?php $f0 = "a"."b"."c | "."d"."e"."f"."g";', ]; yield 'linebreak with indent inside' => [ '<?php $text1 = "intro: | |"." line 2 indent";', '<?php $text1 = "intro: |"." |"." line 2 indent"."";', ]; yield 'linebreak with indent inside + comment' => [ '<?php $text2 = "intro: "." #a line 2 indent";', '<?php $text2 = "intro: "." "." #a line 2 indent"."";', ]; yield 'do not fix' => [ '<?php $a0x = $b . "c"; $a1x = "c" . $b; $b2x = foo() . "X"; $b3x = foo() . \'Y\'; $b4x = "Z" . foo(); $b5x = \'b\' . foo(); $b6x = \'X \n \' . "\n\t"; $b7x = "\n\t" . \'X $a\'; $b7x = "abc". 1; $b7x = "def". 1.2; // bin string $f202 = b"a"."b"; $f201 = b"a".b"b"; $f203 = "a".B"b"; echo b"He drank some juice made of {$fruit}s.".b" Sliced the {$fruit}s."; ', ]; yield 'do not fix if the execution result would be different' => [ '<?php echo "abc $d" . "[$e]"; echo "abc $d" . "[3]"; echo "abc $d" . \'[3]\'; echo "abc $d" . "->e"; echo "abc $d" . \'->e\'; echo "abc $d" . "->$e"; echo "abc $d" . "?->e"; echo "abc $d" . "?->$e"; echo "abc $d" . \'?->e\'; ', ]; yield 'do not fix if variables would be broken' => [ '<?php echo "abc $d" . "e $f"; echo "abc $d" . "e"; echo "abc $d" . " e"; // contains full-width space echo "abc $d" . \'e\'; echo "abc $d" . \' e\'; // contains full-width space echo "abc $d" . "😃"; // with emoji echo "私の名前は$name" . "です"; // multibyte characters (Japanese) ', ]; yield 'fix if variables would not be broken' => [ '<?php echo "$a b"; echo "$a bcde"; echo "abc ${d}e"; echo "abc $d-efg"; echo "$a bcdef"; echo "abc ${d}ef"; echo "abc $d-efgh"; echo "abc $d-$e"; ', '<?php echo "$a" . " b"; echo "$a bc" . "de"; echo "abc ${d}" . "e"; echo "abc $d" . "-efg"; echo "$a bc" . \'def\'; echo "abc ${d}" . \'ef\'; echo "abc $d" . \'-efgh\'; echo "abc $d" . "-$e"; ', ]; yield 'single quote concat single quote but with line break after' => [ "<?php \$fh = 'x'. // some comment 'y';", ]; yield 'single quote concat single quote but with line break before' => [ "<?php \$ff = 'x' // some comment .'y';", ]; yield 'linebreak without indent inside' => [ '<?php $text3 = "intro:"." line 2 indent" ?>', ]; yield 'linebreak before concat + comment' => [ "<?php \$a = 'The first line of some block.' .'The second line' // some comment about this line .'3rd line' ; ", ]; yield 'concat without linebreak, followed by one with linebreak' => [ <<<'PHP' <?php $foo = 'ab' . 'c'; PHP, <<<'PHP' <?php $foo = 'a' . 'b' . 'c'; PHP, ]; $inputWithJuggling = '<?php $a = "x" . \'y\';'; $expectedWithJuggling = '<?php $a = "xy";'; yield [$expectedWithJuggling, $inputWithJuggling, ['juggle_simple_strings' => true]]; yield [$inputWithJuggling, null, ['juggle_simple_strings' => false]]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/ObjectOperatorWithoutWhitespaceFixerTest.php
tests/Fixer/Operator/ObjectOperatorWithoutWhitespaceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\ObjectOperatorWithoutWhitespaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\ObjectOperatorWithoutWhitespaceFixer> * * @author Farhad Safarov <farhad.safarov@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ObjectOperatorWithoutWhitespaceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $object->method();', '<?php $object ->method();', ]; yield [ '<?php $object->method();', '<?php $object -> method();', ]; yield [ '<?php $object->method();', '<?php $object-> method();', ]; yield [ '<?php $object->method();', '<?php $object ->method();', ]; yield [ '<?php $object->method();', '<?php $object-> method();', ]; yield [ '<?php $object->method();', '<?php $object -> method();', ]; yield [ '<?php echo "use it as -> you want";', ]; // Ensure that doesn't break chained multi-line statements yield [ '<?php $object->method() ->method2() ->method3();', ]; yield [ '<?php $this ->add() // Some comment ->delete();', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php $object?->method();', '<?php $object?-> method();', ]; yield [ '<?php $object?->method();', '<?php $object ?-> method();', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/IncrementStyleFixerTest.php
tests/Fixer/Operator/IncrementStyleFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Fixer\Operator\IncrementStyleFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer * @covers \PhpCsFixer\Fixer\Operator\IncrementStyleFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\IncrementStyleFixer> * * @author Gregor Harlan <gharlan@web.de> * @author Kuba Werłos <werlos@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\IncrementStyleFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class IncrementStyleFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { foreach (self::getPreStyleCases() as $preStyleCase) { yield [ $preStyleCase[0], $preStyleCase[1] ?? null, ['style' => IncrementStyleFixer::STYLE_PRE], ]; $postStyleCase = array_reverse($preStyleCase); yield [ $postStyleCase[0], $postStyleCase[1] ?? null, ['style' => IncrementStyleFixer::STYLE_POST], ]; } } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;', '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h++;', ]; yield [ '<?php ++$a{0};', '<?php $a{0}++;', ]; yield [ '<?php ++${$a}->{$b."foo"}->bar[$c]->$baz;', '<?php ${$a}->{$b."foo"}->bar[$c]->$baz++;', ]; } /** * @return iterable<int, array{0: string, 1?: string}> */ private static function getPreStyleCases(): iterable { yield [ '<?php ++$a;', '<?php $a++;', ]; yield [ '<?php ++$$a;', '<?php $$a++;', ]; yield [ '<?php ++${"a"};', '<?php ${"a"}++;', ]; yield [ '<?php --$a;', '<?php $a--;', ]; yield [ '<?php foo(); ++$a;', '<?php foo(); $a++;', ]; yield [ '<?php if (true) { ++$a; }', '<?php if (true) { $a++; }', ]; yield [ '<?php if (true) {} ++$a;', '<?php if (true) {} $a++;', ]; yield [ '<?php for ($i = 0; $i < $count; ++$i) {}', '<?php for ($i = 0; $i < $count; $i++) {}', ]; yield [ '<?php ++$a->foo;', '<?php $a->foo++;', ]; yield [ '<?php ++$a->{"foo"};', '<?php $a->{"foo"}++;', ]; yield [ '<?php ++$a->$b;', '<?php $a->$b++;', ]; yield [ '<?php ++Foo\Bar::$bar;', '<?php Foo\Bar::$bar++;', ]; yield [ '<?php ++$a::$bar;', '<?php $a::$bar++;', ]; yield [ '<?php ++$a[0];', '<?php $a[0]++;', ]; yield [ '<?php ++$a[$b];', '<?php $a[$b]++;', ]; yield ['<?php $a = $b++;']; yield ['<?php $a + $b++;']; yield ['<?php $a++ + $b;']; yield ['<?php foo($b++);']; yield ['<?php foo($a, $b++);']; yield ['<?php $a[$b++];']; yield ['<?php echo $a++;']; yield ['<?php $a = ++$b;']; yield ['<?php $a + ++$b;']; yield ['<?php ++$a + $b;']; yield ['<?php foo(++$b);']; yield ['<?php foo($a, ++$b);']; yield ['<?php $a[++$b];']; yield ['<?php echo ++$a;']; yield ['<?= ++$a;']; yield [ '<?php class Test { public function foo() { $a = 123; ++self::$st; } }', '<?php class Test { public function foo() { $a = 123; self::$st++; } }', ]; yield [ '<?php class Test { public function foo() { $a = 123; ++static::$st; } }', '<?php class Test { public function foo() { $a = 123; static::$st++; } }', ]; yield [ '<?php if ($foo) ++$a;', '<?php if ($foo) $a++;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NewWithParenthesesFixerTest.php
tests/Fixer/Operator/NewWithParenthesesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Operator; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NewWithParenthesesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield ['<?php $x = new X(foo(/**/));']; yield ['<?php $xyz = new X(new Y(new Z(/**/ foo())));']; yield ['<?php $self = new self(a);']; yield [ '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}', ]; yield [ '<?php $static = new self(new \SplFileInfo(__FILE__));', ]; yield [ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', ]; yield [ '<?php $x = new X();', '<?php $x = new X;', ]; yield [ '<?php $y = new Y() ;', '<?php $y = new Y ;', ]; yield [ '<?php $x = new Z() /**/;//', '<?php $x = new Z /**/;//', ]; yield [ '<?php $foo = new $foo();', '<?php $foo = new $foo;', ]; yield [ '<?php $bar1 = new $foo[0]->bar(); $bar2 = new $foo[0][1]->bar(); ', ]; yield [ '<?php $xyz = new X(new Y(new Z()));', '<?php $xyz = new X(new Y(new Z));', ]; yield [ '<?php $foo = (new $bar())->foo;', '<?php $foo = (new $bar)->foo;', ]; yield [ '<?php $foo = (new $bar((new Foo())->bar))->foo;', '<?php $foo = (new $bar((new Foo)->bar))->foo;', ]; yield [ '<?php $self = new self();', '<?php $self = new self;', ]; yield [ '<?php $static = new static();', '<?php $static = new static;', ]; yield [ '<?php $a = array( "key" => new DateTime(), );', '<?php $a = array( "key" => new DateTime, );', ]; yield [ '<?php $a = array( "key" => new DateTime() );', '<?php $a = array( "key" => new DateTime );', ]; yield [ '<?php $a = new $b[$c]();', '<?php $a = new $b[$c];', ]; yield [ '<?php $a = new $b[$c][0]();', '<?php $a = new $b[$c][0];', ]; yield [ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();', '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];', ]; yield [ '<?php $a = new $b[\'class\']();', '<?php $a = new $b[\'class\'];', ]; yield [ '<?php $a = new $b[\'class\'] ($foo[\'bar\']);', ]; yield [ '<?php $a = new $b[\'class\'] () ;', ]; yield [ '<?php $a = new $b[$c] ($hello[$world]) ;', ]; yield [ "<?php \$a = new \$b['class']()\r\n\t ;", "<?php \$a = new \$b['class']\r\n\t ;", ]; yield [ '<?php $a = $b ? new DateTime() : $b;', '<?php $a = $b ? new DateTime : $b;', ]; yield [ '<?php new self::$adapters[$name]["adapter"]();', '<?php new self::$adapters[$name]["adapter"];', ]; yield [ '<?php $a = new \Exception()?> <?php echo 1;', '<?php $a = new \Exception?> <?php echo 1;', ]; yield [ '<?php $b = new \StdClass() /**/?>', '<?php $b = new \StdClass /**/?>', ]; yield [ '<?php $a = new Foo() instanceof Foo;', '<?php $a = new Foo instanceof Foo;', ]; yield [ '<?php $a = new Foo() + 1; $a = new Foo() - 1; $a = new Foo() * 1; $a = new Foo() / 1; $a = new Foo() % 1; ', '<?php $a = new Foo + 1; $a = new Foo - 1; $a = new Foo * 1; $a = new Foo / 1; $a = new Foo % 1; ', ]; yield [ '<?php $a = new Foo() & 1; $a = new Foo() | 1; $a = new Foo() ^ 1; $a = new Foo() << 1; $a = new Foo() >> 1; ', '<?php $a = new Foo & 1; $a = new Foo | 1; $a = new Foo ^ 1; $a = new Foo << 1; $a = new Foo >> 1; ', ]; yield [ '<?php $a = new Foo() and 1; $a = new Foo() or 1; $a = new Foo() xor 1; $a = new Foo() && 1; $a = new Foo() || 1; ', '<?php $a = new Foo and 1; $a = new Foo or 1; $a = new Foo xor 1; $a = new Foo && 1; $a = new Foo || 1; ', ]; yield [ '<?php if (new DateTime() > $this->startDate) {} if (new DateTime() >= $this->startDate) {} if (new DateTime() < $this->startDate) {} if (new DateTime() <= $this->startDate) {} if (new DateTime() == $this->startDate) {} if (new DateTime() != $this->startDate) {} if (new DateTime() <> $this->startDate) {} if (new DateTime() === $this->startDate) {} if (new DateTime() !== $this->startDate) {} ', '<?php if (new DateTime > $this->startDate) {} if (new DateTime >= $this->startDate) {} if (new DateTime < $this->startDate) {} if (new DateTime <= $this->startDate) {} if (new DateTime == $this->startDate) {} if (new DateTime != $this->startDate) {} if (new DateTime <> $this->startDate) {} if (new DateTime === $this->startDate) {} if (new DateTime !== $this->startDate) {} ', ]; yield [ '<?php $a = new \stdClass() ? $b : $c;', '<?php $a = new \stdClass ? $b : $c;', ]; yield [ '<?php foreach (new Collection() as $x) {}', '<?php foreach (new Collection as $x) {}', ]; yield [ '<?php $a = [(string) new Foo() => 1];', '<?php $a = [(string) new Foo => 1];', ]; yield [ '<?php $a = [ "key" => new DateTime(), ];', '<?php $a = [ "key" => new DateTime, ];', ]; yield [ '<?php $a = [ "key" => new DateTime() ];', '<?php $a = [ "key" => new DateTime ];', ]; yield [ '<?php $a = new Foo() ** 1; ', '<?php $a = new Foo ** 1; ', ]; yield [ '<?php $a = new Foo() <=> 1; ', '<?php $a = new Foo <=> 1; ', ]; yield [ "<?php \$a = new \$b['class']/* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class'] /* */()\r\n\t ;", ]; yield [ "<?php \$a = new \$b['class']()/* */;", "<?php \$a = new \$b['class']/* */;", ]; yield [ "<?php \$a = new \$b['class']() /* */;", "<?php \$a = new \$b['class'] /* */;", ]; yield [ '<?php $x = new X(foo(/**/));', null, ['named_class' => false], ]; yield [ '<?php $xyz = new X(new Y(new Z(/**/ foo())));', null, ['named_class' => false], ]; yield [ '<?php $self = new self(a);', null, ['named_class' => false], ]; yield [ '<?php $bar1 = new $foo->bar["baz"];', '<?php $bar1 = new $foo->bar["baz"]();', ['named_class' => false], ]; yield [ '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}', null, ['named_class' => false], ]; yield [ '<?php $static = new self(new \SplFileInfo(__FILE__));', null, ['named_class' => false], ]; yield [ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ /**/ /**/ /**//**//**/ /**//**//**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/', ['named_class' => false], ]; yield [ '<?php $x = new X;', '<?php $x = new X();', ['named_class' => false], ]; yield [ '<?php $y = new Y ;', '<?php $y = new Y() ;', ['named_class' => false], ]; yield [ '<?php $x = new Z /**/;//', '<?php $x = new Z() /**/;//', ['named_class' => false], ]; yield [ '<?php $foo = new $foo;', '<?php $foo = new $foo();', ['named_class' => false], ]; yield [ '<?php $xyz = new X(new Y(new Z));', '<?php $xyz = new X(new Y(new Z()));', ['named_class' => false], ]; yield [ '<?php $foo = (new $bar)->foo;', '<?php $foo = (new $bar())->foo;', ['named_class' => false], ]; yield [ '<?php $foo = (new $bar((new Foo)->bar))->foo;', '<?php $foo = (new $bar((new Foo())->bar))->foo;', ['named_class' => false], ]; yield [ '<?php $self = new self;', '<?php $self = new self();', ['named_class' => false], ]; yield [ '<?php $static = new static;', '<?php $static = new static();', ['named_class' => false], ]; yield [ '<?php $a = array( "key" => new DateTime, );', '<?php $a = array( "key" => new DateTime(), );', ['named_class' => false], ]; yield [ '<?php $a = array( "key" => new DateTime );', '<?php $a = array( "key" => new DateTime() );', ['named_class' => false], ]; yield [ '<?php $a = new $b[$c];', '<?php $a = new $b[$c]();', ['named_class' => false], ]; yield [ '<?php $a = new $b[$c][0];', '<?php $a = new $b[$c][0]();', ['named_class' => false], ]; yield [ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];', '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();', ['named_class' => false], ]; yield [ '<?php $a = new $b[\'class\'];', '<?php $a = new $b[\'class\']();', ['named_class' => false], ]; yield [ '<?php $a = new $b[\'class\'] ($foo[\'bar\']);', null, ['named_class' => false], ]; yield [ '<?php $a = new $b[\'class\'] ;', '<?php $a = new $b[\'class\'] () ;', ['named_class' => false], ]; yield [ '<?php $a = new $b[$c] ($hello[$world]) ;', null, ['named_class' => false], ]; yield [ "<?php \$a = new \$b['class']\r\n\t ;", "<?php \$a = new \$b['class']()\r\n\t ;", ['named_class' => false], ]; yield [ '<?php $a = $b ? new DateTime : $b;', '<?php $a = $b ? new DateTime() : $b;', ['named_class' => false], ]; yield [ '<?php new self::$adapters[$name]["adapter"];', '<?php new self::$adapters[$name]["adapter"]();', ['named_class' => false], ]; yield [ '<?php $a = new \Exception?> <?php echo 1;', '<?php $a = new \Exception()?> <?php echo 1;', ['named_class' => false], ]; yield [ '<?php $b = new \StdClass /**/?>', '<?php $b = new \StdClass() /**/?>', ['named_class' => false], ]; yield [ '<?php $a = new Foo instanceof Foo;', '<?php $a = new Foo() instanceof Foo;', ['named_class' => false], ]; yield [ '<?php $a = new Foo + 1; $a = new Foo - 1; $a = new Foo * 1; $a = new Foo / 1; $a = new Foo % 1; ', '<?php $a = new Foo() + 1; $a = new Foo() - 1; $a = new Foo() * 1; $a = new Foo() / 1; $a = new Foo() % 1; ', ['named_class' => false], ]; yield [ '<?php $a = new Foo & 1; $a = new Foo | 1; $a = new Foo ^ 1; $a = new Foo << 1; $a = new Foo >> 1; ', '<?php $a = new Foo() & 1; $a = new Foo() | 1; $a = new Foo() ^ 1; $a = new Foo() << 1; $a = new Foo() >> 1; ', ['named_class' => false], ]; yield [ '<?php $a = new Foo and 1; $a = new Foo or 1; $a = new Foo xor 1; $a = new Foo && 1; $a = new Foo || 1; ', '<?php $a = new Foo() and 1; $a = new Foo() or 1; $a = new Foo() xor 1; $a = new Foo() && 1; $a = new Foo() || 1; ', ['named_class' => false], ]; yield [ '<?php if (new DateTime > $this->startDate) {} if (new DateTime >= $this->startDate) {} if (new DateTime < $this->startDate) {} if (new DateTime <= $this->startDate) {} if (new DateTime == $this->startDate) {} if (new DateTime != $this->startDate) {} if (new DateTime <> $this->startDate) {} if (new DateTime === $this->startDate) {} if (new DateTime !== $this->startDate) {} ', '<?php if (new DateTime() > $this->startDate) {} if (new DateTime() >= $this->startDate) {} if (new DateTime() < $this->startDate) {} if (new DateTime() <= $this->startDate) {} if (new DateTime() == $this->startDate) {} if (new DateTime() != $this->startDate) {} if (new DateTime() <> $this->startDate) {} if (new DateTime() === $this->startDate) {} if (new DateTime() !== $this->startDate) {} ', ['named_class' => false], ]; yield [ '<?php $a = new \stdClass ? $b : $c;', '<?php $a = new \stdClass() ? $b : $c;', ['named_class' => false], ]; yield [ '<?php foreach (new Collection as $x) {}', '<?php foreach (new Collection() as $x) {}', ['named_class' => false], ]; yield [ '<?php $a = [(string) new Foo => 1];', '<?php $a = [(string) new Foo() => 1];', ['named_class' => false], ]; yield [ '<?php $a = [ "key" => new DateTime, ];', '<?php $a = [ "key" => new DateTime(), ];', ['named_class' => false], ]; yield [ '<?php $a = [ "key" => new DateTime ];', '<?php $a = [ "key" => new DateTime() ];', ['named_class' => false], ]; yield [ '<?php $a = new Foo ** 1; ', '<?php $a = new Foo() ** 1; ', ['named_class' => false], ]; yield [ '<?php $a = new Foo <=> 1; ', '<?php $a = new Foo() <=> 1; ', ['named_class' => false], ]; yield [ "<?php \$a = new \$b['class']/* */\r\n\t ;", "<?php \$a = new \$b['class']/* */()\r\n\t ;", ['named_class' => false], ]; yield [ "<?php \$a = new \$b['class'] /* */\r\n\t ;", "<?php \$a = new \$b['class'] /* */()\r\n\t ;", ['named_class' => false], ]; yield [ "<?php \$a = new \$b['class']/* */;", "<?php \$a = new \$b['class']()/* */;", ['named_class' => false], ]; yield [ "<?php \$a = new \$b['class'] /* */;", "<?php \$a = new \$b['class']() /* */;", ['named_class' => false], ]; yield ['<?php $a = new class($a) {use SomeTrait;};']; yield ['<?php $a = new class(foo(/**/)) implements Foo{};']; yield ['<?php $a = new class($c["d"]) /**/ extends Bar1{};']; yield ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};']; yield ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};']; yield ['<?php $a = new class( $h /**/) {}?>']; yield [ '<?php $a = new Foo() <=> 1; ', '<?php $a = new Foo <=> 1; ', ]; yield [ '<?php $a = new class() {use SomeTrait;}; $a = new class() implements Foo{}; $a = new class() /**/ extends Bar1{}; $a = new class() extends Bar2 implements Foo{}; $a = new class() extends Bar3 implements Foo, Foo2{}; $a = new class() {}?> ', '<?php $a = new class {use SomeTrait;}; $a = new class implements Foo{}; $a = new class /**/ extends Bar1{}; $a = new class extends Bar2 implements Foo{}; $a = new class extends Bar3 implements Foo, Foo2{}; $a = new class {}?> ', ]; yield [ '<?php class A { public function B() { $static = new static(new class(){}); } } ', '<?php class A { public function B() { $static = new static(new class{}); } } ', ]; yield [ '<?php $a = new class($a) {use SomeTrait;};', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class(foo(/**/)) implements Foo{};', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class($c["d"]) /**/ extends Bar1{};', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class($e->f ) extends Bar2 implements Foo{};', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class( $h /**/) {}?>', null, ['anonymous_class' => false], ]; yield [ '<?php $a = new class {use SomeTrait;}; $a = new class implements Foo{}; $a = new class /**/ extends Bar1{}; $a = new class extends Bar2 implements Foo{}; $a = new class extends Bar3 implements Foo, Foo2{}; $a = new class {}?> ', '<?php $a = new class() {use SomeTrait;}; $a = new class() implements Foo{}; $a = new class() /**/ extends Bar1{}; $a = new class() extends Bar2 implements Foo{}; $a = new class() extends Bar3 implements Foo, Foo2{}; $a = new class ( ) {}?> ', ['anonymous_class' => false], ]; yield [ '<?php class A { public function B() { $static = new static(new class{}); } } ', '<?php class A { public function B() { $static = new static(new class(){}); } } ', ['anonymous_class' => false], ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php $a = new $b{$c}();', '<?php $a = new $b{$c};', ]; yield [ '<?php $a = new $b{$c}{0}{1}() ?>', '<?php $a = new $b{$c}{0}{1} ?>', ]; yield [ '<?php $a = new $b{$c}[1]{0}[2]();', '<?php $a = new $b{$c}[1]{0}[2];', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php $a = new (foo());', ]; yield [ '<?php class Bar { public function __construct(int $a = null) { echo $a; } }; $foo = "B"; $a = new ($foo."ar");', ]; yield [ '<?php $bar1 = new $foo[0]?->bar(); $bar2 = new $foo[0][1]?->bar(); ', ]; yield [ '<?php $a = new #[Internal] class(){}; ', '<?php $a = new #[Internal] class{}; ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php function test( $foo = new A(), $baz = new C(x: 2), ) { } class Test { public function __construct( public $prop = new Foo(), ) {} } static $x = new Foo(); const C = new Foo(); function test2($param = new Foo()) {} ', '<?php function test( $foo = new A, $baz = new C(x: 2), ) { } class Test { public function __construct( public $prop = new Foo, ) {} } static $x = new Foo; const C = new Foo; function test2($param = new Foo) {} ', ]; } /** * @dataProvider provideFix84Cases * * @param _AutogeneratedInputConfiguration $configuration * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix84Cases(): iterable { yield [ '<?php class A {public function test(): void {}} new A()->test(); (new A())->test(); ', '<?php class A {public function test(): void {}} new A()->test(); (new A)->test(); ', ]; yield [ '<?php class A {public function test(): void {}} new A()->test(); (new A)->test(); ', '<?php class A {public function test(): void {}} new A()->test(); (new A())->test(); ', ['anonymous_class' => false, 'named_class' => false], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Internal/ConfigurableFixerTemplateFixerTest.php
tests/Fixer/Internal/ConfigurableFixerTemplateFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Internal; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Internal\ConfigurableFixerTemplateFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Internal\ConfigurableFixerTemplateFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @requires OS Linux|Darwin * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ConfigurableFixerTemplateFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(): void { self::markTestIncomplete('Tests not implemented for this class, run the rule on codebase and check if PHPStan accepts the changes.'); } /** * @return iterable<int, array{}> */ public static function provideFixCases(): iterable { yield []; // no tests implemented } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/AbstractNullableTypeDeclarationFixerTestCase.php
tests/Fixer/Whitespace/AbstractNullableTypeDeclarationFixerTestCase.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\AbstractFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @template TFixer of AbstractFixer * * @internal * * @extends AbstractFixerTestCase<TFixer> * * @author Jack Cherng <jfcherng@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ abstract class AbstractNullableTypeDeclarationFixerTestCase extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $a instanceof static ? \DateTime::class : $c;', ]; yield [ '<?php function foo(?int $param): ?int {}', ]; yield [ '<?php function foo(? /* foo */ int $param): ? /* foo */ int {}', ]; yield [ '<?php function foo(? /** foo */ int $param): ? /** foo */ int {}', ]; yield [ '<?php function foo(? // foo int $param): ? // foo int {}', ]; yield [ '<?php function foo(/**? int*/$param): ?int {}', '<?php function foo(/**? int*/$param): ? int {}', ]; yield [ '<?php function foo(?callable $param): ?callable {}', '<?php function foo(? callable $param): ? callable {}', ]; yield [ '<?php function foo(?array &$param): ?array {}', '<?php function foo(? array &$param): ? array {}', ]; yield [ '<?php function foo(?Bar $param): ?Bar {}', '<?php function foo(? Bar $param): ? Bar {}', ]; yield [ '<?php function foo(?\Bar $param): ?\Bar {}', '<?php function foo(? \Bar $param): ? \Bar {}', ]; yield [ '<?php function foo(?Bar\Baz $param): ?Bar\Baz {}', '<?php function foo(? Bar\Baz $param): ? Bar\Baz {}', ]; yield [ '<?php function foo(?Bar\Baz &$param): ?Bar\Baz {}', '<?php function foo(? Bar\Baz &$param): ? Bar\Baz {}', ]; yield [ '<?php $foo = function(?Bar\Baz $param): ?Bar\Baz {};', '<?php $foo = function(? Bar\Baz $param): ? Bar\Baz {};', ]; yield [ '<?php $foo = function(?Bar\Baz &$param): ?Bar\Baz {};', '<?php $foo = function(? Bar\Baz &$param): ? Bar\Baz {};', ]; yield [ '<?php class Test { public function foo(?Bar\Baz $param): ?Bar\Baz {} }', '<?php class Test { public function foo(? Bar\Baz $param): ? Bar\Baz {} }', ]; yield [ '<?php abstract class Test { abstract public function foo(?Bar\Baz $param); }', '<?php abstract class Test { abstract public function foo(? Bar\Baz $param); }', ]; yield [ '<?php $foo = function(?array $a, ?array $b): ?Bar\Baz {};', '<?php $foo = function(? array $a, ? array $b): ? Bar\Baz {};', ]; yield [ '<?php function foo(?array ...$param): ?array {}', '<?php function foo(? array ...$param): ? array {}', ]; yield [ '<?php class Foo { private ?string $foo; }', '<?php class Foo { private ? string $foo; }', ]; yield [ '<?php class Foo { protected ?string $foo; }', '<?php class Foo { protected ? string $foo; }', ]; yield [ '<?php class Foo { public ?string $foo; }', '<?php class Foo { public ? string $foo; }', ]; yield [ '<?php class Foo { var ?Foo\Bar $foo; }', '<?php class Foo { var ? Foo\Bar $foo; }', ]; yield [ '<?php $foo = fn(?Bar\Baz $param): ?Bar\Baz => null;', '<?php $foo = fn(? Bar\Baz $param): ? Bar\Baz => null;', ]; yield [ '<?php $foo = fn(?Bar\Baz &$param): ?Bar\Baz => null;', '<?php $foo = fn(? Bar\Baz &$param): ? Bar\Baz => null;', ]; yield [ '<?php $foo = fn(?array $a, ?array $b): ?Bar\Baz => null;', '<?php $foo = fn(? array $a, ? array $b): ? Bar\Baz => null;', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix80Cases(): iterable { yield 'static return' => [ "<?php\nclass Foo { public function bar(): ?static {} }\n", "<?php\nclass Foo { public function bar(): ? static {} }\n", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/NoExtraBlankLinesFixerTest.php
tests/Fixer/Whitespace/NoExtraBlankLinesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoExtraBlankLinesFixerTest extends AbstractFixerTestCase { public function testInvalidConfiguration(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_extra_blank_lines\] Invalid configuration: The option "tokens" .*\.$/'); $this->fixer->configure(['tokens' => ['__TEST__']]); // @phpstan-ignore-line } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { $code = <<<'PHP' <?php use \DateTime; use \stdClass; use \InvalidArgumentException; class Test { public function testThrow($a) { if ($a) { throw new InvalidArgumentException('test.'); // test } $date = new DateTime(); $class = new stdClass(); $class = (string) $class; $e = new InvalidArgumentException($class.$date->format('Y')); throw $e; } public function testBreak($a) { switch($a) { case 1: echo $a; break; case 2: echo 'test'; break; } } protected static function testContinueAndReturn($a, $b) { while($a < 100) { if ($b < time()) { continue; } return $b; } return $a; } private function test(){ // comment } private function test123(){ // comment } } PHP; /** * @var list<int> $lineNumberRemoved Line numbers expected to be removed after fixing * @var _AutogeneratedInputConfiguration['tokens'] $configTokens */ foreach (self::getLineNumberRemovedAndConfigTokens() as [$lineNumberRemoved, $configTokens]) { yield [ self::removeLinesFromString($code, $lineNumberRemoved), $code, ['tokens' => $configTokens], ]; } yield [ <<<'PHP' <?php $a = new Bar(); $a = new FooBaz(); PHP, <<<'PHP' <?php $a = new Bar(); $a = new FooBaz(); PHP, ]; yield 'many empty lines' => [ <<<'PHP' <?php $a = new Bar(); $a = new FooBaz(); PHP, <<<'PHP' <?php $a = new Bar(); $a = new FooBaz(); PHP, ]; yield 'heredoc' => [ ' <?php $b = <<<TEXT Foo TEXT Bar FooFoo TEXT; ', ]; yield 'nowdoc' => [ ' <?php $b = <<<\'TEXT\' Foo TEXT; Bar1} FooFoo TEXT; ', ]; yield 'encapsulated nowdoc' => [ ' <?php $b = <<<\'TEXT\' Foo TEXT Bar <<<\'TEMPLATE\' BarFooBar TEMPLATE TEMPLATE; FooFoo TEXT; ', ]; yield 'multiline string' => [ <<<'PHP' <?php $a = 'Foo Bar'; PHP, ]; yield 'tricky multiline strings' => [ <<<'PHP' <?php $a = 'Foo'; $b = 'Bar Here\'s an escaped quote ' . ' FooFoo'; PHP, <<<'PHP' <?php $a = 'Foo'; $b = 'Bar Here\'s an escaped quote ' . ' FooFoo'; PHP, ]; yield 'comment with quote' => [ <<<'PHP' <?php $a = 'foo'; // my comment's must have a quote $b = 'foobar'; $c = 'bar'; PHP, <<<'PHP' <?php $a = 'foo'; // my comment's must have a quote $b = 'foobar'; $c = 'bar'; PHP, ]; yield 'trailing inline block' => [ " <?php echo 'hello'; ?> \$a = 0; //a <?php \$a = 0; \$b = 1; //a ?> ", ]; yield [ <<<'PHP' <?php //class Test $a; // $b; /***/ $c; // $d; PHP, <<<'PHP' <?php //class Test $a; // $b; /***/ $c; // $d; PHP, ]; yield [ "<?php\n//a\n\n\$a =1;", "<?php\n//a\n\n\n\n\$a =1;", ]; $input = '<?php // $a = 1; $b = 1; '; $expected = '<?php // $a = 1; $b = 1; '; yield [ "<?php\r\n//a\r\n\r\n\$a =1;", "<?php\r\n//a\r\n\r\n\r\n\r\n\$a =1;", ]; yield [ $expected, $input, ]; yield [ str_replace("\n", "\r\n", $expected), str_replace("\n", "\r\n", $input), ]; yield [ str_replace("\n", "\r", $input), ]; yield [ str_replace("\n", "\r", $expected), ]; foreach (self::getUseCases() as $case) { yield [ $case[0], $case[1] ?? null, ['tokens' => ['use']], ]; } foreach (self::getOneOrInLineCases() as $case) { yield [ $case[0], $case[1] ?? null, ['tokens' => [ 'break', 'continue', 'return', 'throw', 'curly_brace_block', 'square_brace_block', 'parenthesis_brace_block', ]], ]; } yield [ "<?php function test()\n\n{}\n\necho 789;", null, ['tokens' => ['curly_brace_block']], ]; yield [ "<?php switch(\$a){\ncase 1:echo 789;}", "<?php switch(\$a){\n \ncase 1:echo 789;}", ['tokens' => ['curly_brace_block']], ]; yield [ '<?php is_int( 1); function test( $a, $b, $c ) { }', '<?php is_int( 1); function test( $a, $b, $c ) { }', ['tokens' => ['parenthesis_brace_block']], ]; yield [ "<?php array(\n1,\n2,\n3,\n);", "<?php array(\n \n1,\n2,\n3,\n\n\n);", ['tokens' => ['parenthesis_brace_block']], ]; yield [ '<?php function a() { $b->d(e( )); foreach ($a as $x) { } }', null, ['tokens' => ['parenthesis_brace_block']], ]; yield [ '<?php class Foo { public function bar() {return 1;} public function baz() {return 2; } }', '<?php class Foo { public function bar() {return 1;} public function baz() {return 2; } }', ['tokens' => ['return']], ]; yield [ "<?php \$c = \$b[0];\n\n\n\$a = [\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n", "<?php \$c = \$b[0];\n\n\n\$a = [\n\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n", ['tokens' => ['square_brace_block']], ]; foreach (self::getInSwitchStatementCases() as [$configTokens, $expectedCode, $inputCode]) { yield [ $expectedCode, $inputCode, ['tokens' => $configTokens], ]; } yield 'empty lines after open tag' => [ '<?php class Foo {}', '<?php class Foo {}', ]; yield 'comma' => [ <<<'PHP' <?php foo( 0, 'arg1' ); return [ 'a', 'b', 'c' ]; PHP, <<<'PHP' <?php foo( 0, 'arg1' ); return [ 'a', 'b', 'c' ]; PHP, ['tokens' => ['comma']], ]; yield 'comma - do not change outside of element' => [ <<<'PHP' <?php $foo = [1, 2]; $bar = array(3, 4); [$x, $y] = getXY(); baz(); PHP, null, ['tokens' => ['comma']], ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: string}> */ public static function provideFixPre80Cases(): iterable { yield [ "<?php\n\n\$a = \$b{0};\n\n", ]; } /** * @param _AutogeneratedInputConfiguration $config * * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(array $config, string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->fixer->configure($config); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: _AutogeneratedInputConfiguration, 1?: string, 2?: string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ [], "<?php\r\nuse AAA;\r\n\r\nuse BBB;\r\n\r\n", "<?php\r\nuse AAA;\r\n\r\n\r\n\r\nuse BBB;\r\n\r\n", ]; yield [ ['tokens' => ['parenthesis_brace_block']], "<?php is_int(\r\n1);", "<?php is_int(\r\n\r\n\r\n\r\n1);", ]; yield [ ['tokens' => ['square_brace_block']], "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n", "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n", ]; yield [ ['tokens' => ['square_brace_block']], "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\t1,\r\n2];", "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n\t1,\r\n2];", ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input, array $configuration): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, null|string, _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield [ '<?php $nullCoalescingOperator1 = $bar ?? throw new \Exception(); $nullCoalescingOperator2 = $bar ?? throw new \Exception(); $nullCoalescingOperator3 = $bar ?? throw new \Exception(); $ternaryOperator1 = $bar ? 42 : throw new \Exception(); $ternaryOperator2 = $bar ? 42 : throw new \Exception(); $ternaryOperator3 = $bar ? 42 : throw new \Exception(); $orOperator1 = $bar || throw new \Exception(); $orOperator2 = $bar || throw new \Exception(); $orOperator3 = $bar || throw new \Exception(); ', null, ['tokens' => ['throw']], ]; yield [ '<?php $a = $bar ?? throw new \Exception(); // Now, we are going to use it! var_dump($a); ', null, ['tokens' => ['throw']], ]; yield [ '<?php #[Attr] #[AttrFoo1] #[AttrFoo2] function foo(){} ', '<?php #[Attr] #[AttrFoo1] #[AttrFoo2] function foo(){} ', ['tokens' => ['attribute']], ]; yield [ '<?php class Foo { protected function f1(string $x): string { return "r1"; } protected function f2(string $x): string { return "r1"; } protected function f3(#[Attr] string $x): string { return "r1"; } protected function f4(string $x): string { return "r1"; } }', null, ['tokens' => ['attribute']], ]; yield [ '<?php abstract class Foo { abstract protected function f1(string $x): string; abstract protected function f2(string $x): string; abstract protected function f3(#[Attr] string $x): string; abstract protected function f4(string $x): string; }', null, ['tokens' => ['attribute']], ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->fixer->configure(['tokens' => ['case']]); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php enum test { case Baz; public function foo() { switch (bar()) { case 1: echo 1; break; case 2: echo 2; break; } } case Bad; } ', '<?php enum test { case Baz; public function foo() { switch (bar()) { case 1: echo 1; break; case 2: echo 2; break; } } case Bad; } ', ]; $expectedTemplate = '<?php enum Foo { case CASE_1; %s }'; $enumAttributes = [ 'case CASE_2;', 'const CONST_1 = self::CASE_1;', 'private const CONST_1 = self::CASE_1;', 'public function bar(): void {}', 'protected function bar(): void {}', 'private function bar(): void {}', 'static function bar(): void {}', 'final function bar(): void {}', ]; foreach ($enumAttributes as $enumAttribute) { yield [ \sprintf($expectedTemplate, $enumAttribute), ]; } } /** * @dataProvider provideFixDeprecatedCases * * @group legacy */ public function testFixDeprecated(string $expected, string $input): void { $this->expectDeprecation('Option "tokens: use_trait" used in `no_extra_blank_lines` rule is deprecated, use the rule `class_attributes_separation` with `elements: trait_import` instead.'); $this->fixer->configure(['tokens' => ['use_trait']]); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixDeprecatedCases(): iterable { yield [ '<?php class Foo { use Z; // 123 use Bar;/* */use Baz; public function baz() {} use Bar1; use Baz1; public function baz1() {} } ', '<?php class Foo { use Z; // 123 use Bar;/* */use Baz; public function baz() {} use Bar1; use Baz1; public function baz1() {} } ', ]; yield [ '<?php class Foo { use Bar;use Baz; use Bar1;use Baz1; public function baz() {} } ', '<?php class Foo { use Bar;use Baz; use Bar1;use Baz1; public function baz() {} } ', ]; yield [ '<?php namespace T\A; use V; use W; class Test { use A; use B; private function test($b) { $a = function() use ($b) { echo $b;}; $b = function() use ($b) { echo $b;}; } }', '<?php namespace T\A; use V; use W; class Test { use A; use B; private function test($b) { $a = function() use ($b) { echo $b;}; $b = function() use ($b) { echo $b;}; } }', ]; } /** * @return iterable<array{list<int>, list<string>}> */ private static function getLineNumberRemovedAndConfigTokens(): iterable { $tests = [ [ [9, 14, 21, 43, 45, 49, 53, 57], ['curly_brace_block'], ], [ [3, 5], ['use'], ], [ [23, 24], ['extra'], ], [ [49, 53], ['return'], ], [ [45], ['continue'], ], [ [32], ['break'], ], [ [14, 21], ['throw'], ], ]; yield from $tests; $all = [[], []]; foreach ($tests as $test) { $all[0] = array_merge($test[0], $all[0]); $all[1] = array_merge($test[1], $all[1]); } yield $all; } /** * @return iterable<array{0: string, 1?: string}> */ private static function getUseCases(): iterable { yield ['<?php use A\B;']; yield ['<?php use A\B?>']; yield ['<?php use A\B;use A\D; return 1;']; yield ["<?php use A\\B?>\n\n<?php use D\\E\\F?>"]; yield ['<?php use Y\B;use A\D; return 1;']; yield [ '<?php use A\B; use A\C;', '<?php use A\B; use A\C;', ]; yield [ '<?php use A\E;use A\Z; use C; return 1; ', '<?php use A\E;use A\Z; use C; return 1; ', ]; yield [ '<?php class Test { use A; use B; }', ]; yield [ '<?php $example = function () use ($message) { var_dump($message); }; $example = function () use ($message) { var_dump($message); }; ', ]; yield [ '<?php use function A; use function B; echo 1;', ]; yield [ '<?php use some\a\{ClassA, ClassB, ClassC as C,}; use function some\a\{fn_a, fn_b, fn_c,}; use const some\a\{ConstA,ConstB,ConstC , }; use const some\Z\{ConstX,ConstY,ConstZ,}; ', '<?php use some\a\{ClassA, ClassB, ClassC as C,}; use function some\a\{fn_a, fn_b, fn_c,}; use const some\a\{ConstA,ConstB,ConstC , }; '.' use const some\Z\{ConstX,ConstY,ConstZ,}; ', ]; yield [ <<<'PHP' <?php use Zxy\Qux; use Zoo\Bar as Bar2; use Foo\Bar as Bar1; use Foo\Zar\Baz; $c = 1; use Foo\Quxx as Quxx1; use Foo\Zar\Quxx; $a = new Bar1(); $a = new Bar2(); $a = new Baz(); $a = new Qux(); PHP, <<<'PHP' <?php use Zxy\Qux; use Zoo\Bar as Bar2; use Foo\Bar as Bar1; use Foo\Zar\Baz; $c = 1; use Foo\Quxx as Quxx1; use Foo\Zar\Quxx; $a = new Bar1(); $a = new Bar2(); $a = new Baz(); $a = new Qux(); PHP, ]; yield [ '<?php use some\a\{ClassA, ClassB, ClassC as C}; use function some\a\{fn_a, fn_b, fn_c}; use const some\a\{ConstA, ConstB, ConstC}; ', '<?php use some\a\{ClassA, ClassB, ClassC as C}; use function some\a\{fn_a, fn_b, fn_c}; use const some\a\{ConstA, ConstB, ConstC}; ', ]; yield [ '<?php $c = 1; $a = new Baz(); $a = new Qux();', ]; yield [ '<?php use A\B;?>', ]; } /** * @return iterable<array{0: string, 1?: string}> */ private static function getOneOrInLineCases(): iterable { yield [ "<?php\n\n\$a = function() use (\$b) { while(3<1)break; \$c = \$b[1]; while(\$b<1)continue; if (true) throw \$e; return 1; };\n\n", ]; yield [ "<?php throw new \\Exception('do not import.');\n", "<?php throw new \\Exception('do not import.');\n\n", ]; yield [ "<?php\n\n\$a = \$b[0];\n\n", ]; yield [ "<?php\n\n\$a->{'Test'};\nfunction test(){}\n", ]; yield [ "<?php\n\n\$a = new class { public function a () { while(4<1)break; while(3<1)continue; if (true) throw \$e; return 1; }};\n\n", ]; } /** * @return iterable<array{_AutogeneratedInputConfiguration['tokens'], string, string}> */ private static function getInSwitchStatementCases(): iterable { yield [ [ 'break', 'continue', 'extra', 'return', 'throw', ], '<?php /** a */ switch ($a) { case 1: break; case 2: continue; case 3: return 1; case 4: throw $e; case 5: throw new \Exception(); case Token::STRING_TYPE: echo 123; return new ConstantNode($token->getValue()); case 7: return new ConstantNode($token->getValue()); case 8: return 8; default: echo 1; }', '<?php /** a */ switch ($a) { case 1: break; case 2: continue; case 3: return 1; case 4: throw $e; case 5: throw new \Exception(); case Token::STRING_TYPE: echo 123; return new ConstantNode($token->getValue()); case 7: return new ConstantNode($token->getValue()); '.' case 8: return 8; '.' default: echo 1; }', ]; yield [ [ 'switch', 'case', 'default', ], '<?php switch($a) { case 0: case 1: default: return 1; }', '<?php switch($a) { case 0: case 1: default: return 1; }', ]; yield [ [ 'switch', 'case', 'default', ], '<?php switch($a) { case 2: echo 3; default: return 1;} // above stays empty', '<?php switch($a) { case 2: echo 3; default: return 1;} // above stays empty', ]; } /** * @param list<int> $lineNumbers */ private static function removeLinesFromString(string $input, array $lineNumbers): string { sort($lineNumbers); $lines = explode("\n", $input); foreach ($lineNumbers as $lineNumber) { --$lineNumber; unset($lines[$lineNumber]); } return implode("\n", $lines); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/HeredocIndentationFixerTest.php
tests/Fixer/Whitespace/HeredocIndentationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer> * * @author Gregor Harlan * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class HeredocIndentationFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ <<<'EXPECTED' <?php foo(<<<EOD EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD abc def EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD abc def EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<'EOD' abc def EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<'EOD' abc def EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<'EOD' abc def EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<'EOD' abc def EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD $abc $def {$ghi} EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD $abc $def {$ghi} EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php $a = <<<'EOD' <?php $b = <<<FOO abc FOO; EOD; EXPECTED, <<<'INPUT' <?php $a = <<<'EOD' <?php $b = <<<FOO abc FOO; EOD; INPUT, ]; yield [ /* EXPECTED */ ' <?php foo(<<<EOD '.' abc '.' def '.' EOD );', /* INPUT */ ' <?php foo(<<<EOD '.' abc '.' def '.' EOD );', ]; yield [ /* EXPECTED */ ' <?php foo(<<<EOD abc def EOD );', /* INPUT */ ' <?php foo(<<<EOD '.' abc '.' def '.' EOD );', ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD EOD ); INPUT, ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD abc def EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD abc def EOD ); INPUT, ['indentation' => 'same_as_start'], ]; yield [ <<<'EXPECTED' <?php foo(<<<EOD abc def EOD ); EXPECTED, <<<'INPUT' <?php foo(<<<EOD abc def EOD ); INPUT, ['indentation' => 'same_as_start'], ]; } public function testWithWhitespacesConfig(): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t")); $expected = <<<EXPECTED <?php \t\$a = <<<'EOD' \t\tabc \t\t def \t\t\tghi \t\tEOD; EXPECTED; $input = <<<INPUT <?php \t\$a = <<<'EOD' abc def \tghi EOD; INPUT; $this->doTest($expected, $input); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php
tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * @author Andreas Möller <am@localheinz.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class BlankLineBeforeStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidConfigurationCases * * @param mixed $controlStatement */ public function testInvalidConfiguration($controlStatement): void { $this->expectException(InvalidFixerConfigurationException::class); $this->fixer->configure([ 'statements' => [$controlStatement], ]); } /** * @return iterable<string, array{mixed}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'null' => [null]; yield 'false' => [false]; yield 'true' => [true]; yield 'int' => [0]; yield 'float' => [3.14]; yield 'array' => [[]]; yield 'object' => [new \stdClass()]; yield 'unknown' => ['foo']; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void { $this->fixer->configure($configuration); $this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig()); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}> */ public static function provideFixCases(): iterable { yield [ '<?php if ($a) { /* 1 */ /* 2 */ /* 3 */ // something about $a return $b; } ', ]; yield [ '<?php if ($a) { // something about $a return $b; } ', ]; yield [ ' $a = $a; return $a;', ]; yield [ '<?php $a = $a; return $a;', '<?php $a = $a; return $a;', ]; yield [ '<?php $b = $b; return $b;', '<?php $b = $b;return $b;', ]; yield [ '<?php $c = $c; return $c;', '<?php $c = $c; return $c;', ]; yield [ '<?php $d = $d; return $d;', '<?php $d = $d; return $d;', ]; yield [ '<?php if (true) { return 1; }', ]; yield [ '<?php if (true) return 1;', ]; yield [ '<?php if (true) { return 1; } else { return 2; }', ]; yield [ '<?php if (true) return 1; else return 2;', ]; yield [ '<?php if (true) { return 1; } elseif (false) { return 2; }', ]; yield [ '<?php if (true) return 1; elseif (false) return 2;', ]; yield [ '<?php throw new Exception("return true; //.");', ]; yield [ '<?php function foo() { // comment return "foo"; }', ]; yield [ '<?php function foo() { // comment return "bar"; }', ]; yield [ '<?php function foo() { switch ($foo) { case 2: // comment return 1; } }', ]; yield 'do not fix when there is empty line between statement and preceding comment' => [ '<?php function foo() { bar(); // comment return 42; }', ]; yield 'do not fix when there is empty line between preceding comments' => [ '<?php function foo() { bar(); // comment1 // comment2 // comment3 return 42; }', ]; yield [ '<?php switch ($a) { case 42: break; }', null, ['statements' => ['break']], ]; yield [ '<?php switch ($a) { case 42: $foo = $bar; break; }', '<?php switch ($a) { case 42: $foo = $bar; break; }', ['statements' => ['break']], ]; yield [ '<?php while (true) { if ($foo === $bar) { break; } }', null, ['statements' => ['break']], ]; yield [ '<?php while (true) { if ($foo === $bar) { break 1; } }', null, ['statements' => ['break']], ]; yield [ '<?php while (true) { if ($foo === $bar) { echo $baz; break; } }', '<?php while (true) { if ($foo === $bar) { echo $baz; break; } }', ['statements' => ['break']], ]; yield [ '<?php while (true) { if ($foo === $bar) { echo $baz; break 1; } }', '<?php while (true) { if ($foo === $bar) { echo $baz; break 1; } }', ['statements' => ['break']], ]; yield [ '<?php while (true) { if ($foo === $bar) { /** X */ break 1; } }', null, ['statements' => ['break']], ]; yield [ '<?php switch ($a) { case 1: return 1; case 2; return 2; case 3: return 3; }', '<?php switch ($a) { case 1: return 1; case 2; return 2; case 3: return 3; }', ['statements' => ['case']], ]; yield [ '<?php while (true) { continue; }', null, ['statements' => ['continue']], ]; yield [ '<?php while (true) { continue 1; }', null, ['statements' => ['continue']], ]; yield [ '<?php while (true) { while (true) { continue 2; } }', null, ['statements' => ['continue']], ]; yield [ '<?php while (true) { $foo = true; continue; }', '<?php while (true) { $foo = true; continue; }', ['statements' => ['continue']], ]; yield [ '<?php while (true) { $foo = true; continue 1; }', '<?php while (true) { $foo = true; continue 1; }', ['statements' => ['continue']], ]; yield [ '<?php while (true) { while (true) { switch($a) { case 1: echo 1; continue; } $foo = true; continue 2; } }', '<?php while (true) { while (true) { switch($a) { case 1: echo 1; continue; } $foo = true; continue 2; } }', ['statements' => ['continue']], ]; yield [ '<?php declare(ticks=1);', null, ['statements' => ['declare']], ]; yield [ '<?php $foo = "bar"; do { } while (true); $foo = "bar"; declare(ticks=1);', '<?php $foo = "bar"; do { } while (true); $foo = "bar"; declare(ticks=1);', ['statements' => ['declare']], ]; yield [ '<?php switch ($a) { case 1: return 1; default: return 2; } switch ($a1) { default: return 22; }', '<?php switch ($a) { case 1: return 1; default: return 2; } switch ($a1) { default: return 22; }', ['statements' => ['default']], ]; yield [ '<?php do { } while (true);', null, ['statements' => ['do']], ]; yield [ '<?php $foo = "bar"; do { } while (true);', '<?php $foo = "bar"; do { } while (true);', ['statements' => ['do']], ]; yield [ '<?php if ($foo === $bar) { exit(); }', null, ['statements' => ['exit']], ]; yield [ '<?php if ($foo === $bar) { echo $baz; exit(); }', '<?php if ($foo === $bar) { echo $baz; exit(); }', ['statements' => ['exit']], ]; yield [ '<?php if ($foo === $bar) { echo $baz; exit(); }', null, ['statements' => ['exit']], ]; yield [ '<?php mysqli_connect() or exit();', null, ['statements' => ['exit']], ]; yield [ '<?php if ($foo === $bar) { $bar = 9001; mysqli_connect() or exit(); }', null, ['statements' => ['exit']], ]; yield [ '<?php echo 1; for(;;){break;} ', '<?php echo 1; for(;;){break;} ', ['statements' => ['for']], ]; yield [ '<?php a: if ($foo === $bar) { goto a; }', null, ['statements' => ['goto']], ]; yield [ '<?php a: if ($foo === $bar) { echo $baz; goto a; }', '<?php a: if ($foo === $bar) { echo $baz; goto a; }', ['statements' => ['goto']], ]; yield [ '<?php a: if ($foo === $bar) { echo $baz; goto a; }', null, ['statements' => ['goto']], ]; yield [ '<?php if (true) { echo $bar; }', null, ['statements' => ['if']], ]; yield [ '<?php if (true) { echo $bar; }', null, ['statements' => ['if']], ]; yield [ '<?php $foo = $bar; if (true) { echo $bar; }', '<?php $foo = $bar; if (true) { echo $bar; }', ['statements' => ['if']], ]; yield [ '<?php // foo if ($foo) { }', null, ['statements' => ['if']], ]; yield [ '<?php echo 1; foreach($a as $b){break;} ', '<?php echo 1; foreach($a as $b){break;} ', ['statements' => ['foreach']], ]; yield [ '<?php include "foo.php";', null, ['statements' => ['include']], ]; yield [ '<?php $foo = $bar; include "foo.php";', '<?php $foo = $bar; include "foo.php";', ['statements' => ['include']], ]; yield [ '<?php include_once "foo.php";', null, ['statements' => ['include_once']], ]; yield [ '<?php $foo = $bar; include_once "foo.php";', '<?php $foo = $bar; include_once "foo.php";', ['statements' => ['include_once']], ]; yield [ '<?php require "foo.php";', null, ['statements' => ['require']], ]; yield [ '<?php $foo = $bar; require "foo.php";', '<?php $foo = $bar; require "foo.php";', ['statements' => ['require']], ]; yield [ '<?php require_once "foo.php";', null, ['statements' => ['require_once']], ]; yield [ '<?php $foo = $bar; require_once "foo.php";', '<?php $foo = $bar; require_once "foo.php";', ['statements' => ['require_once']], ]; yield [ "<?php\r\n\$a = \$a;\r\n\r\nreturn \$a;", "<?php\r\n\$a = \$a; return \$a;", [], new WhitespacesFixerConfig("\t", "\r\n"), ]; yield [ "<?php\r\n\$b = \$b;\r\n\r\nreturn \$b;", "<?php\r\n\$b = \$b;return \$b;", [], new WhitespacesFixerConfig("\t", "\r\n"), ]; yield [ "<?php\r\n\$c = \$c;\r\n\r\nreturn \$c;", "<?php\r\n\$c = \$c;\r\nreturn \$c;", [], new WhitespacesFixerConfig("\t", "\r\n"), ]; yield [ '<?php switch ($a) { case 42: break; }', null, ['statements' => ['switch']], ]; yield [ '<?php $foo = $bar; switch ($foo) { case $bar: break; }', '<?php $foo = $bar; switch ($foo) { case $bar: break; }', ['statements' => ['switch']], ]; yield [ '<?php if (false) { throw new \Exception("Something unexpected happened."); }', null, ['statements' => ['throw']], ]; yield [ '<?php if (false) { $log->error("No"); throw new \Exception("Something unexpected happened."); }', '<?php if (false) { $log->error("No"); throw new \Exception("Something unexpected happened."); }', ['statements' => ['throw']], ]; yield [ '<?php try { $transaction->commit(); } catch (\Exception $exception) { $transaction->rollback(); }', null, ['statements' => ['try']], ]; yield [ '<?php $foo = $bar; try { $transaction->commit(); } catch (\Exception $exception) { $transaction->rollback(); }', '<?php $foo = $bar; try { $transaction->commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ['statements' => ['try']], ]; yield [ '<?php while (true) { $worker->work(); }', null, ['statements' => ['while']], ]; yield [ '<?php $foo = $bar; while (true) { $worker->work(); }', '<?php $foo = $bar; while (true) { $worker->work(); }', ['statements' => ['while']], ]; yield [ '<?php $foo = $bar; do { echo 1; while($a()); $worker->work(); } while (true);', '<?php $foo = $bar; do { echo 1; while($a()); $worker->work(); } while (true);', ['statements' => ['while']], ]; yield [ '<?php function foo() { yield $a; /* a *//* b */ /* c */ /* d *//* e *//* etc */ '.' yield $b; }', '<?php function foo() { yield $a; /* a *//* b */ /* c */ /* d *//* e *//* etc */ '.' yield $b; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield $a; // test yield $b; // test /* A */ yield $c; yield $d; yield $e;# yield $f; /* @var int $g */ yield $g; /* @var int $h */ yield $i; yield $j; }', '<?php function foo() { yield $a; // test yield $b; // test /* A */ yield $c; yield $d;yield $e;# yield $f; /* @var int $g */ yield $g; /* @var int $h */ yield $i; yield $j; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield $a; }', null, ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield $a; yield $b; }', '<?php function foo() { yield $a; yield $b; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield \'b\' => $a; yield "a" => $b; }', '<?php function foo() { yield \'b\' => $a; yield "a" => $b; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { $a = $a; yield $a; }', null, ['statements' => ['yield']], ]; yield [ '<?php function foo() { $a = $a; yield $a; }', '<?php function foo() { $a = $a; yield $a; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { // yield 1 yield 1; // yield 2 yield 2; }', '<?php function foo() { // yield 1 yield 1; // yield 2 yield 2; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield 1; // yield 2 // or maybe yield 3 // better compromise yield 2.5; }', '<?php function foo() { yield 1; // yield 2 // or maybe yield 3 // better compromise yield 2.5; }', ['statements' => ['yield']], ]; yield [ '<?php function foo() { yield from $a; }', null, ['statements' => ['yield_from']], ]; yield [ '<?php function foo() { yield from $a; yield from $b; }', '<?php function foo() { yield from $a; yield from $b; }', ['statements' => ['yield_from']], ]; yield [ '<?php function foo() { $a = $a; yield from $a; yield $a; yield $b; }', null, ['statements' => ['yield_from']], ]; yield [ '<?php function foo() { $a = $a; yield from $a; }', '<?php function foo() { $a = $a; yield from $a; }', ['statements' => ['yield_from']], ]; $statementsWithoutCaseOrDefault = [ 'break', 'continue', 'declare', 'do', 'for', 'foreach', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', ]; $allStatements = [...$statementsWithoutCaseOrDefault, 'case', 'default']; yield [ '<?php while($a) { if ($c) { switch ($d) { case $e: continue 2; case 4: break; case 5: return 1; default: return 0; } } } ', null, ['statements' => $statementsWithoutCaseOrDefault], ]; yield [ '<?php while($a) { if ($c) { switch ($d) { case $e: continue 2; case 4: break; case 5: return 1; default: return 0; } } } ', null, ['statements' => $allStatements], ]; yield [ '<?php do { echo 0; if ($a) { echo 1; break; } echo 2; throw $f; } while(true);', '<?php do { echo 0; if ($a) { echo 1; break; } echo 2; throw $f; } while(true);', ['statements' => ['break', 'throw']], ]; yield [ '<?php /** @var int $foo */ $foo = 123; /** @var float $bar */ $bar = 45.6; /** @var string */ $baz = "789"; ', '<?php /** @var int $foo */ $foo = 123; /** @var float $bar */ $bar = 45.6; /** @var string */ $baz = "789"; ', ['statements' => ['phpdoc']], ]; yield [ '<?php /* header */ /** * Class description */ class Foo { /** test */ public function bar() {} } ', null, ['statements' => ['phpdoc']], ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->testFix($expected, $input, ['statements' => ['default']]); } /** * @return iterable<string, array{string}> */ public static function provideFix80Cases(): iterable { yield 'match' => [ '<?php match ($foo) { 1 => "a", default => "b" }; match ($foo) { 1 => "a1", default => "b2" }; ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->testFix($expected, $input, ['statements' => ['case']]); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'enum' => [ '<?php enum Suit { case Hearts; case Diamonds; case Clubs; case Spades; } enum UserStatus: string { case Pending = "P"; case Active = "A"; public function label(): string { switch ($a) { case 1: return 1; case 2: return 2; // do fix } return "label"; } } ', '<?php enum Suit { case Hearts; case Diamonds; case Clubs; case Spades; } enum UserStatus: string { case Pending = "P"; case Active = "A"; public function label(): string { switch ($a) { case 1: return 1; case 2: return 2; // do fix } return "label"; } } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/MethodChainingIndentationFixerTest.php
tests/Fixer/Whitespace/MethodChainingIndentationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer> * * @author Vladimir Boliev <voff.web@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class MethodChainingIndentationFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $user->setEmail(\'voff.web@gmail.com\') ->setPassword(\'233434\') ->setEmailConfirmed(false) ->setEmailConfirmationCode(\'123456\') ->setHashsalt(\'1234\') ->setTncAccepted(true); ', '<?php $user->setEmail(\'voff.web@gmail.com\') ->setPassword(\'233434\') ->setEmailConfirmed(false) ->setEmailConfirmationCode(\'123456\') ->setHashsalt(\'1234\') ->setTncAccepted(true); ', ]; yield [ '<?php $foo ->bar1() // comment ->bar2() /* comment */ ->bar3() // comment ->bar4() ->bar5() /** buahaha */ ->bar6() /** buahaha */ ->bar7();', '<?php $foo ->bar1() // comment ->bar2() /* comment */ ->bar3() // comment ->bar4() ->bar5() /** buahaha */ ->bar6() /** buahaha */->bar7();', ]; yield [ '<?php $foo ->bar1() ->bar2();', '<?php $foo ->bar1() ->bar2();', ]; yield [ '<?php $foo ->bar();', '<?php $foo ->bar();', ]; yield [ '<?php $foo->bar()->baz() ->qux();', '<?php $foo->bar()->baz() ->qux();', ]; yield [ '<?php someCodeHereAndMultipleBreaks(); $foo ->bar1() ->bar2();', ]; yield [ '<?php if (null !== $files) { return $files; } $finder = Finder::create() ->files() ;', ]; yield [ '<?php $finder = Finder::create() ->files() ;', ]; yield [ '<?php $replacements = $replacements ->setAllowedTypes([\'array\']) ->setNormalizer(function (Options $options, $value) use ($toTypes, $default) { return $normalizedValue; }) ->setDefault($default) ->setWhitespacesConfig( new WhitespacesFixerConfig($config[\'indent\'], $config[\'lineEnding\']) ) ;', ]; yield [ '<?php return foo() ->bar ( new foo() ) ->bar(); ', ]; yield [ '<?php return new Foo("param", [ (new Bar("param1", "param2")) ->Foo([ (new Bar())->foo(), ]) ]); ', ]; yield [ '<?php (new Foo( \'argument on line 1\', \'argument on line 2\' )) ->foo() ->bar() ;', '<?php (new Foo( \'argument on line 1\', \'argument on line 2\' )) ->foo() ->bar() ;', ]; yield [ '<div> <?php $object ->method() ->method(); ?> </div> <?= $object ->method() ->method(); ?>', '<div> <?php $object ->method() ->method(); ?> </div> <?= $object ->method() ->method(); ?>', ]; yield [ '<?php $user->setFoo(1) ->setBar([ 1 => 1, ]) ->setBaz(true) ->setX(array( 2 => 2, )) ->setY(); ', '<?php $user->setFoo(1) ->setBar([ 1 => 1, ]) ->setBaz(true) ->setX(array( 2 => 2, )) ->setY(); ', ]; yield [ '<?php $user->setEmail("voff.web@gmail.com", ) ->setPassword("233434" ,) ->setEmailConfirmed(false , ) ->setEmailConfirmationCode("123456", ); ', '<?php $user->setEmail("voff.web@gmail.com", ) ->setPassword("233434" ,) ->setEmailConfirmed(false , ) ->setEmailConfirmationCode("123456", ); ', ]; yield [ '<?php $obj = (new Foo) ->setBar((new Bar) ->baz()); ', '<?php $obj = (new Foo) ->setBar((new Bar) ->baz()); ', ]; yield [ '<?php $obj ->foo("bar", function ($baz) { return $baz ->on("table1", "table2"); }) ->where("a", "b"); ', '<?php $obj ->foo("bar", function ($baz) { return $baz ->on("table1", "table2"); }) ->where("a", "b"); ', ]; yield [ '<?php $obj ->foo("baz", fn ($bar) => $bar ->baz("foobar")) ->baz(); ', '<?php $obj ->foo("baz", fn ($bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ '<?php $obj ->foo("baz", fn (string $bar) => otherFunc($bar) ->baz("foobar")) ->baz(); ', '<?php $obj ->foo("baz", fn (string $bar) => otherFunc($bar) ->baz("foobar")) ->baz(); ', ]; yield [ '<?php $obj ->foo("baz", fn (SomeClass $bar) => $bar ->baz("foobar")) ->baz(); ', '<?php $obj ->foo("baz", fn (SomeClass $bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ '<?php $obj ->foo("baz", fn (?AnotherClass $bar) => $bar ->baz("foobar")) ->baz(); ', '<?php $obj ->foo("baz", fn (?AnotherClass $bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ '<?php $obj /*buahaha*/ ->foo("baz", fn ($bar) => $bar ->baz/*buahaha*/("foobar")) ->/**buahaha*/baz(); ', '<?php $obj /*buahaha*/ ->foo("baz", fn ($bar) => $bar ->baz/*buahaha*/("foobar")) ->/**buahaha*/baz(); ', ]; yield [ '<?php $obj -> foo("baz", fn ($bar) => $bar ->baz ("foobar")) -> baz (); ', '<?php $obj -> foo("baz", fn ($bar) => $bar ->baz ("foobar")) -> baz (); ', ]; yield [ '<?php return $foo ->bar;', ]; yield [ '<?php return $foo ->bar; if (foo()) { echo 123; } ', ]; yield [ '<?php return $foo ->bar?> <?php if (foo()) { echo 123; } ', ]; yield [ '<?php return [$foo ->bar, 1, 2, abc(), ]; ', ]; yield [ '<?php $obj ->foo() ->bar; ', '<?php $obj ->foo() ->bar; ', ]; yield [ '<?php return $obj ->foo() ->bar ->baz(); ', '<?php return $obj ->foo() ->bar ->baz(); ', ]; yield [ '<?php foo() ->bar() ->baz; $obj ->foo(\'123\', 456) ->bar(\'789\') ->baz; ', '<?php foo() ->bar() ->baz; $obj ->foo(\'123\', 456) ->bar(\'789\') ->baz; ', ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\t->setPassword('233434')\r\n\t->setEmailConfirmed(false)\r\n\t->setEmailConfirmationCode('123456')\r\n\t->setHashsalt('1234')\r\n\t->setTncAccepted(true);", "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\r\n ->setPassword('233434')\r\n\t\t\t->setEmailConfirmed(false)\r\n\t\t ->setEmailConfirmationCode('123456')\r\n->setHashsalt('1234')\r\n\t\t->setTncAccepted(true);", ]; } /** * @requires PHP 8.0 */ public function testFix80(): void { $this->doTest( '<?php $user?->setEmail("voff.web@gmail.com") ?->setPassword("233434") ?->setEmailConfirmed(false) ?->setEmailConfirmationCode("123456"); ', '<?php $user?->setEmail("voff.web@gmail.com") ?->setPassword("233434") ?->setEmailConfirmed(false) ?->setEmailConfirmationCode("123456"); ', ); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/TypeDeclarationSpacesFixerTest.php
tests/Fixer/Whitespace/TypeDeclarationSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class TypeDeclarationSpacesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php function foo(bool /**bla bla*/$param) {}', '<?php function foo(bool/**bla bla*/$param) {}', ]; yield [ '<?php function foo(bool /**bla bla*/$param) {}', '<?php function foo(bool /**bla bla*/$param) {}', ]; yield [ '<?php function foo(callable $param) {}', '<?php function foo(callable$param) {}', ]; yield [ '<?php function foo(callable $param) {}', '<?php function foo(callable $param) {}', ]; yield [ '<?php function foo(array &$param) {}', '<?php function foo(array&$param) {}', ]; yield [ '<?php function foo(array &$param) {}', '<?php function foo(array &$param) {}', ]; yield [ '<?php function foo(array & $param) {}', '<?php function foo(array& $param) {}', ]; yield [ '<?php function foo(array & $param) {}', '<?php function foo(array & $param) {}', ]; yield [ '<?php function foo(Bar $param) {}', '<?php function foo(Bar$param) {}', ]; yield [ '<?php function foo(Bar $param) {}', '<?php function foo(Bar $param) {}', ]; yield [ '<?php function foo(Bar\Baz $param) {}', '<?php function foo(Bar\Baz$param) {}', ]; yield [ '<?php function foo(Bar\Baz $param) {}', '<?php function foo(Bar\Baz $param) {}', ]; yield [ '<?php function foo(Bar\Baz &$param) {}', '<?php function foo(Bar\Baz&$param) {}', ]; yield [ '<?php function foo(Bar\Baz &$param) {}', '<?php function foo(Bar\Baz &$param) {}', ]; yield [ '<?php function foo(Bar\Baz & $param) {}', '<?php function foo(Bar\Baz& $param) {}', ]; yield [ '<?php function foo(Bar\Baz & $param) {}', '<?php function foo(Bar\Baz & $param) {}', ]; yield [ '<?php $foo = function(Bar\Baz $param) {};', '<?php $foo = function(Bar\Baz$param) {};', ]; yield [ '<?php $foo = function(Bar\Baz $param) {};', '<?php $foo = function(Bar\Baz $param) {};', ]; yield [ '<?php $foo = function(Bar\Baz &$param) {};', '<?php $foo = function(Bar\Baz&$param) {};', ]; yield [ '<?php $foo = function(Bar\Baz &$param) {};', '<?php $foo = function(Bar\Baz &$param) {};', ]; yield [ '<?php $foo = function(Bar\Baz & $param) {};', '<?php $foo = function(Bar\Baz& $param) {};', ]; yield [ '<?php $foo = function(Bar\Baz & $param) {};', '<?php $foo = function(Bar\Baz & $param) {};', ]; yield [ '<?php class Test { public function foo(Bar\Baz $param) {} }', '<?php class Test { public function foo(Bar\Baz$param) {} }', ]; yield [ '<?php class Test { public function foo(Bar\Baz $param) {} }', '<?php class Test { public function foo(Bar\Baz $param) {} }', ]; yield [ '<?php $foo = function(array $a, array $b, array $c, array $d) {};', '<?php $foo = function(array $a, array$b, array $c, array $d) {};', ]; yield [ '<?php $foo = fn(Bar\Baz $param) => null;', '<?php $foo = fn(Bar\Baz$param) => null;', ]; yield [ '<?php $foo = fn(Bar\Baz $param) => null;', '<?php $foo = fn(Bar\Baz $param) => null;', ]; yield [ '<?php $foo = fn(Bar\Baz &$param) => null;', '<?php $foo = fn(Bar\Baz&$param) => null;', ]; yield [ '<?php $foo = fn(Bar\Baz &$param) => null;', '<?php $foo = fn(Bar\Baz &$param) => null;', ]; yield [ '<?php $foo = fn(Bar\Baz & $param) => null;', '<?php $foo = fn(Bar\Baz& $param) => null;', ]; yield [ '<?php $foo = fn(Bar\Baz & $param) => null;', '<?php $foo = fn(Bar\Baz & $param) => null;', ]; yield [ '<?php $foo = fn(array $a, array $b, array $c, array $d) => null;', '<?php $foo = fn(array $a, array$b, array $c, array $d) => null;', ]; yield [ '<?php function foo(array ...$param) {}', '<?php function foo(array...$param) {}', ]; yield [ '<?php function foo(array & ...$param) {}', '<?php function foo(array& ...$param) {}', ]; yield [ '<?php class Foo { public int $x; }', '<?php class Foo { public int$x; }', ]; yield [ '<?php class Foo { public bool $x; }', '<?php class Foo { public bool $x; }', ]; yield [ '<?php class Foo { protected \Bar\Baz $c; }', '<?php class Foo { protected \Bar\Baz$c; }', ]; yield [ '<?php class Foo { protected \Bar\Baz $c; }', '<?php class Foo { protected \Bar\Baz $c; }', ]; yield [ '<?php class Foo { private array $x; }', '<?php class Foo { private array$x; }', ]; yield [ '<?php class Foo { private array $x; }', '<?php class Foo { private array $x; }', ]; yield [ '<?php class Point { public \DateTime $x; protected bool $y = true; private array $z = []; public int $a = 0; protected string $b = \'\'; private float $c = 0.0; } ', '<?php class Point { public \DateTime $x; protected bool $y = true; private array $z = []; public int $a = 0; protected string $b = \'\'; private float $c = 0.0; } ', ]; yield [ '<?php function foo($param) {}', ]; yield [ '<?php function foo($param1,$param2) {}', ]; yield [ '<?php function foo(&$param) {}', ]; yield [ '<?php function foo(& $param) {}', ]; yield [ '<?php function foo(/**int*/$param) {}', ]; yield [ '<?php function foo(bool /**bla bla*/ $param) {}', ]; yield [ '<?php $foo = function( array $a, $b ) {};', ]; yield [ '<?php $foo = function( $a, array $b ) {};', ]; yield [ '<?php function foo(...$param) {}', ]; yield [ '<?php function foo(&...$param) {}', ]; yield [ '<?php use function some\test\{fn_a, fn_b, fn_c};', ]; yield [ '<?php use function some\test\{fn_a, fn_b, fn_c} ?>', ]; yield [ '<?php $foo = fn( array $a, $b ) => null;', ]; yield [ '<?php $foo = fn( $a, array $b ) => null;', ]; yield [ '<?php class Foo { public $p; }', ]; yield [ '<?php class Foo { protected /* int */ $a; }', ]; yield [ '<?php class Foo { private int $a; }', ]; // demonstrate that PHP 8.3 related config doesn\'t harm PHP <8.3 ["constant" in config] yield [ '<?php class Foo { public bool $x; }', '<?php class Foo { public bool $x; }', ['elements' => ['property', 'constant', 'function']], ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: ?string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php function foo(mixed $a) {}', '<?php function foo(mixed$a) {}', ]; yield [ '<?php function foo(mixed $a) {}', '<?php function foo(mixed $a) {}', ]; yield [ '<?php class Foo { public function __construct( public int $a, protected bool $b, private Bar\Baz $c, ) {} } ', '<?php class Foo { public function __construct( public int $a, protected bool$b, private Bar\Baz $c, ) {} } ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: ?string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php class Foo { private readonly int $bar; }', '<?php class Foo { private readonly int$bar; }', ]; yield [ '<?php class Foo { private readonly int $bar; }', '<?php class Foo { private readonly int $bar; }', ]; yield [ '<?php class Foo { readonly int $i; }', '<?php class Foo { readonly int$i; }', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: ?string}> */ public static function provideFix82Cases(): iterable { yield [ '<?php class Foo { public (A&B)|C $bar; }', '<?php class Foo { public (A&B)|C$bar; }', ]; yield [ '<?php class Foo { public (A&B)|C $bar; }', '<?php class Foo { public (A&B)|C $bar; }', ]; } /** * @dataProvider provideFix83Cases * * @requires PHP 8.3 */ public function testFix83(string $expected, ?string $input = null): void { $this->fixer->configure(['elements' => ['property', 'constant', 'function']]); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: ?string}> */ public static function provideFix83Cases(): iterable { yield [ '<?php class Foo {const BAR = ""; }', ]; yield [ '<?php class Foo {public const BAR = 123; }', ]; yield [ '<?php class Foo {const BAR = ""; }', ]; yield [ '<?php class Foo {public const string BAR = ""; }', ]; yield [ '<?php class Foo {public const stringBAR = ""; }', ]; yield [ '<?php class Foo {public const BAR = 123; }', ]; yield [ '<?php class Foo {public const array BAR = []; }', '<?php class Foo {public const array BAR = []; }', ]; yield [ '<?php class Foo {public const bool BAR = true; }', '<?php class Foo {public const bool BAR = true; }', ]; yield [ '<?php class Foo {public int $bar; public const string BAZ = ""; }', '<?php class Foo {public int $bar; public const string BAZ = ""; }', ]; yield [ '<?php class Foo {public const string BAR = ""; }', '<?php class Foo {public const string BAR = ""; }', ]; yield [ '<?php class Foo {public const string BAR = ""; }', '<?php class Foo {public const string BAR = ""; }', ]; yield [ '<?php class Foo {public const int BAR = 0; }', '<?php class Foo {public const int BAR = 0; }', ]; yield [ '<?php class Bar {public const bool BAR = true; }', '<?php class Bar {public const bool BAR = true; }', ]; yield [ '<?php interface Bar {public const string BAR = "value"; }', '<?php interface Bar {public const string BAR = "value"; }', ]; yield [ '<?php trait Bar {public const string FOO = ""; }', '<?php trait Bar {public const string FOO = ""; }', ]; yield [ '<?php class Baz { public const string FIRST = "1"; protected const int SECOND = 2; private const array THIRD = []; }', '<?php class Baz { public const string FIRST = "1"; protected const int SECOND = 2; private const array THIRD = []; }', ]; yield [ '<?php class Test { public function foo(): void {} public const string BAR = "bar"; private $baz = 1; }', '<?php class Test { public function foo(): void {} public const string BAR = "bar"; private $baz = 1; }', ]; yield [ '<?php class Foo {public const ?string BAR = null; }', '<?php class Foo {public const ?string BAR = null; }', ]; yield [ '<?php class Foo {public const int|string BAR = 0; }', '<?php class Foo {public const int|string BAR = 0; }', ]; yield [ '<?php class Foo {public const int /**bla bla*/VALUE = 0; }', '<?php class Foo {public const int/**bla bla*/VALUE = 0; }', ]; yield [ '<?php class Foo {public const int /**bla bla*/ VALUE = 0; }', '<?php class Foo {public const int /**bla bla*/ VALUE = 0; }', ]; } /** * @dataProvider provideFix84Cases * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null): void { $this->fixer->configure(['elements' => ['property', 'constant', 'function']]); $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: ?string}> */ public static function provideFix84Cases(): iterable { yield 'asymmetric visibility' => [ <<<'PHP' <?php class Foo { public(set) bool $a; protected(set) int $i; private(set) string $s; } PHP, <<<'PHP' <?php class Foo { public(set) bool$a; protected(set) int$i; private(set) string$s; } PHP, ]; yield 'final property' => [ '<?php class Foo { final int $i; }', '<?php class Foo { final int$i; }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/NoTrailingWhitespaceFixerTest.php
tests/Fixer/Whitespace/NoTrailingWhitespaceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoTrailingWhitespaceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $a = 1;', '<?php $a = 1; ', ]; yield [ '<?php $a = 1 ;', '<?php $a = 1 ; ', ]; yield [ '<?php $b = 1;', '<?php $b = 1; ', ]; yield [ "<?php \$b = 1;\n ", "<?php \$b = 1; \n ", ]; yield [ "<?php \$b = 1;\n\$c = 1;", "<?php \$b = 1; \n\$c = 1;", ]; yield [ "<?php\necho 1;\n \necho2;", ]; yield [ '<?php $b = 1; ', ]; yield [ "<?php\n\$a=1;\n \n\t\n\$b = 1;", ]; yield [ "<?php\necho 1;\n?>\n\n\n\n", ]; yield [ "<?php\n\techo 1;\n?>\n\n\t a \r\n b \r\n", ]; yield [ "<?php <<<'EOT' Il y eut un rire éclatant des écoliers qui décontenança le pauvre garçon, si bien qu'il ne savait s'il fallait garder sa casquette à la main, la laisser par terre ou la mettre sur sa tête. Il se rassit et la posa sur ses genoux. EOT; ", ]; yield [ "<?php\n\$string = 'x \ny';\necho (strlen(\$string) === 5);", ]; yield [ "<?php\necho <<<'EOT'\nInline Il y eut un \r\nrire éclatant \n \n \r\nEOT;\n\n", ]; yield [ "<?php\necho 'Hello World';", "<?php \necho 'Hello World';", ]; yield [ "<?php\n\necho 'Hello World';", "<?php \n\necho 'Hello World';", ]; yield [ "<?php\r\necho 'Hello World';", "<?php \r\necho 'Hello World';", ]; yield [ "<?php\necho 'Hello World';", "<?php \necho 'Hello World';", ]; yield [ "<?php\necho 'Hello World';", "<?php \necho 'Hello World';", ]; yield [ '<?php ', '<?php ', ]; yield [ "<?php\t", "<?php\t\t", ]; yield [ '<?php ', // do not trim this as "<?php" is not valid PHP ]; yield [ "<?php\n \n \n ", ]; yield [ "<?php\n \n ", "<?php \n \n ", ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php class Foo { #[Required] public $bar; }', '<?php class Foo { #[Required] '.' public $bar; }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/ArrayIndentationFixerTest.php
tests/Fixer/Whitespace/ArrayIndentationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ArrayIndentationFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield from self::withLongArraySyntaxCases([ [ <<<'EXPECTED' <?php $foo = [ 'foo', 'bar' => 'baz', ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo', 'bar' => 'baz', ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'foo', 'bar' => 'baz', ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo', 'bar' => 'baz', ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; EXPECTED, <<<'INPUT' <?php $foo = [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ ['foo', 'bar', ['foo', 'bar', ['foo', 'bar', 'baz']], 'baz'], ]; EXPECTED, <<<'INPUT' <?php $foo = [ ['foo', 'bar', ['foo', 'bar', ['foo', 'bar', 'baz']], 'baz'], ]; INPUT, ], [ <<<'EXPECTED' <?php class Foo { public $foo = [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; public function bar() { return [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; } } EXPECTED, <<<'INPUT' <?php class Foo { public $foo = [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; public function bar() { return [ ['bar', 'baz'], [ 'bar', 'baz' ], ]; } } INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'foo' => foo( 1, 2 ), 'bar' => bar( 1, 2 ), 'baz' => baz( 1, 2 ), ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo' => foo( 1, 2 ), 'bar' => bar( 1, 2 ), 'baz' => baz( 1, 2 ), ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'foo' => ['bar' => [ 'baz', ]], 'qux', ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo' => ['bar' => [ 'baz', ]], 'qux', ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'foo' => [ (new Foo()) ->foo(), ], ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo' => [ (new Foo()) ->foo(), ], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ [new Foo( )], [new Foo( )], ]; EXPECTED, <<<'INPUT' <?php $foo = [ [new Foo( )], [new Foo( )], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = new Foo([ (new Bar()) ->foo([ 'foo', 'foo', ]) ->bar(['bar', 'bar']) ->baz([ 'baz', 'baz', ]) , ]); EXPECTED, <<<'INPUT' <?php $foo = new Foo([ (new Bar()) ->foo([ 'foo', 'foo', ]) ->bar(['bar', 'bar']) ->baz([ 'baz', 'baz', ]) , ]); INPUT, ], [ <<<'EXPECTED' <?php class Foo { public function bar() { $foo = [ 'foo', 'foo', ]; $bar = [ 'bar', 'bar', ]; } } EXPECTED, <<<'INPUT' <?php class Foo { public function bar() { $foo = [ 'foo', 'foo', ]; $bar = [ 'bar', 'bar', ]; } } INPUT, ], [ <<<'EXPECTED' <?php class Foo { public function bar() { return new Bar([ (new Baz()) ->qux([function ($a) { foreach ($a as $b) { if ($b) { throw new Exception(sprintf( 'Oops: %s', $b )); } } }]), ]); } } EXPECTED, <<<'INPUT' <?php class Foo { public function bar() { return new Bar([ (new Baz()) ->qux([function ($a) { foreach ($a as $b) { if ($b) { throw new Exception(sprintf( 'Oops: %s', $b )); } } }]), ]); } } INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'Foo'. foo() .bar() , ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'Foo'. foo() .bar() , ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ [new \stdClass()], 'foo', ]; EXPECTED, <<<'INPUT' <?php $foo = [ [new \stdClass()], 'foo', ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ $bar ? 'bar' : 'foo' , ]; EXPECTED, <<<'INPUT' <?php $foo = [ $bar ? 'bar' : 'foo' , ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ $bar ? 'bar' : 'foo' , ]; EXPECTED, <<<'INPUT' <?php $foo = [ $bar ? 'bar' : 'foo' , ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ [ 'foo', ], [ 'bar', ], ]; EXPECTED, <<<'INPUT' <?php $foo = [ [ 'foo', ], [ 'bar', ], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ 'foo', // comment 'bar', ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo', // comment 'bar', ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [[[ 'foo', 'bar', ], ], ]; EXPECTED, <<<'INPUT' <?php $foo = [[[ 'foo', 'bar', ], ], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ [ [ 'foo', 'bar', ]]]; EXPECTED, <<<'INPUT' <?php $foo = [ [ [ 'foo', 'bar', ]]]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ [ [[ [[[ 'foo', 'bar', ] ]] ] ] ]]; EXPECTED, <<<'INPUT' <?php $foo = [ [ [[ [[[ 'foo', 'bar', ] ]] ] ] ]]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [[ [ [[[],[[ [[[ 'foo', 'bar', ],[[],[]]] ]],[ 'baz', ]] ],[]],[]] ] ]; EXPECTED, <<<'INPUT' <?php $foo = [[ [ [[[],[[ [[[ 'foo', 'bar', ],[[],[]]] ]],[ 'baz', ]] ],[]],[]] ] ]; INPUT, ], [ <<<'EXPECTED' <?php if ($foo): ?> <?php foo([ 'bar', 'baz', ]) ?> <?php endif ?> EXPECTED, <<<'INPUT' <?php if ($foo): ?> <?php foo([ 'bar', 'baz', ]) ?> <?php endif ?> INPUT, ], [ <<<'EXPECTED' <div> <a class="link" href="<?= Url::to([ '/site/page', 'id' => 123, ]); ?>" > Link text </a> </div> EXPECTED, <<<'INPUT' <div> <a class="link" href="<?= Url::to([ '/site/page', 'id' => 123, ]); ?>" > Link text </a> </div> INPUT, ], [ <<<'EXPECTED' <?php $arr = [ 'a' => 'b', // 'c' => 'd', ]; EXPECTED, <<<'INPUT' <?php $arr = [ 'a' => 'b', // 'c' => 'd', ]; INPUT, ], [ '<?php '.' $foo = [ "foo", "bar", ];', ], [ <<<'EXPECTED' <?php $foo = [ 'foo' => 'Some' .' long' .' string', 'bar' => 'Another' .' long' .' string' ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo' => 'Some' .' long' .' string', 'bar' => 'Another' .' long' .' string' ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ $test ? [ 123, ] : [ 321, ], ]; EXPECTED, <<<'INPUT' <?php $foo = [ $test ? [ 123, ] : [ 321, ], ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [[ new Foo( 'foo' ), ]]; EXPECTED, <<<'INPUT' <?php $foo = [[ new Foo( 'foo' ), ]]; INPUT, ], [ <<<'EXPECTED' <?php $array = [ 'foo' => [ 'bar' => [ 'baz', ], ], // <- this one ]; EXPECTED, <<<'INPUT' <?php $array = [ 'foo' => [ 'bar' => [ 'baz', ], ], // <- this one ]; INPUT, ], [ <<<'EXPECTED' <?php $foo = [ ...$foo, ...$bar, ]; EXPECTED, <<<'INPUT' <?php $foo = [ ...$foo, ...$bar, ]; INPUT, ], ]); yield 'array destructuring' => [ <<<'EXPECTED' <?php [ $foo, $bar, $baz ] = $arr; EXPECTED, <<<'INPUT' <?php [ $foo, $bar, $baz ] = $arr; INPUT, ]; yield 'array destructuring using list' => [ <<<'EXPECTED' <?php list( $foo, $bar, $baz ) = $arr; EXPECTED, <<<'INPUT' <?php list( $foo, $bar, $baz ) = $arr; INPUT, ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t")); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield from self::withLongArraySyntaxCases([ [ <<<EXPECTED <?php \$foo = [ \t'foo', \t'bar' => 'baz', ]; EXPECTED, <<<'INPUT' <?php $foo = [ 'foo', 'bar' => 'baz', ]; INPUT, ], [ <<<EXPECTED <?php \$foo = [ \t'foo', \t'bar' => 'baz', ]; EXPECTED, <<<INPUT <?php \$foo = [ \t\t\t'foo', \t\t'bar' => 'baz', ]; INPUT, ], ]); yield 'array destructuring' => [ <<<EXPECTED <?php [ \t\$foo, \t\$bar, \t\$baz ] = \$arr; EXPECTED, <<<'INPUT' <?php [ $foo, $bar, $baz ] = $arr; INPUT, ]; yield 'array destructuring using list' => [ <<<EXPECTED <?php list( \t\$foo, \t\$bar, \t\$baz ) = \$arr; EXPECTED, <<<'INPUT' <?php list( $foo, $bar, $baz ) = $arr; INPUT, ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix80Cases(): iterable { yield 'attribute' => [ '<?php class Foo { #[SimpleAttribute] #[ComplexAttribute( foo: true, bar: [ 1, 2, 3, ] )] public function bar() { } }', '<?php class Foo { #[SimpleAttribute] #[ComplexAttribute( foo: true, bar: [ 1, 2, 3, ] )] public function bar() { } }', ]; } /** * @dataProvider provideFix85Cases * * @requires PHP 8.5 */ public function testFix85(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix85Cases(): iterable { yield 'nested attribute' => [ <<<'PHP' <?php #[Foo([static function (#[SensitiveParameter] $a) { return [ fn (#[Bar([1, 2])] $b) => [ $b[1] ] ] ; }])] class Baz {} PHP, <<<'PHP' <?php #[Foo([static function (#[SensitiveParameter] $a) { return [ fn (#[Bar([1, 2])] $b) => [ $b[1] ] ] ; }])] class Baz {} PHP, ]; } /** * @param list<array{0: string, 1?: string}> $cases * * @return list<array{0: string, 1?: string}> */ private static function withLongArraySyntaxCases(array $cases): array { $longSyntaxCases = []; foreach ($cases as $case) { $case[0] = self::toLongArraySyntax($case[0]); if (isset($case[1])) { $case[1] = self::toLongArraySyntax($case[1]); } $longSyntaxCases[] = $case; } return [...$cases, ...$longSyntaxCases]; } private static function toLongArraySyntax(string $php): string { return strtr($php, [ '[' => 'array(', ']' => ')', ]); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/SpacesInsideParenthesesFixerTest.php
tests/Fixer/Whitespace/SpacesInsideParenthesesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer> * * @author Marc Aubé * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SpacesInsideParenthesesFixerTest extends AbstractFixerTestCase { /** * @param array<string, mixed> $wrongConfig * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($expectedMessage); $this->fixer->configure($wrongConfig); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'missing key' => [ ['a' => 1], '#^\[spaces_inside_parentheses\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#', ]; yield 'invalid value' => [ ['space' => 'double'], '#^\[spaces_inside_parentheses\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { // default leaves new lines alone yield [ "<?php class Foo { private function bar() { if (foo( 'foo' , 'bar' , [1, 2, 3], 'baz' // a comment just to mix things up )) { return 1; }; } } ", ]; yield [ '<?php foo();', '<?php foo( );', ]; yield [ '<?php if (true) { // if body }', '<?php if ( true ) { // if body }', ]; yield [ '<?php if (true) { // if body }', '<?php if ( true ) { // if body }', ]; yield [ '<?php function foo($bar, $baz) { // function body }', '<?php function foo( $bar, $baz ) { // function body }', ]; yield [ '<?php $foo->bar($arg1, $arg2);', '<?php $foo->bar( $arg1, $arg2 );', ]; yield [ '<?php $var = array( 1, 2, 3 ); ', ]; yield [ '<?php $var = [ 1, 2, 3 ]; ', ]; // list call with trailing comma - need to leave alone yield [ '<?php list($path, $mode, ) = foo();', ]; yield [ '<?php list($path, $mode,) = foo();', ]; yield [ '<?php $a = $b->test( // do not remove space $e // between `(` and `)` // and this comment );', ]; yield [ '<?php function hello($value) { // code... }', '<?php function hello( $value ) { // code... }', ]; yield [ '<?php $code = function ($hello, $there) use ($ami, $tumi) { // code }; ', '<?php $code = function ( $hello, $there ) use ( $ami, $tumi ) { // code }; ', ]; yield [ '<?php for ($i = 0; $i < 42; $i++) { // code... } ', '<?php for ( $i = 0; $i < 42; $i++ ) { // code... } ', ]; yield [ '<?php explode($a, $b); ', '<?php explode( $a, $b ); ', ]; yield [ '<?php if ($something) { // code } ', '<?php if ( $something ) { // code } ', ]; yield [ '<?php multiply((2 + 3) * 4); ', '<?php multiply( ( 2 + 3 ) * 4 ); ', ]; yield [ '<?php $x = (new Foo())->bar();', '<?php $x = ( new Foo() )->bar();', ]; yield [ '<?php $x = (new Foo)->bar;', '<?php $x = ( new Foo )->bar;', ]; yield 'leaves new lines alone' => [ "<?php class Foo { private function bar() { if ( foo( 'foo' , 'bar' , [1, 2, 3], 'baz' // a comment just to mix things up ) ) { return 1; }; } }", null, ['space' => 'single'], ]; yield [ '<?php foo();', '<?php foo( );', ['space' => 'single'], ]; yield [ '<?php if ( true ) { // if body }', '<?php if (true) { // if body }', ['space' => 'single'], ]; yield [ '<?php if ( true ) { // if body }', '<?php if ( true ) { // if body }', ['space' => 'single'], ]; yield [ '<?php function foo( $bar, $baz ) { // function body }', '<?php function foo($bar, $baz) { // function body }', ['space' => 'single'], ]; yield [ '<?php $foo->bar( $arg1, $arg2 );', '<?php $foo->bar( $arg1, $arg2 );', ['space' => 'single'], ]; yield [ '<?php $var = array( 1, 2, 3 ); ', '<?php $var = array(1, 2, 3); ', ['space' => 'single'], ]; yield [ '<?php $var = [ 1, 2, 3 ]; ', null, ['space' => 'single'], ]; yield [ '<?php list( $path, $mode, ) = foo();', '<?php list($path, $mode,) = foo();', ['space' => 'single'], ]; yield [ '<?php $a = $b->test( // do not remove space $e // between `(` and `)` // and this comment );', null, ['space' => 'single'], ]; yield [ '<?php function hello( $value ) { // code... }', '<?php function hello($value) { // code... }', ['space' => 'single'], ]; yield [ '<?php $code = function ( $hello, $there ) use ( $ami, $tumi ) { // code }; ', '<?php $code = function ($hello, $there) use ($ami, $tumi) { // code }; ', ['space' => 'single'], ]; yield [ '<?php for ( $i = 0; $i < 42; $i++ ) { // code... } ', '<?php for ($i = 0; $i < 42; $i++) { // code... } ', ['space' => 'single'], ]; yield [ '<?php explode( $a, $b ); ', '<?php explode($a, $b); ', ['space' => 'single'], ]; yield [ '<?php if ( $something ) { // code } ', '<?php if ( $something ) { // code } ', ['space' => 'single'], ]; yield [ '<?php multiply( ( 2 + 3 ) * 4 ); ', '<?php multiply((2 + 3) * 4); ', ['space' => 'single'], ]; yield [ '<?php $x = ( new Foo() )->bar();', '<?php $x = (new Foo())->bar();', ['space' => 'single'], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield 'mixed argument' => [ '<?php function foo(mixed $a){}', '<?php function foo( mixed $a ){}', ]; yield 'mixed argument single space' => [ '<?php function foo( mixed $a ){}', '<?php function foo(mixed $a){}', ['space' => 'single'], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/NoSpacesAroundOffsetFixerTest.php
tests/Fixer/Whitespace/NoSpacesAroundOffsetFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer> * * @author Javier Spagnoletti <phansys@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoSpacesAroundOffsetFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php $foo = array(1, 2, 3); $var = $foo[1];', '<?php $foo = array(1, 2, 3); $var = $foo[ 1 ];', ]; yield [ '<?php $arr = [2, 2 , ]; $var = $arr[0];', '<?php $arr = [2, 2 , ]; $var = $arr[ 0 ];', ]; yield [ '<?php $arr[2] = 3;', '<?php $arr[ 2 ] = 3;', ]; yield [ '<?php $arr[] = 3;', '<?php $arr[ ] = 3;', ]; yield [ '<?php $arr[]["some_offset"][] = 3;', '<?php $arr[ ][ "some_offset" ][ ] = 3;', ]; yield [ '<?php $arr[]["some offset with spaces"][] = 3;', '<?php $arr[ ][ "some offset with spaces" ][ ] = 3;', ]; yield [ '<?php $var = $arr[0];', '<?php $var = $arr[ 0 ];', ]; yield [ '<?php $var = $arr[0][0];', '<?php $var = $arr[ 0 ][ 0 ];', ]; yield [ '<?php $var = $arr[$a[$b]];', '<?php $var = $arr[ $a [ $b ] ];', ]; yield [ '<?php $var = $arr[$a[$b]];', '<?php $var = $arr[ $a [ $b ] ];', ]; yield [ '<?php $var = $arr[0][ 0];', '<?php $var = $arr[0][ 0 ];', ]; yield [ '<?php $var = $arr[0][0 ];', '<?php $var = $arr[0][ 0 ];', ]; yield [ '<?php $a = $b[0] ;', '<?php $a = $b [0] ;', ]; yield [ '<?php $a = array($b[0] , $b[0] );', '<?php $a = array($b [0] , $b [0] );', ]; yield [ '<?php $withComments[0] // here is a comment [1] // and here is another [2][3] = 4;', '<?php $withComments [0] // here is a comment [1] // and here is another [2] [3] = 4;', ]; yield [ '<?php $c = SOME_CONST[0][1][2];', '<?php $c = SOME_CONST [0] [1] [2];', ]; yield [ '<?php $f = someFunc()[0][1][2];', '<?php $f = someFunc() [0] [1] [2];', ]; yield [ '<?php $foo[][0][1][2] = 3;', '<?php $foo [] [0] [1] [2] = 3;', ]; yield [ '<?php $foo[0][1][2] = 3;', '<?php $foo [0] [1] [2] = 3;', ]; yield [ '<?php $bar = $foo[0][1][2];', '<?php $bar = $foo [0] [1] [2];', ]; yield [ '<?php $baz[0][1][2] = 3;', '<?php $baz [0] [1] [2] = 3;', ]; yield 'leave new lines alone' => [<<<'EOF' <?php class Foo { private function bar() { if ([1, 2, 3] && [ 'foo', 'bar' , 'baz'// a comment just to mix things up ]) { return 1; }; } } EOF]; yield [ '<?php $withComments[0] // here is a comment [1] // and here is another [2] = 3;', ]; yield [ '<?php $a = $b[# z 1#z ];', '<?php $a = $b[ # z 1#z ];', ]; yield 'leave complex string' => [<<<'EOF' <?php echo "I am printing some spaces here {$foo->bar[1]} {$foo->bar[1]}."; EOF]; yield 'leave functions' => [<<<'EOF' <?php function someFunc() { $someVar = []; } EOF]; yield 'Config "default".' => [ '<?php [ $a ] = $a; if ($controllerName = $request->attributes->get(1)) { return false; } [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName); $a = $b[0]; ', '<?php [ $a ] = $a; if ($controllerName = $request->attributes->get(1)) { return false; } [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName); $a = $b [0]; ', ['positions' => ['inside', 'outside']], ]; } public function testInvalidConfiguration(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_spaces_around_offset\] Invalid configuration: The option "positions" .*\.$/'); $this->fixer->configure(['positions' => ['foo']]); // @phpstan-ignore-line } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php $foo{0}{1}{2} = 3;', '<?php $foo {0} {1} {2} = 3;', ]; yield [ '<?php $foobar = $foo{0}[1]{2};', '<?php $foobar = $foo {0} [1] {2};', ]; yield [ '<?php $var = $arr[0]{0 };', '<?php $var = $arr[0]{ 0 };', ]; yield from self::provideMultiDimensionalArrayCases(); } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input, array $configuration): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string, _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { foreach (self::provideMultiDimensionalArrayCases() as $index => $test) { $test[0] = str_replace('{', '[', $test[0]); $test[0] = str_replace('}', ']', $test[0]); $test[1] = str_replace('{', '[', $test[1]); $test[1] = str_replace('}', ']', $test[1]); yield $index => $test; } } /** * @return iterable<int, array{string, string, _AutogeneratedInputConfiguration}> */ private static function provideMultiDimensionalArrayCases(): iterable { yield [ <<<'EOT' <?php $arr1[] ["some_offset"] [] {"foo"} = 3; EOT, <<<'EOT' <?php $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3; EOT, ['positions' => ['inside']], ]; yield [ <<<'EOT' <?php $arr1[ ][ "some_offset" ][ ]{ "foo" } = 3; EOT, <<<'EOT' <?php $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3; EOT, ['positions' => ['outside']], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/IndentationTypeFixerTest.php
tests/Fixer/Whitespace/IndentationTypeFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\IndentationTypeFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\IndentationTypeFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class IndentationTypeFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, ?WhitespacesFixerConfig $whitespacesConfig = null): void { $this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig()); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: WhitespacesFixerConfig}> */ public static function provideFixCases(): iterable { yield [ '<?php echo ALPHA;', "<?php \t\techo ALPHA;", ]; yield [ '<?php echo BRAVO;', "<?php \t\techo BRAVO;", ]; yield [ '<?php echo CHARLIE;', "<?php \t\techo CHARLIE;", ]; yield [ '<?php echo DELTA;', "<?php \t\techo DELTA;", ]; yield [ "<?php echo 'ECHO';", "<?php \t\techo 'ECHO';", ]; yield [ '<?php echo FOXTROT;', "<?php \t \techo FOXTROT;", ]; yield [ '<?php echo GOLF;', "<?php \t \techo GOLF;", ]; yield [ '<?php echo HOTEL;', "<?php \t \techo HOTEL;", ]; yield [ '<?php echo INDIA;', "<?php \t echo INDIA;", ]; yield [ '<?php echo JULIET;', "<?php \t \techo JULIET;", ]; yield [ '<?php echo KILO;', "<?php \t \techo KILO;", ]; yield [ '<?php echo MIKE;', "<?php \t \techo MIKE;", ]; yield [ '<?php echo NOVEMBER;', "<?php \techo NOVEMBER;", ]; yield [ '<?php echo OSCAR;', "<?php \t \t echo OSCAR;", ]; yield [ '<?php echo PAPA;', "<?php \t \t echo PAPA;", ]; yield [ '<?php echo QUEBEC;', "<?php \t \t echo QUEBEC;", ]; yield [ '<?php $x = "a: \t";', ]; yield [ "<?php \$x = \" \tLike \ta \tdog\";", ]; yield [ '<?php /** * Test that tabs in docblocks are converted to spaces. * * @test * * @return */', "<?php \t/** \t * Test that tabs in docblocks are converted to spaces. \t * \t * @test \t * \t * @return \t */", ]; yield [ '<?php /** * Test that tabs in docblocks are converted to spaces. */', "<?php \t\t/** \t\t * Test that tabs in docblocks are converted to spaces. \t\t */", ]; yield [ '<?php /* | Test that tabs in comments are converted to spaces '."\t".'. */', "<?php \t/* \t | Test that tabs in comments are converted to spaces \t. \t */", ]; yield [ "<?php /** * This variable * should not be '\t', really! */", "<?php \t/** \t * This variable \t * should not be '\t', really! \t */", ]; yield [ "<?php\necho 1;\n?>\r\n\t\$a = ellow;", ]; foreach (self::getFixCases() as $name => $case) { yield 'tabs - '.$name => [...$case, new WhitespacesFixerConfig("\t", "\r\n")]; if ('mix indentation' === $name) { continue; } yield 'spaces - '.$name => [$case[1], $case[0], new WhitespacesFixerConfig(' ', "\r\n")]; } yield [ '<?php if (true) { if (true) { (new stdClass())->foo( "text", "text2" ); } }', null, new WhitespacesFixerConfig(' '), ]; yield [ "<?php if (true) { if (true) { (new stdClass())->foo( 'text', 'text2' ); } }", "<?php if (true) { if (true) { \t(new stdClass())->foo( \t 'text', \t 'text2' \t); } }", new WhitespacesFixerConfig(' '), ]; yield [ '<?php /* * Foo */ ', "<?php \t/* \t * Foo \t */ ", new WhitespacesFixerConfig(' '), ]; } /** * @return iterable<array{string, string}> */ private static function getFixCases(): iterable { yield [ "<?php \t\techo KILO;", '<?php echo KILO;', ]; yield [ "<?php \t\t echo QUEBEC;", '<?php echo QUEBEC;', ]; yield [ "<?php \t/** \t * This variable \t * should not be '\t', really! \t */", "<?php /** * This variable * should not be '\t', really! */", ]; yield 'mix indentation' => [ "<?php \t\t/* \t\t * multiple indentation \t\t * shall be handled properly \t\t */", "<?php \t\t/* \t\t * multiple indentation \t * shall be handled properly \t */", ]; yield 'do not touch whitespace that is not indentation' => [ "<?php function myFunction() { \t\$foo = 1; \t//abc \t\$myFunction = 2; \t\$middleVar = 1; }", '<?php function myFunction() { $foo = 1; //abc $myFunction = 2; $middleVar = 1; }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/CompactNullableTypehintFixerTest.php
tests/Fixer/Whitespace/CompactNullableTypehintFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer * * @extends AbstractNullableTypeDeclarationFixerTestCase<\PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer> * * @author Jack Cherng <jfcherng@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class CompactNullableTypehintFixerTest extends AbstractNullableTypeDeclarationFixerTestCase {}
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/SingleBlankLineAtEofFixerTest.php
tests/Fixer/Whitespace/SingleBlankLineAtEofFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SingleBlankLineAtEofFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'Not adding an empty line in empty file.' => [ '', ]; yield 'Not adding an empty line in file with only white space.' => [ ' ', ]; yield [ "<?php\n", ]; yield [ '<?php $a = 1; ', '<?php $a = 1;', ]; yield [ '<?php $a = 2; ', ]; yield [ '<?php $a = 3; ', '<?php $a = 3; ', ]; yield [ "<?php\r\n\$a = 4;\n", "<?php\r\n\$a = 4;", ]; yield [ "<?php\r\n\$a = 5;\n", "<?php\r\n\$a = 5;\r\n \r\n", ]; yield [ '<?php $a = 6; //test ?> ', ]; yield [ // test for not adding an empty line after PHP tag has been closed '<?php $a = 7; //test ?>', ]; yield [ // test for not adding an empty line after PHP tag has been closed '<?php $a = 8; //test ?> Outside of PHP tags rendering ', ]; yield [ // test for not adding an empty line after PHP tag has been closed "<?php //test ?> inline 1 <?php ?>Inline2\r\n", ]; yield [ "<?php return true;\n// A comment\n", "<?php return true;\n// A comment", ]; yield [ "<?php return true;\n// A comment\n", "<?php return true;\n// A comment\n\n", ]; yield [ "<?php return true;\n# A comment\n", "<?php return true;\n# A comment", ]; yield [ "<?php return true;\n# A comment\n", "<?php return true;\n# A comment\n\n", ]; yield [ "<?php return true;\n/*\nA comment\n*/\n", "<?php return true;\n/*\nA comment\n*/", ]; yield [ "<?php return true;\n/*\nA comment\n*/\n", "<?php return true;\n/*\nA comment\n*/\n\n", ]; yield [ "<?= 1;\n", '<?= 1;', ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\n\$a = 4;\r\n", "<?php\r\n\$a = 4;", ]; yield [ "<?php\r\n\$a = 5;\r\n", "<?php\r\n\$a = 5;\r\n \r\n", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/NoSpacesInsideParenthesisFixerTest.php
tests/Fixer/Whitespace/NoSpacesInsideParenthesisFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesInsideParenthesisFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoSpacesInsideParenthesisFixer> * * @author Marc Aubé * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoSpacesInsideParenthesisFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php foo();', '<?php foo( );', ]; yield [ '<?php if (true) { // if body }', '<?php if ( true ) { // if body }', ]; yield [ '<?php if (true) { // if body }', '<?php if ( true ) { // if body }', ]; yield [ '<?php function foo($bar, $baz) { // function body }', '<?php function foo( $bar, $baz ) { // function body }', ]; yield [ '<?php $foo->bar($arg1, $arg2);', '<?php $foo->bar( $arg1, $arg2 );', ]; yield [ '<?php $var = array( 1, 2, 3 ); ', ]; yield [ '<?php $var = [ 1, 2, 3 ]; ', ]; // list call with trailing comma - need to leave alone yield [ '<?php list($path, $mode, ) = foo();', ]; yield [ '<?php list($path, $mode,) = foo();', ]; yield [ '<?php $a = $b->test( // do not remove space $e // between `(` and `)` // and this comment );', ]; } public function testLeaveNewLinesAlone(): void { $expected = <<<'EOF' <?php class Foo { private function bar() { if (foo( 'foo' , 'bar' , [1, 2, 3], 'baz' // a comment just to mix things up )) { return 1; }; } } EOF; $this->doTest($expected); } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php function foo(mixed $a){}', '<?php function foo( mixed $a ){}', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'first callable class' => [ '<?php $a = strlen(...);', '<?php $a = strlen( ... );', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/BlankLineBetweenImportGroupsFixerTest.php
tests/Fixer/Whitespace/BlankLineBetweenImportGroupsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class BlankLineBetweenImportGroupsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php use Aaa\Ccc; use Foo\Zar\Baz; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use some\b\{ ClassF, ClassG }; use Some\Cloz; use Aaa\Bbb; use const some\a\{ConstD}; use const some\a\{ConstA}; use const some\a\{ConstB, ConstC as CC}; use const some\b\{ConstE}; use function some\f\{fn_g, fn_h, fn_i}; use function some\c\{fn_f}; use function some\a\{fn_x}; use function some\b\{fn_c, fn_d, fn_e}; use function some\a\{fn_a, fn_b}; ', '<?php use Aaa\Ccc; use Foo\Zar\Baz; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use some\b\{ ClassF, ClassG }; use Some\Cloz; use Aaa\Bbb; use const some\a\{ConstD}; use const some\a\{ConstA}; use const some\a\{ConstB, ConstC as CC}; use const some\b\{ConstE}; use function some\f\{fn_g, fn_h, fn_i}; use function some\c\{fn_f}; use function some\a\{fn_x}; use function some\b\{fn_c, fn_d, fn_e}; use function some\a\{fn_a, fn_b}; ', ]; yield [ '<?php use Aaa\Ccc; use Foo\Zar\Baz; use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', '<?php use Aaa\Ccc; use Foo\Zar\Baz; use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', ]; yield [ '<?php use Aaa\Ccc; use Foo\Zar\Baz; #use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; /** Import ConstA to do some nice magic */ use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', '<?php use Aaa\Ccc; use Foo\Zar\Baz; #use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; /** Import ConstA to do some nice magic */ use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', ]; yield [ '<?php /** use Aaa\Ccc; use Foo\Zar\Baz; */ use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; // Ignore the following content // use function some\a\{fn_x}; // use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', '<?php /** use Aaa\Ccc; use Foo\Zar\Baz; */ use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; // Ignore the following content // use function some\a\{fn_x}; // use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; use function some\a\{fn_a, fn_b}; ', ]; yield [ '<?php use Aaa\Ccc; /*use Foo\Zar\Baz; use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; */ use function some\a\{fn_a, fn_b}; ', '<?php use Aaa\Ccc; /*use Foo\Zar\Baz; use function some\f\{fn_g, fn_h, fn_i}; use some\a\{ClassA}; use some\b\{ClassD, ClassB, ClassC as C}; use Bar\Biz\Boooz\Bum; use function some\c\{fn_f}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstD}; use Some\Cloz; use function some\a\{fn_x}; use const some\a\{ConstA}; use function some\b\{fn_c, fn_d, fn_e}; use const some\a\{ConstB, ConstC as CC}; use Aaa\Bbb; use const some\b\{ConstE}; */ use function some\a\{fn_a, fn_b}; ', ]; yield [ '<?php use Aaa\Ccc; use function some\a\{fn_a, fn_b}; use function some\b\{ fn_c, fn_d }; ', '<?php use Aaa\Ccc; use function some\a\{fn_a, fn_b}; use function some\b\{ fn_c, fn_d }; ', ]; yield [ '<?php use Aaa\Ccc; use function some\a\{fn_a, fn_b}; // Do this because of reasons use function some\b\{ fn_c, fn_d }; ', '<?php use Aaa\Ccc; use function some\a\{fn_a, fn_b}; // Do this because of reasons use function some\b\{ fn_c, fn_d }; ', ]; yield [ '<?php use Aaa\Ccc; /** Beginning of line comment as well */use function some\a\{fn_a, fn_b}; use function some\b\{ fn_c, fn_d }; ', '<?php use Aaa\Ccc; /** Beginning of line comment as well */use function some\a\{fn_a, fn_b}; use function some\b\{ fn_c, fn_d }; ', ]; yield [ '<?php use /* x */ function /* x */ Psl\Str\trim; use /* x */ Psl\Str /* x */; use /* x */ const /* x */ Psl\Str\OTHER_ALPHABET; use /* x */ const /* x */ Psl\Str\ALPHABET; use /* x */ function /* x */ Psl\Str\ /* x */ { /* x */ trim_left /* x */, /* x */ trim_right /* x */, }; ', '<?php use /* x */ function /* x */ Psl\Str\trim; use /* x */ Psl\Str /* x */; use /* x */ const /* x */ Psl\Str\OTHER_ALPHABET; use /* x */ const /* x */ Psl\Str\ALPHABET; use /* x */ function /* x */ Psl\Str\ /* x */ { /* x */ trim_left /* x */, /* x */ trim_right /* x */, }; ', ]; yield 'lots of inserts in same namespace' => [ '<?php namespace A\B6 { use C1\B1; use const C6\Z1; use C2\B2; use const C7\Z2; use C3\B3; use const C8\Z3; use C4\B4; use const C9\Z4; use C5\B5; use const C0\Z5; } ', '<?php namespace A\B6 { use C1\B1;use const C6\Z1; use C2\B2;use const C7\Z2; use C3\B3;use const C8\Z3; use C4\B4;use const C9\Z4; use C5\B5;use const C0\Z5; } ', ]; yield 'lots of inserts in multiple namespaces' => [ '<?php namespace A\B1 { use C\B; use const C\Z; } namespace A\B2 { use C\B; use const C\Z; } namespace A\B3 { use C\B; use const C\Z; } namespace A\B4 { use C\B; use const C\Z; } namespace A\B5 { use C\B; use const C\Z; } ', '<?php namespace A\B1 { use C\B;use const C\Z; } namespace A\B2 { use C\B;use const C\Z; } namespace A\B3 { use C\B;use const C\Z; } namespace A\B4 { use C\B;use const C\Z; } namespace A\B5 { use C\B;use const C\Z; } ', ]; yield [ '<?php use A\B; /*1*/ use const C\D;', '<?php use A\B; /*1*/ use const C\D;', ]; yield [ '<?php namespace Foo; use A\B; /* foo */ /* A */ /* B */ # X use const C\D; // bar ', '<?php namespace Foo; use A\B; /* foo */ /* A */ /* B */ # X use const C\D; // bar ', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP <8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php use Some\/**Namespace*/Weird\Be/**ha*/; use function Other\/**Namespace*/Weird\/**Comments*/have; ', '<?php use Some\/**Namespace*/Weird\Be/**ha*/; use function Other\/**Namespace*/Weird\/**Comments*/have; ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/CompactNullableTypeDeclarationFixerTest.php
tests/Fixer/Whitespace/CompactNullableTypeDeclarationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\CompactNullableTypeDeclarationFixer * * @extends AbstractNullableTypeDeclarationFixerTestCase<\PhpCsFixer\Fixer\Whitespace\CompactNullableTypeDeclarationFixer> * * @author Jack Cherng <jfcherng@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class CompactNullableTypeDeclarationFixerTest extends AbstractNullableTypeDeclarationFixerTestCase {}
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/LineEndingFixerTest.php
tests/Fixer/Whitespace/LineEndingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\LineEndingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\LineEndingFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LineEndingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield from self::provideCommonCases(); yield [ "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n", "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\n", // both cases ]; yield [ "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n", "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \n |\r\nTEST;\r\n", // both cases ]; yield 'T_INLINE_HTML' => [ "<?php ?>\nZ\r\n<?php ?>\nZ\r\n", ]; yield '!T_CONSTANT_ENCAPSED_STRING' => [ "<?php \$a=\"a\r\n\";", ]; yield [ "<?php echo 'foo',\n\n'bar';", "<?php echo 'foo',\r\r\n'bar';", ]; yield 'T_CLOSE_TAG' => [ "<?php\n?>\n<?php\n", "<?php\n?>\r\n<?php\n", ]; yield 'T_CLOSE_TAG II' => [ "<?php\n?>\n<?php\n?>\n<?php\n", "<?php\n?>\r\n<?php\n?>\r\n<?php\n", ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield from array_map(static fn (array $case): array => array_reverse($case), self::provideCommonCases()); yield [ "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n", "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\r\n", // both types ]; yield [ "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n", "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \r\n |\nTEST;\n", // both types ]; } /** * @return array<int|string, array{string, string}> */ private static function provideCommonCases(): array { return [ 'T_OPEN_TAG' => [ "<?php\n \$a = 1;", "<?php\r\n \$a = 1;", ], 'T_WHITESPACE' => [ "<?php \n \$a\n= 1;\n", "<?php \r\n \$a\r\n= 1;\r\n", ], 'T_COMMENT' => [ "<?php /*\n*/", "<?php /*\r\n*/", ], 'T_DOC_COMMENT' => [ "<?php /**\n*/", "<?php /**\r\n*/", ], 'T_START_HEREDOC' => [ "<?php \$a = <<<'TEST'\nAA\nTEST;\n", "<?php \$a = <<<'TEST'\r\nAA\r\nTEST;\r\n", ], [ "<?php \$a = <<<TEST\nAAA\nTEST;\n", "<?php \$a = <<<TEST\r\nAAA\r\nTEST;\r\n", ], 'T_ENCAPSED_AND_WHITESPACE' => [ "<?php \$a = <<<'TEST'\nAAAA 1\n \$b\nTEST;\n", "<?php \$a = <<<'TEST'\r\nAAAA 1\r\n \$b\r\nTEST;\r\n", ], [ "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n", "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n", ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/TypesSpacesFixerTest.php
tests/Fixer/Whitespace/TypesSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class TypesSpacesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php try {} catch (ErrorA|ErrorB $e) {}', '<?php try {} catch (ErrorA | ErrorB $e) {}', ]; yield [ '<?php try {} catch (ErrorA|ErrorB $e) {}', '<?php try {} catch (ErrorA | ErrorB $e) {}', ]; yield [ '<?php try {} catch (ErrorA | ErrorB $e) {}', '<?php try {} catch (ErrorA|ErrorB $e) {}', ['space' => 'single'], ]; yield [ '<?php try {} catch (ErrorA | ErrorB $e) {}', '<?php try {} catch (ErrorA | ErrorB $e) {}', ['space' => 'single'], ]; yield [ '<?php try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} try {} catch (ErrorA | ErrorB $e) {} ', '<?php try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} try {} catch (ErrorA|ErrorB $e) {} ', ['space' => 'single'], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield [ '<?php function foo(TypeA|TypeB $x) {}', '<?php function foo(TypeA | TypeB $x) {}', ]; yield [ '<?php function foo(TypeA|TypeB|TypeC|TypeD $x, TypeE|TypeF $y, TypeA|TypeB $z) {}', '<?php function foo(TypeA | TypeB | TypeC | TypeD $x, TypeE | TypeF $y, TypeA| TypeB $z) {}', ]; yield [ '<?php function foo(): TypeA|TypeB {}', '<?php function foo(): TypeA | TypeB {}', ]; yield [ '<?php class Foo { private array|int $x; }', '<?php class Foo { private array | int $x; }', ]; yield [ '<?php function foo(TypeA | TypeB $x) {}', ]; yield [ '<?php function foo(TypeA | TypeB $x) {}', null, ['space' => 'single'], ]; yield [ '<?php function foo(TypeA/* not a space */|/* not a space */TypeB $x) {}', ]; yield [ '<?php function foo(TypeA/* not a space */ | /* not a space */TypeB $x) {}', '<?php function foo(TypeA/* not a space */|/* not a space */TypeB $x) {}', ['space' => 'single'], ]; yield [ '<?php function foo(TypeA// not a space |//not a space TypeB $x) {}', ]; yield [ '<?php function foo(TypeA// not a space | //not a space TypeB $x) {}', '<?php function foo(TypeA// not a space |//not a space TypeB $x) {}', ['space' => 'single'], ]; yield [ '<?php class Foo { public function __construct( public int|string $a, protected int|string $b, private int|string $c ) {} }', '<?php class Foo { public function __construct( public int | string $a, protected int | string $b, private int | string $c ) {} }', ]; yield [ '<?php function foo(TypeA | TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', '<?php function foo(TypeA |TypeB $x) {} try {} catch (ErrorA| ErrorB $e) {} ', [ 'space' => 'single', 'space_multiple_catch' => 'none', ], ]; yield [ '<?php function foo(TypeA|TypeB $x) {} try {} catch (ErrorA | ErrorB $e) {} ', '<?php function foo(TypeA | TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', [ 'space' => 'none', 'space_multiple_catch' => 'single', ], ]; yield [ '<?php function foo(TypeA|TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', '<?php function foo(TypeA| TypeB $x) {} try {} catch (ErrorA |ErrorB $e) {} ', [ 'space' => 'none', 'space_multiple_catch' => 'none', ], ]; yield [ '<?php function foo(TypeA | TypeB $x) {} try {} catch (ErrorA | ErrorB $e) {} ', '<?php function foo(TypeA |TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', [ 'space' => 'single', 'space_multiple_catch' => 'single', ], ]; yield [ '<?php function foo(TypeA | TypeB $x) {} try {} catch (ErrorA | ErrorB $e) {} ', '<?php function foo(TypeA|TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', [ 'space' => 'single', ], ]; yield [ '<?php function foo(TypeA|TypeB $x) {} try {} catch (ErrorA|ErrorB $e) {} ', '<?php function foo(TypeA | TypeB $x) {} try {} catch (ErrorA | ErrorB $e) {} ', [ 'space' => 'none', ], ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php class Foo { public function __construct( public readonly int|string $a, protected readonly int|string $b, private readonly int|string $c ) {} }', '<?php class Foo { public function __construct( public readonly int | string $a, protected readonly int | string $b, private readonly int | string $c ) {} }', ]; yield [ '<?php function foo(): \Foo&Bar {}', '<?php function foo(): \Foo & Bar {}', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
tests/Fixer/Whitespace/StatementIndentationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class StatementIndentationFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void { $this->fixer->configure($configuration); $this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig()); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'no brace block' => [ '<?php foo(); bar();', '<?php foo(); bar();', ]; yield 'simple' => [ '<?php if ($foo) { foo(); bar(); }', '<?php if ($foo) { foo(); bar(); }', ]; yield 'braces on same line as code' => [ '<?php if ($foo) { foo(); if ($bar) { bar(); } }', '<?php if ($foo) { foo(); if ($bar) { bar(); } }', ]; yield 'with several closing braces on same line' => [ '<?php if ($foo) { foo(); if ($bar) { bar(); if ($baz) { baz(); }} foo(); } foo();', '<?php if ($foo) { foo(); if ($bar) { bar(); if ($baz) { baz(); }} foo(); } foo();', ]; yield 'with meaningful content on closing line' => [ '<?php if ($foo) { foo(); } foo();', '<?php if ($foo) { foo(); } foo();', ]; // @TODO brace at line 6 should have one level of indentation yield 'with several opening braces on same line' => [ '<?php if ($foo) { if ($foo) { foo(); if ($bar) { if ($bar) { bar(); } baz(); } } baz(); } baz();', '<?php if ($foo) { if ($foo) { foo(); if ($bar) { if ($bar) { bar(); } baz(); } } baz(); } baz();', ]; yield 'function definition arguments' => [ '<?php function foo( $bar, $baz ) { }', '<?php function foo( $bar, $baz ) { }', ]; yield 'anonymous function definition arguments' => [ '<?php $foo = function( $bar, $baz ) { };', '<?php $foo = function( $bar, $baz ) { };', ]; yield 'interface method definition arguments' => [ '<?php interface Foo { public function foo( $bar, $baz ); }', '<?php interface Foo { public function foo( $bar, $baz ); }', ]; yield 'class method definition arguments' => [ '<?php class Foo { public function foo( $bar, $baz ) { } }', '<?php class Foo { public function foo( $bar, $baz ) { } }', ]; yield 'multiple class methods with many permutations of visibility modifiers' => [ '<?php abstract class Test { final protected function test_final_protected() {} static private function test_static_private() {} private function test_private() {} private static function test_private_static() {} abstract public static function test_abstract_public_static(); abstract static public function test_abstract_static_public(); abstract public function test_abstract_public(); protected abstract function test_protected_abstract(); public abstract function test_public_abstract(); final static protected function test_final_static_protected() {} final private static function test_final_private_static() {} public final function test_public_final() {} final private function test_final_private() {} static final public function test_static_final_public() {} protected abstract static function test_protected_abstract_static(); public static abstract function test_public_static_abstract(); protected static abstract function test_protected_static_abstract(); static final function test_static_final() {} final static private function test_final_static_private() {} static protected abstract function test_static_protected_abstract(); public abstract static function test_public_abstract_static(); static final protected function test_static_final_protected() {} final public static function test_final_public_static() {} static final private function test_static_final_private() {} abstract protected function test_abstract_protected(); abstract static protected function test_abstract_static_protected(); private static final function test_private_static_final() {} final static function test_final_static() {} protected static function test_protected_static() {} protected function test_protected() {} public static function test_public_static() {} final function test_final() {} abstract protected static function test_abstract_protected_static(); static protected function test_static_protected() {} static abstract function test_static_abstract(); static abstract protected function test_static_abstract_protected(); protected final static function test_protected_final_static() {} static public final function test_static_public_final() {} public final static function test_public_final_static() {} abstract static function test_abstract_static(); public static final function test_public_static_final() {} static function test_static() {} abstract function test_abstract(); static protected final function test_static_protected_final() {} static private final function test_static_private_final() {} private final function test_private_final() {} static public abstract function test_static_public_abstract(); protected static final function test_protected_static_final() {} final protected static function test_final_protected_static() {} final static public function test_final_static_public() {} static public function test_static_public() {} function test_() {} static abstract public function test_static_abstract_public(); final public function test_final_public() {} private final static function test_private_final_static() {} protected final function test_protected_final() {} public function test_public() {} }', '<?php abstract class Test { final protected function test_final_protected() {} static private function test_static_private() {} private function test_private() {} private static function test_private_static() {} abstract public static function test_abstract_public_static(); abstract static public function test_abstract_static_public(); abstract public function test_abstract_public(); protected abstract function test_protected_abstract(); public abstract function test_public_abstract(); final static protected function test_final_static_protected() {} final private static function test_final_private_static() {} public final function test_public_final() {} final private function test_final_private() {} static final public function test_static_final_public() {} protected abstract static function test_protected_abstract_static(); public static abstract function test_public_static_abstract(); protected static abstract function test_protected_static_abstract(); static final function test_static_final() {} final static private function test_final_static_private() {} static protected abstract function test_static_protected_abstract(); public abstract static function test_public_abstract_static(); static final protected function test_static_final_protected() {} final public static function test_final_public_static() {} static final private function test_static_final_private() {} abstract protected function test_abstract_protected(); abstract static protected function test_abstract_static_protected(); private static final function test_private_static_final() {} final static function test_final_static() {} protected static function test_protected_static() {} protected function test_protected() {} public static function test_public_static() {} final function test_final() {} abstract protected static function test_abstract_protected_static(); static protected function test_static_protected() {} static abstract function test_static_abstract(); static abstract protected function test_static_abstract_protected(); protected final static function test_protected_final_static() {} static public final function test_static_public_final() {} public final static function test_public_final_static() {} abstract static function test_abstract_static(); public static final function test_public_static_final() {} static function test_static() {} abstract function test_abstract(); static protected final function test_static_protected_final() {} static private final function test_static_private_final() {} private final function test_private_final() {} static public abstract function test_static_public_abstract(); protected static final function test_protected_static_final() {} final protected static function test_final_protected_static() {} final static public function test_final_static_public() {} static public function test_static_public() {} function test_() {} static abstract public function test_static_abstract_public(); final public function test_final_public() {} private final static function test_private_final_static() {} protected final function test_protected_final() {} public function test_public() {} }', ]; yield 'trait method definition arguments' => [ '<?php trait Foo { public function foo( $bar, $baz ) { } }', '<?php trait Foo { public function foo( $bar, $baz ) { } }', ]; yield 'function call arguments' => [ '<?php foo( $bar, $baz );', '<?php foo( $bar, $baz );', ]; yield 'variable function call arguments' => [ '<?php $foo( $bar, $baz );', '<?php $foo( $bar, $baz );', ]; yield 'chained method calls' => [ '<?php if ($foo) { $foo ->bar() ->baz() ; }', '<?php if ($foo) { $foo ->bar() ->baz() ; }', ]; yield 'nested arrays (long syntax)' => [ '<?php if ($foo) { $foo = array( $foo, $bar ->bar() , array($baz) ) ; }', '<?php if ($foo) { $foo = array( $foo, $bar ->bar() , array($baz) ) ; }', ]; yield 'nested arrays (short syntax)' => [ '<?php if ($foo) { $foo = [ $foo, $bar ->bar() , [$baz] ] ; }', '<?php if ($foo) { $foo = [ $foo, $bar ->bar() , [$baz] ] ; }', ]; yield 'array (long syntax) with function call' => [ '<?php if ($foo) { $foo = array( foo( $bar, $baz ) ) ; }', '<?php if ($foo) { $foo = array( foo( $bar, $baz ) ) ; }', ]; yield 'array (short syntax) with function call' => [ '<?php if ($foo) { $foo = [ foo( $bar, $baz ) ] ; }', '<?php if ($foo) { $foo = [ foo( $bar, $baz ) ] ; }', ]; yield 'array (long syntax) with class instantiation' => [ '<?php if ($foo) { $foo = array( new Foo( $bar, $baz ) ) ; }', '<?php if ($foo) { $foo = array( new Foo( $bar, $baz ) ) ; }', ]; yield 'array (short syntax) with class instantiation' => [ '<?php if ($foo) { $foo = [ new Foo( $bar, $baz ) ] ; }', '<?php if ($foo) { $foo = [ new Foo( $bar, $baz ) ] ; }', ]; yield 'implements list' => [ '<?php class Foo implements Bar, Baz {}', '<?php class Foo implements Bar, Baz {}', ]; yield 'extends list' => [ '<?php interface Foo extends Bar, Baz {}', '<?php interface Foo extends Bar, Baz {}', ]; yield 'use list' => [ '<?php class Foo { use Bar, Baz; }', '<?php class Foo { use Bar, Baz; }', ]; yield 'chained method call with argument' => [ '<?php $foo ->bar( $baz );', '<?php $foo ->bar( $baz );', ]; yield 'argument separator on its own line' => [ '<?php foo( 1 , 2 );', '<?php foo( 1 , 2 );', ]; yield 'statement end on its own line' => [ '<?php if (true) { $foo = $a && $b ; }', '<?php if (true) { $foo = $a && $b ; }', ]; yield 'multiline control structure conditions' => [ '<?php if ($a && $b) { foo(); }', '<?php if ($a && $b) { foo(); }', ]; yield 'switch' => [ '<?php switch ($foo) { case 1: echo "foo"; break; case 2: echo "bar"; break; case 3: default: echo "baz"; }', '<?php switch ($foo) { case 1: echo "foo"; break; case 2: echo "bar"; break; case 3: default: echo "baz"; }', ]; yield 'array (long syntax) with anonymous class' => [ '<?php if ($foo) { $foo = array( new class ( $bar, $baz ) { private $foo; public function foo( $foo ) { return $foo; } } ) ; }', '<?php if ($foo) { $foo = array( new class ( $bar, $baz ) { private $foo; public function foo( $foo ) { return $foo; } } ) ; }', ]; yield 'array (short syntax) with anonymous class' => [ '<?php if ($foo) { $foo = [ new class ( $bar, $baz ) { private $foo; public function foo( $foo ) { return $foo; } } ] ; }', '<?php if ($foo) { $foo = [ new class ( $bar, $baz ) { private $foo; public function foo( $foo ) { return $foo; } } ] ; }', ]; yield 'expression function call arguments' => [ '<?php (\'foo\')( $bar, $baz );', '<?php (\'foo\')( $bar, $baz );', ]; yield 'arrow function definition arguments' => [ '<?php $foo = fn( $bar, $baz ) => null;', '<?php $foo = fn( $bar, $baz ) => null;', ]; yield 'multiline list in foreach' => [ '<?php foreach ($array as [ "foo" => $foo, "bar" => $bar, ]) { }', ]; yield 'switch case with control structure' => [ '<?php switch ($foo) { case true: if ($bar) { bar(); } return true; }', '<?php switch ($foo) { case true: if ($bar) { bar(); } return true; }', ]; yield 'comment in method calls chain' => [ '<?php $foo ->baz() /* ->baz() */ ;', ]; yield 'multiple anonymous functions as function arguments' => [ '<?php foo(function () { bar(); }, function () { baz(); });', ]; yield 'multiple anonymous functions as method arguments' => [ '<?php $this ->bar(function ($a) { echo $a; }, function ($b) { echo $b; }) ;', ]; yield 'semicolon on a newline inside a switch case without break statement' => [ '<?php switch (true) { case $foo: $foo ->baz() ; }', ]; yield 'alternative syntax' => [ '<?php if (1): ?> <div></div> <?php else: ?> <?php if (2): ?> <div></div> <?php else: ?> <div></div> <?php endif; ?> <?php endif; ?> ', ]; yield 'trait import with conflict resolution' => [ '<?php class Foo { use Bar, Baz { Baz::baz insteadof Bar; } }', '<?php class Foo { use Bar, Baz { Baz::baz insteadof Bar; } }', ]; yield 'multiline class definition' => [ '<?php class Foo extends BaseFoo implements Bar, Baz { public function foo() { } }', '<?php class Foo extends BaseFoo implements Bar, Baz { public function foo() { } }', ]; yield 'comment at end of switch case' => [ '<?php switch ($foo) { case 1: // Nothing to do }', ]; yield 'comment at end of switch default' => [ '<?php switch ($foo) { case 1: break; case 2: break; default: // Nothing to do }', ]; yield 'switch ending with empty case' => [ '<?php switch ($foo) { case 1: }', ]; yield 'switch ending with empty default' => [ '<?php switch ($foo) { default: }', ]; yield 'function ending with a comment and followed by a comma' => [ '<?php foo(function () { bar(); // comment }, );', ]; yield 'multiline arguments starting with "new" keyword' => [ '<?php $result1 = foo( new Bar1(), 1 ); $result2 = ($function)( new Bar2(), 2 ); $result3 = (new Argument())( new Bar3(), 3 );', ]; yield 'if with only a comment and followed by else' => [ '<?php if (true) { // foo } else { // bar }', '<?php if (true) { // foo } else { // bar }', ]; yield 'comment before else blocks WITHOUT stick_comment_to_next_continuous_control_statement' => [ '<?php // foo if ($foo) { echo "foo"; // bar } else { $aaa = 1; }', '<?php // foo if ($foo) { echo "foo"; // bar } else { $aaa = 1; }', ['stick_comment_to_next_continuous_control_statement' => false], ]; yield 'comment before else blocks WITH stick_comment_to_next_continuous_control_statement' => [ '<?php // foo if ($foo) { echo "foo"; // bar } else { $aaa = 1; }', '<?php // foo if ($foo) { echo "foo"; // bar } else { $aaa = 1; }', ['stick_comment_to_next_continuous_control_statement' => true], ]; yield 'multiline comment in block - describing next block' => [ '<?php if (1) { $b = "a"; // multiline comment line 1 // multiline comment line 2 // multiline comment line 3 } else { $c = "b"; }', '<?php if (1) { $b = "a"; // multiline comment line 1 // multiline comment line 2 // multiline comment line 3 } else { $c = "b"; }', ['stick_comment_to_next_continuous_control_statement' => true], ]; yield 'multiline comment in block - the only content in block' => [ '<?php if (1) { // multiline comment line 1 // multiline comment line 2 // multiline comment line 3 } else { $c = "b"; }', '<?php if (1) { // multiline comment line 1 // multiline comment line 2 // multiline comment line 3 } else { $c = "b"; }', ]; yield 'comment before elseif blocks' => [ '<?php // foo if ($foo) { echo "foo"; // bar } elseif(1) { echo "bar"; } elseif(2) { // do nothing } elseif(3) { $aaa = 1; // end comment in final block }', '<?php // foo if ($foo) { echo "foo"; // bar } elseif(1) { echo "bar"; } elseif(2) { // do nothing } elseif(3) { $aaa = 1; // end comment in final block }', ['stick_comment_to_next_continuous_control_statement' => true], ]; yield 'comments at the end of if/elseif/else blocks' => [ '<?php if ($foo) { echo "foo"; // foo } elseif ($bar) { echo "bar"; // bar } else { echo "baz"; // baz }', '<?php if ($foo) { echo "foo"; // foo } elseif ($bar) { echo "bar"; // bar } else { echo "baz"; // baz }', ['stick_comment_to_next_continuous_control_statement' => true], ]; yield 'if-elseif-else without braces' => [ '<?php if ($foo) foo(); elseif ($bar) bar(); else baz();', '<?php if ($foo) foo(); elseif ($bar) bar(); else baz();', ]; yield 'for without braces' => [ '<?php for (;;) foo();', '<?php for (;;) foo();', ]; yield 'foreach without braces' => [ '<?php foreach ($foo as $bar) foo();', '<?php foreach ($foo as $bar) foo();', ]; yield 'while without braces' => [ '<?php while (true) foo();', '<?php while (true) foo();', ]; yield 'do-while without braces' => [ '<?php do foo(); while (true);', '<?php do foo(); while (true);', ]; yield 'nested control structures without braces' => [ '<?php if (true) if (true) if (true) for ($i = 0; $i < 1; ++$i) echo 1; elseif (true) foreach ([] as $foo) echo 2; else if (true) while (true) echo 3; else do echo 4; while (true); else echo 5;', '<?php if (true) if (true) if (true) for ($i = 0; $i < 1; ++$i) echo 1; elseif (true) foreach ([] as $foo) echo 2; else if (true) while (true) echo 3; else do echo 4; while (true); else echo 5;', ]; yield 'mixex if-else with and without braces' => [ '<?php if (true) if (true) { if (true) echo 1; else echo 2; } else { echo 3; } else echo 4;', '<?php if (true) if (true) { if (true) echo 1; else echo 2; } else { echo 3; } else echo 4;', ]; yield 'empty if and else without braces' => [ '<?php if (true) { if (false); elseif (false); else if (false); else echo 1; }', '<?php if (true) { if (false); elseif (false); else if (false); else echo 1; }', ]; yield 'multiline class constant' => [ '<?php class Foo { const FOO = 1; }', '<?php class Foo { const FOO = 1; }', ]; yield 'multiline class constant with visibility' => [ '<?php class Foo { public const FOO = 1; protected const BAR = 1; private const BAZ = 1; }', '<?php class Foo { public const FOO = 1; protected const BAR = 1; private const BAZ = 1; }', ]; yield 'multiline comma-separated class constants' => [ '<?php class Foo { const FOO = 1, BAR = 2; }', '<?php class Foo { const FOO = 1, BAR = 2; }', ]; yield 'multiline class constant with array value' => [ '<?php class Foo { const FOO = [ 1 ]; }', '<?php class Foo { const FOO = [ 1 ]; }', ]; yield 'multiline class constants with array key/value' => [ '<?php class Foo { const READY = "GO", FOO = [ "foo" => "bar", ], BAR = "Foo", STOP = "STOP"; }', '<?php class Foo { const READY = "GO", FOO = [ "foo" => "bar", ], BAR = "Foo", STOP = "STOP"; }', ]; yield 'multiline class constant with semicolon on next line' => [ '<?php class Foo { public const FOO = 1 ; }', '<?php class Foo { public const FOO = 1 ; }', ]; yield 'multiline class property' => [ '<?php class Foo { public $foo; protected $bar; private $baz; }', '<?php class Foo { public $foo; protected $bar; private $baz; }', ]; yield 'multiline class property with default value' => [ '<?php class Foo { public $foo = 1; }', '<?php class Foo { public $foo = 1; }', ]; yield 'multiline class typed property' => [ '<?php class Foo { public int $foo; }', '<?php class Foo { public int $foo; }', ]; yield 'multiline class static property' => [ '<?php class Foo { public static $foo; static public $bar; }', '<?php class Foo { public static $foo; static public $bar; }', ]; yield 'multiline comma-separated class properties' => [ '<?php class Foo { public $foo, $bar; }', '<?php class Foo { public $foo, $bar; }', ]; yield 'multiline class property with array value' => [ '<?php class Foo { public $foo = [ 1 ]; }', '<?php class Foo { public $foo = [ 1 ]; }', ]; yield 'multiline class property with semicolon on next line' => [ '<?php class Foo { public $foo ; }', '<?php class Foo { public $foo ; }', ]; yield 'multiline class property with var' => [ '<?php class Foo { var $foo; }', '<?php class Foo { var $foo; }', ]; yield 'property with multiline array containing explicit keys' => [ '<?php class Foo { private $foo = [ \'a\' => 1, \'b\' => 2, ]; }', ]; yield 'array with static method call' => [ '<?php $foo = [ static::createNoErrorReport(), 1, ];', ]; yield 'ternary operator in property' => [ <<<'PHP' <?php class Foo { public int $bar = BAZ ? -1 : 1; } PHP, ]; yield 'braceless if with return' => [ <<<'PHP' <?php if (1 == 2) return; if (1 == 2) return false; PHP, ]; yield 'functions "set" and "get" (like property hooks, but not)' => [ <<<'PHP' <?php if ($x) { set(); } elseif ($y) { SET(); } else { get(); } PHP, ]; yield 'with tabs' => [ "<?php if (\$foo) { \tfoo(); \tbar(); }", '<?php if ($foo) { foo(); bar(); }', [], new WhitespacesFixerConfig("\t"), ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield 'match expression' => [ '<?php return match ($bool) { 0 => false, 1 => true, default => throw new Exception(), };', '<?php return match ($bool) { 0 => false, 1 => true, default => throw new Exception(), };', ]; yield 'attribute' => [ '<?php class Foo { #[SimpleAttribute] #[ MultilineAttribute ] #[ComplexAttribute( foo: true, bar: [ 1, 2, 3, ] )] public function bar() { } }', '<?php class Foo { #[SimpleAttribute] #[ MultilineAttribute ] #[ComplexAttribute( foo: true, bar: [ 1, 2, 3, ] )] public function bar() { } }', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix81Cases *
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Whitespace/NoWhitespaceInBlankLineFixerTest.php
tests/Fixer/Whitespace/NoWhitespaceInBlankLineFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoWhitespaceInBlankLineFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ "<?php\n", ]; yield [ '<?php ', ]; yield [ '<?php ', '<?php ', ]; yield [ '<?php ', '<?php '.' ', ]; yield [ '<?php $a = 1; ', '<?php '.' $a = 1; ', ]; yield [ '<?php $r = 5 +6; '.' $t = true> 9; '.' ', ]; yield [ '<?php $a = 1; ', ]; yield [ "<?php \t\$b = 1;\t\t", ]; yield [ '<?php $b = 2; ', '<?php $b = 2; ', ]; yield [ '<?php $b = 3; ', '<?php $b = 3; '.' '.' ', ]; yield [ '<?php $b = 4; $b += 4;', '<?php $b = 4; '.' '.' '.' $b += 4;', ]; yield [ "<?php\n\n\n\$b = 5;", "<?php\n \n\t\n\$b = 5;", ]; yield [ "<?php\necho 1;\n?>\n\n\n\n", ]; yield [ "<?php\necho <<<HTML\ndata \n \n \t \n \nHTML\n;\n//a", ]; yield [ "<?php\n\$sql = 'SELECT * FROM products WHERE description = \"This product\n \nis nice\"';", ]; yield [ '<?php /** * @const Foo. */ const FOO = "BAR"; ', ]; yield [ "<?php\n\n \$a = 1;\n\n \$b = 2;", "<?php\n\n \$a = 1;\n \n \$b = 2;", ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\n\r\n \$a = 1;\r\n\r\n \$b = 2;", "<?php\r\n\r\n \$a = 1;\r\n \r\n \$b = 2;", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ListNotation/ListSyntaxFixerTest.php
tests/Fixer/ListNotation/ListSyntaxFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ListNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ListSyntaxFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'default' => [ '<?php $a = [$a, $b] = $a; [$b] = $a;', '<?php $a = list($a, $b) = $a; [$b] = $a;', ]; yield from self::getFixToShortSyntaxCases(); foreach (self::getFixToShortSyntaxCases() as $label => $shortCase) { if ('messy comments case' === $label) { continue; } yield 'to long - '.$label => [$shortCase[1], $shortCase[0], ['syntax' => 'long']]; } // the reverse of this is different because of all the comments and white space, // therefore we override with a similar case here yield 'comment case' => [ '<?php # list(# $a# )# =# $a# ;#', '<?php # [# $a# ]# =# $a# ;#', ['syntax' => 'long'], ]; yield ['<?php class Test { public function updateAttributeKey($key, $value) { $this->{camel_case($attributes)}[$key] = $value; } }', null, ['syntax' => 'long'], ]; yield [ '<?php [$b[$a]] = $foo();', null, ['syntax' => 'long'], ]; yield [ '<?php [$iHaveList => list($x, $y) = getList()];', '<?php [$iHaveList => [$x, $y] = getList()];', ['syntax' => 'long'], ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string}> */ public static function provideFix81Cases(): iterable { yield 'simple 8.1' => [ '<?php $a = _list(...);', ]; } /** * @return iterable<array{string, string}> */ private static function getFixToShortSyntaxCases(): iterable { yield [ '<?php [$x] = $a;', '<?php list($x) = $a;', ]; yield [ '<?php [$a, $b, $c] = $array;', '<?php list($a, $b, $c) = $array;', ]; yield [ '<?php ["a" => $a, "b" => $b, "c" => $c] = $array;', '<?php list("a" => $a, "b" => $b, "c" => $c) = $array;', ]; yield [ '<?php # [// $x] =/**/$a?>', '<?php # list(// $x) =/**/$a?>', ]; yield 'messy comments case' => [ '<?php #a #g [#h #f $a# #e ]# # =#c # $a;# # ', '<?php #a list#g (#h #f $a# #e )# # =#c # $a;# # ', ]; yield [ '<?php [$a, $b,, [$c, $d]] = $a;', '<?php list($a, $b,, list($c, $d)) = $a;', ]; yield [ '<?php [[$a, $b], [$c, $d]] = $a;', '<?php list(list($a, $b), list($c, $d)) = $a;', ]; yield [ '<?php [[$a, [$b]], [[$c, [$d]]]] = $a;', '<?php list(list($a, list($b)), list(list($c, list($d)))) = $a;', ]; yield [ '<?php [[$a]] = $foo();', '<?php list(list($a)) = $foo();', ]; yield [ '<?php foreach ($z as [$a, $b]) {}', '<?php foreach ($z as list($a, $b)) {}', ]; yield [ '<?php [$a, $b,, [$c, $d]] = $a;', '<?php list($a, $b,, list($c, $d)) = $a;', ]; yield [ '<?php [&$a, $b] = $a;', '<?php list(&$a, $b) = $a;', ]; yield [ '<?php [&$a,/* */&$b] = $a;', '<?php list(&$a,/* */&$b) = $a;', ]; yield [ '<?php [&$a, $b,, [&$c, $d]] = $a;', '<?php list(&$a, $b,, list(&$c, $d)) = $a;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/ProtectedToPrivateFixerTest.php
tests/Fixer/ClassNotation/ProtectedToPrivateFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ProtectedToPrivateFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { $attributesAndMethodsOriginal = self::getAttributesAndMethods(true); $attributesAndMethodsFixed = self::getAttributesAndMethods(false); yield 'final-extends' => [ "<?php final class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }", ]; yield 'normal-extends' => [ "<?php class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }", ]; yield 'abstract' => [ "<?php abstract class MyAbstractClass { {$attributesAndMethodsOriginal} }", ]; yield 'normal' => [ "<?php class MyClass { {$attributesAndMethodsOriginal} }", ]; yield 'trait' => [ "<?php trait MyTrait { {$attributesAndMethodsOriginal} }", ]; yield 'final-with-trait' => [ "<?php final class MyClass { use MyTrait; {$attributesAndMethodsOriginal} }", ]; yield 'multiline-comment' => [ '<?php final class MyClass { /* public protected private */ }', ]; yield 'inline-comment' => [ "<?php final class MyClass { \n // public protected private \n }", ]; yield 'final' => [ "<?php final class MyClass { {$attributesAndMethodsFixed} } class B {use C;}", "<?php final class MyClass { {$attributesAndMethodsOriginal} } class B {use C;}", ]; yield 'final-implements' => [ "<?php final class MyClass implements MyInterface { {$attributesAndMethodsFixed} }", "<?php final class MyClass implements MyInterface { {$attributesAndMethodsOriginal} }", ]; yield 'final-with-use-before' => [ "<?php use stdClass; final class MyClass { {$attributesAndMethodsFixed} }", "<?php use stdClass; final class MyClass { {$attributesAndMethodsOriginal} }", ]; yield 'final-with-use-after' => [ "<?php final class MyClass { {$attributesAndMethodsFixed} } use stdClass;", "<?php final class MyClass { {$attributesAndMethodsOriginal} } use stdClass;", ]; yield 'multiple-classes' => [ "<?php final class MyFirstClass { {$attributesAndMethodsFixed} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsFixed} } ", "<?php final class MyFirstClass { {$attributesAndMethodsOriginal} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsOriginal} } ", ]; yield 'minimal-set' => [ '<?php final class MyClass { private $v1; }', '<?php final class MyClass { protected $v1; }', ]; yield 'anonymous-class-inside' => [ "<?php final class Foo { {$attributesAndMethodsFixed} private function bar() { new class { {$attributesAndMethodsOriginal} }; } } ", "<?php final class Foo { {$attributesAndMethodsOriginal} protected function bar() { new class { {$attributesAndMethodsOriginal} }; } } ", ]; yield [ '<?php $a = new class{protected function A(){ echo 123; }};', ]; yield [ '<?php final class Foo { private int $foo; }', '<?php final class Foo { protected int $foo; }', ]; yield [ '<?php final class Foo { private ?string $foo; }', '<?php final class Foo { protected ?string $foo; }', ]; yield [ '<?php final class Foo { private array $foo; }', '<?php final class Foo { protected array $foo; }', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield 'type union' => [ '<?php final class Foo2 { private int|float $a; } ', '<?php final class Foo2 { protected int|float $a; } ', ]; yield 'promoted properties' => [ <<<'PHP' <?php final class Foo { public function __construct( private null|Bar $x, private ?Bar $u, ) {} } PHP, <<<'PHP' <?php final class Foo { public function __construct( protected null|Bar $x, protected ?Bar $u, ) {} } PHP, ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php final class Foo { private readonly int $d; } ', '<?php final class Foo { protected readonly int $d; } ', ]; yield 'protected final const' => [ // '<?php final class Foo { final private const Y = "i"; }', 'Fatal error: Private constant Foo::Y cannot be final as it is not visible to other classes on line 1. '<?php final class Foo1 { final protected const Y = "abc"; } final class Foo2 { protected final const Y = "def"; } ', ]; yield [ '<?php final class Foo2 { private const X = "tty"; }', '<?php final class Foo2 { protected const X = "tty"; }', ]; yield [ '<?php final class Foo { private Foo1&Bar $foo; }', '<?php final class Foo { protected Foo1&Bar $foo; }', ]; // https://wiki.php.net/rfc/enumerations // Methods may be public, private, or protected, although in practice private and protected are equivalent as inheritance is not allowed. yield 'enum' => [ '<?php enum Foo: string { private const Spades = 123; case Hearts = "H"; private function test() { echo 123; } } Foo::Hearts->test(); ', '<?php enum Foo: string { protected const Spades = 123; case Hearts = "H"; protected function test() { echo 123; } } Foo::Hearts->test(); ', ]; yield 'enum with trait' => [ '<?php trait NamedDocumentStatus { public function getStatusName(): string { return $this->getFoo(); } } enum DocumentStats { use NamedDocumentStatus; case DRAFT; private function getFoo(): string { return "X"; } } echo DocumentStats::DRAFT->getStatusName(); ', '<?php trait NamedDocumentStatus { public function getStatusName(): string { return $this->getFoo(); } } enum DocumentStats { use NamedDocumentStatus; case DRAFT; protected function getFoo(): string { return "X"; } } echo DocumentStats::DRAFT->getStatusName(); ', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix82Cases(): iterable { yield 'final readonly' => [ '<?php final readonly class Foo { private function noop(): void{} }', '<?php final readonly class Foo { protected function noop(): void{} }', ]; yield 'final readonly reversed' => [ '<?php readonly final class Foo { private function noop(): void{} }', '<?php readonly final class Foo { protected function noop(): void{} }', ]; } /** * @dataProvider provideFix84Cases * * @requires PHP >= 8.4 */ public function testFix84(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix84Cases(): iterable { yield 'asymmetric visibility with only set visibility' => [ '<?php final class Foo { private(set) int $a; }', '<?php final class Foo { protected(set) int $a; }', ]; yield 'asymmetric visibility with both visibilities' => [ '<?php final class Foo { public private(set) int $a; private private(set) int $b; private private(set) int $c; }', '<?php final class Foo { public protected(set) int $a; protected protected(set) int $b; protected private(set) int $c; }', ]; } private static function getAttributesAndMethods(bool $original): string { $attributesAndMethodsOriginal = ' public $v1; protected $v2; private $v3; public static $v4; protected static $v5; private static $v6; public function f1(){} protected function f2(){} private function f3(){} public static function f4(){} protected static function f5(){} private static function f6(){} '; if ($original) { return $attributesAndMethodsOriginal; } return str_replace('protected', 'private', $attributesAndMethodsOriginal); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/StringableForToStringFixerTest.php
tests/Fixer/ClassNotation/StringableForToStringFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\StringableForToStringFixer * * @requires PHP 8.0 * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\StringableForToStringFixer> * * @author Santiago San Martin <sanmartindev@gmail.com> * @author Kuba Werłos <werlos@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class StringableForToStringFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield ['<?php class Foo {}']; yield ['<?php class Foo { public function toString() { return "Foo"; } }']; yield ['<?php class Foo implements STRINGABLE { public function __toString() { return "Foo"; } }']; yield ['<?php class Foo implements Stringable { public function __toString() { return "Foo"; } }']; yield ['<?php class Foo implements \Stringable { public function __toString() { return "Foo"; } }']; yield ['<?php namespace Foo; use Stringable; class Bar implements Stringable { public function __toString() { return ""; } } ']; yield ['<?php use Stringable as Stringy; class Bar implements Stringy { public function __toString() { return ""; } } ']; yield ['<?php namespace Foo; use Stringable as Stringy; class Bar implements Stringy { public function __toString() { return ""; } } ']; yield ['<?php namespace Foo; use \Stringable; class Bar implements Stringable { public function __toString() { return ""; } } ']; yield ['<?php namespace Foo; use Bar; use STRINGABLE; use Baz; class Qux implements Stringable { public function __toString() { return ""; } } ']; yield ['<?php class Foo { public function toString() { function () { return 0; }; return "Foo"; } }']; yield ['<?php class Foo { public function bar() { $ohject->__toString(); } }']; yield [ '<?php class Foo implements \Stringable { public function __toString() { return "Foo"; } } ', '<?php class Foo { public function __toString() { return "Foo"; } } ', ]; yield [ '<?php namespace FooNamespace; class Foo implements \Stringable { public function __toString() { return "Foo"; } } ', '<?php namespace FooNamespace; class Foo { public function __toString() { return "Foo"; } } ', ]; yield [ '<?php namespace FooNamespace; use Bar\Stringable; class Foo implements \Stringable, Stringable { public function __toString() { return "Foo"; } } ', '<?php namespace FooNamespace; use Bar\Stringable; class Foo implements Stringable { public function __toString() { return "Foo"; } } ', ]; yield [ <<<'PHP' <?php use NotStringable as Stringy; class Bar implements \Stringable, Stringy { public function __toString() { return ""; } } PHP, <<<'PHP' <?php use NotStringable as Stringy; class Bar implements Stringy { public function __toString() { return ""; } } PHP, ]; $template = '<?php namespace FooNamespace; class Test implements %s { public function __toString() { return "Foo"; } } '; $implementedInterfacesCases = [ \Stringable::class, 'Foo\Stringable', '\Foo\Stringable', 'Foo\Stringable\Bar', '\Foo\Stringable\Bar', 'Foo\Stringable, Bar\Stringable', 'Stringable\Foo, Stringable\Bar', '\Stringable\Foo, Stringable\Bar', 'Foo\Stringable\Bar', '\Foo\Stringable\Bar', ]; foreach ($implementedInterfacesCases as $implementedInterface) { yield [ \sprintf($template, '\Stringable, '.$implementedInterface), \sprintf($template, $implementedInterface), ]; } yield [ '<?php class Foo implements \Stringable, FooInterface { public function __toString() { return "Foo"; } } ', '<?php class Foo implements FooInterface { public function __toString() { return "Foo"; } } ', ]; yield [ '<?php class Foo extends Bar implements \Stringable { public function __toString() { return "Foo"; } } ', '<?php class Foo extends Bar { public function __toString() { return "Foo"; } } ', ]; yield [ '<?php class Foo implements \Stringable { public function __TOSTRING() { return "Foo"; } } ', '<?php class Foo { public function __TOSTRING() { return "Foo"; } } ', ]; yield [ '<?php namespace Foo; use Bar; class Baz implements \Stringable, Stringable { public function __toString() { return ""; } } ', '<?php namespace Foo; use Bar; class Baz implements Stringable { public function __toString() { return ""; } } ', ]; yield [ '<?php new class implements \Stringable { public function __construct() {} public function __toString() {} }; ', '<?php new class { public function __construct() {} public function __toString() {} }; ', ]; yield [ '<?php new class() implements \Stringable { public function __construct() {} public function __toString() {} }; ', '<?php new class() { public function __construct() {} public function __toString() {} }; ', ]; yield [ '<?php class Foo1 implements \Stringable { public function __toString() { return "1"; } } class Foo2 { public function __noString() { return "2"; } } class Foo3 implements \Stringable { public function __toString() { return "3"; } } class Foo4 { public function __noString() { return "4"; } } class Foo5 { public function __noString() { return "5"; } } ', '<?php class Foo1 { public function __toString() { return "1"; } } class Foo2 { public function __noString() { return "2"; } } class Foo3 { public function __toString() { return "3"; } } class Foo4 { public function __noString() { return "4"; } } class Foo5 { public function __noString() { return "5"; } } ', ]; yield [ '<?php namespace Foo { class C implements \Stringable, I { public function __toString() { return ""; } }} namespace Bar { class C implements \Stringable, I { public function __toString() { return ""; } }} namespace Baz { class C implements I, \Stringable { public function __toString() { return ""; } }} namespace Qux { class C implements \Stringable, I { public function __toString() { return ""; } }} ; ', '<?php namespace Foo { class C implements I { public function __toString() { return ""; } }} namespace Bar { class C implements \Stringable, I { public function __toString() { return ""; } }} namespace Baz { class C implements I, \Stringable { public function __toString() { return ""; } }} namespace Qux { class C implements I { public function __toString() { return ""; } }} ; ', ]; yield [ '<?php namespace Foo { use Stringable as Stringy; class C {} } namespace Bar { class C implements \Stringable, Stringy { public function __toString() { return ""; } }} ; ', '<?php namespace Foo { use Stringable as Stringy; class C {} } namespace Bar { class C implements Stringy { public function __toString() { return ""; } }} ; ', ]; yield ['<?php namespace Foo; use Stringable; class Bar { public function foo() { new class () implements Stringable { public function __toString() { return ""; } }; } } ']; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/NoUnneededFinalMethodFixerTest.php
tests/Fixer/ClassNotation/NoUnneededFinalMethodFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\NoUnneededFinalMethodFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\NoUnneededFinalMethodFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\NoUnneededFinalMethodFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoUnneededFinalMethodFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'default' => [ '<?php final class Foo { public function foo() {} protected function bar() {} private function baz() {} }', '<?php final class Foo { final public function foo() {} final protected function bar() {} final private function baz() {} }', ]; yield 'final-after-visibility' => [ '<?php final class Foo { public function foo() {} protected function bar() {} private function baz() {} }', '<?php final class Foo { public final function foo() {} protected final function bar() {} private final function baz() {} }', ]; yield 'default-static' => [ '<?php final class SomeClass { public static function foo() {} protected static function bar() {} private static function baz() {} }', '<?php final class SomeClass { final public static function foo() {} final protected static function bar() {} final private static function baz() {} }', ]; yield 'visibility-then-final-then-static' => [ '<?php final class SomeClass { public static function foo() {} protected static function bar() {} private static function baz() {} }', '<?php final class SomeClass { public final static function foo() {} protected final static function bar() {} private final static function baz() {} }', ]; yield 'visibility-then-static-then-final' => [ '<?php final class SomeClass { public static function foo() {} protected static function bar() {} private static function baz() {} }', '<?php final class SomeClass { public static final function foo() {} protected static final function bar() {} private static final function baz() {} }', ]; yield 'static-then-visibility-then-final' => [ '<?php final class SomeClass { static public function foo() {} static protected function bar() {} static private function baz() {} }', '<?php final class SomeClass { static public final function foo() {} static protected final function bar() {} static private final function baz() {} }', ]; yield 'static-then-final-then-visibility' => [ '<?php final class SomeClass { static public function foo() {} static protected function bar() {} static private function baz() {} }', '<?php final class SomeClass { static final public function foo() {} static final protected function bar() {} static final private function baz() {} }', ]; yield 'no-visibility' => [ '<?php final class Foo { function foo() {} function bar() {} function baz() {} }', '<?php final class Foo { final function foo() {} final function bar() {} final function baz() {} }', ]; yield 'no-visibility-final-then-static' => [ '<?php final class SomeClass { static function foo() {} static function bar() {} static function baz() {} }', '<?php final class SomeClass { final static function foo() {} final static function bar() {} final static function baz() {} }', ]; yield 'no-visibility-static-then-final' => [ '<?php final class SomeClass { static function foo() {} static function bar() {} static function baz() {} }', '<?php final class SomeClass { static final function foo() {} static final function bar() {} static final function baz() {} }', ]; yield 'private-method' => [ '<?php class Foo { final function bar0() {} final public function bar1() {} final protected function bar2() {} final static public function bar4() {} final public static function bar5() {} private function bar31() {} private function bar32() {} }', '<?php class Foo { final function bar0() {} final public function bar1() {} final protected function bar2() {} final static public function bar4() {} final public static function bar5() {} final private function bar31() {} private final function bar32() {} }', ]; yield 'private-method-with-visibility-before-final' => [ '<?php class Foo { private function bar() {} }', '<?php class Foo { private final function bar() {} }', ]; yield 'preserve-comment' => [ '<?php final class Foo { /* comment */public function foo() {} }', '<?php final class Foo { final/* comment */public function foo() {} }', ]; yield 'multiple-classes-per-file' => [ '<?php final class Foo { public function foo() {} } abstract class Bar { final public function bar() {} }', '<?php final class Foo { final public function foo() {} } abstract class Bar { final public function bar() {} }', ]; yield 'non-final' => [ '<php class Foo { final public function foo() {} }', ]; yield 'abstract-class' => [ '<php abstract class Foo { final public function foo() {} }', ]; yield 'final-method-with-private-attribute' => [ '<?php abstract class Foo { private static $var; final public function foo() {} }', ]; yield 'trait' => [ '<php trait Foo { final public function foo() {} }', ]; yield 'do not fix constructors' => [ '<?php class Bar { final private function __construct() { } }', ]; yield 'anonymous-class-inside' => [ '<?php final class Foo { public function foo() { } private function bar() { new class { final public function baz() { } }; } } ', '<?php final class Foo { final public function foo() { } private function bar() { new class { final public function baz() { } }; } } ', ]; yield 'anonymous-class-inside-with-final-private-method' => [ '<?php class Foo { private function bar() { new class { private function qux() { } }; } } ', '<?php class Foo { private function bar() { new class { final private function qux() { } }; } } ', ]; yield 'final private static' => [ '<?php class Foo { public function bar(){} private static function bar1() {echo 1;} private static function bar2() {echo 2;} static private function bar3() {echo 3;} private static function bar4() {echo 4;} static private function bar5() {echo 5;} static private function bar6() {echo 6;} } ', '<?php class Foo { public function bar(){} private static final function bar1() {echo 1;} private final static function bar2() {echo 2;} final static private function bar3() {echo 3;} final private static function bar4() {echo 4;} static final private function bar5() {echo 5;} static private final function bar6() {echo 6;} } ', ]; yield [ '<?php abstract class Foo { public final function bar1(){ $this->bar3(); } private function bar2(){ echo 1; } private function bar3(){ echo 2; } }', '<?php abstract class Foo { public final function bar1(){ $this->bar3(); } private function bar2(){ echo 1; } private final function bar3(){ echo 2; } }', ]; yield [ '<?php final class Foo { private function baz() {} } class Bar { final private function bar1() {} } ', '<?php final class Foo { final private function baz() {} } class Bar { final private function bar1() {} } ', ['private_methods' => false], ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php final class Foo81 { public readonly string $prop1; readonly public string $prop2; readonly string $prop3; } ', ]; yield [ '<?php class Foo81 { public readonly string $prop1; readonly public string $prop2; readonly string $prop3; } ', ]; yield [ '<?php final class Foo81 { public function foo81() {} protected function bar81() {} private function baz81() {} public readonly string $prop81; final public const Y = "i81"; final const XY = "i81"; } ', '<?php final class Foo81 { final public function foo81() {} final protected function bar81() {} final private function baz81() {} public readonly string $prop81; final public const Y = "i81"; final const XY = "i81"; } ', ]; yield 'enum' => [ '<?php enum Foo: string { case Hearts = "H"; public function test() { echo 123; } } var_dump(Foo::Spades);', '<?php enum Foo: string { case Hearts = "H"; final public function test() { echo 123; } } var_dump(Foo::Spades);', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix82Cases(): iterable { yield 'final readonly class - final after visibility method' => [ '<?php final readonly class Foo { public function foo() {} protected function bar() {} private function baz() {} }', '<?php final readonly class Foo { public final function foo() {} protected final function bar() {} private final function baz() {} }', ]; yield 'readonly comment final class - final before visibility method' => [ '<?php readonly /* X */ final class Foo { public function foo() {} protected function bar() {} private function baz() {} }', '<?php readonly /* X */ final class Foo { final public function foo() {} final protected function bar() {} final private function baz() {} }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/PhpdocReadonlyClassCommentToKeywordFixerTest.php
tests/Fixer/ClassNotation/PhpdocReadonlyClassCommentToKeywordFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\PhpdocReadonlyClassCommentToKeywordFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\PhpdocReadonlyClassCommentToKeywordFixer> * * @author Marcel Behrmann <marcel@behrmann.dev> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class PhpdocReadonlyClassCommentToKeywordFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @requires PHP 8.2 */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ <<<'EOT' <?php readonly class C { } EOT, <<<'EOT' <?php /** @readonly */ class C { } EOT, ]; yield [ <<<'EOT' <?php /** */ readonly class C { } EOT, <<<'EOT' <?php /** * @readonly */ class C { } EOT, ]; yield [ <<<'EOT' <?php /** * Very impressive class * */ readonly class C { } EOT, <<<'EOT' <?php /** * Very impressive class * * @readonly */ class C { } EOT, ]; yield [ <<<'EOT' <?php /** */ final readonly class C { } EOT, <<<'EOT' <?php /** * @readonly */ final class C { } EOT, ]; yield [ <<<'EOT' <?php /** */ abstract readonly class C { } EOT, <<<'EOT' <?php /** * @readonly */ abstract class C { } EOT, ]; yield [ <<<'EOT' <?php /** */ readonly class A { } EOT, <<<'EOT' <?php /** * @readonly */ readonly class A { } EOT, ]; yield [ <<<'EOT' <?php /** Class A. */ class A { } EOT, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/FinalClassFixerTest.php
tests/Fixer/ClassNotation/FinalClassFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\FinalClassFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\FinalClassFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class FinalClassFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield ['<?php /** @Entity */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @ORM\Entity */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @ORM\Entity(repositoryClass="MyRepository") */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping; /** @Mapping\Entity */ class MyEntity {}']; yield ['<?php use Doctrine\ORM; /** @ORM\Mapping\Entity */ class MyEntity {}']; yield ['<?php /** @Document */ class MyDocument {}']; yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** @ODM\Document */ class MyEntity {}']; yield ['<?php /** @entity */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @orm\entity */ class MyEntity {}']; yield ['<?php abstract class MyAbstract {}']; yield ['<?php trait MyTrait {}']; yield ['<?php interface MyInterface {}']; yield ['<?php echo Exception::class;']; yield [ '<?php final class MyClass {}', '<?php class MyClass {}', ]; yield [ '<?php final class MyClass extends MyAbstract {}', '<?php class MyClass extends MyAbstract {}', ]; yield [ '<?php final class MyClass implements MyInterface {}', '<?php class MyClass implements MyInterface {}', ]; yield [ '<?php /** @codeCoverageIgnore */ final class MyEntity {}', '<?php /** @codeCoverageIgnore */ class MyEntity {}', ]; yield [ '<?php final class A {} abstract class B {} final class C {}', '<?php class A {} abstract class B {} class C {}', ]; yield [ '<?php /** @internal Map my app to an @Entity */ final class MyMapper {}', '<?php /** @internal Map my app to an @Entity */ class MyMapper {}', ]; yield ['<?php $anonymClass = new class {};']; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield ['<?php #[Entity] class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass:"MyRepository")] class MyEntity {}']; yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] class MyEntity {}']; yield ['<?php #[Document] class MyDocument {}']; yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\entity] class MyEntity {}']; yield ['<?php #[IgnoredAttribute] #[Entity] class MyEntity {}']; yield ['<?php #[IgnoredAttribute("Some-Value"), Entity] class MyEntity {}']; // Test with comments in between attributes and class yield ['<?php #[Entity] /* some comment */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] /* some comment */ class MyEntity {}']; yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] /* some comment */ class MyEntity {}']; yield ['<?php #[Document] /* some comment */ class MyDocument {}']; yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] /* some comment */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute] #[Entity] /* some comment */ class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute("Some-Value"), Entity] /* some comment */ class MyEntity {}']; // Test with comments before the class yield ['<?php /* some comment */ #[Entity] class MyEntity {}']; yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class MyEntity {}']; yield ['<?php /* some comment */ use Doctrine\ORM; #[ORM\Mapping\Entity] class MyEntity {}']; yield ['<?php /* some comment */ #[Document] class MyDocument {}']; yield ['<?php /* some comment */ use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] class MyEntity {}']; yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[ORM\entity] class MyEntity {}']; yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute] #[Entity] class MyEntity {}']; yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute, Entity] class MyEntity {}']; // Multiline tests yield [ <<<'EOF' <?php use Doctrine\ORM; #[IgnoredAttribute("Some-Value"), IgnoredAttribute("Another-Value")] #[ORM\Mapping\Entity] /** * multi * line */ class MyEntity {} EOF, ]; yield [ <<<'EOF' <?php use Doctrine\ORM; #[ IgnoredAttribute("Some-Value"), IgnoredAttribute("Another-Value"),# ORM\Mapping\Entity, ] /** * multi * line */ class MyEntity {} EOF, ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix82Cases(): iterable { yield ['<?php #[Entity] readonly class MyEntity {}']; yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] readonly class MyEntity {}']; yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] readonly class MyEntity {}']; yield ['<?php #[Document] readonly class MyDocument {}']; yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] readonly class MyEntity {}']; yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] readonly /* ... */ class MyEntity {}']; yield [ <<<'EOF' <?php use Doctrine\ORM; #[ORM\Mapping\Entity] readonly /** * multi * line */ class MyEntity {} EOF, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/SingleTraitInsertPerStatementFixerTest.php
tests/Fixer/ClassNotation/SingleTraitInsertPerStatementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SingleTraitInsertPerStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple' => [ '<?php final class Example { use Foo;use Bar; } ', '<?php final class Example { use Foo, Bar; } ', ]; yield 'simple I' => [ '<?php final class Example { use Foo;use Bar; } ', '<?php final class Example { use Foo,Bar; } ', ]; yield 'simple II' => [ '<?php use Foo\Bar, Foo\Bar2; // do not touch final class Example { use Foo;use Bar ; } ', '<?php use Foo\Bar, Foo\Bar2; // do not touch final class Example { use Foo, Bar ; } ', ]; yield 'simple III' => [ '<?php class Example { use Foo;use Bar; public function baz() {} } ', '<?php class Example { use Foo, Bar; public function baz() {} } ', ]; yield 'multiple' => [ '<?php final class Example { use Foo; use Foo00;use Bar01; use Foo10;use Bar11;use Bar110; use Foo20;use Bar20;use Bar200;use Bar201; } ', '<?php final class Example { use Foo; use Foo00, Bar01; use Foo10, Bar11, Bar110; use Foo20, Bar20, Bar200, Bar201; } ', ]; yield 'multiple_multiline' => [ '<?php final class Example { use Foo; use Bar; use Baz; } ', '<?php final class Example { use Foo, Bar, Baz; } ', ]; yield 'multiple_multiline_with_comment' => [ '<?php final class Example { use Foo; use Bar; // Bazz, use Baz; } ', '<?php final class Example { use Foo, Bar, // Bazz, Baz; } ', ]; yield 'namespaces' => [ '<?php class Z { use X\Y\Z0;use X\Y\Z0;use M; use X\Y\Z1;use X\Y\Z1; } ', '<?php class Z { use X\Y\Z0, X\Y\Z0, M; use X\Y\Z1, X\Y\Z1; } ', ]; yield 'comments' => [ '<?php class ZZ {#1 use#2 Z/* 2 */ #3 #4 ;#5 #6 use T#7 #8 ;#9 #10 } ', '<?php class ZZ {#1 use#2 Z/* 2 */ #3 #4 ,#5 #6 T#7 #8 ;#9 #10 } ', ]; yield 'two classes. same file' => [ '<?php namespace Foo; class Test1 { use A;use B; /** use A2, B2; */ } ?> <?php class Test2 { use A1;use B1; # use A2, B2; } ', '<?php namespace Foo; class Test1 { use A, B; /** use A2, B2; */ } ?> <?php class Test2 { use A1, B1; # use A2, B2; } ', ]; yield 'do not fix group' => [ '<?php class Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; } }', ]; yield 'anonymous class' => [ '<?php new class { use A;use B;}?>', '<?php new class { use A, B;}?>', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php
tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. * * @TODO v4 remove file (thus also no need to fix above phpstan issues) * * @phpstan-ignore class.extendsFinalByPhpDoc,generics.wrongParent */ final class VisibilityRequiredFixerTest extends ModifierKeywordsFixerTest {}
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/StaticPrivateMethodFixerTest.php
tests/Fixer/ClassNotation/StaticPrivateMethodFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\StaticPrivateMethodFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\StaticPrivateMethodFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class StaticPrivateMethodFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: null|string}> */ public static function provideFixCases(): iterable { yield 'call from other method' => [ <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = $this->baz; $var = self::baz(); if (true) { $var = self::baz(); } } private static function baz() { if (true) { return 1; } } } PHP, <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = $this->baz; $var = $this->baz(); if (true) { $var = $this->baz(); } } private function baz() { if (true) { return 1; } } } PHP, ]; yield 'multiple classes' => [ <<<'PHP' <?php abstract class Foo { private static function baz() { return 1; } public function xyz() { return 1; } } abstract class Bar { public function baz() { return 1; } abstract protected function xyz1(); protected abstract function xyz2(); abstract function xyz3(); } PHP, <<<'PHP' <?php abstract class Foo { private function baz() { return 1; } public function xyz() { return 1; } } abstract class Bar { public function baz() { return 1; } abstract protected function xyz1(); protected abstract function xyz2(); abstract function xyz3(); } PHP, ]; yield 'already static' => [ <<<'PHP' <?php class Foo { private static function foo1() { return 1; } static private function foo2() { return 1; } } PHP, ]; yield 'methods containing closures' => [ <<<'PHP' <?php class Foo { private function bar() { return function() {}; } private function baz() { return static function() {}; } } PHP, ]; yield 'instance reference' => [ <<<'PHP' <?php class Foo { private function bar() { if (true) { return $this; } } } PHP, ]; yield 'debug_backtrace' => [ <<<'PHP' <?php class Foo { private function bar() { return debug_backtrace()[1]['object']; } } PHP, ]; yield 'references inside non static closures' => [ <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = function() { $var = $this->baz; $var = self::baz(); }; // Non valid in runtime, but valid syntax $var = static function() { $var = $this->baz(); }; } private static function baz() { return 1; } } PHP, <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = function() { $var = $this->baz; $var = $this->baz(); }; // Non valid in runtime, but valid syntax $var = static function() { $var = $this->baz(); }; } private function baz() { return 1; } } PHP, ]; yield 'magic methods' => [ <<<'PHP' <?php class Foo { private function __clone() {} private function __construct() {} private function __destruct() {} private function __wakeup() {} } PHP, ]; yield 'multiple methods' => [ self::generate50Samples(true), self::generate50Samples(false), ]; yield 'method calling itself' => [ <<<'PHP' <?php class Foo { private static function baz() { return self::baz(); } } PHP, <<<'PHP' <?php class Foo { private function baz() { return $this->baz(); } } PHP, ]; yield 'trait' => [ <<<'PHP' <?php class Foo { use A, B, C { asd as lol; } private static function bar() {} } PHP, <<<'PHP' <?php class Foo { use A, B, C { asd as lol; } private function bar() {} } PHP, ]; yield 'final' => [ <<<'PHP' <?php class Foo { final private static function baz1() { return 1; } private final static function baz2() { return 1; } } PHP, <<<'PHP' <?php class Foo { final private function baz1() { return 1; } private final function baz2() { return 1; } } PHP, ]; yield 'call from other method with anonymous class' => [ <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = $this->baz; $var = self::baz(); if (true) { $var = self::baz(); } } private static function baz() { if (true) { return new class() { public function baz() { return $this; } }; } } } PHP, <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = $this->baz; $var = $this->baz(); if (true) { $var = $this->baz(); } } private function baz() { if (true) { return new class() { public function baz() { return $this; } }; } } } PHP, ]; yield 'multiple classes with anonymous class' => [ <<<'PHP' <?php abstract class Foo { private static function baz() { return 1; } public function xyz() { return new class() extends Wut { public function anonym_xyz() { return $this->baz(); } }; } } abstract class Bar { public function baz() { return 1; } abstract protected function xyz1(); protected abstract function xyz2(); abstract function xyz3(); } PHP, <<<'PHP' <?php abstract class Foo { private function baz() { return 1; } public function xyz() { return new class() extends Wut { public function anonym_xyz() { return $this->baz(); } }; } } abstract class Bar { public function baz() { return 1; } abstract protected function xyz1(); protected abstract function xyz2(); abstract function xyz3(); } PHP, ]; yield 'references inside non-static closures with anonymous class' => [ <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = function() { $var = $this->baz; $var = self::baz(); $var = new class() { public function foo() { return $this->baz(); } }; }; // Non valid in runtime, but valid syntax $var = static function() { $var = $this->baz(); }; } private static function baz() { return 1; } } PHP, <<<'PHP' <?php class Foo { public $baz; public function bar() { $var = function() { $var = $this->baz; $var = $this->baz(); $var = new class() { public function foo() { return $this->baz(); } }; }; // Non valid in runtime, but valid syntax $var = static function() { $var = $this->baz(); }; } private function baz() { return 1; } } PHP, ]; yield 'nested calls' => [ <<<'PHP' <?php class Foo { public function the_function() { return self::function_0(); } private static function function_2() { return self::function_3(); } private static function function_1() { return self::function_2(); } private static function function_3() { return self::function_4(); } private static function function_7() { return self::function_8(); } private static function function_9() { return null; } private static function function_5() { return self::function_6(); } private static function function_8() { return self::function_9(); } private static function function_0() { return self::function_1(); } private static function function_4() { return self::function_5(); } private static function function_6() { return self::function_7(); } } PHP, <<<'PHP' <?php class Foo { public function the_function() { return $this->function_0(); } private function function_2() { return $this->function_3(); } private function function_1() { return $this->function_2(); } private function function_3() { return $this->function_4(); } private function function_7() { return $this->function_8(); } private function function_9() { return null; } private function function_5() { return $this->function_6(); } private function function_8() { return $this->function_9(); } private function function_0() { return $this->function_1(); } private function function_4() { return $this->function_5(); } private function function_6() { return $this->function_7(); } } PHP, ]; } private static function generate50Samples(bool $fixed): string { $template = <<<'PHP' <?php class Foo { public function userMethodStart() { %s } %s } PHP; $usage = ''; $signature = ''; for ($inc = 0; $inc < 50; ++$inc) { $usage .= \sprintf('$var = %sbar%02s();%s', $fixed ? 'self::' : '$this->', $inc, \PHP_EOL); $signature .= \sprintf('private %sfunction bar%02s() {}%s', $fixed ? 'static ' : '', $inc, \PHP_EOL); } return \sprintf($template, $usage, $signature); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/SingleClassElementPerStatementFixerTest.php
tests/Fixer/ClassNotation/SingleClassElementPerStatementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer> * * @author Javier Spagnoletti <phansys@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SingleClassElementPerStatementFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php class Foo { private static $bar1 = array(1,2,3); private static $bar2 = [1,2,3]; private static $baz1 = array(array(1,2), array(3, 4)); private static $baz2 = array(1,2,3); private static $aaa1 = 1; private static $aaa2 = array(2, 2); private static $aaa3 = 3; }', '<?php class Foo { private static $bar1 = array(1,2,3), $bar2 = [1,2,3]; private static $baz1 = array(array(1,2), array(3, 4)), $baz2 = array(1,2,3); private static $aaa1 = 1, $aaa2 = array(2, 2), $aaa3 = 3; }', ]; yield [ '<?php class Foo { const A = 1; const B = 2; } echo Foo::A, Foo::B; ', '<?php class Foo { const A = 1, B = 2; } echo Foo::A, Foo::B; ', ]; yield [ <<<'EOT' <?php class Foo { protected static $foo = 1; protected static $bar; protected static $baz=2 ; } EOT, <<<'EOT' <?php class Foo { protected static $foo = 1,$bar,$baz=2 ; } EOT, ]; yield [ <<<'EOT' <?php class Foo {} class Bar { } EOT, ]; yield [ <<<'EOT' <?php class Foo { protected static $foo = 1; protected static $bar; protected static $baz=2 ; } EOT, <<<'EOT' <?php class Foo { protected static $foo = 1, $bar, $baz=2 ; } EOT, ]; yield [ <<<'EOT' <?php class Foo { const ONE = 1; const TWO = 2; protected static $foo = 1; protected static $bar; protected static $baz=2 ; const THREE = 3; } EOT, <<<'EOT' <?php class Foo { const ONE = 1, TWO = 2; protected static $foo = 1, $bar, $baz=2 ; const THREE = 3; } EOT, ]; yield [ <<<'EOT' <?php class Foo { protected static $foo = 1; protected static $bar; protected static $baz=2; } EOT, <<<'EOT' <?php class Foo { protected static $foo = 1, $bar, $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { /** * Some great docblock */ protected static $foo = 1; protected static $bar; protected static $baz=2; } EOT, <<<'EOT' <?php class Foo { /** * Some great docblock */ protected static $foo = 1, $bar, $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { /** * @int */ protected static $foo = 1; protected static $bar; protected static $baz=2; // this is an inline comment, not a docblock private $var = false; } EOT, <<<'EOT' <?php class Foo { /** * @int */ protected static $foo = 1, $bar, $baz=2; // this is an inline comment, not a docblock private $var = false; } EOT, ]; yield [ <<<'EOT' <?php class Foo { /** * @int */ protected static $foo = 1; protected static $bar; protected static $baz=2; function doSomething() { } } EOT, <<<'EOT' <?php class Foo { /** * @int */ protected static $foo = 1, $bar, $baz=2; function doSomething() { } } EOT, ]; yield 'line_breaks_1' => [ <<<'EOT' <?php class Foo { public $bar = null; public $initialised = false; public $configured = false; public $called = false; public $arguments = array(); public $baz = null; public $drop = false; function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $bar = null, $initialised = false, $configured = false, $called = false, $arguments = array(); public $baz = null, $drop = false; function doSomething() { } } EOT, ]; yield 'line_breaks_2' => [ <<<'EOT' <?php class Foo { const TWO = '2'; public $bar = null; public $initialised = false; function doSomething() { } } EOT, <<<'EOT' <?php class Foo { const TWO = '2'; public $bar = null, $initialised = false; function doSomething() { } } EOT, ]; yield 'line_breaks_3' => [ <<<'EOT' <?php class Foo { const TWO = '2'; public $bar = null; public $initialised = false; function doSomething() { } } EOT, <<<'EOT' <?php class Foo { const TWO = '2'; public $bar = null, $initialised = false; function doSomething() { } } EOT, ]; yield 'line_breaks_4' => [ <<<'EOT' <?php class Foo { public $one = 1; public $bar = null; public $initialised = false; public $configured = false; public $called = false; public $arguments = array(); function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $one = 1; public $bar = null, $initialised = false, $configured = false, $called = false, $arguments = array(); function doSomething() { } } EOT, ]; yield 'line_breaks_5' => [ <<<'EOT' <?php class Foo { public $one = 1; public $bar = null; public $initialised = false; public $configured = false; public $called = false; public $arguments = array(); function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $one = 1; public $bar = null, $initialised = false, $configured = false, $called = false, $arguments = array(); function doSomething() { } } EOT, ]; yield 'line_breaks_6' => [ <<<'EOT' <?php class Foo { public $one = 1;public $bar = null;public $initialised = false;public $configured = false;public $called = false;public $arguments = array(); function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $one = 1;public $bar = null, $initialised = false, $configured = false, $called = false, $arguments = array(); function doSomething() { } } EOT, ]; yield 'whitespace_1' => [ <<<'EOT' <?php class Foo { public $one = 1; public $bar = null; public $initialised = false; public $configured = false; public $called = false; public $arguments = array(); function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $one = 1; public $bar = null,$initialised = false,$configured = false,$called = false,$arguments = array(); function doSomething() { } } EOT, ]; yield 'whitespace_2' => [ <<<'EOT' <?php class Foo { public $one = 1; public $bar = null; public $initialised = false; public $configured = false; public $called=false; public $arguments = array(); function doSomething() { } } EOT, <<<'EOT' <?php class Foo { public $one = 1; public $bar = null,$initialised = false,$configured = false,$called=false,$arguments = array(); function doSomething() { } } EOT, ]; yield [ <<<'EOT' <?php class Foo { protected static $foo = 1; protected static $bar; protected static $baz=1; } EOT, <<<'EOT' <?php class Foo { protected static $foo = 1, $bar, $baz=1; } EOT, ]; yield [ <<<'EOT' <?php class Foo { protected static $foo = 1; protected static $bar; protected static $baz=1; } EOT, <<<'EOT' <?php class Foo { protected static $foo = 1, $bar, $baz=1; } EOT, ]; yield [ <<<'EOT' <?php class Foo { protected $foo = 1; protected $bar; protected $baz=2; } EOT, <<<'EOT' <?php class Foo { protected $foo = 1, $bar, $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { var $foo = 1; var $bar; var $baz=2; } EOT, <<<'EOT' <?php class Foo { var $foo = 1, $bar, $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { var $foo = 1; var $bar; public function doSomething1() {} var $baz=2; } EOT, <<<'EOT' <?php class Foo { var $foo = 1, $bar; public function doSomething1() {} var $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { var $foo = 1; var $bar; public function doSomething2() { global $one, $two, $three; } var $baz=2; } EOT, <<<'EOT' <?php class Foo { var $foo = 1, $bar; public function doSomething2() { global $one, $two, $three; } var $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { public function doSomething3() {} protected $foo = 1; protected $bar; protected $baz=2; } EOT, <<<'EOT' <?php class Foo { public function doSomething3() {} protected $foo = 1, $bar, $baz=2; } EOT, ]; yield [ <<<'EOT' <?php class Foo { public function doSomethingElse() {} protected $foo = 1; protected $bar; protected $baz=2; private $acme =array(); } EOT, <<<'EOT' <?php class Foo { public function doSomethingElse() {} protected $foo = 1, $bar, $baz=2; private $acme =array(); } EOT, ]; yield [ <<<'EOT' <?php class Foo { public function doSomewhere() {} protected $foo = 1; protected $bar; protected $baz=2; private $acme1 =array(); } EOT, <<<'EOT' <?php class Foo { public function doSomewhere() {} protected $foo = 1, $bar, $baz=2; private $acme1 =array(); } EOT, ]; yield [ <<<'EOT' <?php class Foo { public function doThis() { global $one1, $two2, $three3; } protected $foo = 1; protected $bar; protected $baz=2; private $acme2 =array(); } EOT, <<<'EOT' <?php class Foo { public function doThis() { global $one1, $two2, $three3; } protected $foo = 1, $bar, $baz=2; private $acme2 =array(); } EOT, ]; yield [ '<?php class Foo { const A = 1; const # B# =# 2# ;# } echo Foo::A, Foo::B; ', '<?php class Foo { const A = 1,# B# =# 2# ;# } echo Foo::A, Foo::B; ', ]; yield [ '<?php class Token { const PUBLIC_CONST = 0; private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; public const TEST_71 = 0; } ', '<?php class Token { const PUBLIC_CONST = 0; private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0, TEST_71 = 0; } ', ]; yield [ '<?php class Foo { private int $foo; private int $bar; }', '<?php class Foo { private int $foo, $bar; }', ]; yield [ '<?php class Foo { protected ?string $foo; protected ?string $bar; }', '<?php class Foo { protected ?string $foo, $bar; }', ]; yield [ '<?php class Foo { public ? string $foo; public ? string $bar; }', '<?php class Foo { public ? string $foo, $bar; }', ]; yield [ '<?php class Foo { var ? Foo\Bar $foo; var ? Foo\Bar $bar; }', '<?php class Foo { var ? Foo\Bar $foo, $bar; }', ]; yield [ '<?php class Foo { var array $foo; var array $bar; }', '<?php class Foo { var array $foo, $bar; }', ]; yield [ <<<'EOT' <?php class Foo { const SOME_CONST = 'a'; const OTHER_CONST = 'b'; protected static $foo = 1; protected static $bar = 2; } EOT, <<<'EOT' <?php class Foo { const SOME_CONST = 'a', OTHER_CONST = 'b'; protected static $foo = 1, $bar = 2; } EOT, ['elements' => ['const', 'property']], ]; yield [ <<<'EOT' <?php class Foo { const SOME_CONST = 'a'; const OTHER_CONST = 'b'; protected static $foo = 1, $bar = 2; } EOT, <<<'EOT' <?php class Foo { const SOME_CONST = 'a', OTHER_CONST = 'b'; protected static $foo = 1, $bar = 2; } EOT, ['elements' => ['const']], ]; yield [ <<<'EOT' <?php class Foo { const SOME_CONST = 'a', OTHER_CONST = 'b'; protected static $foo = 1; protected static $bar = 2; } EOT, <<<'EOT' <?php class Foo { const SOME_CONST = 'a', OTHER_CONST = 'b'; protected static $foo = 1, $bar = 2; } EOT, ['elements' => ['property']], ]; yield 'anonymous class' => [ '<?php $a = new class() { const PUBLIC_CONST_TWO = 0; const TEST_70 = 0; public function a() { } }; class C { public function A() { $a = new class() { const PUBLIC_CONST_TWO = 0; const TEST_70 = 0; public function a() {} }; } } ', '<?php $a = new class() { const PUBLIC_CONST_TWO = 0, TEST_70 = 0; public function a() { } }; class C { public function A() { $a = new class() { const PUBLIC_CONST_TWO = 0, TEST_70 = 0; public function a() {} }; } } ', ]; yield 'tabs and Windows line breaks' => [ "<?php\r\n\tclass Foo {\r\n\t\tconst AAA=0;\r\n\t\tconst BBB=1;\r\n\t}", "<?php\r\n\tclass Foo {\r\n\t\tconst AAA=0, BBB=1;\r\n\t}", ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php class Foo { private string|int $prop1; private string|int $prop2; } ', '<?php class Foo { private string|int $prop1, $prop2; } ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php class Foo { readonly int $a; readonly int $b; public readonly int $c; public readonly int $d; readonly private string /*1*/$e; readonly private string /*2*/$f; readonly float $g; protected readonly float $h1; protected readonly float $h2; readonly float $z1; readonly float $z2; readonly float $z3; }', '<?php class Foo { readonly int $a, $b; public readonly int $c, $d; readonly private string /*1*/$e,/*2*/$f; readonly float $g; protected readonly float $h1, $h2; readonly float $z1, $z2, $z3; }', ]; yield [ '<?php class Foo { final public const B1 = "2"; final public const B2 = "2"; readonly float $z2; } ', ]; yield [ '<?php class Foo { private Foo&Bar $prop1; private Foo&Bar $prop2; } ', '<?php class Foo { private Foo&Bar $prop1, $prop2; } ', ]; yield [ "<?php enum Foo: string { public const A = 'A'; public const B = 'B'; case Hearts = 'H'; case Spades = 'S'; } var_dump(Foo::A.Foo::B);", "<?php enum Foo: string { public const A = 'A', B = 'B'; case Hearts = 'H'; case Spades = 'S'; } var_dump(Foo::A.Foo::B);", ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix82Cases(): iterable { yield [ '<?php trait Foo { public const Bar = 1; public const Baz = 1; }', '<?php trait Foo { public const Bar = 1, Baz = 1; }', ]; } /** * @dataProvider provideFix84Cases * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: string}> */ public static function provideFix84Cases(): iterable { yield 'asymmetric visibility' => [ <<<'PHP' <?php trait Foo { public public(set) int $a; public public(set) int $b; public protected(set) int $c; public protected(set) int $d; public private(set) int $e; public private(set) int $f; } PHP, <<<'PHP' <?php trait Foo { public public(set) int $a, $b; public protected(set) int $c, $d; public private(set) int $e, $f; } PHP, ]; yield 'property hook' => [ <<<'PHP' <?php class Foo { public int $i { get {} } } PHP, ]; } public function testInvalidConfiguration(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[single_class_element_per_statement\] Invalid configuration: The option "elements" .*\.$/'); $this->fixer->configure(['elements' => ['foo']]); // @phpstan-ignore-line } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/FinalInternalClassFixerTest.php
tests/Fixer/ClassNotation/FinalInternalClassFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class FinalInternalClassFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { $input = $expected = '<?php '; for ($i = 1; $i < 10; ++$i) { $input .= \sprintf("/** @internal */\nclass class%d\n{\n}\n", $i); $expected .= \sprintf("/** @internal */\nfinal class class%d\n{\n}\n", $i); } yield 'fix multiple classes' => [ $expected, $input, ]; yield [ '<?php /** @internal */ final class class1 { } interface A {} trait B{} /** @internal */ final class class2 { } ', '<?php /** @internal */ class class1 { } interface A {} trait B{} /** @internal */ class class2 { } ', ]; yield [ '<?php /** @internal */ final class class1 { } /** @internal */ final class class2 { } /** * @internal * @final */ class class3 { } /** * @internal */ abstract class class4 {} ', '<?php /** @internal */ final class class1 { } /** @internal */ class class2 { } /** * @internal * @final */ class class3 { } /** * @internal */ abstract class class4 {} ', ]; yield [ '<?php /** * @ annotation_with_space_after_at_sign */ class A {} ', ]; yield 'indent before `class`' => [ '<?php /** @internal */ final class class1 { }', '<?php /** @internal */ class class1 { }', ]; yield 'multiple classes, first with internal annotation and second without internal annotation' => [ '<?php /** @internal */ final class Foo {} class Bar {} ', '<?php /** @internal */ class Foo {} class Bar {} ', ]; yield 'multiple classes, first without internal annotation and second with internal annotation' => [ '<?php class Foo {} /** @internal */ final class Bar {} ', '<?php class Foo {} /** @internal */ class Bar {} ', ]; yield [ "<?php\n/** @CUSTOM */final class A{}", "<?php\n/** @CUSTOM */class A{}", [ 'include' => ['@Custom'], ], ]; yield [ '<?php /** * @CUSTOM * @abc */ final class A{} /** * @CUSTOM */ final class B{} ', '<?php /** * @CUSTOM * @abc */ class A{} /** * @CUSTOM */ class B{} ', [ 'include' => ['@Custom', '@abc'], ], ]; yield [ '<?php /** * @CUSTOM * @internal */ final class A{} /** * @CUSTOM * @internal * @other */ final class B{} /** * @CUSTOM * @internal * @not-fix */ class C{} ', '<?php /** * @CUSTOM * @internal */ class A{} /** * @CUSTOM * @internal * @other */ class B{} /** * @CUSTOM * @internal * @not-fix */ class C{} ', [ 'include' => ['@Custom', '@internal'], 'exclude' => ['@not-fix'], ], ]; yield [ '<?php /** * @internal */ final class A{} /** * @abc */ class B{} ', '<?php /** * @internal */ class A{} /** * @abc */ class B{} ', [ 'exclude' => ['abc'], ], ]; yield [ '<?php final class A{}', '<?php class A{}', ['consider_absent_docblock_as_internal_class' => true], ]; yield 'class with annotation with matching include and partial matching exclude' => [ '<?php /** @HelloWorld */ final class Foo {} ', '<?php /** @HelloWorld */ class Foo {} ', [ 'include' => ['HelloWorld'], 'exclude' => ['Hello'], ], ]; yield [ '<?php /** @internal */ $a = new class (){};', ]; yield [ '<?php /** @internal */ $a = new class{};', ]; yield [ '<?php $object = new /**/ class(){};', ]; } /** * @group legacy * * @param array<string, mixed> $config * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $config, string $exceptionExpression, ?string $deprecationMessage = null): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($exceptionExpression); if (null !== $deprecationMessage) { $this->expectDeprecation($deprecationMessage); } $this->fixer->configure($config); } /** * @return iterable<string, array{array<string, mixed>, string, 2?: string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'same annotation in both lists' => [ [ 'include' => ['@internal123', 'a'], 'exclude' => ['@internal123', 'b'], ], \sprintf('#^%s$#', preg_quote('[final_internal_class] Annotation cannot be used in both "include" and "exclude" list, got duplicates: "internal123".', '#')), ]; yield 'both new and old include set' => [ [ 'annotation_include' => ['@internal', 'a'], 'include' => ['@internal', 'b'], ], \sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_include" and new option "include".', '#')), 'Option "annotation_include" for rule "final_internal_class" is deprecated and will be removed in version 4.0. Use "include" to configure PHPDoc annotations tags and attributes.', ]; yield 'both new and old exclude set' => [ [ 'annotation_exclude' => ['@internal', 'a'], 'exclude' => ['@internal', 'b'], ], \sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_exclude" and new option "exclude".', '#')), 'Option "annotation_exclude" for rule "final_internal_class" is deprecated and will be removed in version 4.0. Use "exclude" to configure PHPDoc annotations tags and attributes.', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input, array $configuration): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1: null|string, 2: _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield 'multiple attributes, all configured as not to fix' => [ '<?php #[X] #[A] class Foo {}', null, ['exclude' => ['a', 'X']], ]; yield 'multiple attributes, one configured as to fix, one as not to fix' => [ '<?php #[Internal] #[A] class Foo {}', null, [ 'include' => ['internal'], 'exclude' => ['A'], ], ]; yield 'multiple attributes, one configured as to fix' => [ '<?php #[Internal] #[A] final class Foo {}', '<?php #[Internal] #[A] class Foo {}', ['include' => ['internal']], ]; yield 'single attribute configured as to fix' => [ '<?php #[Internal] final class Foo {}', '<?php #[Internal] class Foo {}', ['include' => ['internal']], ]; yield 'class that should be ignored as it has an attribute not included with absent docblock as true' => [ '<?php #[StandWithUkraine] class Foo {}', null, ['consider_absent_docblock_as_internal_class' => true], ]; yield 'mixed bag of cases' => [ '<?php #[Entity(repositoryClass: PostRepository::class)] class User {} #[ORM\Entity] #[Index(name: "category_idx", columns: ["category"])] final class Article {} #[A] class ArticleB {} #[B] final class Foo {} #[C] class FooX {} $object1 = new #[ExampleAttribute] class(){}; $object2 = new /* */ class(){}; $object3 = new #[B] #[ExampleAttribute] class(){}; /** * @B */ final class PhpDocClass{} ', '<?php #[Entity(repositoryClass: PostRepository::class)] class User {} #[ORM\Entity] #[Index(name: "category_idx", columns: ["category"])] class Article {} #[A] class ArticleB {} #[B] class Foo {} #[C] class FooX {} $object1 = new #[ExampleAttribute] class(){}; $object2 = new /* */ class(){}; $object3 = new #[B] #[ExampleAttribute] class(){}; /** * @B */ class PhpDocClass{} ', [ 'exclude' => ['Entity', 'A'], 'include' => ['orm\entity', 'B'], ], ]; yield 'multiple classes, first configured with attribute, second without attribute' => [ '<?php #[Internal] final class Foo {} class Bar {}', '<?php #[Internal] class Foo {} class Bar {}', ['include' => ['internal']], ]; yield 'multiple classes, first configured without attribute, second with attribute' => [ '<?php class Foo {} #[Internal] final class Bar {}', '<?php class Foo {} #[Internal] class Bar {}', ['include' => ['internal']], ]; yield 'include by attribute, but exclude by doc' => [ '<?php /** @final */ #[A] class Foo {}', null, [ 'exclude' => ['final'], 'include' => ['A'], ], ]; yield 'include by phpDoc, but exclude by attribute' => [ '<?php /** @a */ #[Internal] class Foo {}', null, [ 'exclude' => ['Internal'], 'include' => ['A'], ], ]; yield 'comment between attributes' => [ '<?php #[A] /** * @B */ #[C] final class Foo {}', '<?php #[A] /** * @B */ #[C] class Foo {}', [ 'include' => ['A', 'C'], ], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input, array $configuration): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1: null|string, 2: _AutogeneratedInputConfiguration}> */ public static function provideFix82Cases(): iterable { yield 'readonly with enabled `consider_absent_docblock_as_internal_class`' => [ '<?php readonly final class A{}', '<?php readonly class A{}', ['consider_absent_docblock_as_internal_class' => true], ]; yield 'readonly with `internal` attribute and comment in-between' => [ '<?php #[Internal] readonly /* comment */ final class A{}', '<?php #[Internal] readonly /* comment */ class A{}', ['consider_absent_docblock_as_internal_class' => true], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/SelfStaticAccessorFixerTest.php
tests/Fixer/ClassNotation/SelfStaticAccessorFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\SelfStaticAccessorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SelfStaticAccessorFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SelfStaticAccessorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple' => [ '<?php final class Sample { public function getBar() { return self::class.self::test(); } private static function test() { return \'test\'; } } ', '<?php final class Sample { public function getBar() { return static::class.static::test(); } private static function test() { return \'test\'; } } ', ]; yield 'multiple' => [ '<?php final class Foo0 { public function A(){ return self::A; }} final class Foo1 { public function A(){ return self::A; }} final class Foo2 { public function A(){ return self::A; }} final class Foo3 { public function A(){ return self::A; }} final class Foo4{public function A(){return self::A;}}final class Foo5{public function A(){return self::A;}} ', '<?php final class Foo0 { public function A(){ return static::A; }} final class Foo1 { public function A(){ return static::A; }} final class Foo2 { public function A(){ return static::A; }} final class Foo3 { public function A(){ return static::A; }} final class Foo4{public function A(){return static::A;}}final class Foo5{public function A(){return static::A;}} ', ]; yield 'comments and casing' => [ '<?php FINAL CLASS Sample { public function getBar() { return/* 0 */self/* 1 */::/* 2 */CLASS/* 3 */; } } ', '<?php FINAL CLASS Sample { public function getBar() { return/* 0 */STATIC/* 1 */::/* 2 */CLASS/* 3 */; } } ', ]; yield 'not final' => [ '<?php class Sample { public function getBar() { return static::class; } } ', ]; yield 'abstract' => [ '<?php abstract class Sample { public function getBar() { return static::class; } } ', ]; yield [ '<?php final class Foo { public function bar() { $a = new Foo(); return new self(); } } ', '<?php final class Foo { public function bar() { $a = new Foo(); return new static(); } } ', ]; yield 'instance of' => [ '<?php final class Foo { public function isBar($foo) { return $foo instanceof self; } } ', '<?php final class Foo { public function isBar($foo) { return $foo instanceof static; } } ', ]; yield 'in method as new' => [ '<?php final class A { public static function b() { return new self(); } }', '<?php final class A { public static function b() { return new static(); } }', ]; yield 'in method as new with comments' => [ '<?php final class A { public static function b() { return new /* hmm */ self(); } }', '<?php final class A { public static function b() { return new /* hmm */ static(); } }', ]; yield 'in method as new without parentheses' => [ '<?php final class A { public static function b() { return new self; } }', '<?php final class A { public static function b() { return new static; } }', ]; yield 'simple anonymous class' => [ '<?php $a = new class { public function getBar() { return self::class; } };', '<?php $a = new class { public function getBar() { return static::class; } };', ]; yield 'nested anonymous class' => [ '<?php final class Foo { public function Foo() { return self::class; } public function getClass() { $a = new class() { public function getBar() { return self::class; } }; } public function Foo2() { return self::class; } } ', '<?php final class Foo { public function Foo() { return static::class; } public function getClass() { $a = new class() { public function getBar() { return static::class; } }; } public function Foo2() { return static::class; } } ', ]; yield 'anonymous classes inside lambda' => [ '<?php final class Foo { public function bar() { echo self::class; // do fix return function () { echo static::class; // do not fix $a = new class { public function bar2() { echo self::class; // do fix return function () { echo static::class; // do not fix $a = new class { public function bar2() { echo self::class; // do fix } }; echo static::class; // do not fix return $a; }; } }; echo static::class; // do not fix $b = new class { public function test() { echo self::class; // do fix } }; return $a; }; echo self::class; // do fix } } ', '<?php final class Foo { public function bar() { echo static::class; // do fix return function () { echo static::class; // do not fix $a = new class { public function bar2() { echo static::class; // do fix return function () { echo static::class; // do not fix $a = new class { public function bar2() { echo static::class; // do fix } }; echo static::class; // do not fix return $a; }; } }; echo static::class; // do not fix $b = new class { public function test() { echo static::class; // do fix } }; return $a; }; echo static::class; // do fix } } ', ]; yield 'no scope' => [ '<?php echo static::class;', ]; yield 'do not fix inside lambda' => [ '<?php final class Foo { public function Bar() { return function() { return static::class; }; } } $a = static function() { return static::class; }; $b = function() { return static::class; }; ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'enums' => [ '<?php enum Foo { case Baz; private const BAR = \'foo\'; public static function bar(): Foo { return self::Baz; } public static function baz(mixed $other): void { if ($other instanceof self) { echo self::BAR; } } } ', '<?php enum Foo { case Baz; private const BAR = \'foo\'; public static function bar(): Foo { return static::Baz; } public static function baz(mixed $other): void { if ($other instanceof static) { echo static::BAR; } } } ', ]; yield 'enum with nested anonymous class' => [ '<?php enum Suit: int implements SomeIntInterface, Z { case Hearts = 1; case Clubs = 3; public const HEARTS = self::Hearts; public function Foo(): string { return self::Hearts->Bar()->getBar() . self::class . self::Clubs->value; } public function Bar(): object { return new class { public function getBar() { return self::class; } }; } } ', '<?php enum Suit: int implements SomeIntInterface, Z { case Hearts = 1; case Clubs = 3; public const HEARTS = self::Hearts; public function Foo(): string { return static::Hearts->Bar()->getBar() . static::class . static::Clubs->value; } public function Bar(): object { return new class { public function getBar() { return static::class; } }; } } ', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix82Cases(): iterable { yield 'simple' => [ '<?php final readonly class Sample { public function getBar() { return self::class.self::test(); } private static function test() { return \'test\'; } } ', '<?php final readonly class Sample { public function getBar() { return static::class.static::test(); } private static function test() { return \'test\'; } } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/OrderedTypesFixerTest.php
tests/Fixer/ClassNotation/OrderedTypesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer> * * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedTypesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void { if (null !== $configuration) { $this->fixer->configure($configuration); } $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'catch with default, no spaces, with both leading slash' => [ '<?php try { $this->foo(); } catch (\LogicException|\RuntimeException $e) { // $e } ', '<?php try { $this->foo(); } catch (\RuntimeException|\LogicException $e) { // $e } ', ]; yield 'catch with default, with spaces, with both leading slash' => [ '<?php try { $this->foo(); } catch (\LogicException|\RuntimeException $e) { // $e } ', '<?php try { $this->foo(); } catch (\RuntimeException | \LogicException $e) { // $e } ', ]; yield 'catch with default, no spaces, with no leading slash' => [ '<?php try { cache()->save(); } catch (CacheException|SimpleCacheException $e) { // $e } ', '<?php try { cache()->save(); } catch (SimpleCacheException|CacheException $e) { // $e } ', ]; yield 'catch with default, with spaces, with one leading slash' => [ '<?php try { cache()->save(); } catch (CacheException|\RuntimeException $e) { // $e } ', '<?php try { cache()->save(); } catch (\RuntimeException | CacheException $e) { // $e } ', ]; yield 'catch with no sorting' => [ '<?php try { $this->foo(); } catch (\RuntimeException|\LogicException $e) { // $e } ', null, ['sort_algorithm' => 'none'], ]; yield 'nothing to fix' => [ '<?php try { $this->foo(); } catch (\LogicException $e) { // $e } ', ]; yield 'already fixed' => [ '<?php try { $this->foo(); } catch (LogicException|RuntimeException $e) { // $e } ', ]; } /** * @dataProvider provideFix80Cases * * @param _AutogeneratedInputConfiguration $configuration * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null, ?array $configuration = null): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield 'sort alpha, null none' => [ "<?php\nclass Foo\n{\n public A|null|Z \$bar;\n}\n", "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield 'sort alpha, null first' => [ "<?php\nclass Foo\n{\n public null|A|Z \$bar;\n}\n", "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_first'], ]; yield 'sort alpha, null last' => [ "<?php\nclass Foo\n{\n public A|Z|null \$bar;\n}\n", "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield 'sort none, null first' => [ "<?php\nclass Foo\n{\n public null|Z|A \$bar;\n}\n", "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", ['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'], ]; yield 'sort none, null last' => [ "<?php\nclass Foo\n{\n public Z|A|null \$bar;\n}\n", "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", ['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'], ]; yield 'sort none, null none' => [ "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n", null, ['sort_algorithm' => 'none', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|A|B \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|\\A|B \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|\\A|\\B \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|int|string/* array */ \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public /* int */null|A|B \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|A|B \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n private function bar(null|array|callable|int \$cb) {}\n}\n", "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n", ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): null|array|callable|int {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n", ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public function bar(null|string \$str, null|array|int \$arr) {}\n}\n", "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n", "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n", ]; yield [ '<?php function foo(null|int|string $bar): null|\Stringable {}', '<?php function foo(string|int|null $bar): \Stringable|null {}', ]; yield [ '<?php fn (null|\Countable|int $number): null|int => $number;', '<?php fn (int|\Countable|null $number): int|null => $number;', ]; yield [ "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n", "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n", ]; yield [ '<?php class Foo { public ?string $foo = null; public /* int|string */ $bar; private null|array $baz = null; public function baz(): null|string {} protected function bar(string $str, ?array $config = null): callable {} } try { (new Foo)->baz(); } catch (Exception $e) { return $e; } ', ]; yield [ "<?php\nclass Foo\n{\n public int|string|null \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public \\A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public \\A|\\B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public int|string|null/* array */ \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public /* int */A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(array|callable|int|null \$cb) {}\n}\n", "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): array|callable|int|null {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public function bar(string|null \$str, array|int|null \$arr) {}\n}\n", "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n", "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ '<?php function foo(int|string|null $bar): \Stringable|null {}', '<?php function foo(string|int|null $bar): \Stringable|null {}', ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ '<?php fn (\Countable|int|null $number): int|null => $number;', '<?php fn (int|\Countable|null $number): int|null => $number;', ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n", "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ '<?php class Foo { public ?string $foo = null; public /* int|string */ $bar; private array|null $baz = null; public function baz(): string|null {} protected function bar(string $str, ?array $config = null): callable {} } try { (new Foo)->baz(); } catch (Exception $e) { return $e; } ', null, ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public int|null|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public \\A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public \\A|\\B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public int|null|string/* array */ \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public /* int */A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n", "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(array|callable|int|null \$cb) {}\n}\n", "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): array|callable|int|null {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public function bar(null|string \$str, array|int|null \$arr) {}\n}\n", "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n", "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ '<?php function foo(int|null|string $bar): null|\Stringable {}', '<?php function foo(string|int|null $bar): \Stringable|null {}', ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ '<?php fn (\Countable|int|null $number): int|null => $number;', '<?php fn (int|\Countable|null $number): int|null => $number;', ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n", "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n", ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ '<?php class Foo { public ?string $foo = null; public /* int|string */ $bar; private array|null $baz = null; public function baz(): null|string {} protected function bar(string $str, ?array $config = null): callable {} } try { (new Foo)->baz(); } catch (Exception $e) { return $e; } ', null, ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string | null | int \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string | null | int \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string |/* array| */null|int \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n", "<?php\nclass Foo\n{\n private function bar(\$cb): static /* |int */ | null {}\n}\n", ]; yield 'case sensitive cases' => [ "<?php\nclass Foo\n{\n public null|AAa|Aa \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public Aa|AAa|null \$bar = null;\n}\n", ['case_sensitive' => true], ]; } /** * @dataProvider provideFix81Cases * * @param _AutogeneratedInputConfiguration $configuration * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null, ?array $configuration = null): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<int, array{string, 1: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix81Cases(): iterable { yield [ "<?php\nclass Foo\n{\n public A&B \$bar;\n}\n", "<?php\nclass Foo\n{\n public B&A \$bar;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public Ae&At \$bar;\n}\n", "<?php\nclass Foo\n{\n public At&Ae \$bar;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public readonly null|A|B \$bar;\n}\n", "<?php\nclass Foo\n{\n public readonly B|null|A \$bar;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public readonly A|B|null \$bar;\n}\n", "<?php\nclass Foo\n{\n public readonly B|null|A \$bar;\n}\n", ['null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public readonly A|null|X \$bar;\n}\n", "<?php\nclass Foo\n{\n public readonly X|A|null \$bar;\n}\n", ['null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public B&A \$bar;\n}\n", null, ['sort_algorithm' => 'none'], ]; } /** * Provisional support for PHP 8.2's Disjunctive Normal Form (DNF) Types. * * @dataProvider provideFix82Cases * * @param _AutogeneratedInputConfiguration $configuration * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null, ?array $configuration = null): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<int, array{string, 1: ?string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix82Cases(): iterable { yield [ "<?php\nclass Foo\n{\n public null|array|(At&Bz)|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n", ]; yield [ "<?php\nclass Foo\n{\n public array|(At&Bz)|string|null \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n", ['null_adjustment' => 'always_last'], ]; yield [ "<?php\nclass Foo\n{\n public array|(At&Bz)|null|string \$bar = null;\n}\n", "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n", ['null_adjustment' => 'none'], ]; yield [ "<?php\nclass Foo\n{\n public (A&B)|(A&C)|(B&D)|(C&D) \$bar;\n}\n", "<?php\nclass Foo\n{\n public (D&B)|(C&A)|(B&A)|(D&C) \$bar;\n}\n", ['sort_algorithm' => 'alpha'], ]; yield [ "<?php\nclass Foo\n{\n public (A&B)|(\\A&C)|(B&\\D)|(C&D) \$bar;\n}\n", "<?php\nclass Foo\n{\n public (\\D&B)|(C&\\A)|(B&A)|(D&C) \$bar;\n}\n", ['sort_algorithm' => 'alpha'], ]; yield [ "<?php\nclass Foo\n{\n public (A&C)|(B&D) \$bar;\n}\n", "<?php\nclass Foo\n{\n public (D&B)|(C&A) \$bar;\n}\n", ['sort_algorithm' => 'alpha'], ]; yield [ "<?php\nclass Foo\n{\n public (\\A&\\C)|(\\B&\\D) \$bar;\n}\n", "<?php\nclass Foo\n{\n public (\\D&\\B)|(\\C&\\A) \$bar;\n}\n", ['sort_algorithm' => 'alpha'], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix84Cases * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null, ?array $configuration = null): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix84Cases(): iterable { yield 'asymmetric visibility' => [ <<<'PHP' <?php class Foo { public public(set) A|Z $a; public protected(set) A|Z $b; public private(set) A|Z $c; protected private(set) A|Z $e; protected protected(set) A|Z $f; private private(set) A|Z $g; } PHP, <<<'PHP' <?php class Foo { public public(set) Z|A $a; public protected(set) Z|A $b; public private(set) Z|A $c; protected private(set) Z|A $e; protected protected(set) Z|A $f; private private(set) Z|A $g; } PHP, ['sort_algorithm' => 'alpha'], ]; yield 'asymmetric visibility for promoted property' => [ <<<'PHP' <?php class Foo { public function __construct( public public(set) A|Z $a, public protected(set) A|Z $b, public private(set) A|Z $c, protected private(set) A|Z $e, protected protected(set) A|Z $f, private private(set) A|Z $g, ) {} } PHP, <<<'PHP' <?php class Foo { public function __construct( public public(set) Z|A $a, public protected(set) Z|A $b, public private(set) Z|A $c, protected private(set) Z|A $e, protected protected(set) Z|A $f, private private(set) Z|A $g, ) {} } PHP, ['sort_algorithm' => 'alpha'], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/NoPhp4ConstructorFixerTest.php
tests/Fixer/ClassNotation/NoPhp4ConstructorFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\NoPhp4ConstructorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\NoPhp4ConstructorFixer> * * @author Matteo Beccati <matteo@beccati.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoPhp4ConstructorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $a = new class {};', ]; yield [ '<?php $a = new class {}?>', ]; yield [ '<?php $a = new Foo() <=> 1; $a = new Foo <=> 1; $a = new class() {}; $a = new class() implements Foo{}; $a = new class() /**/ extends Bar1{}; $a = new class() extends Bar2 implements Foo{}; $a = new class() extends Bar3 implements Foo, Foo2{}; $a = new class() {}; $a = new class {}; $a = new class implements Foo{}; $a = new class /**/ extends Bar1{}; $a = new class extends Bar2 implements Foo{}; $a = new class extends Bar3 implements Foo, Foo2{}; $a = new class {}?> ', ]; yield 'simple class 1' => [ <<<'EOF' <?php class Foo { public function __construct($bar) { var_dump(1); } } EOF, <<<'EOF' <?php class Foo { public function Foo($bar) { var_dump(1); } } EOF, ]; yield 'simple class 2' => [ <<<'EOF' <?php class Foo { public# function# __construct# (# $bar# )# {} } EOF, <<<'EOF' <?php class Foo { public# function# Foo# (# $bar# )# {} } EOF, ]; yield 'namespace' => [<<<'EOF' <?php namespace Baz\Qux; class Foo { public function __construct($bar) { var_dump(1); } public function Foo($bar) { var_dump(2); } } EOF]; yield 'namespace 2' => [<<<'EOF' <?php namespace Baz\Qux { class Foo { public function __construct($bar) { var_dump(1); } public function Foo($bar) { var_dump(2); } } class Bar { public function Bar() { var_dump(3); } } } EOF]; yield 'namespace global' => [ <<<'EOF' <?php namespace { class Foo { function __construct($bar) { var_dump(1); } } } EOF, <<<'EOF' <?php namespace { class Foo { function Foo($bar) { var_dump(1); } } } EOF, ]; yield 'PHP 5 only' => [<<<'EOF' <?php class Foo { function __construct($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF]; yield 'PHP 4 only' => [ <<<'EOF' <?php class Foo { /** * Constructor */ function __construct($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo { /** * Constructor */ function foO($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF, ]; yield 'both the right way 1' => [ <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct() { var_dump(1); } public function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct() { var_dump(1); } /** * PHP-4 Constructor */ function Foo() { // Call PHP5! $this->__construct(); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the right way 2' => [ <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct($bar) { var_dump(1); } public function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct($bar) { var_dump(1); } /** * PHP-4 Constructor */ function Foo($bar) { // Call PHP5! $this->__construct($bar); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the right way 3' => [ <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct($bar = 1, $baz = null) { var_dump(1); } public function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo { /** * Constructor */ public function __construct($bar = 1, $baz = null) { var_dump(1); } /** * PHP-4 Constructor */ function Foo($bar = 1, $baz = null) { // Call PHP5! $this->__construct($bar, $baz); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the other way around 1' => [ <<<'EOF' <?php class Foo { /** * PHP-4 Constructor. * * This is the real constructor. It's the one that most likely contains any meaningful info in the docblock. */ private function __construct($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo { /** * PHP-5 Constructor. * * This docblock is removed, along with the entire wrapper method. */ protected function __construct($bar) { // Call The Real Constructor, not the hippy fake one! $this->Foo($bar); } /** * PHP-4 Constructor. * * This is the real constructor. It's the one that most likely contains any meaningful info in the docblock. */ private function Foo($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF, ]; yield 'PHP 4 parent' => [ <<<'EOF' <?php class Foo extends FooParEnt { /** * Constructor */ function __construct($bar) { parent::__construct(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParEnt { /** * Constructor */ function Foo($bar) { parent::FooPaRent(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'PHP 4 parent init' => [ <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construct($bar) { parent::init(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function Foo($bar) { parent::init(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'mixed parent' => [ <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construcT($bar) { parent::__construct(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construcT($bar) { parent::FooParenT(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'mixed parent 2' => [ <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construcT($bar) { parent::__construct(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construcT($bar) { $this->FooParenT(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'parent other' => [ <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construct($bar) { parent::__construct(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function Foo($bar) { $this->FooParent(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'parent other 2' => [ <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function __construct($bar) { parent::__construct(1); var_dump(9); } function bar() { var_dump(3); } } EOF, <<<'EOF' <?php class Foo extends FooParent { /** * Constructor */ function Foo($bar) { FooParent::FooParent(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'class with anonymous' => [ <<<'EOF' <?php class Foo { private $bar; public function __construct() { $this->bar = function () {}; } } EOF, <<<'EOF' <?php class Foo { private $bar; public function Foo() { $this->bar = function () {}; } } EOF, ]; yield 'class with comments' => [ <<<'EOF' <?php class /* test */ // another Foo { public function /* test */ __construct($param) { } } EOF, <<<'EOF' <?php class /* test */ // another Foo { public function /* test */ Foo($param) { } } EOF, ]; yield 'alpha beta' => [<<<'EOF' <?php class Foo { public function Foo() { echo 'alpha'; } public function __construct() { echo 'beta'; } } EOF]; yield 'alpha beta trick 1' => [<<<'EOF' <?php class Foo { public function Foo() { // This is not $this->__construct() echo 'alpha'; } public function __construct() { echo 'beta'; } } EOF]; yield 'alpha beta trick 2' => [<<<'EOF' <?php class Foo { public function Foo() { echo 'alpha'; } public function __construct() { // This is not $this->Foo() echo 'beta'; } } EOF]; yield 'alpha beta trick 3' => [<<<'EOF' <?php class Foo { public function Foo() { echo 'alpha'; /* yeah, ok let's construct it anyway */ $this->__construct(); } public function __construct() { echo 'beta'; } } EOF]; yield 'alpha beta trick 4 with another class' => [ <<<'EOF' <?php class Foo { public function Foo() { echo 'alpha'; } public function __construct() { $this->Foo(); // Do something more! echo 'beta'; } } Class Bar { function __construct() { $this->foo = 1; } } EOF, <<<'EOF' <?php class Foo { public function Foo() { echo 'alpha'; } public function __construct() { $this->Foo(); // Do something more! echo 'beta'; } } Class Bar { function bar() { $this->foo = 1; } } EOF, ]; yield 'abstract' => [<<<'EOF' <?php abstract class Foo { abstract function Foo(); } EOF]; yield 'abstract trick' => [<<<'EOF' <?php abstract class Foo { abstract public function Foo(); public function bar() { // This is messed up, I know $this->__construct(); } public function __construct() { $this->baz = 1; } } EOF]; yield 'parent multiple classes' => [ <<<'EOF' <?php class Class1 extends Parent1 { function __construct($foo) { parent::__construct(); echo "something"; } } class Class2 extends Parent2 { function __construct($foo) { echo "something"; } } ?> EOF, <<<'EOF' <?php class Class1 extends Parent1 { function __construct($foo) { $this->Parent1(); echo "something"; } } class Class2 extends Parent2 { function __construct($foo) { echo "something"; } } ?> EOF, ]; yield 'infinite recursion' => [ <<<'EOF' <?php class Parent1 { function __construct() { echo "foobar"; } } class Class1 extends Parent1 { function __construct($foo) { parent::__construct(); echo "something"; } } ?> EOF, <<<'EOF' <?php class Parent1 { function __construct() { echo "foobar"; } } class Class1 extends Parent1 { function Class1($foo) { $this->__construct(); echo "something"; } } ?> EOF, ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ <<<'EOF' <?php class Foo { public function __construct($bar,) { var_dump(1); } } EOF, <<<'EOF' <?php class Foo { public function Foo($bar,) { var_dump(1); } } EOF, ]; yield [ '<?php class Foo { public function __construct() { } }', '<?php class Foo { public function Foo() { } }', ]; yield [ '<?php class Foo { public function __construct() { $this?->__construct(); } }', '<?php class Foo { public function Foo() { $this?->__construct(); } }', ]; yield [ '<?php class Foo extends Bar { public function __construct() { parent::__construct(); } }', '<?php class Foo extends Bar { public function Foo() { $this?->Bar(); } }', ]; yield [ '<?php class Foo { /** * Constructor */ public function __construct($bar = 1, $baz = null) { var_dump(1); } } ', '<?php class Foo { /** * Constructor */ public function __construct($bar = 1, $baz = null) { var_dump(1); } /** * PHP-4 Constructor */ function Foo($bar = 1, $baz = null) { $this?->__construct($bar, $baz); } } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/OrderedInterfacesFixerTest.php
tests/Fixer/ClassNotation/OrderedInterfacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer> * * @author Dave van der Brugge <dmvdbrugge@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedInterfacesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'single' => [ '<?php class T implements A {}', ]; yield 'multiple' => [ '<?php class T implements A, B, C {}', '<?php class T implements C, A, B {}', ]; yield 'newlines' => [ "<?php class T implements\nB,\nC\n{}", "<?php class T implements\nC,\nB\n{}", ]; yield 'newlines and comments' => [ "<?php class T implements\n// Here's A\nA,\n// Here's B\nB\n{}", "<?php class T implements\n// Here's B\nB,\n// Here's A\nA\n{}", ]; yield 'no whitespace' => [ '<?php class T implements/*An*/AnInterface,/*Second*/SecondInterface/*end*/{}', '<?php class T implements/*Second*/SecondInterface,/*An*/AnInterface/*end*/{}', ]; yield 'FQCN' => [ '<?php class T implements \F\Q\C\N, \F\Q\I\N {}', '<?php class T implements \F\Q\I\N, \F\Q\C\N {}', ]; yield 'mixed' => [ '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}', '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}', ]; yield 'multiple in file' => [ '<?php class A1 implements A\B\C, Z\X\Y {} class B2 implements A\B, Z\X {} class C3 implements A, Z\X {} class D4 implements A\B, B\V, Z\X\V {} class E5 implements U\B, X\B, Y\V, Z\X\V {} ', '<?php class A1 implements Z\X\Y, A\B\C {} class B2 implements Z\X, A\B {} class C3 implements Z\X, A {} class D4 implements Z\X\V, B\V, A\B {} class E5 implements Z\X\V, Y\V, X\B, U\B {} ', ]; yield 'interface extends' => [ '<?php interface T extends A, B, C {}', '<?php interface T extends C, A, B {}', ]; yield 'nested anonymous classes' => [ '<?php class T implements A, B, C { public function getAnonymousClassObject() { return new class() implements C, D, E { public function getNestedAnonymousClassObject() { return new class() implements E, F, G {}; } }; } } ', '<?php class T implements C, A, B { public function getAnonymousClassObject() { return new class() implements E, C, D { public function getNestedAnonymousClassObject() { return new class() implements F, G, E {}; } }; } } ', ]; yield 'single line after interfaces' => [ '<?php class Foo implements B, C //, A {} ', '<?php class Foo implements C, B //, A {} ', ]; yield 'descend single' => [ '<?php class T implements A {}', null, [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND], ]; yield 'descend multiple' => [ '<?php class T implements C, B, A {}', '<?php class T implements C, A, B {}', [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND], ]; yield 'descend mixed' => [ '<?php class T implements /* Who mixes these? */ UnNamespaced, Partially\Q\C\N, \F\Q\C\N {}', '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}', [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND], ]; yield 'length single' => [ '<?php class A implements A {}', null, [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH], ]; yield 'length multiple' => [ '<?php class A implements Short, Longer, MuchLonger {}', '<?php class A implements MuchLonger, Short, Longer {}', [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH], ]; yield 'length mixed' => [ '<?php class T implements \F\Q\C\N, /* Who mixes these? */ UnNamespaced, Partially\Q\C\N {}', '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}', [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH], ]; yield 'length normalized' => [ '<?php class A implements ABCDE, A\B\C\D { /* */ } ', '<?php class A implements A\B\C\D, ABCDE { /* */ } ', [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH], ]; yield 'length, descend single' => [ '<?php class A implements A {}', null, [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH, OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND, ], ]; yield 'length, descend multiple' => [ '<?php class A implements MuchLonger, Longer, Short {}', '<?php class A implements MuchLonger, Short, Longer {}', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH, OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND, ], ]; yield 'length, descend mixed' => [ '<?php class T implements Partially\Q\C\N, /* Who mixes these? */ UnNamespaced, \F\Q\C\N {}', '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH, OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND, ], ]; yield 'length, descend normalized' => [ '<?php class A implements A\B\C\D, ABCDE { /* */ } ', '<?php class A implements ABCDE, A\B\C\D { /* */ } ', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH, OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND, ], ]; yield 'case sensitive single' => [ '<?php class A implements A {}', null, [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA, 'case_sensitive' => true, ], ]; yield 'alpha multiple' => [ '<?php class A implements AA, Aaa, FF, Fff {}', '<?php class A implements Fff, Aaa, FF, AA {}', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA, 'case_sensitive' => true, ], ]; yield 'alpha mixed' => [ '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}', '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA, 'case_sensitive' => true, ], ]; yield 'alpha normalized' => [ '<?php class A implements A\B\C\D, AAa\B\C\D, ABCDE, Aaa\B\C\D { /* */ } ', '<?php class A implements Aaa\B\C\D, AAa\B\C\D, ABCDE, A\B\C\D { /* */ } ', [ OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA, 'case_sensitive' => true, ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php
tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer> * * @author Gregor Harlan <gharlan@web.de> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedClassElementsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ <<<'EOT' <?php class Foo {} class Bar { } EOT, ]; yield [ <<<'EOT' <?php class Foo { const C1 = 1; protected $abc = 'abc'; public function baz($y, $z) {} private function bar1($x) { return 1; } } EOT, <<<'EOT' <?php class Foo { private function bar1($x) { return 1; } protected $abc = 'abc'; const C1 = 1; public function baz($y, $z) {} } EOT, ]; yield [ <<<'EOT' <?php interface FooInterface { /** const 1 */ const CONST1 = 'const1'; // foo // const 2 const CONST2 = 'const2'; public function xyz($x, $y, $z); // comment /** * @param array $a * * @return string */ function abc(array &$a = null); public function def(); } EOT, <<<'EOT' <?php interface FooInterface { public function xyz($x, $y, $z); // comment /** const 1 */ const CONST1 = 'const1'; /** * @param array $a * * @return string */ function abc(array &$a = null); // foo // const 2 const CONST2 = 'const2'; public function def(); } EOT, ]; yield [ <<<'EOT' <?php abstract class Foo extends FooParent implements FooInterface1, FooInterface2 { use Bar; use Baz { abc as private; } const C1 = 1; /* comment for C2 */ const C2 = 2; public $fooPublic; // comment 3 protected $fooProtected = array(1, 2); // comment 1 private $fooPrivate; protected function __construct() { } public function __destruct() {} public function __clone() {} public static function setUpBeforeClass() {} public static function tearDownAfterclass() { } /* multiline comment */ protected function setUp() {} protected function assertPreConditions() {} protected function assertPostConditions() {} protected function tearDown() {} abstract public function foo1($a, $b = 1); // foo2 function foo2() { return $this->foo1(1); } public static function foo3(self $foo) { return $foo->foo2(); } /* comment 1 */ /* comment 2 */ // comment /** * Docblock */ protected function foo4(\ArrayObject $object, array $array, $a = null) { bar(); if (!$a) { $a = function ($x) { var_dump($x); }; } } private function foo5() { } // end foo5 } EOT, <<<'EOT' <?php abstract class Foo extends FooParent implements FooInterface1, FooInterface2 { // comment 1 private $fooPrivate; abstract public function foo1($a, $b = 1); protected function setUp() {} protected function tearDown() {} public function __clone() {} const C1 = 1; // foo2 function foo2() { return $this->foo1(1); } public static function setUpBeforeClass() {} public function __destruct() {} use Bar; public static function foo3(self $foo) { return $foo->foo2(); } /* comment 1 */ /* comment 2 */ // comment 3 protected $fooProtected = array(1, 2); public $fooPublic; /* comment for C2 */ const C2 = 2; public static function tearDownAfterclass() { } /* multiline comment */ protected function assertPostConditions() {} use Baz { abc as private; } protected function assertPreConditions() {} private function foo5() { } // end foo5 protected function __construct() { } // comment /** * Docblock */ protected function foo4(\ArrayObject $object, array $array, $a = null) { bar(); if (!$a) { $a = function ($x) { var_dump($x); }; } } } EOT, ]; yield [ <<<'EOT' <?php class Foo { const C = 'C'; public function abc() {} protected function xyz() {} } class Bar { const C = 1; public function foo($a) { return 1; } public function baz() {} } EOT, <<<'EOT' <?php class Foo { protected function xyz() {} const C = 'C'; public function abc() {} } class Bar { public function foo($a) { return 1; } const C = 1; public function baz() {} } EOT, ]; yield [ <<<'EOT' <?php trait FooTrait { use BarTrait; use BazTrait; protected function abc() { } } EOT, <<<'EOT' <?php trait FooTrait { protected function abc() { } use BarTrait; use BazTrait; } EOT, ]; yield [ <<<'EOT' <?php /** * @see #4086 */ class ComplexStringVariableAndUseTrait { use FooTrait; public function sayHello($name) { echo "Hello, {$name}!"; } } EOT, <<<'EOT' <?php /** * @see #4086 */ class ComplexStringVariableAndUseTrait { public function sayHello($name) { echo "Hello, {$name}!"; } use FooTrait; } EOT, ]; yield [ <<<'EOT' <?php class Foo { use BarTrait; use BazTrait; const C1 = 1; const C2 = 2; protected static $protStatProp; public static $pubStatProp1; public $pubProp1; protected $protProp; var $pubProp2; private static $privStatProp; private $privProp; public static $pubStatProp2; public $pubProp3; protected function __construct() {} private static function privStatFunc() {} public function pubFunc1() {} public function __toString() {} protected function protFunc() {} function pubFunc2() {} public static function pubStatFunc1() {} public function pubFunc3() {} static function pubStatFunc2() {} private function privFunc() {} public static function pubStatFunc3() {} protected static function protStatFunc() {} public function __destruct() {} } EOT, <<<'EOT' <?php class Foo { private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, ['order' => ['use_trait', 'constant', 'property', 'construct', 'method', 'destruct']], ]; yield [ <<<'EOT' <?php class Foo { public static $pubStatProp1; public function pubFunc1() {} public $pubProp1; public function __toString() {} function pubFunc2() {} public function __destruct() {} var $pubProp2; public static function pubStatFunc1() {} public function pubFunc3() {} const C1 = 1; static function pubStatFunc2() {} public static $pubStatProp2; const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static $protStatProp; protected function protFunc() {} protected $protProp; protected function __construct() {} protected static function protStatFunc() {} private static function privStatFunc() {} private static $privStatProp; private $privProp; private function privFunc() {} use BarTrait; use BazTrait; } EOT, <<<'EOT' <?php class Foo { private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, ['order' => ['public', 'protected', 'private']], ]; yield [ <<<'EOT' <?php class Foo { use BarTrait; use BazTrait; const C1 = 1; const C2 = 2; public static $pubStatProp1; public static $pubStatProp2; protected static $protStatProp; private static $privStatProp; public $pubProp1; var $pubProp2; public $pubProp3; protected $protProp; private $privProp; protected function __construct() {} public function __destruct() {} public function __toString() {} public static function pubStatFunc1() {} static function pubStatFunc2() {} public static function pubStatFunc3() {} protected static function protStatFunc() {} private static function privStatFunc() {} public function pubFunc1() {} function pubFunc2() {} public function pubFunc3() {} protected function protFunc() {} private function privFunc() {} } EOT, <<<'EOT' <?php class Foo { private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, [ 'order' => [ 'use_trait', 'constant', 'property_public_static', 'property_protected_static', 'property_private_static', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'method_public_static', 'method_protected_static', 'method_private_static', 'method_public', 'method_protected', 'method_private', ], ], ]; yield [ <<<'EOT' <?php abstract class Foo { use BarTrait; use BazTrait; const C1 = 1; const C2 = 2; protected static $protStatProp; public static $pubStatProp1; public $pubProp1; protected $protProp; var $pubProp2; private static $privStatProp; private $privProp; public static $pubStatProp2; public $pubProp3; protected function __construct() {} abstract public function absPubFunc(); private static function privStatFunc() {} public function pubFunc1() {} abstract protected function absProtFunc(); public function __toString() {} protected function protFunc() {} function pubFunc2() {} abstract protected static function absProtStatFunc(); public static function pubStatFunc1() {} public function pubFunc3() {} static function pubStatFunc2() {} private function privFunc() {} abstract public static function absPubStatFunc(); public static function pubStatFunc3() {} protected static function protStatFunc() {} public function __destruct() {} } EOT, <<<'EOT' <?php abstract class Foo { abstract public function absPubFunc(); private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} abstract protected function absProtFunc(); use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} abstract protected static function absProtStatFunc(); public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; abstract public static function absPubStatFunc(); protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, ['order' => ['use_trait', 'constant', 'property', 'construct', 'method', 'destruct']], ]; yield [ <<<'EOT' <?php abstract class Foo { abstract public function absPubFunc(); public static $pubStatProp1; public function pubFunc1() {} public $pubProp1; public function __toString() {} function pubFunc2() {} public function __destruct() {} var $pubProp2; public static function pubStatFunc1() {} public function pubFunc3() {} const C1 = 1; static function pubStatFunc2() {} public static $pubStatProp2; abstract public static function absPubStatFunc(); const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static $protStatProp; abstract protected function absProtFunc(); protected function protFunc() {} protected $protProp; abstract protected static function absProtStatFunc(); protected function __construct() {} protected static function protStatFunc() {} private static function privStatFunc() {} private static $privStatProp; private $privProp; private function privFunc() {} use BarTrait; use BazTrait; } EOT, <<<'EOT' <?php abstract class Foo { abstract public function absPubFunc(); private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} abstract protected function absProtFunc(); use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} abstract protected static function absProtStatFunc(); public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; abstract public static function absPubStatFunc(); protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, ['order' => ['public', 'protected', 'private']], ]; yield [ <<<'EOT' <?php abstract class Foo { use BarTrait; use BazTrait; const C1 = 1; const C2 = 2; public static $pubStatProp1; public static $pubStatProp2; protected static $protStatProp; private static $privStatProp; public $pubProp1; var $pubProp2; public $pubProp3; protected $protProp; private $privProp; protected function __construct() {} public function __destruct() {} public function __toString() {} public static function pubStatFunc1() {} static function pubStatFunc2() {} public static function pubStatFunc3() {} abstract public static function absPubStatFunc(); protected static function protStatFunc() {} abstract protected static function absProtStatFunc(); private static function privStatFunc() {} public function pubFunc1() {} function pubFunc2() {} public function pubFunc3() {} abstract public function absPubFunc(); protected function protFunc() {} abstract protected function absProtFunc(); private function privFunc() {} } EOT, <<<'EOT' <?php abstract class Foo { abstract public function absPubFunc(); private static function privStatFunc() {} protected static $protStatProp; public static $pubStatProp1; public function pubFunc1() {} abstract protected function absProtFunc(); use BarTrait; public $pubProp1; public function __toString() {} protected function protFunc() {} protected $protProp; function pubFunc2() {} abstract protected static function absProtStatFunc(); public function __destruct() {} var $pubProp2; private static $privStatProp; use BazTrait; public static function pubStatFunc1() {} public function pubFunc3() {} private $privProp; const C1 = 1; static function pubStatFunc2() {} private function privFunc() {} public static $pubStatProp2; abstract public static function absPubStatFunc(); protected function __construct() {} const C2 = 2; public static function pubStatFunc3() {} public $pubProp3; protected static function protStatFunc() {} } EOT, [ 'order' => [ 'use_trait', 'constant', 'property_public_static', 'property_protected_static', 'property_private_static', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'method_public_static', 'method_public_abstract_static', 'method_protected_static', 'method_protected_abstract_static', 'method_private_static', 'method_public', 'method_public_abstract', 'method_protected', 'method_protected_abstract', 'method_private', ], ], ]; yield [ <<<'EOT' <?php abstract class Foo { public function test2(){} public abstract function test1(); } EOT, <<<'EOT' <?php abstract class Foo { public abstract function test1(); public function test2(){} } EOT, [ 'order' => [ 'method_public', 'method_abstract', ], ], ]; yield [ <<<'EOT' <?php abstract class Foo { protected function __construct() {} public function __invoke() {} public function __destruct() {} public static function pubStatFunc1() {} static function pubStatFunc2() {} public static function pubStatFunc3() {} private function custom1() {} abstract public static function absPubStatFunc(); protected static function protStatFunc() {} abstract protected static function absProtStatFunc(); private static function privStatFunc() {} public function pubFunc1() {} function pubFunc2() {} public function pubFunc3() {} abstract public function custom3(); abstract public function absPubFunc(); protected function protFunc() {} abstract protected function absProtFunc(); private function privFunc() {} protected static function custom2() {} public function __get($prop) {} public function __toString() {} } EOT, <<<'EOT' <?php abstract class Foo { public function __get($prop) {}
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php
tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ClassAttributesSeparationFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php class Sample { private $a; // foo /** second in a hour */ private $b; } ', '<?php class Sample {private $a; // foo /** second in a hour */ private $b; } ', ]; yield 'empty class' => [ '<?php class Foo {}', ]; yield 'simple top class' => [ '<?php class A { public function Foo(){} }', '<?php class A {public function Foo(){}}', ]; yield 'comment' => [ '<?php class A {/* function comment */ public function Bar(){} }', '<?php class A {/* function comment */public function Bar(){} }', ]; yield 'comment, multiple lines' => [ '<?php class A { /* some comment */ public function Bar(){} }', '<?php class A { /* some comment */ public function Bar(){} }', ]; yield 'simple PHPDoc case' => [ '<?php class Foo { /** Doc 1 */ public function A(){} /** Doc 2 */ public function B(){} }', '<?php class Foo {/** Doc 1 */public function A(){} /** Doc 2 */ public function B(){} }', ]; yield 'add a newline at the end of a class with trait group' => [ '<?php class A { use Bar { __construct as barConstruct; baz as barBaz; } }', '<?php class A { use Bar { __construct as barConstruct; baz as barBaz; }}', ]; yield 'add a newline at the end of a class with trait' => [ '<?php class A { use A\B\C; }', '<?php class A { use A\B\C;}', ]; yield 'removes extra lines at the end of an interface' => [ '<?php interface F { public function A(); }', '<?php interface F { public function A(); }', ]; yield 'removes extra lines at the end of an abstract class' => [ '<?php abstract class F { public abstract function A(); }', '<?php abstract class F { public abstract function A(); }', ]; yield 'add a newline at the end of a class' => [ '<?php class A { public function A(){} }', '<?php class A { public function A(){}}', ]; yield 'add a newline at the end of a class: with comments' => [ '<?php class A { public const A = 1; /* foo */ /* bar */ }', '<?php class A { public const A = 1; /* foo */ /* bar */}', ]; yield 'add a newline at the end of a class: with comments with trailing space' => [ '<?php class A { public const A = 1; /* foo */ /* bar */ }', '<?php class A { public const A = 1; /* foo */ /* bar */ }', ]; $to = $from = '<?php '; for ($i = 0; $i < 15; ++$i) { $from .= \sprintf('class A%d{public function GA%d(){return new class {public function B6B%d(){}};}public function otherFunction%d(){}}', $i, $i, $i, $i); $to .= \sprintf("class A%d{\npublic function GA%d(){return new class {\npublic function B6B%d(){}\n};}\n\npublic function otherFunction%d(){}\n}", $i, $i, $i, $i); } yield [$to, $from]; yield [ '<?php $a = new class { public function H(){} public function B7(){} private function C(){} };', '<?php $a = new class { public function H(){} public function B7(){} private function C(){} };', ]; yield [ '<?php class Foo extends X\Y { // @phpstan-ignore method.internal // regular comment };', ]; yield [ '<?php class Foo extends X\Y { // @phpstan-ignore method.internal public $p; };', ]; yield [ '<?php $a = new class extends X\Y { // @phpstan-ignore method.internal public function m(){} };', ]; yield [ '<?php class Foo extends X\Y { // @phpstan-ignore method.internal public $p; };', '<?php class Foo extends X\Y { // @phpstan-ignore method.internal public $p; };', ]; yield [ '<?php class A { public function getFilter() { return new class () implements FilterInterface { private $d = 123; public function pass($a, $b) { echo $a; } public $e = 5; };} } ', '<?php class A {public function getFilter() { return new class () implements FilterInterface {private $d = 123; public function pass($a, $b) { echo $a; } public $e = 5;};} } ', ]; yield ['<?php class SomeClass1 { // This comment // is multiline. public function echoA() { echo "a"; } } ']; yield [ '<?php class SomeClass2 { // This comment /* is multiline. */ public function echoA() { echo "a"; } } ', '<?php class SomeClass2 { // This comment /* is multiline. */public function echoA() { echo "a"; } } ', ]; yield [ '<?php class SomeClass3 { // This comment // is multiline. public function echoA() { echo "a"; } } ', ]; yield [ '<?php class SomeClass1 { private $a; // public function methodA() { } private $b; // public function methodB() { } // C public function methodC() { } // D public function methodD() { } /* E */ public function methodE() { } /* F */ public function methodF() { } } ', '<?php class SomeClass1 { private $a; // public function methodA() { } private $b; // public function methodB() { } // C public function methodC() { } // D public function methodD() { } /* E */ public function methodE() { } /* F */ public function methodF() { } } ', ]; yield ['<?php class SomeClass { // comment public function echoA() { echo "a"; } } ']; yield ['<?php class SomeClass { // This comment // is multiline. public function echoA() { echo "a"; } } ']; yield [ '<?php class SomeClass { // comment public function echoA() { echo "a"; } } ', '<?php class SomeClass { // comment public function echoA() { echo "a"; } } ', ]; yield [ '<?php class SomeClass { /* comment */ public function echoB() { echo "a"; } } ', '<?php class SomeClass { /* comment */public function echoB() { echo "a"; } } ', ]; yield [ '<?php class SomeClass { /* comment */ public function echoC() { echo "a"; } } ', '<?php class SomeClass { /* comment */ public function echoC() { echo "a"; } } ', ]; yield [ '<?php abstract class MethodTest2 { public function method045() { $files = null; if (!empty($files)) { $this->filter( function (\SplFileInfo $file) use ($files) { return !in_array($file->getRelativePathname(), $files, true); } ); } } private $a; public static function method145() { } abstract protected function method245(); // comment final private function method345() { } } function some1(){ echo 1;} function some2(){ echo 2;}', '<?php abstract class MethodTest2 { public function method045() { $files = null; if (!empty($files)) { $this->filter( function (\SplFileInfo $file) use ($files) { return !in_array($file->getRelativePathname(), $files, true); } ); } } private $a; public static function method145() { } abstract protected function method245(); // comment final private function method345() { } } function some1(){ echo 1;} function some2(){ echo 2;}', ]; yield [ '<?php /* * This file is part of the PHP CS utility. * * (c) Fabien Potencier <fabien@symfony.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Linter; /** * Dummy linter. No linting is performed. No error is raised. * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @internal */ final class NullLinter implements LinterInterface { /** * {@inheritdoc} */ public function lintFile($path) { unset($path); } /** * {@inheritdoc} */ public function lintSource($source) { unset($source); } } ', ]; // do not touch anonymous functions (since PHP doesn't allow // for class attributes being functions :(, we only have to test // those used within methods) yield [ '<?php class MethodTestAnonymous { public function method444a() { $text = "hello"; $example = function ($arg) use ($message) { var_dump($arg . " " . $message); }; $example($text); $example = function($arg) use ($message) { var_dump($arg . " " . $message); }; $example = function /*test*/ ($arg) use ($message) { var_dump($arg . " " . $message); }; } }', ]; yield [ '<?php class MethodTest1 { private $c; // public function method444a() { } /** * */ public function method444b() { } // public function method444c() { } private $a; public function method444d() { } private $b; // public function method444e() { } public function method444f() { } private $d; // public function method444f1() { } /**/ public function method444g() { } }', '<?php class MethodTest1 { private $c; // public function method444a() { } /** * */ public function method444b() { } // public function method444c() { } private $a; public function method444d() { } private $b; // public function method444e() { } public function method444f() { } private $d; // public function method444f1() { } /**/ public function method444g() { } }', ]; // spaces between methods yield [ '<?php abstract class MethodTest3 { public function method021() { } public static function method121() { } abstract protected function method221(); '.' final private function method321a() { } }', '<?php abstract class MethodTest3 { public function method021() { } public static function method121() { } abstract protected function method221(); '.' final private function method321a() { } }', ]; // don't change correct code yield [ '<?php class SmallHelperException extends \Exception { public function getId111() { return 1; } public function getMessage111() { return \'message\'; } } class MethodTest123124124 { public function method111a(){} public function method211a(){} }', ]; // do not touch function out of class scope yield [ '<?php function some0() { } class MethodTest4 { public function method122b() { } public function method222b() { } } function some() { } function some2() { } ', ]; yield [ '<?php interface A { public function B1(); // allowed comment public function C(); // allowed comment }', '<?php interface A {public function B1(); // allowed comment public function C(); // allowed comment }', ]; yield [ '<?php class Foo { var $a; var $b; }', '<?php class Foo { var $a; var $b; }', ]; yield [ '<?php class A { /** 1 */ function A2() {} /** 2 */ function B2() {} } ', '<?php class A { /** 1 */ function A2() {} /** 2 */ function B2() {} } ', ]; // do not touch well formatted traits yield [ '<?php trait OkTrait { function getReturnTypeOk() { } /** * */ function getReturnDescriptionOk() { } }', ]; yield [ '<?php trait ezcReflectionReturnInfo { public $x = 1; protected function getA(){echo 1;} function getB(){echo 2;} protected function getC(){echo 3;} /** Description */ function getD(){echo 4;} protected function getE(){echo 3;} private $a; function getF(){echo 4;} }', '<?php trait ezcReflectionReturnInfo { public $x = 1; protected function getA(){echo 1;}function getB(){echo 2;} protected function getC(){echo 3;}/** Description */function getD(){echo 4;} protected function getE(){echo 3;}private $a;function getF(){echo 4;} }', ]; yield [ '<?php trait SomeReturnInfo { function getReturnType() { } function getReturnDescription() { } function getReturnDescription2() { } abstract public function getWorld(); }', '<?php trait SomeReturnInfo { function getReturnType() { } function getReturnDescription() { } function getReturnDescription2() { } abstract public function getWorld(); }', ]; yield [ '<?php interface TestInterface { public function someInterfaceMethod4(); public function someInterfaceMethod5(); /** * {@link} */ '.' public function someInterfaceMethod6(); public function someInterfaceMethod7(); public function someInterfaceMethod8(); }', '<?php interface TestInterface { public function someInterfaceMethod4(); public function someInterfaceMethod5(); /** * {@link} */ '.' public function someInterfaceMethod6(); public function someInterfaceMethod7(); public function someInterfaceMethod8(); }', ]; // do not touch well formatted interfaces yield [ '<?php interface TestInterfaceOK { public function someMethod1(); public function someMethod2(); }', ]; // method after trait use yield [ '<?php trait ezcReflectionReturnInfo { function getReturnDescription() {} } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo; function afterUseTrait(){} function afterUseTrait2(){} }', '<?php trait ezcReflectionReturnInfo { function getReturnDescription() {} } class ezcReflectionMethod extends ReflectionMethod { use ezcReflectionReturnInfo;function afterUseTrait(){}function afterUseTrait2(){} }', ]; yield 'multi line property' => [ '<?php class Foo { private $prop = [ 1 => true, 2 => false, ]; // comment2 private $bar = 1; }', '<?php class Foo { private $prop = [ 1 => true, 2 => false, ]; // comment2 private $bar = 1; }', ['elements' => ['property' => 'one']], ]; yield 'trait group import none' => [ '<?php class Foo { use Ao; use B0 { X0 as Y0;} // test use A; use B { X as Y;} // test use Char; use Bar { __construct as barConstruct; baz as barBaz; } use Dua; }', '<?php class Foo { use Ao; use B0 { X0 as Y0;} // test use A; use B { X as Y;} // test use Char; use Bar { __construct as barConstruct; baz as barBaz; } use Dua; }', ['elements' => ['trait_import' => 'none']], ]; yield [ '<?php class Foo { /** A */ private $email; private $foo0; #0 /* test */ private $foo1; #1 private $foo2; /* @2 */ }', '<?php class Foo { /** A */ private $email; private $foo0; #0 /* test */ private $foo1; #1 private $foo2; /* @2 */ }', ['elements' => ['property' => 'none']], ]; yield [ '<?php class Sample { /** @var int */ const FOO = 1; /** @var int */ const BAR = 2; const BAZ = 3; const OTHER = 4; const OTHER2 = 5; }', '<?php class Sample { /** @var int */ const FOO = 1; /** @var int */ const BAR = 2; const BAZ = 3; const OTHER = 4; const OTHER2 = 5; }', ['elements' => ['const' => 'none']], ]; yield 'multiple trait import 5954' => [ '<?php class Foo { use Bar, Baz; }', null, ['elements' => ['method' => 'one']], ]; yield 'multiple trait import with method 5954' => [ '<?php class Foo { use Bar, Baz; public function f() {} }', '<?php class Foo { use Bar, Baz; public function f() {} }', ['elements' => ['method' => 'one']], ]; yield 'trait group import 5843' => [ '<?php class Foo { use Ao; use B0 { X0 as Y0;} // test use A; use B { X as Y;} // test use Char; use Bar { __construct as barConstruct; baz as barBaz; } use Dua; public function aaa() { } }', '<?php class Foo { use Ao; use B0 { X0 as Y0;} // test use A; use B { X as Y;} // test use Char; use Bar { __construct as barConstruct; baz as barBaz; } use Dua; public function aaa() { } }', ['elements' => ['method' => 'one', 'trait_import' => 'one']], ]; yield [ '<?php class Foo { use SomeTrait1; use SomeTrait2; public function Bar(){} } ', '<?php class Foo { use SomeTrait1; use SomeTrait2; public function Bar(){} } ', ['elements' => ['method' => 'one', 'trait_import' => 'one']], ]; yield 'trait group import 5852' => [ '<?php class Foo { use A; use B; /** * */ public function A(){} }', '<?php class Foo { use A; use B; /** * */ public function A(){} }', ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one', 'trait_import' => 'none']], ]; yield [ '<?php abstract class Example { use SomeTrait; use AnotherTrait; public $property; abstract public function method(): void; }', '<?php abstract class Example { use SomeTrait; use AnotherTrait; public $property; abstract public function method(): void; }', ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one']], ]; yield [ '<?php class A { private $a = null; public $b = 1; function A() {} } ', '<?php class A { private $a = null; public $b = 1; function A() {} } ', ['elements' => ['property' => 'one']], ]; yield [ '<?php class A { private $a = null; public $b = 1; function A() {} } ', '<?php class A { private $a = null; public $b = 1; function A() {} } ', ['elements' => ['property' => 'none']], ]; yield [ '<?php class A { const A = 1; const THREE = ONE + self::TWO; /* test */ # test const B = 2; } ', '<?php class A { const A = 1; const THREE = ONE + self::TWO; /* test */ # test const B = 2; } ', ['elements' => ['const' => 'one']], ]; yield [ '<?php class A { const A = 1; const THREE = ONE + self::TWO; const B = 2; } ', '<?php class A { const A = 1; const THREE = ONE + self::TWO; const B = 2; } ', ['elements' => ['const' => 'none']], ]; yield [ '<?php class A { function D() {} function B4() {} } ', '<?php class A { function D() {} function B4() {} } ', ['elements' => ['method' => 'one']], ]; yield [ '<?php class A { function A() {} function B() {} } ', '<?php class A { function A() {} function B() {} } ', ['elements' => ['method' => 'none']], ]; yield [ '<?php class A { private $x; private $y; final function f1() {} final function f2() {} } ', '<?php class A { private $x; private $y; final function f1() {} final function f2() {} } ', ['elements' => ['property' => 'none', 'method' => 'one']], ]; yield [ '<?php class A { const FOO = 1; const BAR = 2; function f1() {} function f2() {} } ', '<?php class A { const FOO = 1; const BAR = 2; function f1() {} function f2() {} } ', ['elements' => ['const' => 'none', 'method' => 'one']], ]; yield [ '<?php class A { const FOO = 1; const BAR = 2; public function f1() {} public function f2() {} } ', '<?php class A { const FOO = 1; const BAR = 2; public function f1() {} public function f2() {} } ', ['elements' => ['const' => 'none', 'method' => 'one']], ]; yield [ '<?php class A { const B = 2; const FOO = 1; const BAR = 2; /** @var int */ const BAZ = 3; /** @var int */ const NEW = 4; /** @var int */ const A = 5; } ', '<?php class A { const B = 2; const FOO = 1; const BAR = 2; /** @var int */ const BAZ = 3; /** @var int */ const NEW = 4; /** @var int */ const A = 5; } ', ['elements' => ['const' => 'only_if_meta']], ]; yield [ '<?php class B { public $foo; /** @var string */ public $bar; public $baz; } ', '<?php class B { public $foo; /** @var string */ public $bar; public $baz; } ', ['elements' => ['property' => 'only_if_meta']], ]; yield [ '<?php class C { public function f1() {} public function f2() {} public function f3() {} /** @return string */ public function f4() {} } ', '<?php class C { public function f1() {} public function f2() {} public function f3() {} /** @return string */ public function f4() {} } ', ['elements' => ['method' => 'only_if_meta']], ]; yield [ '<?php class Sample { /** @var int */ const ART = 1; const SCIENCE = 2; /** @var string */ public $a; /** @var int */ public $b; public $c; /** * @param string $a * @param int $b * @param int $c */ public function __construct($a, $b, $c) {} public function __destruct() {} } ', '<?php class Sample { /** @var int */ const ART = 1; const SCIENCE = 2; /** @var string */ public $a; /** @var int */ public $b; public $c; /** * @param string $a * @param int $b * @param int $c */ public function __construct($a, $b, $c) {} public function __destruct() {} } ', ['elements' => ['const' => 'only_if_meta', 'property' => 'only_if_meta', 'method' => 'only_if_meta']], ]; yield [ '<?php class A { use A; use B; private $a = null; public $b = 1; } ', '<?php class A { use A; use B; private $a = null; public $b = 1; } ', ['elements' => ['property' => 'none', 'trait_import' => 'none']], ]; yield [ '<?php class Foo { public function H1(){} /** */ public const BAR = 123; /** */ private const BAZ = "a"; }', '<?php class Foo { public function H1(){} /** */ public const BAR = 123; /** */ private const BAZ = "a"; }', ]; yield [ '<?php class Foo { private ?int $foo; protected string $bar; public iterable $baz; var ? Foo\Bar $qux; }', '<?php class Foo { private ?int $foo; protected string $bar; public iterable $baz; var ? Foo\Bar $qux; }', ]; yield [ '<?php class Foo { private array $foo;
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/ModifierKeywordsFixerTest.php
tests/Fixer/ClassNotation/ModifierKeywordsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\ModifierKeywordsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ModifierKeywordsFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\ModifierKeywordsFixer * * @final * * @todo move `final` from annotation to code when `VisibilityRequiredFixer` is removed * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ class ModifierKeywordsFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'properties' => [ <<<'EOF' <?php class Foo { public $var; protected $var_foo; private $FooBar; public static $var1; protected static $var_foo2; private static $FooBar1; public static $var2; protected static $var_foo3; private static $FooBar2; private static $FooBar3; public $old = 'foo'; } EOF, <<<'EOF' <?php class Foo { public $var; protected $var_foo; private $FooBar; static public $var1; static protected $var_foo2; static private $FooBar1; public static $var2; protected static $var_foo3; private static $FooBar2; private static $FooBar3; var $old = 'foo'; } EOF, ]; yield 'properties after method' => [ <<<'EOF' <?php class Foo { public function aaa() {} public $bbb; } EOF, <<<'EOF' <?php class Foo { public function aaa() {} var $bbb; } EOF, ]; yield 'methods' => [ <<<'EOF' <?php class MyTestWithAnonymousClass extends TestCase { public function setUp() { $provider = new class(function () {}) {}; } public function testSomethingWithMoney( Money $amount ) { } } EOF, <<<'EOF' <?php class MyTestWithAnonymousClass extends TestCase { function setUp() { $provider = new class(function () {}) {}; } public function testSomethingWithMoney( Money $amount ) { } } EOF, ]; yield [ <<<'EOF' <?php abstract class Foo { public function& foo0() {} public function & foo1() {} public function &foo2() {} protected function foo3($b) {} abstract protected function foo4(); private function foo5() {} final public function foo6() {} abstract public function foo7(); final public function foo8() {} abstract public function foo9(); public static function fooA() {} public static function fooD() {} final public static function fooE() {} abstract public function fooF(); public function fooG ($foo) {} public function fooH() { static $foo; $bar = function($baz) {}; } } EOF, <<<'EOF' <?php abstract class Foo { public function& foo0() {} public function & foo1() {} function &foo2() {} protected function foo3($b) {} protected abstract function foo4(); private function foo5() {} final public function foo6() {} abstract public function foo7(); public final function foo8() {} public abstract function foo9(); public static function fooA() {} public static function fooD() {} final static function fooE() {} abstract function fooF(); function fooG ($foo) {} function fooH() { static $foo; $bar = function($baz) {}; } } EOF, ]; yield [ <<<'EOF' <?php abstract class Foo1 { public function& foo0($a) {} } EOF, <<<'EOF' <?php abstract class Foo1 { function& foo0($a) {} } EOF, ]; yield 'leave functions alone' => [<<<'EOF' <?php function foo() { static $foo; } EOF]; yield 'leave functions alone with variables matching OOP words' => [<<<'EOF' <?php function foo() { static $class; $interface = 'foo'; $trait = 'bar'; } EOF]; yield 'leave functions alone inside conditionals' => [<<<'EOF' <?php if (!function_exists('foo')) { function foo($arg) { return $arg; } } EOF]; yield 'leave functions alone inside conditionals with OOP word in comment' => [<<<'EOF' <?php /* class <= this is just a stop-word */ if (!function_exists('foo')) { function foo($arg) { return $arg; } } EOF]; yield 'leave functions alone with OOP word in comment' => [<<<'EOF' <?php /* class */ function foo($arg) { return $arg; } EOF]; yield 'leave functions alone outside classes with OOP word in inline HTML' => [<<<'EOF' <?php if (!function_exists('foo')) { function foo($arg) { ?> <div class="test"></div> <?php return $arg; } } EOF]; yield 'leave functions alone outside classes with OOP word in string value' => [<<<'EOF' <?php if (!function_exists('foo')) { function foo($arg) { return 'she has class right?'; } } EOF]; yield 'leave functions alone outside classes with OOP word in function name' => [<<<'EOF' <?php comment_class(); if (!function_exists('foo')) { function foo($arg) { return $arg; } } EOF]; yield 'leave functions alone after class' => [<<<'EOF' <?php class Foo { public $foo; } if (!function_exists('bar')) { function bar() { return 'bar'; } } EOF]; yield 'curly open syntax' => [<<<'EOF' <?php class Foo { private $bar; public function foo() { $foo = "foo"; $fooA = "ab{$foo}cd"; $bar = "bar"; // test if variable after T_CURLY_OPEN is intact } } EOF]; yield 'dollar open curly braces syntax' => [<<<'EOF' <?php class Foo { public function bar() { $foo = "foo${width}foo"; $bar = "bar"; // test if variable after T_DOLLAR_OPEN_CURLY_BRACES is intact } } EOF]; yield 'leave JavaScript outside PHP alone' => [<<<'EOF' <?php function foo() { return "foo"; } ?> <script type="text/javascript"> function foo(bar) { alert(bar); } </script> EOF]; yield 'leave JavaScript in string alone' => [<<<'EOF' <?php function registerJS() { echo '<script type="text/javascript"> function foo(bar) { alert(bar); } </script>'; } EOF]; yield 'leave JavaScript in variable alone' => [<<<'EOF' <?php class Foo { public function bar() { $script = <<<JAVASCRIPT <script type="text/javascript"> function foo(bar) { alert(bar); } </script> JAVASCRIPT; return $script; } } EOF]; yield 'comma separated properties' => [<<<'EOF' <?php class Foo { public $foo1; private $foo2; protected $bar1, $bar2; public $baz1 = null, $baz2, $baz3 = false; public $foo, $bar; } EOF, <<<'EOF' <?php class Foo { var $foo1; private $foo2; protected $bar1, $bar2; public $baz1 = null, $baz2, $baz3 = false; var $foo, $bar; } EOF]; yield 'var declarations with array value' => [<<<'EOF' <?php class Foo { public $foo1 = 1; public $foo2a = array('foo'); public $foo2b = ['foo']; public $foo3a = array('foo', 'bar'); public $foo3b = ['foo', 'bar']; public $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10; public $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10; } EOF, <<<'EOF' <?php class Foo { var $foo1 = 1; var $foo2a = array('foo'); var $foo2b = ['foo']; var $foo3a = array('foo', 'bar'); var $foo3b = ['foo', 'bar']; public $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10; public $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10; } EOF]; yield [ '<?php class A { public const B=1; }', '<?php class A { const B=1; }', ['elements' => ['const']], ]; yield [ '<?php class A { public const B=1;public const C=1;/**/public const#a D=1;public const E=1;// public const F=1; }', '<?php class A { const B=1;const C=1;/**/const#a D=1;const E=1;// const F=1; }', ['elements' => ['const']], ]; yield [ '<?php class A { private const B=1; protected const C=2; public const D=4; public $a; function A(){} }', '<?php class A { private const B=1; protected const C=2; const D=4; public $a; function A(){} }', ['elements' => ['const']], ]; yield [ '<?php class foo { public const A = 1, B =2, C =3; public const TWO = ONE * 2; public const THREE = ONE + self::TWO; public const SENTENCE = "The value of THREE is ".self::THREE; } ', '<?php class foo { const A = 1, B =2, C =3; const TWO = ONE * 2; const THREE = ONE + self::TWO; const SENTENCE = "The value of THREE is ".self::THREE; } ', ['elements' => ['const']], ]; yield 'comment' => [ '<?php class A {# We will have a function below # It will be static # and awesome public static function# <- this is the function AB# <- this is the name (# )# {# }# } ', '<?php class A {# We will have a function below static# It will be static # and awesome function# <- this is the function AB# <- this is the name (# )# {# }# } ', ]; yield 'anonymous class' => [ '<?php $a = new class() { public function a() { } }; class C { public function A() { $a = new class() {public function a() {}}; } } ', '<?php $a = new class() { function a() { } }; class C { function A() { $a = new class() {function a() {}}; } } ', ]; yield 'removing newlines between keywords' => [ '<?php class Foo { public $bar; final public static function bar() {} final public static function baz() {} }', '<?php class Foo { var $bar; final public static function bar() {} static final function baz() {} }', ]; yield 'keeping comment' => [ '<?php class Foo { /* constant */ private const BAR = 3; /* variable */ private $bar; /* function */ private function bar() {} }', '<?php class Foo { private /* constant */ const BAR = 3; private /* variable */ $bar; private /* function */ function bar() {} }', ['elements' => ['property', 'method', 'const']], ]; yield 'fixing with all keywords' => [ '<?php abstract class Foo { abstract protected static function fooA(); abstract protected static function fooB(); abstract protected static function fooC(); abstract protected static function fooD(); abstract protected static function fooE(); abstract protected static function fooF(); abstract public static function fooG(); abstract public static function fooH(); } ', '<?php abstract class Foo { abstract protected static function fooA(); abstract static protected function fooB(); protected abstract static function fooC(); protected static abstract function fooD(); static abstract protected function fooE(); static protected abstract function fooF(); abstract static function fooG(); static abstract function fooH(); } ', ]; yield [ '<?php class Foo { private int $foo; }', ]; yield [ '<?php class Foo { protected ?string $foo; }', ]; yield [ '<?php class Foo { public ? string $foo; }', ]; yield [ '<?php class Foo { public ? string $foo; }', '<?php class Foo { var ? string $foo; }', ]; yield [ '<?php class Foo { public static Foo\Bar $foo; }', '<?php class Foo { static public Foo\Bar $foo; }', ]; yield [ '<?php class Foo { public array $foo; }', ]; yield [ '<?php class Foo { public ?array $foo; }', ]; yield [ '<?php class Foo { public static ?array $foo; }', '<?php class Foo { static public ?array $foo; }', ]; } /** * @requires PHP 8.0 * * @dataProvider provideFix80Cases */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php class Foo { private int|float|null $foo; }', ]; yield [ '<?php class Foo { private int | /* or empty */ null $foo; }', ]; yield [ '<?php class Foo { private array|null $foo; }', ]; yield [ '<?php class Foo { private null|array $foo; }', ]; yield [ '<?php class Foo { public static null|array $foo; }', '<?php class Foo { static null|array $foo; }', ]; yield 'promoted properties' => [ <<<'PHP' <?php class Foo { public function __construct( public string $a, protected string $b, private string $c, private array &$withReference, ) {} } PHP, ]; yield 'promoted property with visibility and reference, but without type' => [ <<<'PHP' <?php class Foo { public function __construct( private &$bar, ) {} } PHP, ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php class Foo { public Foo1&Bar $foo; }', ]; yield [ '<?php class Foo { public readonly string $prop2a; } ', '<?php class Foo { readonly public string $prop2a; } ', ]; yield [ '<?php class Foo { public readonly string $prop1; public readonly string $prop2; } ', '<?php class Foo { readonly string $prop1; public readonly string $prop2; } ', ]; yield [ '<?php class Foo { final public const B = "2"; } ', '<?php class Foo { public final const B = "2"; } ', ]; yield [ '<?php class Foo { final public const B = "2"; } ', '<?php class Foo { final const B = "2"; } ', ]; yield [ '<?php enum Foo { case CAT; public function test(): self { return $this; } } var_dump(Foo::CAT->test());', '<?php enum Foo { case CAT; function test(): self { return $this; } } var_dump(Foo::CAT->test());', ]; yield 'promoted property without visibility' => [ '<?php class Foo { public function __construct(public readonly string $bar) { } }', '<?php class Foo { public function __construct(readonly string $bar) { } }', ]; yield 'promoted property without visibility with reference' => [ '<?php class Foo { public function __construct(public readonly string &$bar) { } }', '<?php class Foo { public function __construct(readonly string &$bar) { } }', ]; yield 'promoted readonly properties' => [ <<<'PHP' <?php class Foo { public function __construct( public readonly bool $one, public readonly bool $two, public readonly string $three ) {} } PHP, <<<'PHP' <?php class Foo { public function __construct( readonly public bool $one, readonly public bool $two, readonly public string $three ) {} } PHP, ]; } /** * @requires PHP 8.2 * * @dataProvider provideFix82Cases */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix82Cases(): iterable { yield [ '<?php trait Foo { public const Bar = 1; }', '<?php trait Foo { const Bar = 1; }', ]; yield [ '<?php class Foo { public (A&B)|C|D $x; protected A|(B&C)|D $y; private A|B|(C&D) $z; }', ]; yield 'readonly class' => [ <<<'PHP' <?php readonly class Foo { public function __construct() {} public function bar(int $x): void {} } PHP, ]; } /** * @dataProvider provideFix84Cases * * @requires PHP >= 8.4 */ public function testFix84(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix84Cases(): iterable { yield 'fixing multiple keyword modifiers' => [ <<<'PHP' <?php abstract class ClassName { abstract protected string $bar { get => "a"; set; } final protected readonly string $foo; final protected int $beep; final public private(set) int $counter1; public protected(set) int $counter2; public public(set) int $counter3; final public static function bar() {} abstract protected function zim(); } PHP, <<<'PHP' <?php abstract class ClassName { protected abstract string $bar { get => "a"; set; } readonly final protected string $foo; protected final int $beep; final public private(set) int $counter1; public protected(set) int $counter2; public public(set) int $counter3; static public final function bar() {} protected abstract function zim(); } PHP, ]; yield 'property hooks' => [ <<<'PHP' <?php class Foo { protected int $bar { set => $this->bar = $value; } } PHP, ]; yield 'asymmetric visibility with only set-visibility' => [ <<<'PHP' <?php class Foo { public public(set) Bar $a; public protected(set) Bar $b; public private(set) Baz $c; } PHP, <<<'PHP' <?php class Foo { public(set) Bar $a; protected(set) Bar $b; private(set) Baz $c; } PHP, ]; yield 'asymmetric visibility with set-visibility before general visibility' => [ <<<'PHP' <?php class Foo { public public(set) Bar $a; public protected(set) Bar $b; public private(set) Baz $c; } PHP, <<<'PHP' <?php class Foo { public(set) public Bar $a; protected(set) public Bar $b; private(set) public Baz $c; } PHP, ]; yield 'asymmetric visibility with readonly' => [ <<<'PHP' <?php class Foo { protected private(set) readonly Bar $a; public protected(set) readonly Bar $b; public private(set) readonly Baz $c; } PHP, <<<'PHP' <?php class Foo { readonly private(set) protected Bar $a; protected(set) readonly public Bar $b; private(set) public readonly Baz $c; } PHP, ]; yield 'promoted property with visibility, set-visibility and reference' => [ <<<'PHP' <?php class Foo { public function __construct( protected private(set) int &$bar, ) {} } PHP, <<<'PHP' <?php class Foo { public function __construct( private(set) protected int &$bar, ) {} } PHP, ]; } /** * @dataProvider provideFix85Cases * * @requires PHP >= 8.5 */ public function testFix85(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix85Cases(): iterable { yield 'promoted property without visibility' => [ '<?php class Foo { public function __construct(final public string $bar) { } }', '<?php class Foo { public function __construct(final string $bar) { } }', ]; yield 'promoted final properties' => [ <<<'PHP' <?php class Foo { public function __construct( final public bool $one, final public bool $two, final public string $three ) {} } PHP, <<<'PHP' <?php class Foo { public function __construct( public final bool $one, public final bool $two, public final string $three ) {} } PHP, ]; } /** * @param array<string, mixed> $config * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $config, string $expectedMessage): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($expectedMessage); $this->fixer->configure($config); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'invalid type' => [ ['elements' => [null]], '/^\[modifier_keywords\] Invalid configuration: The option "elements" .*\.$/', ]; yield 'invalid value' => [ ['elements' => ['_unknown_']], '/^\[modifier_keywords\] Invalid configuration: The option "elements" .*\.$/', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/NoBlankLinesAfterClassOpeningFixerTest.php
tests/Fixer/ClassNotation/NoBlankLinesAfterClassOpeningFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer> * * @author Ceeram <ceeram@cakephp.org> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoBlankLinesAfterClassOpeningFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php class Good { public function firstMethod() { //code here } }', '<?php class Good { public function firstMethod() { //code here } }', ]; yield [ '<?php class Good { /** * Also no blank line before DocBlock */ public function firstMethod() { //code here } }', '<?php class Good { /** * Also no blank line before DocBlock */ public function firstMethod() { //code here } }', ]; yield [ '<?php interface Good { /** * Also no blank line before DocBlock */ public function firstMethod(); }', '<?php interface Good { /** * Also no blank line before DocBlock */ public function firstMethod(); }', ]; // check if some fancy whitespaces aren't modified yield [ '<?php class Good {public function firstMethod() { //code here } }', ]; // check if line with spaces is removed when next token is indented yield [ '<?php class Foo { function bar() {} } ', '<?php class Foo { '.' function bar() {} } ', ]; // check if line with spaces is removed when next token is not indented yield [ '<?php class Foo { function bar() {} } ', '<?php class Foo { '.' function bar() {} } ', ]; yield [ '<?php trait Good { /** * Also no blank line before DocBlock */ public function firstMethod() {} }', '<?php trait Good { /** * Also no blank line before DocBlock */ public function firstMethod() {} }', ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\nclass Foo\n{\r\n public function bar() {}\n}", "<?php\nclass Foo\n{\n\n public function bar() {}\n}", ]; yield [ "<?php\nclass Foo\n{\r\n public function bar() {}\n}", "<?php\nclass Foo\n{\r\n\r\n public function bar() {}\n}", ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php enum Good { public function firstMethod() {} }', '<?php enum Good { public function firstMethod() {} }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/FinalPublicMethodForAbstractClassFixerTest.php
tests/Fixer/ClassNotation/FinalPublicMethodForAbstractClassFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\FinalPublicMethodForAbstractClassFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\FinalPublicMethodForAbstractClassFixer> * * @author Filippo Tessarotto <zoeslam@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class FinalPublicMethodForAbstractClassFixerTest extends AbstractFixerTestCase { /** * @param string $expected PHP source code * @param null|string $input PHP source code * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { $original = $fixed = self::getClassElementStubs(); $fixed = str_replace('public function f1', 'final public function f1', $fixed); $fixed = str_replace('public static function f4', 'final public static function f4', $fixed); $fixed = str_replace('static public function f7', 'final static public function f7', $fixed); yield 'regular-class' => ["<?php class MyClass { {$original} }"]; yield 'final-class' => ["<?php final class MyClass { {$original} }"]; yield 'trait' => ["<?php trait MyClass { {$original} }"]; yield 'interface' => [ '<?php interface MyClass { public function f1(); public static function f4(); static public function f7(); }', ]; yield 'magic-methods' => [ '<?php abstract class MyClass { public function __construct() {} public function __destruct() {} public function __call($a, $b) {} public static function __callStatic($a, $b) {} public function __get($a) {} public function __set($a, $b) {} public function __isset($a) {} public function __unset($a) {} public function __sleep() {} public function __wakeup() {} public function __toString() {} public function __invoke() {} public function __clone() {} public function __debugInfo() {} }', ]; yield 'magic-methods-casing' => [ '<?php abstract class MyClass { public function __Construct() {} public function __SET($a, $b) {} public function __ToString() {} public function __DeBuGiNfO() {} }', ]; yield 'non magic-methods' => [ '<?php abstract class MyClass { final public function __foo() {} final public static function __bar($a, $b) {} }', '<?php abstract class MyClass { public function __foo() {} public static function __bar($a, $b) {} }', ]; yield 'abstract-class' => [ "<?php abstract class MyClass { {$fixed} }", "<?php abstract class MyClass { {$original} }", ]; yield 'abstract-class-with-abstract-public-methods' => [ '<?php abstract class MyClass { abstract public function foo(); abstract public static function bar(); }', ]; yield 'anonymous-class' => [ \sprintf( '<?php abstract class MyClass { private function test() { $a = new class { %s }; } }', self::getClassElementStubs(), ), ]; yield 'constant visibility' => [ '<?php abstract class MyClass { public const A = 1; protected const B = 2; private const C = 3; }', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix82Cases(): iterable { yield 'abstract keyword after readonly/public keywords' => [ '<?php readonly abstract class Foo { public abstract function bar(); }', ]; yield 'abstract keyword before readonly/public keywords' => [ '<?php abstract readonly class Foo { abstract public function bar(); }', ]; yield 'abstract readonly class' => [ '<?php abstract readonly class Foo { final public function bar() {} }', '<?php abstract readonly class Foo { public function bar() {} }', ]; yield 'readonly abstract class' => [ '<?php readonly abstract class Foo { final public function bar() {} }', '<?php readonly abstract class Foo { public function bar() {} }', ]; } private static function getClassElementStubs(): string { return ' public $a1; protected $a2; private $a3; public static $a4; protected static $a5; private static $a6; public function f1(){} protected function f2(){} private function f3(){} public static function f4(){} protected static function f5(){} private static function f6(){} static public function f7(){} static protected function f8(){} static private function f9(){} '; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/SelfAccessorFixerTest.php
tests/Fixer/ClassNotation/SelfAccessorFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer> * * @author Gregor Harlan <gharlan@web.de> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SelfAccessorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php class Foo { const BAR = self::BAZ; }', '<?php class Foo { const BAR = Foo::BAZ; }', ]; yield [ '<?php class Foo { private $bar = self::BAZ; }', '<?php class Foo { private $bar = fOO::BAZ; }', // case insensitive ]; yield [ '<?php class Foo { function bar($a = self::BAR) {} }', '<?php class Foo { function bar($a = Foo::BAR) {} }', ]; yield [ '<?php class Foo { function bar() { self::baz(); } }', '<?php class Foo { function bar() { Foo::baz(); } }', ]; yield [ '<?php class Foo { function bar() { self::class; } }', '<?php class Foo { function bar() { Foo::class; } }', ]; yield [ '<?php class Foo { function bar() { $x instanceof self; } }', '<?php class Foo { function bar() { $x instanceof Foo; } }', ]; yield [ '<?php class Foo { function bar() { new self(); } }', '<?php class Foo { function bar() { new Foo(); } }', ]; yield [ '<?php interface Foo { const BAR = self::BAZ; function bar($a = self::BAR); }', '<?php interface Foo { const BAR = Foo::BAZ; function bar($a = Foo::BAR); }', ]; yield [ '<?php class Foo { const Foo = 1; }', ]; yield [ '<?php class Foo { function foo() { } }', ]; yield [ '<?php class Foo { function bar() { new \Baz\Foo(); } }', ]; yield [ '<?php class Foo { function bar() { new Foo\Baz(); } }', ]; yield [ '<?php class Foo { function bar() { Baz\Foo::class; } }', ]; yield [ // In trait "self" will reference the class it's used in, not the actual trait, so we can't replace "Foo" with "self" here '<?php trait Foo { function bar() { Foo::bar(); } } class Bar { use Foo; }', ]; yield [ '<?php class Foo { public function bar(self $foo, self $bar) { return new self(); } }', '<?php class Foo { public function bar(Foo $foo, Foo $bar) { return new Foo(); } }', ]; yield [ '<?php interface Foo { public function bar(self $foo, self $bar); }', '<?php interface Foo { public function bar(Foo $foo, Foo $bar); }', ]; yield [ '<?php interface Foo { public function bar(self $foo); }', '<?php interface Foo { public function bar(\Foo $foo); }', ]; yield [ '<?php namespace Foo; interface Bar { public function baz(\Bar $bar); }', ]; yield [ '<?php namespace Foo; interface Bar { public function baz(self $bar); }', '<?php namespace Foo; interface Bar { public function baz(Bar $bar); }', ]; yield [ '<?php namespace Foo; interface Bar { public function baz(self $bar); }', '<?php namespace Foo; interface Bar { public function baz(\Foo\Bar $bar); }', ]; yield [ '<?php namespace Foo; interface Bar { public function baz(Foo\Bar $bar); }', ]; yield [ '<?php namespace Foo; interface Bar { public function baz(Bar\Foo $bar); }', ]; yield [ '<?php namespace Foo\Foo2; interface Bar { public function baz00(Foo2\Bar $bar); public function baz10(\Foo2\Bar $bar); public function baz20(Foo\Foo2\Bar $bar); public function baz21(self $bar); public function baz30(Test\Foo\Foo2\Bar $bar); public function baz31(\Test\Foo\Foo2\Bar $bar); }', '<?php namespace Foo\Foo2; interface Bar { public function baz00(Foo2\Bar $bar); public function baz10(\Foo2\Bar $bar); public function baz20(Foo\Foo2\Bar $bar); public function baz21(\Foo\Foo2\Bar $bar); public function baz30(Test\Foo\Foo2\Bar $bar); public function baz31(\Test\Foo\Foo2\Bar $bar); }', ]; yield [ '<?php class Foo { function bar() { new class() { function baz() { new Foo(); } }; } }', ]; yield [ '<?php class Foo { protected $foo; function bar() { return $this->foo::find(2); } }', ]; yield [ '<?php class Foo { public function bar(self $foo, self $bar): self { return new self(); } }', '<?php class Foo { public function bar(Foo $foo, Foo $bar): Foo { return new Foo(); } }', ]; yield [ '<?php interface Foo { public function bar(self $foo, self $bar): self; }', '<?php interface Foo { public function bar(Foo $foo, Foo $bar): Foo; }', ]; yield [ '<?php class Foo { public function bar(?self $foo, ?self $bar): ?self { return new self(); } }', '<?php class Foo { public function bar(?Foo $foo, ?Foo $bar): ?Foo { return new Foo(); } }', ]; yield [ "<?php interface Foo{ public function bar()\t/**/:?/**/self; }", "<?php interface Foo{ public function bar()\t/**/:?/**/Foo; }", ]; yield 'do not replace in lambda' => [ '<?php final class A { public static function Z(): void { (function () { var_dump(self::class, A::class); })->bindTo(null, B::class)(); } }', ]; yield 'do not replace in arrow function' => [ '<?php final class A { public function Z($b): void { $a = fn($b) => self::class. A::class . $b; echo $a; } }', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php interface Foo { public function bar(self $foo, self $bar,): self; }', '<?php interface Foo { public function bar(Foo $foo, Foo $bar,): Foo; }', ]; yield [ '<?php class Foo { function bar() { $x instanceof (Foo()); } }', ]; yield [ '<?php class Foo { protected $foo; function bar() { return $this?->foo::find(2); } }', ]; yield [ '<?php class Foo { public function f(self|Bar|Baz $b) {} }', '<?php class Foo { public function f(Foo|Bar|Baz $b) {} }', ]; yield [ '<?php class Foo { public function f(Bar|self|Baz $b) {} }', '<?php class Foo { public function f(Bar|Foo|Baz $b) {} }', ]; yield [ '<?php class Foo { public function f(Bar|Baz|self $b) {} }', '<?php class Foo { public function f(Bar|Baz|Foo $b) {} }', ]; yield [ '<?php class Foo { public function f(Bar|Foo\C|Baz $b) {} }', ]; yield [ '<?php class Foo { public function f(Bar|C\Foo|Baz $b) {} }', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix82Cases(): iterable { yield [ '<?php class Foo { public function f(self|(Bar&Baz)|Qux $b) {} }', '<?php class Foo { public function f(Foo|(Bar&Baz)|Qux $b) {} }', ]; yield [ '<?php class Foo { public function f(Bar|(Baz&Qux)|self $b) {} }', '<?php class Foo { public function f(Bar|(Baz&Qux)|Foo $b) {} }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php
tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer * @phpstan-import-type _ClassyDefinitionInfo from \PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ClassDefinitionFixerTest extends AbstractFixerTestCase { /** * @param array<string, mixed> $config * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $config, string $exceptionExpression): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($exceptionExpression); $this->fixer->configure($config); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'invalid configuration key' => [ ['a' => false], '/^\[class_definition\] Invalid configuration: The option "a" does not exist\. Defined options are: "inline_constructor_arguments", "multi_line_extends_each_single_line", "single_item_single_line", "single_line", "space_before_parenthesis"\.$/', ]; yield 'invalid configuration value' => [ ['single_line' => 'z'], '/^\[class_definition\] Invalid configuration: The option "single_line" with value "z" is expected to be of type "bool", but is of type "string"\.$/', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php $a = new class(0) extends SomeClass implements SomeInterface, D {};', "<?php \$a = new class(0) extends\nSomeClass\timplements SomeInterface, D {};", ]; yield [ '<?php $a = new class(1) extends SomeClass implements SomeInterface, D {};', "<?php \$a = new class(1) extends\nSomeClass\timplements SomeInterface, D {};", ['single_line' => true], ]; yield [ "<?php \$a = new class('1a') implements\nA\n{};", "<?php \$a = new class('1a') implements\nA{};", ]; yield [ "<?php \$a = new class('1a') implements A {};", "<?php \$a = new class('1a') implements\nA{};", ['single_item_single_line' => true], ]; yield [ '<?php $a = new class {};', '<?php $a = new class{};', ]; yield [ '<?php $a = new class {};', "<?php \$a = new class\n{};", ]; yield [ '<?php $a = new class() {};', "<?php \$a = new\n class ( ){};", ]; yield [ '<?php $a = new class( ) {};', "<?php \$a = new\n class ( ){};", ['inline_constructor_arguments' => false], ]; yield [ '<?php $a = new class implements Foo {};', "<?php \$a = new\n class implements Foo {};", ['inline_constructor_arguments' => false], ]; yield [ '<?php $a = new class( $this->foo() , bar ( $a) ) {};', "<?php \$a = new\n class ( \$this->foo() , bar ( \$a) ){};", ['inline_constructor_arguments' => false], ]; yield [ '<?php $a = new class(10, 1, /**/ 2) {};', '<?php $a = new class( 10, 1,/**/2 ){};', ]; yield [ '<?php $a = new class( 10, 1,/**/2 ) {};', '<?php $a = new class( 10, 1,/**/2 ){};', ['inline_constructor_arguments' => false], ]; yield [ '<?php $a = new class(2) {};', '<?php $a = new class(2){};', ]; yield [ '<?php $a = new class($this->prop) {};', '<?php $a = new class( $this->prop ){};', ]; yield [ '<?php $a = new class( $this->prop ) {};', '<?php $a = new class( $this->prop ){};', ['inline_constructor_arguments' => false], ]; yield [ "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};", "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};", ['inline_constructor_arguments' => false], ]; yield [ "<?php \$a = new class(\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, B {};", "<?php \$a = new class (\n\t\$a,\n\t\$b,\n\t\$c,\n\t\$d) implements A, \t B{};", ['inline_constructor_arguments' => false], ]; yield [ '<?php $a = new class($this->prop, $v[3], 4) {};', '<?php $a = new class( $this->prop,$v[3], 4) {};', ]; yield 'PSR-12 Extends/Implements Parenthesis on the next line.' => [ '<?php $instance = new class extends \Foo implements \ArrayAccess, \Countable, \Serializable {};', '<?php $instance = new class extends \Foo implements \ArrayAccess,\Countable,\Serializable{};', ]; yield 'PSR-12 Implements Parenthesis on the next line.' => [ '<?php $instance = new class implements \ArrayAccess, \Countable, \Serializable {};', '<?php $instance = new class implements \ArrayAccess,\Countable,\Serializable{};', ]; yield 'PSR-12 Extends Parenthesis on the next line.' => [ '<?php $instance = new class extends ArrayAccess {};', '<?php $instance = new class extends ArrayAccess {};', ]; yield [ "<?php \$a = new # class # ( # '1a', # 1 # ) # implements# A, # B, C # {# # }# ;", "<?php \$a = new# class# (# '1a',# 1 # )# implements# A, # B,C# {# # }# ;", ]; yield [ "<?php \$a = new # class # ( # '1a', # 1 # ) # implements # A # {# # }# ;", "<?php \$a = new# class# (# '1a',# 1 # )# implements# A# {# # }# ;", ['single_item_single_line' => true], ]; yield [ '<?php $a = new class() # {};', '<?php $a = new class()# {};', ]; yield 'space_before_parenthesis 1' => [ '<?php $z = new class () {};', '<?php $z = new class() {};', ['space_before_parenthesis' => true], ]; yield 'space_before_parenthesis 2' => [ '<?php $z = new class () {};', '<?php $z = new class () {};', ['space_before_parenthesis' => true], ]; yield 'space_before_parenthesis and inline_constructor_arguments' => [ '<?php $z = new class ( static::foo($this->bar()) ,baz() ) {};', '<?php $z = new class ( static::foo($this->bar()) ,baz() ) {};', ['space_before_parenthesis' => true, 'inline_constructor_arguments' => false], ]; yield 'single attribute on separate line' => [ <<<'EOF' <?php $a = new #[FOO] class() {}; EOF, ]; yield 'multiple attributes on separate line' => [ <<<'EOF' <?php $a = new #[FOO] #[\Ns\Bar] class() {}; EOF, ]; yield 'single line phpdoc on separate line' => [ <<<'EOF' <?php $a = new /** @property string $x */ class() {}; EOF, ]; yield 'multi line phpdoc on separate line' => [ <<<'EOF' <?php $a = new /** @property string $x */ class() {}; EOF, ]; yield 'phpdoc and single attribute on separate line' => [ <<<'EOF' <?php $a = new /** @property string $x */ #[FOO] class() {}; EOF, ]; yield 'phpdoc and multiple attributes on separate line' => [ <<<'EOF' <?php $a = new /** @property string $x */ #[FOO] #[\Ns\Bar] class() {}; EOF, ]; yield from self::provideClassyCases('class'); yield from self::provideClassyExtendingCases('class'); yield from self::provideClassyImplementsCases(); yield [ "<?php class configA implements B, C\n{}", "<?php class configA implements\nB, C{}", ['single_line' => true], ]; yield [ "<?php class configA1 extends B\n{}", "<?php class configA1\n extends\nB{}", ['single_line' => true], ]; yield [ "<?php class configA1a extends B\n{}", "<?php class configA1a\n extends\nB{}", ['single_line' => false, 'single_item_single_line' => true], ]; yield [ "<?php class configA2 extends D implements B, C\n{}", "<?php class configA2 extends D implements\nB,\nC{}", ['single_line' => true], ]; yield [ "<?php class configA3 extends D implements B, C\n{}", "<?php class configA3\n extends\nD\n\t implements\nB,\nC{}", ['single_line' => true], ]; yield [ "<?php class configA4 extends D implements B, #\nC\n{}", "<?php class configA4\n extends\nD\n\t implements\nB,#\nC{}", ['single_line' => true], ]; yield [ "<?php class configA5 implements A\n{}", "<?php class configA5 implements\nA{}", ['single_line' => false, 'single_item_single_line' => true], ]; yield [ "<?php interface TestWithMultiExtendsMultiLine extends\n A,\nAb,\n C,\n D\n{}", "<?php interface TestWithMultiExtendsMultiLine extends A,\nAb,C,D\n{}", [ 'single_line' => false, 'single_item_single_line' => false, 'multi_line_extends_each_single_line' => true, ], ]; yield from self::provideClassyCases('interface'); yield from self::provideClassyExtendingCases('interface'); yield [ '<?php interface Test extends /*a*/ /*b*/TestInterface1 , \A\B\C , /* test */ TestInterface2 , // test '.' // Note: PSR does not have a rule for multiple extends TestInterface3, /**/ TestInterface4 , TestInterface5 , '.' /**/TestInterface65 {} ', '<?php interface Test extends /*a*/ /*b*/TestInterface1 , \A\B\C , /* test */ TestInterface2 , // test '.' // Note: PSR does not have a rule for multiple extends TestInterface3, /**/ TestInterface4 , TestInterface5 , '.' /**/TestInterface65 {} ', ]; yield from self::provideClassyCases('trait'); yield [ '<?php $a = new class implements \RFb, \Fcc, \GFddZz { };', '<?php $a = new class implements \RFb, \Fcc, \GFddZz { };', ]; yield [ '<?php $a = new class implements \RFb, \Fcc, \GFddZz { }?>', '<?php $a = new class implements \RFb, \Fcc, \GFddZz { }?>', ]; yield [ '<?php new class(1, 2, 3, ) {};', '<?php new class(1, 2, 3,) {};', ]; yield [ '<?php new class(1, 2, 3, ) {};', '<?php new class( 1, 2, 3, ) {};', ]; } /** * @param _ClassyDefinitionInfo $expected * * @dataProvider provideClassyDefinitionInfoCases */ public function testClassyDefinitionInfo(string $source, array $expected): void { Tokens::clearCache(); $tokens = Tokens::fromCode($source); $result = \Closure::bind(static fn (ClassDefinitionFixer $fixer): array => $fixer->getClassyDefinitionInfo($tokens, $expected['classy']), null, ClassDefinitionFixer::class)($this->fixer); ksort($expected); ksort($result); self::assertSame($expected, $result); } /** * @return iterable<int, array{string, _ClassyDefinitionInfo}> */ public static function provideClassyDefinitionInfoCases(): iterable { yield [ '<?php class A{}', [ 'start' => 1, 'classy' => 1, 'open' => 4, 'extends' => false, 'implements' => false, 'anonymousClass' => false, 'final' => false, 'abstract' => false, 'readonly' => false, ], ]; yield [ '<?php final class A{}', [ 'start' => 1, 'classy' => 3, 'open' => 6, 'extends' => false, 'implements' => false, 'anonymousClass' => false, 'final' => 1, 'abstract' => false, 'readonly' => false, ], ]; yield [ '<?php abstract /**/ class A{}', [ 'start' => 1, 'classy' => 5, 'open' => 8, 'extends' => false, 'implements' => false, 'anonymousClass' => false, 'final' => false, 'abstract' => 1, 'readonly' => false, ], ]; yield [ '<?php class A extends B {}', [ 'start' => 1, 'classy' => 1, 'open' => 9, 'extends' => [ 'start' => 5, 'count' => 1, 'multiLine' => false, ], 'implements' => false, 'anonymousClass' => false, 'final' => false, 'abstract' => false, 'readonly' => false, ], ]; yield [ '<?php interface A extends B,C,D {}', [ 'start' => 1, 'classy' => 1, 'open' => 13, 'extends' => [ 'start' => 5, 'count' => 3, 'multiLine' => false, ], 'implements' => false, 'anonymousClass' => false, 'final' => false, 'abstract' => false, 'readonly' => false, ], ]; } /** * @param _ClassyDefinitionInfo $expected * * @dataProvider provideClassyInheritanceInfoCases */ public function testClassyInheritanceInfo(string $source, array $expected): void { $this->doTestClassyInheritanceInfo($source, $expected); } /** * @return iterable<array{0: string, 1?: _AutogeneratedInputConfiguration}> */ public static function provideClassyInheritanceInfoCases(): iterable { yield '1' => [ '<?php class X11 implements Z , T,R { }', ['start' => 5, 'count' => 3, 'multiLine' => false], ]; yield '2' => [ '<?php class X10 implements Z , T,R // { }', ['start' => 5, 'count' => 3, 'multiLine' => false], ]; yield '3' => [ '<?php class A implements B {}', ['start' => 5, 'count' => 1, 'multiLine' => false], ]; yield '4' => [ "<?php class A implements B,\n I{}", ['start' => 5, 'count' => 2, 'multiLine' => true], ]; yield '5' => [ "<?php class A implements Z\\C\\B,C,D {\n\n\n}", ['start' => 5, 'count' => 3, 'multiLine' => false], ]; yield [ '<?php namespace A { interface X {} } namespace { class B{} class A extends // B implements /* */ \A\C, Z{ public function test() { echo 1; } } $a = new A(); $a->test(); }', ['start' => 36, 'count' => 2, 'multiLine' => false], ]; yield [ "<?php \$a = new class(3) extends\nSomeClass\timplements SomeInterface, D {};", ['start' => 12, 'count' => 1, 'multiLine' => true], ]; yield [ "<?php \$a = new class(4) extends\nSomeClass\timplements SomeInterface, D\n\n{};", ['start' => 16, 'count' => 2, 'multiLine' => false], ]; yield [ "<?php \$a = new class(5) extends SomeClass\nimplements SomeInterface, D {};", ['start' => 12, 'count' => 1, 'multiLine' => true], ]; } /** * @param _ClassyDefinitionInfo $expected * * @dataProvider provideClassyInheritanceInfoPre80Cases * * @requires PHP <8.0 */ public function testClassyInheritanceInfoPre80(string $source, array $expected): void { $this->doTestClassyInheritanceInfo($source, $expected); } /** * @return iterable<int, array{0: string, 1?: _AutogeneratedInputConfiguration}> */ public static function provideClassyInheritanceInfoPre80Cases(): iterable { yield [ '<?php namespace A { interface X {} } namespace { class B{} class A extends // B implements /* */ \A \C, Z{ public function test() { echo 1; } } $a = new A(); $a->test(); }', ['start' => 36, 'count' => 2, 'multiLine' => true], ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\nclass Aaa implements\r\n\tBbb,\r\n\tCcc,\r\n\tDdd\r\n\t{\r\n\t}", "<?php\r\nclass Aaa implements\r\n\tBbb, Ccc,\r\n\tDdd\r\n\t{\r\n\t}", ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix80Cases(): iterable { yield 'anonymous class, single attribute' => [ '<?php $a = new #[FOO] class(2) {};', '<?php $a = new #[FOO] class(2){};', ]; yield 'anonymous class, multiple attributes' => [ '<?php $a = new #[FOO] #[BAR] class {};', '<?php $a = new #[FOO] #[BAR] class {};', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ "<?php enum SomeEnum implements SomeInterface, D\n{};", "<?php enum SomeEnum \timplements SomeInterface, D {};", ]; yield [ "<?php enum SomeEnum : int\n{}", '<?php enum SomeEnum : int {}', ]; yield [ "<?php enum SomeEnum\n{}", "<?php enum\tSomeEnum{}", ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix82Cases(): iterable { yield 'final readonly works' => [ '<?php final readonly class a {}', '<?php final readonly class a {}', ]; yield 'final - readonly modifiers get sorted' => [ '<?php final readonly class a {}', '<?php readonly final class a {}', ]; yield 'abstract - readonly modifiers get sorted' => [ '<?php abstract readonly class a {}', '<?php readonly abstract class a {}', ]; } /** * @dataProvider provideFix83Cases * * @requires PHP 8.3 */ public function testFix83(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix83Cases(): iterable { yield 'anonymous class, readonly, missing spacing' => [ '<?php $a = new readonly class {};', '<?php $a = new readonly class{};', ]; yield 'anonymous class, readonly, to much spacing' => [ '<?php $a = new readonly class {};', '<?php $a = new readonly class {};', ]; yield 'anonymous class, single attribute' => [ '<?php $a = new #[BAR] readonly class {};', '<?php $a = new #[BAR] readonly class{};', ]; yield 'anonymous class, multiple attributes' => [ '<?php $a = new #[FOO] #[BAR] readonly class {};', '<?php $a = new #[FOO] #[BAR] readonly class {};', ]; } /** * @param _ClassyDefinitionInfo $expected */ private function doTestClassyInheritanceInfo(string $source, array $expected): void { Tokens::clearCache(); $tokens = Tokens::fromCode($source); self::assertTrue($tokens[$expected['start']]->isGivenKind([\T_IMPLEMENTS, \T_EXTENDS]), \sprintf('Token must be "implements" or "extends", got "%s".', $tokens[$expected['start']]->getContent())); $result = \Closure::bind(static fn (ClassDefinitionFixer $fixer): array => $fixer->getClassyInheritanceInfo($tokens, $expected['start']), null, ClassDefinitionFixer::class)($this->fixer); self::assertSame($expected, $result); } /** * @return iterable<int, array{string, string}> */ private static function provideClassyCases(string $classy): iterable { return [ [ \sprintf("<?php %s A\n{}", $classy), \sprintf('<?php %s A {}', $classy), ], [ \sprintf("<?php %s B\n{}", $classy), \sprintf('<?php %s B{}', $classy), ], [ \sprintf("<?php %s C\n{}", $classy), \sprintf("<?php %s\n\tC{}", $classy), ], [ \sprintf("<?php %s D //\n{}", $classy), \sprintf("<?php %s D//\n{}", $classy), ], [ \sprintf("<?php %s /**/ E //\n{}", $classy), \sprintf("<?php %s/**/E//\n{}", $classy), ], [ \sprintf( "<?php %s A {} %s /**/ B // /**/\n{}", $classy, $classy, ), \sprintf( '<?php %s A {} %s/**/B // /**/ {}', $classy, $classy, ), ], [ \sprintf( '<?php namespace { %s IndentedNameSpacedClass { } }', $classy, ), \sprintf( '<?php namespace { %s IndentedNameSpacedClass { } }', $classy, ), ], ]; } /** * @return iterable<int, array{string, string}> */ private static function provideClassyExtendingCases(string $classy): iterable { return [ [ \sprintf("<?php %s AE0 extends B\n{}", $classy), \sprintf('<?php %s AE0 extends B {}', $classy), ], [ \sprintf("<?php %s /**/ AE1 /**/ extends /**/ B /**/\n{}", $classy), \sprintf('<?php %s/**/AE1/**/extends/**/B/**/{}', $classy), ], [ \sprintf("<?php %s /*%s*/ AE2 extends\nB\n{}", $classy, $classy), \sprintf("<?php %s /*%s*/ AE2 extends\nB{}", $classy, $classy), ], [ \sprintf('<?php %s Test124 extends \Exception {}', $classy), \sprintf('<?php %s Test124 extends \Exception {}', $classy), ], ]; } /** * @return iterable<array{string, string}> */ private static function provideClassyImplementsCases(): iterable { return [ [ '<?php class LotOfImplements implements A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q {}', '<?php class LotOfImplements implements A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q{}', ], [ "<?php class E implements B\n{}", "<?php class E \nimplements B \t{}", ], [ "<?php abstract class F extends B implements C\n{}", '<?php abstract class F extends B implements C {}', ], 'multiline abstract extends implements with comments' => [ "<?php abstract class G extends // B /* */ implements C\n{}", '<?php abstract class G extends // B/* */implements C{}', ], 'final extends implement' => [ "<?php final class G extends // B /* */ implements C\n{}", '<?php final class G extends // B/* */implements C{}', ], 'final' => [ '<?php final class G // /* */ {}', '<?php final class G // /* */{}', ], [ '<?php class Aaa IMPLEMENTS \RFb, \Fcc, \GFddZz { }', '<?php class Aaa IMPLEMENTS \RFb, \Fcc, \GFddZz { }', ], [ '<?php class // X // extends // Y // implements // Z, // U // {} //', '<?php class // X // extends // Y // implements // Z , // U // {} //', ], [ '<?php class Aaa implements PhpCsFixer\Tests\Fixer, \RFb, \Fcc1, \GFdd { }', '<?php class Aaa implements PhpCsFixer\Tests\Fixer,\RFb, \Fcc1, \GFdd { }', ], [ '<?php class /**/ Test123 EXtends /**/ \RuntimeException implements TestZ { }', '<?php class/**/Test123 EXtends /**/ \RuntimeException implements TestZ { }', ], [ '<?php class Aaa implements Ebb, \Ccc { }', '<?php class Aaa implements Ebb, \Ccc { }', ], [ '<?php class X2 IMPLEMENTS Z, // U, D { }', '<?php class X2 IMPLEMENTS Z , // U, D { }', ], [ '<?php class VeryLongClassNameWithLotsOfLetters extends AnotherVeryLongClassName implements VeryLongInterfaceNameThatIDontWantOnTheSameLine { }', '<?php class VeryLongClassNameWithLotsOfLetters extends AnotherVeryLongClassName implements VeryLongInterfaceNameThatIDontWantOnTheSameLine { }', ], [ '<?php class /**/ Test125 //aaa extends /* */ // \Exception // {}', '<?php class/**/Test125 //aaa extends /* */ // \Exception // {}', ], [ '<?php class Test extends TestInterface8 implements /*a*/ /*b*/ TestInterface1, /* test */ TestInterface2, // test '.' // test TestInterface3, /**/ TestInterface4, TestInterface5, '.' /**/TestInterface6c { }', '<?php class Test extends TestInterface8 implements /*a*/ /*b*/TestInterface1 , /* test */ TestInterface2 , // test '.' // test TestInterface3, /**/ TestInterface4 , TestInterface5 , '.' /**/TestInterface6c { }', ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/OrderedTraitsFixerTest.php
tests/Fixer/ClassNotation/OrderedTraitsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer * * @internal * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedTraitsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: ?string}> */ public static function provideFixCases(): iterable { yield 'simple' => [ '<?php class Foo { use A; use B; }', '<?php class Foo { use B; use A; }', ]; yield 'in multiple classes' => [ '<?php class Foo { use A; use C; } class Bar { use B; use D; }', '<?php class Foo { use C; use A; } class Bar { use D; use B; }', ]; yield 'separated by a property' => [ '<?php class Foo { use A; use C; private $foo; use B; use D; }', '<?php class Foo { use C; use A; private $foo; use D; use B; }', ]; yield 'separated by a method' => [ '<?php class Foo { use A; use C; public function foo() { } use B; use D; }', '<?php class Foo { use C; use A; public function foo() { } use D; use B; }', ]; yield 'grouped statements' => [ '<?php class Foo { use A, C; use B; }', '<?php class Foo { use B; use A, C; }', ]; yield 'with aliases and conflicts' => [ '<?php class Foo { use A { A::foo insteadof B; A::bar as bazA; A::baz as protected; } use B { B::bar as bazB; } }', '<?php class Foo { use B { B::bar as bazB; } use A { A::foo insteadof B; A::bar as bazA; A::baz as protected; } }', ]; yield 'symbol imports' => [ '<?php use C; use B; use A;', ]; yield 'anonymous function with inherited variables' => [ '<?php $foo = function () use ($b, $a) { }; $bar = function () use ($a, $b) { };', ]; yield 'multiple traits in a single statement' => [ '<?php class Foo { use A, B, C, D; }', '<?php class Foo { use C, B, D, A; }', ]; yield 'multiple traits per statement' => [ '<?php class Foo { use A, D; use B, C; }', '<?php class Foo { use C, B; use D, A; }', ]; $uses = []; for ($i = 0; $i < 25; ++$i) { $uses[] = \sprintf(' use A%02d;', $i); } yield 'simple, multiple I' => [ \sprintf("<?php\nclass Foo {\n%s\n}", implode("\n", $uses)), \sprintf("<?php\nclass Foo {\n%s\n}", implode("\n", array_reverse($uses))), ]; yield 'simple, length diff. I' => [ '<?php class Foo { use A; use B\B; use C\C\C; use D\D\D\D; }', '<?php class Foo { use D\D\D\D; use C\C\C; use B\B; use A; }', ]; yield 'comments handling' => [ '<?php class Foo { /* A */use A\A\A\A/* A */; /* B */use B\B\B/* B */; /* C */use C\C/* C */; /* D */use D/* D */; }', '<?php class Foo { /* D */use D/* D */; /* C */use C\C/* C */; /* B */use B\B\B/* B */; /* A */use A\A\A\A/* A */; }', ]; yield 'grouped statements II' => [ '<?php class Foo { use A\Z, C\Y; use B\E; }', '<?php class Foo { use B\E; use A\Z, C\Y; }', ]; yield 'simple, leading \\' => [ '<?php class Foo { use \A; use \B; }', '<?php class Foo { use \B; use \A; }', ]; yield 'simple, leading \ before character order' => [ '<?php class Foo { use A; use \B; use C; }', '<?php class Foo { use C; use \B; use A; }', ]; yield 'with phpdoc' => [ '<?php class Foo { // foo 1 /** @phpstan-use A<Foo> */ use A; /** @phpstan-use B<Foo> */ use B; /** @phpstan-use C<Foo> */ use C; }', '<?php class Foo { /** @phpstan-use C<Foo> */ use C; /** @phpstan-use B<Foo> */ use B; // foo 1 /** @phpstan-use A<Foo> */ use A; }', ]; yield 'simple and with namespace' => [ '<?php class User { use Test\B, TestA; }', '<?php class User { use TestA, Test\B; }', ]; yield 'with case sensitive order' => [ '<?php class Foo { use AA; use Aaa; }', '<?php class Foo { use Aaa; use AA; }', [ 'case_sensitive' => true, ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassNotation/NoNullPropertyInitializationFixerTest.php
tests/Fixer/ClassNotation/NoNullPropertyInitializationFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\NoNullPropertyInitializationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\NoNullPropertyInitializationFixer> * * @author ntzm * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoNullPropertyInitializationFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php class Foo { public $bar; }', '<?php class Foo { public $bar = null; }', ]; yield [ '<?php class Foo { protected $bar; }', '<?php class Foo { protected $bar = null; }', ]; yield [ '<?php class Foo { private $bar; }', '<?php class Foo { private $bar = null; }', ]; yield [ '<?php class Foo { var $bar; }', '<?php class Foo { var $bar = null; }', ]; yield [ '<?php class Foo { VAR $bar; }', '<?php class Foo { VAR $bar = null; }', ]; yield [ '<?php class Foo { public $bar; }', '<?php class Foo { public $bar = NULL; }', ]; yield [ '<?php class Foo { PUblic $bar; }', '<?php class Foo { PUblic $bar = nuLL; }', ]; yield [ '<?php trait Foo { public $bar; }', '<?php trait Foo { public $bar = nuLL; }', ]; yield [ '<?php class Foo { public $bar; }', '<?php class Foo { public $bar = \null; }', ]; yield [ '<?php class Foo {/* */public/* A */$bar/* B *//** C */;/* D */}', '<?php class Foo {/* */public/* A */$bar/* B */=/** C */null;/* D */}', ]; yield [ '<?php class Foo { public $bar; protected $baz; }', '<?php class Foo { public $bar = null; protected $baz = null; }', ]; yield [ '<?php class Foo { public $bar = \'null\'; }', ]; yield [ '<?php class Foo { public function bar() { return null; } }', ]; yield [ '<?php class Foo { protected $bar, $baz, $qux; }', '<?php class Foo { protected $bar = null, $baz = null, $qux = null; }', ]; yield [ '<?php class Foo { protected $bar, $baz = \'baz\', $qux; }', '<?php class Foo { protected $bar, $baz = \'baz\', $qux = null; }', ]; yield [ '<?php trait Foo { public $bar; } abstract class Bar { protected $bar, $baz = \'baz\', $qux; }', '<?php trait Foo { public $bar = null; } abstract class Bar { protected $bar, $baz = \'baz\', $qux = null; }', ]; yield [ '<?php class Foo { public function foo() { return null; } public $bar; public function baz() { return null; } }', '<?php class Foo { public function foo() { return null; } public $bar = null; public function baz() { return null; } }', ]; yield [ '<?php class#1 Foo#2 {#3 protected#4 $bar#5 #6 ,#7 $baz#8 #9 ,#10 $qux#11 #12 ;#13 } ', '<?php class#1 Foo#2 {#3 protected#4 $bar#5 =#6 null,#7 $baz#8 =#9 null,#10 $qux#11 =#12 null;#13 } ', ]; yield [ '<?php class Foo { public static $bar; }', '<?php class Foo { public static $bar = null; }', ]; yield [ '<?php class Foo { protected static $bar; }', '<?php class Foo { protected static $bar = null; }', ]; yield [ '<?php class Foo { private static $bar; }', '<?php class Foo { private static $bar = null; }', ]; yield [ '<?php class Foo { static $bar; }', '<?php class Foo { static $bar = null; }', ]; yield [ '<?php class Foo { STATIC $bar; }', '<?php class Foo { STATIC $bar = null; }', ]; yield [ '<?php class Foo { public static $bar; }', '<?php class Foo { public static $bar = NULL; }', ]; yield [ '<?php class Foo { PUblic STatic $bar; }', '<?php class Foo { PUblic STatic $bar = nuLL; }', ]; yield [ '<?php trait Foo { public static $bar; }', '<?php trait Foo { public static $bar = nuLL; }', ]; yield [ '<?php class Foo { public static $bar; }', '<?php class Foo { public static $bar = \null; }', ]; yield [ '<?php class Foo {/* */public/* */static/* A */$bar/* B *//** C */;/* D */}', '<?php class Foo {/* */public/* */static/* A */$bar/* B */=/** C */null;/* D */}', ]; yield [ '<?php class Foo { public static $bar; protected static $baz; }', '<?php class Foo { public static $bar = null; protected static $baz = null; }', ]; yield [ '<?php class Foo { public static $bar = \'null\'; }', ]; yield [ '<?php class Foo { public static function bar() { return null; } }', ]; yield [ '<?php class Foo { protected static $bar, $baz, $qux; }', '<?php class Foo { protected static $bar = null, $baz = null, $qux = null; }', ]; yield [ '<?php class Foo { protected static $bar, $baz = \'baz\', $qux; }', '<?php class Foo { protected static $bar, $baz = \'baz\', $qux = null; }', ]; yield [ '<?php trait Foo { public static $bar; } abstract class Bar { protected static $bar, $baz = \'baz\', $qux; }', '<?php trait Foo { public static $bar = null; } abstract class Bar { protected static $bar, $baz = \'baz\', $qux = null; }', ]; yield [ '<?php class Foo { public function foo() { return null; } public static $bar; public function baz() { return null; } }', '<?php class Foo { public function foo() { return null; } public static $bar = null; public function baz() { return null; } }', ]; yield [ '<?php class#1 Foo#2 {#3 protected#4 static#4.5 $bar#5 #6 ,#7 $baz#8 #9 ,#10 $qux#11 #12 ;#13 } ', '<?php class#1 Foo#2 {#3 protected#4 static#4.5 $bar#5 =#6 null,#7 $baz#8 =#9 null,#10 $qux#11 =#12 null;#13 } ', ]; yield [ '<?php class Foo { const FOO = null; }', ]; yield [ '<?php class Foo { public function foo() { static $foo = null; } }', ]; yield [ '<?php function foo() { static $foo = null; }', ]; yield [ '<?php new class () { public $bar; };', '<?php new class () { public $bar = null; };', ]; yield [ '<?php class Foo { public function foo() { return new class() { private $bar; }; } }', '<?php class Foo { public function foo() { return new class() { private $bar = null; }; } }', ]; yield [ '<?php class Foo { public function foo() { return new class() { private $bar; }; } } trait Baz { public $baz; }', '<?php class Foo { public function foo() { return new class() { private $bar = null; }; } } trait Baz { public $baz = null; }', ]; yield [ '<?php new class () { public static $bar; };', '<?php new class () { public static $bar = null; };', ]; yield [ '<?php class Foo { public function foo() { return new class() { private static $bar; }; } }', '<?php class Foo { public function foo() { return new class() { private static $bar = null; }; } }', ]; yield [ '<?php class Foo { public function foo() { return new class() { private static $bar; }; } } trait Baz { public static $baz; }', '<?php class Foo { public function foo() { return new class() { private static $bar = null; }; } } trait Baz { public static $baz = null; }', ]; yield [ '<?php class Foo { public function foo() { return new class() { public function foo() { static $foo = null; } }; } }', ]; yield [ '<?php function foo() { return new class() { public function foo() { static $foo = null; } }; }', ]; yield [ '<?php class Foo { public const FOO = null; }', ]; yield [ '<?php class Foo { protected ?int $bar = null; }', ]; yield [ '<?php class Foo { protected ? string $bar = null; }', ]; yield [ '<?php class Foo { protected ? array $bar = null; }', ]; yield [ '<?php class Foo { protected static ?int $bar = null; }', ]; yield [ '<?php class Foo { protected static ? string $bar = null; }', ]; yield [ '<?php class Foo { protected static ? array $bar = null; }', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php class Foo { public $bar; }', '<?php class Foo { public $bar = \ null; }', ]; yield [ '<?php class Foo { public $bar/* oh hai! */; }', '<?php class Foo { public $bar = \/* oh hai! */null; }', ]; yield [ '<?php class Foo { public static $bar; }', '<?php class Foo { public static $bar = \ null; }', ]; yield [ '<?php class Foo { public static $bar/* oh hai! */; }', '<?php class Foo { public static $bar = \/* oh hai! */null; }', ]; } /** * @requires PHP 8.0 */ public function testFix80(): void { $this->doTest('<?php class Point { public function __construct( public ?float $x = null, protected ?float $y = null, private ?float $z = null, ) {} } '); } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string}> */ public static function provideFix81Cases(): iterable { yield 'readonly - cannot have default value, fixer should not crash' => [ '<?php final class Foo { public readonly string $prop; }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/ConstantCaseFixerTest.php
tests/Fixer/Casing/ConstantCaseFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\ConstantCaseFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\ConstantCaseFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Casing\ConstantCaseFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ConstantCaseFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php if (true) if (false) if (null) {}', '<?php if (TRUE) if (FALSE) if (NULL) {}', ]; yield [ '<?php if (!true) if (!false) if (!null) {}', '<?php if (!TRUE) if (!FALSE) if (!NULL) {}', ]; yield [ '<?php if ($a == true) if ($a == false) if ($a == null) {}', '<?php if ($a == TRUE) if ($a == FALSE) if ($a == NULL) {}', ]; yield [ '<?php if ($a === true) if ($a === false) if ($a === null) {}', '<?php if ($a === TRUE) if ($a === FALSE) if ($a === NULL) {}', ]; yield [ '<?php if ($a != true) if ($a != false) if ($a != null) {}', '<?php if ($a != TRUE) if ($a != FALSE) if ($a != NULL) {}', ]; yield [ '<?php if ($a !== true) if ($a !== false) if ($a !== null) {}', '<?php if ($a !== TRUE) if ($a !== FALSE) if ($a !== NULL) {}', ]; yield [ '<?php if (true && true and true AND true || false or false OR false xor null XOR null) {}', '<?php if (TRUE && TRUE and TRUE AND TRUE || FALSE or FALSE OR FALSE xor NULL XOR NULL) {}', ]; yield [ '<?php /* foo */ true; /** bar */ false;', '<?php /* foo */ TRUE; /** bar */ FALSE;', ]; yield ['<?php echo $null;']; yield ['<?php $x = False::foo();']; yield ['<?php $x = Foo::NULL;']; yield ['<?php namespace Null;']; yield ['<?php namespace Foo\Null;']; yield ['<?php class Foo extends True {}']; yield ['<?php class Foo implements False {}']; yield ['<?php $foo instanceof True; $foo instanceof False; $foo instanceof Null;']; yield ['<?php return Null\Foo::class;']; yield ['<?php return Null\Foo::bar();']; yield [ '<?php class Foo { const TRUE = 1; const FALSE = true; const NULL = null; }', '<?php class Foo { const TRUE = 1; const FALSE = TRUE; const NULL = NULL; }', ]; yield ['<?php $x = new /**/False?>']; yield ['<?php Null/**/::test();']; yield ['<?php True// ::test();']; yield ['<?php class Foo { public function Bar() { $this->False = 1; $this->True = 2; $this->Null = 3; } }']; foreach (['true', 'false', 'null'] as $case) { yield [ \sprintf('<?php $x = %s;', $case), \sprintf('<?php $x = %s;', strtoupper($case)), ['case' => 'lower'], ]; yield [ \sprintf('<?php $x = %s;', $case), \sprintf('<?php $x = %s;', ucfirst($case)), ['case' => 'lower'], ]; yield [ \sprintf('<?php $x = new %s;', ucfirst($case)), null, ['case' => 'lower'], ]; yield [ \sprintf('<?php $x = new %s;', strtoupper($case)), null, ['case' => 'lower'], ]; yield [ \sprintf('<?php $x = "%s story";', $case), null, ['case' => 'lower'], ]; yield [ \sprintf('<?php $x = "%s";', $case), null, ['case' => 'lower'], ]; } foreach (['true', 'false', 'null'] as $case) { yield [ \sprintf('<?php $x = %s;', strtoupper($case)), \sprintf('<?php $x = %s;', $case), ['case' => 'upper'], ]; yield [ \sprintf('<?php $x = %s;', strtoupper($case)), \sprintf('<?php $x = %s;', ucfirst($case)), ['case' => 'upper'], ]; yield [ \sprintf('<?php $x = new %s;', ucfirst($case)), null, ['case' => 'upper'], ]; yield [ \sprintf('<?php $x = new %s;', strtoupper($case)), null, ['case' => 'upper'], ]; yield [ \sprintf('<?php $x = "%s story";', $case), null, ['case' => 'upper'], ]; yield [ \sprintf('<?php $x = "%s";', $case), null, ['case' => 'upper'], ]; } } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string}> */ public static function provideFix80Cases(): iterable { yield 'nullsafe operator' => ['<?php class Foo { public function Bar() { return $this?->False; } }']; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'final class const' => [ '<?php class Foo { final const TRUE = 1; public final const FALSE = true; final public const NULL = null; } ', '<?php class Foo { final const TRUE = 1; public final const FALSE = TRUE; final public const NULL = NULL; } ', ]; yield 'enum and switch' => [ '<?php enum Foo { case True; case False; case Null; public function methodWithSwitch(mixed $value): void { switch ($value) { case true: case false: case null: break; } } } ', '<?php enum Foo { case True; case False; case Null; public function methodWithSwitch(mixed $value): void { switch ($value) { case TRUE: case FALSE: case NULL: break; } } } ', ]; yield 'enum' => [ '<?php $y = false; enum Foo: string { case FALSE = "false"; } $x = true; ', '<?php $y = FALSE; enum Foo: string { case FALSE = "false"; } $x = TRUE; ', ]; } /** * @dataProvider provideFix83Cases * * @requires PHP 8.3 */ public function testFix83(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix83Cases(): iterable { yield 'typed constant' => [ <<<'PHP' <?php class Foo1 { const array NULL = []; } class Foo2 { const int NULL = 0; } class Foo3 { const mixed NULL = 0; } class Foo4 { const string NULL = ''; } class Foo5 { const Foo|null NULL = null; } class Foo6 { const null|Foo NULL = null; } class Foo7 { const null|(Foo&Bar) NULL = null; } class Foo8 { const bool TRUE = true; } class Foo9 { const false FALSE = false; } class Foo10 { const true TRUE = true; } PHP, <<<'PHP' <?php class Foo1 { const array NULL = []; } class Foo2 { const int NULL = 0; } class Foo3 { const mixed NULL = 0; } class Foo4 { const string NULL = ''; } class Foo5 { const Foo|NULL NULL = NULL; } class Foo6 { const NULL|Foo NULL = NULL; } class Foo7 { const NULL|(Foo&Bar) NULL = NULL; } class Foo8 { const bool TRUE = TRUE; } class Foo9 { const FALSE FALSE = FALSE; } class Foo10 { const TRUE TRUE = TRUE; } PHP, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/LowercaseKeywordsFixerTest.php
tests/Fixer/Casing/LowercaseKeywordsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LowercaseKeywordsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield ['<?php $x = (1 and 2);', '<?php $x = (1 AND 2);']; yield ['<?php foreach(array(1, 2, 3) as $val) {}', '<?php FOREACH(array(1, 2, 3) AS $val) {}']; yield ['<?php echo "GOOD AS NEW";']; yield ['<?php echo X::class ?>', '<?php echo X::ClASs ?>']; yield [ '<?php $fn = fn() => true;', '<?php $fn = FN() => true;', ]; yield ['<?php __HALT_COMPILER();']; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php echo match (1) { 1 => 9, 2 => 7, };', '<?php echo MATCH (1) { 1 => 9, 2 => 7, };', ]; yield [ '<?php class Point { public function __construct( public float $x = 0.0, protected float $y = 0.0, private float $z = 0.0, ) {} } ', '<?php class Point { public function __construct( PUBLIC float $x = 0.0, Protected float $y = 0.0, privatE float $z = 0.0, ) {} } ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php final class Foo { public readonly string $prop; } ', '<?php final class Foo { public READONLY string $prop; } ', ]; yield [ '<?php class Point { public function __construct( public readonly float $x = 0.0, readonly protected float $y = 0.0, private readonly float $z = 0.0, ) {} } ', '<?php class Point { public function __construct( PUBLIC rEADONLY float $x = 0.0, READonly Protected float $y = 0.0, privatE READONLY float $z = 0.0, ) {} } ', ]; yield 'enum full caps' => [ '<?php enum Suit { case Hearts; } ', '<?php ENUM Suit { case Hearts; } ', ]; } /** * @dataProvider provideFix84Cases * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix84Cases(): iterable { yield 'asymmetric visibility' => [ <<<'PHP' <?php class Foo { public(set) Bar $a; protected(set) Bar $b; private(set) Baz $c; } PHP, <<<'PHP' <?php class Foo { PUBLIC(SET) Bar $a; PROTECTED(SET) Bar $b; PRIVATE(SET) Baz $c; } PHP, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/LowercaseStaticReferenceFixerTest.php
tests/Fixer/Casing/LowercaseStaticReferenceFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @covers \PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer * * @internal * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer> * * @author Kuba Werłos <werlos@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LowercaseStaticReferenceFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php class Foo extends Bar { public function baz() { self::qux(); } }', '<?php class Foo extends Bar { public function baz() { SELF::qux(); } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { static::qux(); } }', '<?php class Foo extends Bar { public function baz() { STATIC::qux(); } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { parent::baz(); } }', '<?php class Foo extends Bar { public function baz() { PARENT::baz(); } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { parent::baz(); } }', '<?php class Foo extends Bar { public function baz() { Parent::baz(); } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { return new self(); } }', '<?php class Foo extends Bar { public function baz() { return new Self(); } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { return SelfFoo::FOO; } }', ]; yield [ '<?php class Foo extends Bar { public function baz() { return FooSelf::FOO; } }', ]; yield [ '<?php class Foo extends Bar { private STATIC $baz; }', ]; yield [ '<?php class Foo extends Bar { STATIC private $baz; }', ]; yield [ '<?php class Foo extends Bar { public function paRent() {} }', ]; yield [ '<?php $foo->Self();', ]; yield [ '<?php Foo::Self();', ]; yield [ '<?php if ($foo instanceof self) { return true; }', '<?php if ($foo instanceof Self) { return true; }', ]; yield [ '<?php if ($foo instanceof static) { return true; }', '<?php if ($foo instanceof Static) { return true; }', ]; yield [ '<?php if ($foo instanceof parent) { return true; }', '<?php if ($foo instanceof Parent) { return true; }', ]; yield [ '<?php if ($foo instanceof Self\Bar) { return true; }', ]; yield [ '<?php if ($foo instanceof MySelf) { return true; }', ]; yield [ '<?php class Foo extends Bar { public function baz(self $x) {} }', '<?php class Foo extends Bar { public function baz(Self $x) {} }', ]; yield [ '<?php class Foo extends Bar { public function baz(parent $x) {} }', '<?php class Foo extends Bar { public function baz(Parent $x) {} }', ]; yield [ '<?php class Foo extends Bar { public function baz(MySelf $x) {} }', ]; yield [ '<?php class Foo extends Bar { public function baz(Self\Qux $x) {} }', ]; yield [ '<?php $a = STATIC function() {};', ]; yield [ '<?php class A { public function B() { STATIC $a; echo $a; }}', ]; yield [ '<?php class A { public function B() { $collection = $static ? new static($b) : new self(); } }', '<?php class A { public function B() { $collection = $static ? new STATIC($b) : new self(); } }', ]; yield [ '<?php class A { STATIC public function B() {} }', ]; yield [ '<?php $a = function () { STATIC $B = false; if ($B) { echo 1; } $B = true; }; ', ]; yield [ '<?php class A { const PARENT = 42; }', ]; yield [ '<?php namespace Foo\Parent;', ]; yield [ '<?php namespace Parent\Foo;', ]; yield [ '<?php class Foo extends Bar { public function baz() : self {} }', '<?php class Foo extends Bar { public function baz() : Self {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : parent {} }', '<?php class Foo extends Bar { public function baz() : Parent {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : MySelf {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : Self\Qux {} }', ]; yield [ '<?php class Foo extends Bar { public function baz(?self $x) {} }', '<?php class Foo extends Bar { public function baz(?Self $x) {} }', ]; yield [ '<?php class Foo extends Bar { public function baz(?parent $x) {} }', '<?php class Foo extends Bar { public function baz(?Parent $x) {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : ?self {} }', '<?php class Foo extends Bar { public function baz() : ?Self {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : ?parent {} }', '<?php class Foo extends Bar { public function baz() : ?Parent {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : ?MySelf {} }', ]; yield [ '<?php class Foo extends Bar { public function baz() : ?Self\Qux {} }', ]; yield [ '<?php class Foo { private STATIC int $baz1; private STATIC ?int $baz2; }', ]; yield [ '<?php class Foo { public function bar() {} } class FooChild extends Foo { public function bar() { switch (true) { case parent::bar(): } } }', '<?php class Foo { public function bar() {} } class FooChild extends Foo { public function bar() { switch (true) { case PARENT::bar(): } } }', ]; yield [ <<<'PHP' <?php class Foo { public self $a; protected self $b; private self $c; } PHP, <<<'PHP' <?php class Foo { public SELF $a; protected SELF $b; private SELF $c; } PHP, ]; yield [ <<<'PHP' <?php define("SELF", "foo"); define("PARENT", "bar"); bar(SELF); echo PARENT; class Foo { public static function f() { return SELF; } } PHP, ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield ['<?php $foo?->Self();']; yield [ '<?php class Foo extends A { public function baz1() : int|parent {} public function baz2() : parent|int {} public function baz3() : ?parent {} }', '<?php class Foo extends A { public function baz1() : int|Parent {} public function baz2() : Parent|int {} public function baz3() : ?Parent {} }', ]; yield [ '<?php class Foo extends A { public function baz1() : int|static {} public function baz2() : static|int {} public function baz3() : ?static {} }', '<?php class Foo extends A { public function baz1() : int|STATIC {} public function baz2() : STATIC|int {} public function baz3() : ?STATIC {} }', ]; yield [ '<?php class Foo { private int|self $prop1, $prop2; private self|int $prop3, $prop4; } ', '<?php class Foo { private int|SELF $prop1, $prop2; private SELF|int $prop3, $prop4; } ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php class A { final const PARENT = 42; }', ]; yield [<<<'PHP' <?php enum Foo: string { case SELF = 'self'; case STATIC = 'static'; case PARENT = 'parent'; } PHP]; yield [<<<'PHP' <?php enum Foo { case SELF; case STATIC; case PARENT; } PHP]; } /** * @dataProvider provideFix83Cases * * @requires PHP 8.3 */ public function testFix83(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string}> */ public static function provideFix83Cases(): iterable { yield [<<<'PHP' <?php class Foo { private const array PARENT = ['parent']; private const array SELF = ['self']; private const array STATIC = ['static']; } PHP]; yield [<<<'PHP' <?php class Foo { private const int PARENT = 1; private const int SELF = 2; private const int STATIC = 3; } PHP]; yield [<<<'PHP' <?php class Foo { private const int|static PARENT = 1; private const int|static SELF = 2; private const int|static STATIC = 3; } PHP]; yield [<<<'PHP' <?php class Foo { private const string|(Bar&Baz) PARENT = 'parent'; private const string|(Bar&Baz) SELF = 'self'; private const string|(Bar&Baz) STATIC = 'static'; } PHP]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/NativeTypeDeclarationCasingFixerTest.php
tests/Fixer/Casing/NativeTypeDeclarationCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\NativeTypeDeclarationCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\NativeTypeDeclarationCasingFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NativeTypeDeclarationCasingFixerTest extends AbstractFixerTestCase { /** * @requires PHP <8.0 */ public function testFixPre80(): void { $this->doTest('<?php class D { private MIXED $m; }; '); } /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php function A(int $a): void {} class Foo { private bool $c = false; private bool $d = false; public function B(int $a): bool { return $this->c || $this->d; } } function C(float $a): array { return [$a];} function D(array $a): array { return [$a];} ', '<?php function A(INT $a): VOID {} class Foo { private BOOL $c = false; private BOOL $d = false; public function B(INT $a): BOOL { return $this->c || $this->d; } } function C(FLOAT $a): ARRAY { return [$a];} function D(ARRAY $a): ARRAY { return [$a];} ', ]; yield [ '<?php class Foo { private function Bar(array $bar) { return false; } } ', '<?php class Foo { private function Bar(ARRAY $bar) { return false; } } ', ]; yield [ '<?php interface Foo { public function Bar(array $bar); } ', '<?php interface Foo { public function Bar(ArrAY $bar); } ', ]; yield [ '<?php function Foo(/**/array/**/$bar) { return false; } ', '<?php function Foo(/**/ARRAY/**/$bar) { return false; } ', ]; yield [ '<?php class Bar { function Foo(array $a, callable $b, self $c) {} } ', '<?php class Bar { function Foo(ARRAY $a, CALLABLE $b, Self $c) {} } ', ]; yield [ '<?php function Foo(INTEGER $a) {} ', ]; yield [ '<?php function Foo( String\A $x, B\String\C $y ) {}', ]; yield [ '<?php final class Foo1 { final public function Foo(bool $A, float $B, int $C, string $D): int {} }', '<?php final class Foo1 { final public function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {} }', ]; yield [ '<?php function Foo(bool $A, float $B, int $C, string $D): int {}', '<?php function Foo(BOOL $A, FLOAT $B, INT $C, STRING $D): INT {}', ]; yield [ '<?php function Foo(): Foo\A { return new Foo(); }', ]; yield [ '<?php trait XYZ { function Foo(iterable $A): void {} }', '<?php trait XYZ { function Foo(ITERABLE $A): VOID {} }', ]; yield [ '<?php function Foo(iterable $A): void {}', '<?php function Foo(ITERABLE $A): VOID {}', ]; yield [ '<?php function Foo(?int $A): void {}', '<?php function Foo(?INT $A): VOID {}', ]; yield [ '<?php function Foo(string $A): ?/* */int {}', '<?php function Foo(STRING $A): ?/* */INT {}', ]; yield [ '<?php function Foo(object $A): void {}', '<?php function Foo(OBJECT $A): VOID {}', ]; yield [ '<?php return function (callable $c) {};', '<?php return function (CALLABLE $c) {};', ]; yield [ '<?php return fn (callable $c): int => 1;', '<?php return fn (CALLABLE $c): INT => 1;', ]; yield [ '<?php class Foo { const A = 1; const B = []; const INT = "A"; // class constant; INT is the name of the const, not the type const FLOAT=1.2; } const INT = "A"; // outside class; INT is the name of the const, not the type ', ]; yield 'class properties single type' => [ '<?php class D{} $a = new class extends D { private array $ax; private bool $bx = false; private float $cx = 3.14; private int $dx = 667; private iterable $ex = []; private object $f; private parent $g; private self $h; private static $i; private ?string $j; private $INT = 1; private FOO $bar; private A\INT\B $z; }; ', '<?php class D{} $a = new class extends D { private ARRAY $ax; private BOOL $bx = false; private FLOAT $cx = 3.14; private INT $dx = 667; private ITERABLE $ex = []; private OBJECT $f; private PARENT $g; private Self $h; private STatic $i; private ?STRIng $j; private $INT = 1; private FOO $bar; private A\INT\B $z; }; ', ]; yield 'var keyword' => [ '<?php class Foo { var $bar; }', ]; yield 'static property without type' => [ '<?php class Foo { static $bar; }', '<?php class Foo { STATIC $bar; }', ]; yield 'dynamic property' => [ '<?php class Foo { public function doFoo() { $this->Object->doBar(); } }', ]; yield 'constants' => [ <<<'PHP' <?php function f() {} if (True === $x) { } elseif (True == $y) { } elseif ($z === False) {} PHP, ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFix80Cases(): iterable { yield 'class properties single type' => [ '<?php class D { private mixed $m; }; ', '<?php class D { private MIXED $m; }; ', ]; yield [ '<?php class T { public function Foo(object $A): static {}}', '<?php class T { public function Foo(object $A): StatiC {}}', ]; yield [ '<?php class T { public function Foo(object $A): ?static {}}', '<?php class T { public function Foo(object $A): ?StatiC {}}', ]; yield [ '<?php class T { public function Foo(mixed $A): mixed {}}', '<?php class T { public function Foo(Mixed $A): MIXED {}}', ]; yield 'mixed in arrow function' => [ '<?php return fn (mixed $c): mixed => 1;', '<?php return fn (MiXeD $c): MIXED => 1;', ]; yield [ '<?php function foo(int|bool $x) {}', '<?php function foo(INT|BOOL $x) {}', ]; yield [ '<?php function foo(int | bool $x) {}', '<?php function foo(INT | BOOL $x) {}', ]; yield [ '<?php function foo(): int|bool {}', '<?php function foo(): INT|BOOL {}', ]; yield 'return type string|false' => [ '<?php function foo(): string|false {}', '<?php function foo(): string|FALSE {}', ]; yield 'return type string|null' => [ '<?php function foo(): string|null {}', '<?php function foo(): string|NULL {}', ]; yield 'union types in arrow function' => [ '<?php return fn (string|null $c): int|null => 1;', '<?php return fn (string|NULL $c): INT|NULL => 1;', ]; yield 'union Types' => [ '<?php $a = new class { private null|int|bool $a4 = false; };', '<?php $a = new class { private NULL|INT|BOOL $a4 = false; };', ]; yield 'promoted properties' => [ <<<'PHP' <?php class Foo extends Bar { public function __construct( public int $i, protected parent $p, private string $s ) {} } PHP, <<<'PHP' <?php class Foo extends Bar { public function __construct( public INT $i, protected PARENT $p, private STRING $s ) {} } PHP, ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'return type `never`' => [ '<?php class T { public function Foo(object $A): never {die;}}', '<?php class T { public function Foo(object $A): NEVER {die;}}', ]; yield 'class readonly property' => [ '<?php class Z { private readonly array $ax; };', '<?php class Z { private readonly ARRAY $ax; };', ]; } /** * @dataProvider provideFix82Cases * * @requires PHP 8.2 */ public function testFix82(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix82Cases(): iterable { yield 'disjunctive normal form types in arrow function' => [ '<?php return fn ((A&B)|C|null $c): (X&Y)|Z|null => 1;', '<?php return fn ((A&B)|C|Null $c): (X&Y)|Z|NULL => 1;', ]; yield 'iterable: disjunctive normal form types in arrow function' => [ '<?php return fn (iterable|C|null $c): (X&Y)|Z|null => 1;', '<?php return fn (ITERABLE|C|Null $c): (X&Y)|Z|NULL => 1;', ]; foreach (['true', 'false', 'null'] as $type) { yield \sprintf('standalone type `%s` in class method', $type) => [ \sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type), \sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', strtoupper($type)), ]; yield \sprintf('standalone type `%s` in function', $type) => [ \sprintf('<?php function Foo(%s $A): %1$s {return $A;}', $type), \sprintf('<?php function Foo(%s $A): %1$s {return $A;}', strtoupper($type)), ]; yield \sprintf('standalone type `%s` in closure', $type) => [ \sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type), \sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)), ]; yield \sprintf('standalone type `%s` in arrow function', $type) => [ \sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', $type), \sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', strtoupper($type)), ]; } yield 'intersection Types' => [ '<?php $a = new class { private (A&B)|int|D $d5; private (A\STRING\B&B\INT\C)|int|(A&B) $e6; };', '<?php $a = new class { private (A&B)|INT|D $d5; private (A\STRING\B&B\INT\C)|int|(A&B) $e6; };', ]; } /** * @dataProvider provideFix83Cases * * @requires PHP 8.3 */ public function testFix83(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string}> */ public static function provideFix83Cases(): iterable { yield 'simple case' => [ '<?php class Foo { const int FOO = 6; } ', '<?php class Foo { const INT FOO = 6; } ', ]; yield 'single types' => [ '<?php class Foo { const int SOME_INT = 3; const array SOME_ARRAY = [7]; const float SOME_FLOAT = 1.23; const iterable SOME_ITERABLE = [1, 2]; const mixed SOME_MIXED = 1; const null SOME_NULL = NULL; const string SOME_STRING = "X"; } ', '<?php class Foo { const INT SOME_INT = 3; const ARRAY SOME_ARRAY = [7]; const Float SOME_FLOAT = 1.23; const ITERABLE SOME_ITERABLE = [1, 2]; const MIXED SOME_MIXED = 1; const NULL SOME_NULL = NULL; const STRING SOME_STRING = "X"; } ', ]; yield 'nullables `self`, `parent` and `object`' => [ '<?php class Foo extends FooParent { const ?object SOME_OBJECT = NULL; const ?parent SOME_PARENT = NULL; const ?self SOME_SELF = NULL; const ?int/* x */A/* y */= 3; const ?BAR B = null; const ?BAR C = null; const ?\BAR D = null; const ?\INT\B E = null; public const ?INT\A/* x */X=C; } ', '<?php class Foo extends FooParent { const ?OBJECT SOME_OBJECT = NULL; const ?PARENT SOME_PARENT = NULL; const ?Self SOME_SELF = NULL; const ?INT/* x */A/* y */= 3; const ?BAR B = null; const ?BAR C = null; const ?\BAR D = null; const ?\INT\B E = null; public const ?INT\A/* x */X=C; } ', ]; yield 'simple `|`' => [ '<?php class Foo1 { const D|float BAR = 1.0; }', '<?php class Foo1 { const D|Float BAR = 1.0; }', ]; yield 'multiple `|`' => [ '<?php class Foo2 { const int|array|null|A\B|\C\D|float BAR_1 = NULL; }', '<?php class Foo2 { const INT|ARRAY|NULL|A\B|\C\D|FLOAT BAR_1 = NULL; }', ]; yield 'handle mix of `|` and `(X&Y)`' => [ '<?php class Foo extends FooParent { private const Z|A\S\T\R|int|float|iterable SOMETHING = 123; protected const \A\B|(Foo & Stringable )|null|\X D = null; public const Foo&Stringable G = V::A; } ', '<?php class Foo extends FooParent { private const Z|A\S\T\R|INT|FLOAT|ITERABLE SOMETHING = 123; protected const \A\B|(Foo & Stringable )|NULL|\X D = null; public const Foo&Stringable G = V::A; } ', ]; yield 'interface, nullable int' => [ '<?php interface SomeInterface { const ?int FOO = 1; }', '<?php interface SomeInterface { const ?INT FOO = 1; }', ]; yield 'trait, string' => [ '<?php trait T { const string TEST = E::TEST; }', '<?php trait T { const STRING TEST = E::TEST; }', ]; yield 'enum, int' => [ '<?php enum E: string { case Hearts = "H"; const int TEST = 789; const self A = self::Hearts; const static B = self::Hearts; }', '<?php enum E: STRING { case Hearts = "H"; const INT TEST = 789; const SELF A = self::Hearts; const STATIC B = self::Hearts; }', ]; yield 'enum with "Mixed" case' => [ <<<'PHP' <?php enum Foo { case Mixed; public function bar() { self::Mixed; } } PHP, ]; yield 'do not fix' => [ '<?php class Foo { PUBLIC CONST FOO&STRINGABLE G = A::B; } CONST A = 1;', ]; yield 'fix "false" in type' => [ '<?php class Foo { private false|int $bar; private false $baz; }', '<?php class Foo { private FALSE|INT $bar; private FALSE $baz; }', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/MagicMethodCasingFixerTest.php
tests/Fixer/Casing/MagicMethodCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class MagicMethodCasingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { $allMethodNames = \Closure::bind(static fn (): array => MagicMethodCasingFixer::MAGIC_NAMES, null, MagicMethodCasingFixer::class)(); // '__callStatic' yield 'method declaration for "__callstatic".' => [ '<?php class Foo {public static function __callStatic($a, $b){}}', '<?php class Foo {public static function __CALLStatic($a, $b){}}', ]; yield 'static call to "__callstatic".' => [ '<?php Foo::__callStatic() ?>', '<?php Foo::__callstatic() ?>', ]; unset($allMethodNames['__callstatic']); // static version of '__set_state' yield 'method declaration for "__set_state".' => [ '<?php class Foo {public static function __set_state($a){}}', '<?php class Foo {public static function __set_STATE($a){}}', ]; yield 'static call to "__set_state".' => [ '<?php Foo::__set_state() ?>', '<?php Foo::__set_STATE() ?>', ]; // '__clone' yield 'method declaration for "__clone".' => [ '<?php class Foo {public function __clone(){}}', '<?php class Foo {public function __CLONE(){}}', ]; unset($allMethodNames['__clone'], $allMethodNames['__set_state']); // two arguments $methodNames = ['__call', '__set']; foreach ($methodNames as $name) { unset($allMethodNames[$name]); yield \sprintf('method declaration for "%s".', $name) => [ \sprintf('<?php class Foo {public function %s($a, $b){}}', $name), \sprintf('<?php class Foo {public function %s($a, $b){}}', strtoupper($name)), ]; } foreach ($methodNames as $name) { yield \sprintf('method call "%s".', $name) => [ \sprintf('<?php $a->%s($a, $b);', $name), \sprintf('<?php $a->%s($a, $b);', strtoupper($name)), ]; } // single argument $methodNames = ['__get', '__isset', '__unset', '__unserialize']; foreach ($methodNames as $name) { unset($allMethodNames[$name]); yield \sprintf('method declaration for "%s".', $name) => [ \sprintf('<?php class Foo {public function %s($a){}}', $name), \sprintf('<?php class Foo {public function %s($a){}}', strtoupper($name)), ]; } foreach ($methodNames as $name) { yield \sprintf('method call "%s".', $name) => [ \sprintf('<?php $a->%s($a);', $name), \sprintf('<?php $a->%s($a);', strtoupper($name)), ]; } // no argument foreach ($allMethodNames as $name) { yield \sprintf('method declaration for "%s".', $name) => [ \sprintf('<?php class Foo {public function %s(){}}', $name), \sprintf('<?php class Foo {public function %s(){}}', strtoupper($name)), ]; } foreach ($allMethodNames as $name) { yield \sprintf('method call "%s".', $name) => [ \sprintf('<?php $a->%s();', $name), \sprintf('<?php $a->%s();', strtoupper($name)), ]; } yield 'method declaration in interface' => [ '<?php interface Foo {public function __toString();}', '<?php interface Foo {public function __tostring();}', ]; yield 'method declaration in trait' => [ '<?php trait Foo {public function __toString(){}}', '<?php trait Foo {public function __tostring(){}}', ]; yield '(un)serialize' => [ '<?php class Foo extends Bar { public function __serialize() { $this->__serialize(); } public function __unserialize($payload) { $this->__unserialize($this->$a); } } ', '<?php class Foo extends Bar { public function __SERIALIZE() { $this->__SERIALIZE(); } public function __unSERIALIZE($payload) { $this->__unSERIALIZE($this->$a); } } ', ]; yield 'PHP 7 syntax' => [ '<?php function __TOSTRING(){} // do not fix trait FooTrait { public function __invoke($a){} // fix } function __GET($a){} // do not fix interface Foo { public function __sleep(); // fix } final class Foo { private function __construct($a, $b, $c, $d = null, $e = 1) // fix { } public function __isset($a) // fix { return $b->__isset($b); // fix } private function bar() { new class { public function __unset($a) // fix { $b = null === $a ? $b->__unset($a) // fix : $a->__unset($a) // fix ; return $b; } }; } } function __ISSET($bar){} // do not fix $a->__unset($foo); // fix ', '<?php function __TOSTRING(){} // do not fix trait FooTrait { public function __INVOKE($a){} // fix } function __GET($a){} // do not fix interface Foo { public function __SlEeP(); // fix } final class Foo { private function __consTRUCT($a, $b, $c, $d = null, $e = 1) // fix { } public function __ISSET($a) // fix { return $b->__IsseT($b); // fix } private function bar() { new class { public function __UnSet($a) // fix { $b = null === $a ? $b->__UnSet($a) // fix : $a->__UnSet($a) // fix ; return $b; } }; } } function __ISSET($bar){} // do not fix $a->__UnSet($foo); // fix ', ]; yield [ '<?php $foo->__invoke(1, );', '<?php $foo->__INVOKE(1, );', ]; yield [ '<?php __Tostring();', ]; yield [ '<?php function __Tostring() {}', ]; yield [ '<?php #->__sleep() /** ->__sleep() */ echo $a->__sleep; ', ]; yield [ '<?php class B { public function _not_magic() { } } ', ]; yield [ '<?php function __alsoNotMagic() { } ', ]; yield [ '<?php function __() { } ', ]; yield [ '<?php function a() { } ', ]; yield [ '<?php $a->__not_magic(); ', ]; yield [ '<?php $a->a(); ', ]; yield [ '<?php A\B\__callstatic(); echo $a->b;', ]; } /** * @requires PHP 8.0 */ public function testFix80(): void { $this->doTest( '<?php $foo?->__invoke(1, );', '<?php $foo?->__INVOKE(1, );', ); } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, string}> */ public static function provideFix81Cases(): iterable { yield 'static call to "__set_state".' => [ '<?php $f = Foo::__set_state(...);', '<?php $f = Foo::__set_STATE(...);', ]; yield 'isset' => [ '<?php $a->__isset(...);', '<?php $a->__ISSET(...);', ]; yield 'enum' => [ '<?php enum Foo { public static function __callStatic(string $method, array $parameters){ echo $method;} } Foo::test();', '<?php enum Foo { public static function __CALLStatic(string $method, array $parameters){ echo $method;} } Foo::test();', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixerTest.php
tests/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Fixer\DeprecatedFixerInterface; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\NativeFunctionTypeDeclarationCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\NativeFunctionTypeDeclarationCasingFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NativeFunctionTypeDeclarationCasingFixerTest extends AbstractFixerTestCase { public function testFunctionIsDeprecatedProperly(): void { $fixer = $this->fixer; self::assertInstanceOf(DeprecatedFixerInterface::class, $fixer); self::assertSame( ['native_type_declaration_casing'], $fixer->getSuccessorsNames(), ); } /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield from NativeTypeDeclarationCasingFixerTest::provideFixCases(); } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/NativeFunctionCasingFixerTest.php
tests/Fixer/Casing/NativeFunctionCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\NativeFunctionCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\NativeFunctionCasingFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NativeFunctionCasingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php namespace Bar { function STRLEN($str) { return "overridden" . \strlen($str); } } namespace { echo \Bar\STRLEN("xxx"); }', ]; yield [ '<?php echo strtolower("hello 1"); ', '<?php echo STRTOLOWER("hello 1"); ', ]; yield [ '<?php echo strtolower //a ("hello 2"); ', '<?php echo STRTOLOWER //a ("hello 2"); ', ]; yield [ '<?php echo strtolower /**/ ("hello 3"); ', '<?php echo STRTOLOWER /**/ ("hello 3"); ', ]; yield [ '<?php echo \sqrt(4); ', '<?php echo \sQrT(4); ', ]; yield [ '<?php echo "1".\sqrt("hello 5"); ', '<?php echo "1".\SQRT("hello 5"); ', ]; yield [ '<?php class Test{ public function gettypE() { return 1; } function sqrT($a) { } function &END($a) { } } ', ]; yield [ '<?php new STRTOLOWER(); ', ]; yield [ '<?php new \STRTOLOWER(); ', ]; yield [ '<?php new \A\B\STRTOLOWER(); ', ]; yield [ '<?php a::STRTOLOWER(); ', ]; yield [ '<?php $a->STRTOLOWER(); ', ]; yield [ '<?php fOoO();', ]; yield [ '<?php namespace Foo { function &Next() { return prev(-1); } }', ]; yield [ '<?php $next1 = & next($array1); $next2 = & \next($array2); ', '<?php $next1 = & Next($array1); $next2 = & \Next($array2); ', ]; yield [ '<?php namespace Foo; use function MyStuff\StrToLower; class Bar { public function getName() { return StrToLower($this->name); } }', ]; yield [ '<?php echo \sqrt(4 , ); ', '<?php echo \sQrT(4 , ); ', ]; yield [ '<?php $a->STRTOLOWER(1,); ', ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected): void { $this->doTest($expected); } /** * @return iterable<int, array{string}> */ public static function provideFix80Cases(): iterable { yield ['<?php $a?->STRTOLOWER(1,);']; yield [ '<?php final class SomeClass { #[File(mimeTypes: ["application/pdf", "image/*"])] public FileBlob $attachment; } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ClassReferenceNameCasingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $a = new Exception; $b = new \Exception; $c = new Exception(); $d = new \Exception(); $e = "a".Exception::class; $f = "a".\Exception::class; $g .= "exception"; echo \Exception::class; print(Exception::class); // $a = new exception(); /** $a = new exception(); */ ', '<?php $a = new exception; $b = new \exception; $c = new exception(); $d = new \exception(); $e = "a".exception::class; $f = "a".\exception::class; $g .= "exception"; echo \exception::class; print(exception::class); // $a = new exception(); /** $a = new exception(); */ ', ]; yield [ '<?php namespace Foo { $a = new exception; $b = new \Exception; }', '<?php namespace Foo { $a = new exception; $b = new \exception; }', ]; yield [ '<?php namespace Foo; $a = new exception; $b = new \Exception; ', '<?php namespace Foo; $a = new exception; $b = new \EXCEPTION; ', ]; yield [ '<?php $a = exception(); $b = new A\exception; $c = new A\B\C\exception; $a1 = \exception(); $b1 = new \A\exception; $c1 = new \A\B\C\exception; ', ]; yield [ '<?php class Foo extends Exception {};', '<?php class Foo extends exception {};', ]; yield [ '<?php class exception {}; new foO();', ]; yield [ '<?php interface exception {};', ]; yield [ '<?php trait exception {};', ]; yield [ '<?php function exception() {};', ]; yield [ '<?php const exception = "abc";', ]; yield [ '<?php $a = Foo::exception;', ]; yield [ '<?php $a = $foo->exception;', ]; yield [ '<?php use Foo as exception;', ]; yield [ '<?php class Foo { use exception; }', ]; yield [ '<?php $foo = ["const" => exception];', ]; yield [ '<?php namespace { $a = new Exception; $b = new \Exception; } namespace Bar { $a = new exception; $b = new \Exception; } namespace Foo { $a = new exception; $b = new \Exception; $c = new foO(); }', '<?php namespace { $a = new exception; $b = new \exception; } namespace Bar { $a = new exception; $b = new \exception; } namespace Foo { $a = new exception; $b = new \exception; $c = new foO(); }', ]; yield [ '<?php use Exception as baR;', '<?php use exception as baR;', ]; yield [ '<?php try { foo(); } catch(\LogicException $e) {}', '<?php try { foo(); } catch(\logicexception $e) {}', ]; yield [ '<?php try { foo(); } catch(LogicException $e) {}', '<?php try { foo(); } catch(logicexception $e) {}', ]; yield [ '<?php Closure::bind(fn () => null, null, new class {}); \Closure::bind(fn () => null, null, new class {}); Foo\Bar::bind(fn () => null, null, new class {}); \A\B\Bar::bind(fn () => null, null, new class {}); ', '<?php CLOSURE::bind(fn () => null, null, new class {}); \CLOSURE::bind(fn () => null, null, new class {}); Foo\Bar::bind(fn () => null, null, new class {}); \A\B\Bar::bind(fn () => null, null, new class {}); ', ]; yield [ "<?php declare(strict_types=1); use Sonata\\Exporter\\Writer\\EXCEPTION; \$services->set('sonata.exporter.writer.xml', EXCEPTION::class); ", ]; yield [ '<?php const error = 1; foo(error); $b2 = $a[error]; $b21 = [1,error]; $b22 = [error,2]; $b23 = [1,error,2]; $b24 = [1,error,2,]; $b3 = [error]; $b4 = $a->{error}; ', ]; yield [ '<?php foo(\error); $b2 = $a[\error]; $b21 = [1,\error]; $b22 = [\error,2]; $b23 = [1,\error,2]; $b24 = [1,\error,2,]; $b3 = [\error]; $b4 = $a->{\error}; ', ]; yield ['<?php echo error ?><?php echo error;']; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFix80Cases(): iterable { yield [ '<?php if ($var?->exception instanceof Exception) {};', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php enum exception {}', ]; yield [ '<?php enum Foo { case exception; }', ]; yield 'multiple type catch with variable' => [ '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}', '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception $e) {}', ]; yield 'multiple type catch without variable 3' => [ '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}', '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception) {}', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/MagicConstantCasingFixerTest.php
tests/Fixer/Casing/MagicConstantCasingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer> * * @author ntzm * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class MagicConstantCasingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php echo __LINE__;', '<?php echo __line__;', ]; yield [ '<?php echo __FILE__;', '<?php echo __FILe__;', ]; yield [ '<?php echo __DIR__;', '<?php echo __dIr__;', ]; yield [ '<?php echo __FUNCTION__;', '<?php echo __fUncTiOn__;', ]; yield [ '<?php echo __CLASS__;', '<?php echo __clasS__;', ]; yield [ '<?php echo __METHOD__;', '<?php echo __mEthoD__;', ]; yield [ '<?php echo __NAMESPACE__;', '<?php echo __namespace__;', ]; yield [ '<?php echo __TRAIT__;', '<?php echo __trait__;', ]; yield [ '<?php echo __TRAIT__;', '<?php echo __trAIt__;', ]; yield [ '<?php echo Exception::class;', '<?php echo Exception::CLASS;', ]; yield [ '<?php echo Exception::class;', '<?php echo Exception::ClAss;', ]; } /** * @requires PHP <8.0 * * @dataProvider provideFixPre80Cases */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php class Bar { const __line__ = "foo"; } namespace { echo \Bar::__line__; }', ]; } /** * @requires PHP 8.4 * * @dataProvider provideFix84Cases */ public function testFix84(string $expected, ?string $input = null): void { $this->testFix($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFix84Cases(): iterable { yield [ '<?php echo __PROPERTY__;', '<?php echo __property__;', ]; yield [ '<?php echo __PROPERTY__;', '<?php echo __PrOpErTy__;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Casing/IntegerLiteralCaseFixerTest.php
tests/Fixer/Casing/IntegerLiteralCaseFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Casing; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Casing\IntegerLiteralCaseFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\IntegerLiteralCaseFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class IntegerLiteralCaseFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $foo1 = 0xFF; $foo2 = 0xDEFA; $foo3 = 0xFA; $foo4 = 0xFA;', '<?php $foo1 = 0XFF; $foo2 = 0xdefa; $foo3 = 0Xfa; $foo4 = 0xFA;', ]; yield [ '<?php $foo = 0xA1FB20;', '<?php $foo = 0xa1fb20;', ]; yield [ '<?php $foo = -0xA1FB20;', '<?php $foo = -0xa1fb20;', ]; yield [ '<?php $foo = 0b1101;', '<?php $foo = 0B1101;', ]; yield [ '<?php $A = 1_234_567;', ]; yield [ '<?php $A = +0xAA_FF_00;', '<?php $A = +0Xaa_ff_00;', ]; yield [ '<?php $A = -0x00_AA_FF_00;', '<?php $A = -0X00_aa_ff_00;', ]; yield 'bin_PHP_INT_MAX' => [ '<?php $foo = 0b111111111111111111111111111111111111111111111111111111111111111;', '<?php $foo = 0B111111111111111111111111111111111111111111111111111111111111111;', ]; yield 'hex_plus_PHP_INT_MAX' => [ '<?php $foo = +0x7FFFFFFFFFFFFFFF;', '<?php $foo = +0X7fffffffffffffff;', ]; yield 'hex_minus_PHP_INT_MAX' => [ '<?php $foo = -0x7FFFFFFFFFFFFFFF;', '<?php $foo = -0X7fffffffffffffff;', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php $foo = 0o123;', '<?php $foo = 0O123;', ]; yield [ '<?php $foo = -0o123;', '<?php $foo = -0O123;', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/AttributeNotation/OrderedAttributesFixerTest.php
tests/Fixer/AttributeNotation/OrderedAttributesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\AttributeNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer * * @requires PHP 8.0 * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer> * * @author HypeMC <hypemc@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedAttributesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidConfigurationCases * * @param array<string, mixed> $configuration */ public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void { self::expectException(InvalidFixerConfigurationException::class); self::expectExceptionMessage($expectedExceptionMessage); $this->fixer->configure($configuration); } /** * @return iterable<string, array{array<string, mixed>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'Custom order strategy without `order` option' => [ ['sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM], 'The custom order strategy requires providing `order` option with a list of attributes\'s FQNs.', ]; yield 'Custom order strategy with empty `order` option' => [ [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => [], ], 'The custom order strategy requires providing `order` option with a list of attributes\'s FQNs.', ]; yield 'Non unique attributes throw an exception' => [ [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Bar', 'Test\Corge', 'A\B\Bar'], ], 'The list includes attributes that are not unique.', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'Alpha on various declarations' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] class X { #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] const Y = 1; #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] public $y; #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] public function y() {} } #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] function f( #[AB\Baz(prop: \'baz\')] #[BarAlias(3)] #[Foo(4, \'baz qux\')] string $param1, #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[Corge] string $param2 ) {} function f2(#[BarAlias(3)] #[Foo(4, \'baz qux\')] string $param1, #[\A\B\Qux()] #[Corge] string $param2) {} $anon = #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] function () {}; $short = #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] fn () => null; ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] class X { #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] const Y = 1; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] public $y; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] public function y() {} } #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f( #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] string $param1, #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] string $param2 ) {} function f2(#[Foo(4, \'baz qux\')] #[BarAlias(3)] string $param1, #[\A\B\Qux()] #[Corge] string $param2) {} $anon = #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function () {}; $short = #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] fn () => null; ', ]; yield 'Explicit in namespace' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[BarAlias(3)] #[Corge] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[\A\B\Qux()] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Explicit in global namespace' => [ '<?php use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[BarAlias(3)] #[Corge] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[\A\B\Qux()] function f() {} ', '<?php use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Quux', 'A\B\Bar', 'Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Explicit with unconfigured attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[Corge] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Baz', 'A\B\Qux'], ], ]; yield 'Multiple namespaces' => [ '<?php namespace Test { use A\B\Foo; use A\B\Bar as BarAlias; #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] function f() {} } namespace Test2 { use A\B\Bar as BarAlias; use A\B as AB; #[BarAlias(3)] #[\A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[AB\Baz(prop: \'baz\')] function f() {} } namespace Test { use A\B\Foo; use A\B\Bar as BarAlias2; #[BarAlias2(3)] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] function f2() {} } namespace { use A\B\Foo; use A\B\Bar as BarAlias3; #[BarAlias3(3)] #[Foo(4, \'baz qux\')] #[AB\Baz(prop: \'baz\')] function f() {} } ', '<?php namespace Test { use A\B\Foo; use A\B\Bar as BarAlias; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias(3)] function f() {} } namespace Test2 { use A\B\Bar as BarAlias; use A\B as AB; #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function f() {} } namespace Test { use A\B\Foo; use A\B\Bar as BarAlias2; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias2(3)] function f2() {} } namespace { use A\B\Foo; use A\B\Bar as BarAlias3; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias3(3)] function f() {} } ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Bar', 'Test\AB\Baz', 'A\B\Quux', 'A\B\Baz', 'A\B\Foo', 'AB\Baz'], ], ]; yield 'With whitespaces' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz (prop: \'baz\') ] #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') ] #[ \A\B\Qux() ] #[ BarAlias (3) ] #[ Corge ] #[ Foo (4, \'baz qux\') ] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ Foo (4, \'baz qux\') ] #[ BarAlias (3) ] #[ AB\Baz (prop: \'baz\') ] #[ \A\B\Qux() ] #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') ] #[ Corge ] function f() {} ', ]; yield 'With docblock' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; /** * Start docblock */ /** * AB\Baz docblock */ #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] /** * Corge docblock */ #[Corge] #[Foo(4, \'baz qux\')] /** * End docblock */ class X {} function f2(/** Start docblock */#[\A\B\Qux()] #[BarAlias(3)] /** Corge docblock */#[Corge] #[Foo(4, \'baz qux\')] /** End docblock */string $param) {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; /** * Start docblock */ #[Foo(4, \'baz qux\')] #[BarAlias(3)] /** * AB\Baz docblock */ #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] /** * Corge docblock */ #[Corge] /** * End docblock */ class X {} function f2(/** Start docblock */#[Foo(4, \'baz qux\')] #[BarAlias(3)] #[\A\B\Qux()] /** Corge docblock */#[Corge] /** End docblock */string $param) {} ', ]; yield 'With comments' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[/* comment */A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') /* comment */] #[ /* comment */ BarAlias/* comment */(3)/* comment */] #[/* comment */ Corge/* comment */] #[/* comment */AB\Baz /* comment */ (prop: \'baz\') /* comment */ ] #[/* comment */Foo/* comment */(4, \'baz qux\') /* comment */ ] #[ /* comment */ \A\B\Qux()/* comment */] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[/* comment */Foo/* comment */(4, \'baz qux\') /* comment */ ] #[ /* comment */ BarAlias/* comment */(3)/* comment */] #[/* comment */AB\Baz /* comment */ (prop: \'baz\') /* comment */ ] #[ /* comment */ \A\B\Qux()/* comment */] #[/* comment */A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') /* comment */] #[/* comment */ Corge/* comment */] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Alpha with multiple attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge, Foo(4, \'baz qux\'), ] class X { #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge,Foo(4, \'baz qux\')] public function y() {} } ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge, ] class X { #[Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge] public function y() {} } ', ]; yield 'Explicit with multiple attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), BarAlias(3), Corge, AB\Baz(prop: \'baz\'), \A\B\Qux(), Foo(4, \'baz qux\'), ] class X { #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), BarAlias(3), Corge, AB\Baz(prop: \'baz\'), \A\B\Qux(),Foo(4, \'baz qux\')] public function y() {} } ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge, ] class X { #[Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge] public function y() {} } ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Qux'], ], ]; yield 'Multiline with no trailing comma' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge, Foo(4, \'baz qux\') ] class X {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge ] class X {} ', ]; yield 'Multiple with comments' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ /* * AB\Baz comment */ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), /* * Corge comment */ Corge, /** * Foo docblock */ Foo(4, \'baz qux\'), ] class X { #[ /* AB\Baz comment */AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), /* Corge comment */Corge,/** Foo docblock */Foo(4, \'baz qux\')] public function y() {} } ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ /** * Foo docblock */ Foo(4, \'baz qux\'), BarAlias(3), /* * AB\Baz comment */ AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), /* * Corge comment */ Corge, ] class X { #[/** Foo docblock */Foo(4, \'baz qux\'), BarAlias(3), /* AB\Baz comment */AB\Baz(prop: \'baz\'), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), /* Corge comment */Corge] public function y() {} } ', ]; yield 'Alpha with mixed multiple attribute declarations and attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[AB\Baz(prop: \'baz\')] #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'),Corge] #[ \A\B\Qux(), BarAlias(3),Foo(4, \'baz qux\')] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\'), \A\B\Qux(), BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[Corge, A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function f() {} ', ]; yield 'Explicit with mixed multiple attribute declarations and attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'),Corge] #[ BarAlias(3), \A\B\Qux(),Foo(4, \'baz qux\')] #[AB\Baz(prop: \'baz\')] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\'), \A\B\Qux(), BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[Corge, A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Qux'], ], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null, array $configuration = []): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<string, array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix81Cases(): iterable { yield 'Alpha with nested attribute' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[AB\Baz(prop: new P\R())] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] #[Corge] #[Foo(4, \'baz qux\')] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: new P\R())] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f() {} ', ]; yield 'Explicit multiple with nested attribute' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), BarAlias(3), Corge, AB\Baz(prop: new P\R()), Foo(4, \'baz qux\'), \A\B\Qux(), ] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ Foo(4, \'baz qux\'), BarAlias(3), AB\Baz(prop: new P\R()), \A\B\Qux(), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), Corge, ] function f() {} ', [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/AttributeNotation/GeneralAttributeRemoveFixerTest.php
tests/Fixer/AttributeNotation/GeneralAttributeRemoveFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\AttributeNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AttributeNotation\GeneralAttributeRemoveFixer * * @requires PHP 8.0 * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\AttributeNotation\GeneralAttributeRemoveFixer * * @phpstan-type _InputConfiguration array{ * attributes?: list<string>, * } * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\AttributeNotation\GeneralAttributeRemoveFixer> * * @author Raffaele Carelle <raffaele.carelle@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class GeneralAttributeRemoveFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: string, 2?: _InputConfiguration}> */ public static function provideFixCases(): iterable { yield 'Explicit in namespace' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[Corge] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[Foo(4, \'baz qux\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Qux()] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[Corge] function f() {} ', [ 'attributes' => ['A\B\Bar', 'Test\A\B\Quux'], ], ]; yield 'Explicit in global namespace' => [ '<?php use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] function f() {} ', '<?php use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[\A\B\Qux()] #[Corge] function f() {} ', [ 'attributes' => ['\A\B\Qux', '\Corge', 'A\B\Bar'], ], ]; yield 'Multiple namespaces' => [ '<?php namespace Test { use A\B\Foo; use A\B\Bar as BarAlias; function f() {} } namespace Test2 { use A\B\Bar as BarAlias; use A\B as AB; #[\A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function f() {} } namespace Test { use A\B\Foo; use A\B\Bar as BarAlias2; function f2() {} } namespace { use A\B\Foo; use A\B\Bar as BarAlias3; function f() {} } ', '<?php namespace Test { use A\B\Foo; use A\B\Bar as BarAlias; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias(3)] function f() {} } namespace Test2 { use A\B\Bar as BarAlias; use A\B as AB; #[BarAlias(3)] #[AB\Baz(prop: \'baz\')] #[\A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function f() {} } namespace Test { use A\B\Foo; use A\B\Bar as BarAlias2; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias2(3)] function f2() {} } namespace { use A\B\Foo; use A\B\Bar as BarAlias3; #[AB\Baz(prop: \'baz\')] #[Foo(4, \'baz qux\')] #[BarAlias3(3)] function f() {} } ', [ 'attributes' => ['A\B\Bar', 'Test\AB\Baz', 'A\B\Quux', 'A\B\Baz', 'A\B\Foo', '\AB\Baz'], ], ]; yield 'With whitespaces' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ \A\B\Qux() ] #[ BarAlias (3) ] #[ Corge ] function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz (prop: \'baz\') ] #[ A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') ] #[ \A\B\Qux() ] #[ BarAlias (3) ] #[ Corge ] #[ Foo (4, \'baz qux\') ] function f() {} ', [ 'attributes' => ['A\B\Foo', 'Test\A\B\Quux', 'A\B\Baz'], ], ]; yield 'With docblock' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; /** * Start docblock */ /** * AB\Baz docblock */ #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[BarAlias(3)] #[Foo(4, \'baz qux\')] /** * End docblock */ class X {} function f2(/** Start docblock */#[Foo(4, \'baz qux\')] #[BarAlias(3)] /** End docblock */string $param) {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; /** * Start docblock */ /** * AB\Baz docblock */ #[AB\Baz(prop: \'baz\')] #[A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Qux()] #[BarAlias(3)] /** * Corge docblock */ #[Corge] #[Foo(4, \'baz qux\')] /** * End docblock */ class X {} function f2(/** Start docblock */#[Foo(4, \'baz qux\')] #[BarAlias(3)] #[\A\B\Qux()] /** Corge docblock */#[Corge] /** End docblock */string $param) {} ', [ 'attributes' => ['Test\Corge', '\A\B\Qux'], ], ]; yield 'With comments' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; function f() {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[/* comment */A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\') /* comment */] #[ /* comment */ BarAlias/* comment */(3)/* comment */] #[/* comment */ Corge/* comment */] #[/* comment */AB\Baz /* comment */ (prop: \'baz\') /* comment */ ] #[/* comment */Foo/* comment */(4, \'baz qux\') /* comment */ ] #[ /* comment */ \A\B\Qux()/* comment */] function f() {} ', [ 'attributes' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', '\A\B\Qux'], ], ]; yield 'With multiple attributes' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), BarAlias(3), Corge ] class X { #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), BarAlias(3), Corge] public function y() {} } ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge, Foo(4, \'baz qux\'), ] class X { #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge,Foo(4, \'baz qux\')] public function y() {} } ', [ 'attributes' => ['A\B\Foo', '\A\B\Qux'], ], ]; yield 'Multiline with no trailing comma' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; class X {} ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), Corge, Foo(4, \'baz qux\') ] class X {} ', [ 'attributes' => ['A\B\Foo', '\A\B\Qux', 'A\B\Baz', 'Test\A\B\Quux', 'A\B\Bar', 'Test\Corge'], ], ]; yield 'Multiple with comments' => [ '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ /* * AB\Baz comment */ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3) ] class X { #[ /* AB\Baz comment */AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3)] public function y() {} } ', '<?php namespace Test; use A\B\Foo; use A\B\Bar as BarAlias; use A\B as AB; #[ /* * AB\Baz comment */ AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), /* * Corge comment */ Corge, /** * Foo docblock */ Foo(4, \'baz qux\'), ] class X { #[ /* AB\Baz comment */AB\Baz(prop: \'baz\'), A\B\Quux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), \A\B\Qux(), BarAlias(3), /* Corge comment */Corge,/** Foo docblock */Foo(4, \'baz qux\')] public function y() {} } ', [ 'attributes' => ['A\B\Foo', 'Test\Corge'], ], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/AttributeNotation/AttributeEmptyParenthesesFixerTest.php
tests/Fixer/AttributeNotation/AttributeEmptyParenthesesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\AttributeNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\AttributeNotation\AttributeEmptyParenthesesFixer * * @requires PHP 8.0 * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\AttributeNotation\AttributeEmptyParenthesesFixer> * * @author HypeMC <hypemc@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\AttributeNotation\AttributeEmptyParenthesesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class AttributeEmptyParenthesesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'Without parentheses on various declarations' => [ '<?php namespace Test; #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] class X { #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] const Y = 1; #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public $y; #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public function y() {} } #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] function f( #[\A\B\Foo] #[\Bar ] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] string $param, ) {} $anon = #[\A\B\Foo] #[\Bar ] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function () {}; $short = #[\A\B\Foo] #[\Bar ] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] fn () => null; ', '<?php namespace Test; #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] class X { #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] const Y = 1; #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public $y; #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public function y() {} } #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] function f( #[\A\B\Foo()] #[\Bar( )] #[Baz] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] string $param, ) {} $anon = #[\A\B\Foo()] #[\Bar( )] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function () {}; $short = #[\A\B\Foo()] #[\Bar( )] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] fn () => null; ', ]; yield 'With parentheses on various declarations' => [ '<?php namespace Test; #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] class X { #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] const Y = 1; #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public $y; #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public function y() {} } #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] function f( #[\A\B\Foo()] #[\Bar()] #[Baz()] #[\Bar(), Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] string $param, ) {} $anon = #[\A\B\Foo()] #[\Bar()] #[Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function () {}; $short = #[\A\B\Foo()] #[\Bar()] #[Baz()] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] fn () => null; ', '<?php namespace Test; #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] class X { #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] const Y = 1; #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public $y; #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] public function y() {} } #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] function f( #[\A\B\Foo()] #[\Bar] #[Baz] #[\Bar, Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\'), ] string $param, ) {} $anon = #[\A\B\Foo()] #[\Bar] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] function () {}; $short = #[\A\B\Foo()] #[\Bar] #[Baz] #[Corge(4, \'baz qux\')] #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\')] fn () => null; ', ['use_parentheses' => true], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null, array $configuration = []): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFix81Cases(): iterable { yield 'Without parentheses' => [ '<?php namespace Test; #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[\A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[ \A\B\Foo, \Bar , Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R()), ] class X {} ', '<?php namespace Test; #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[\A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[ \A\B\Foo(), \Bar( ), Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R()), ] class X {} ', ]; yield 'With parentheses' => [ '<?php namespace Test; #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[\A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[ \A\B\Foo(), \Bar(), Baz(), Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R()), ] class X {} ', '<?php namespace Test; #[A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[\A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R())] #[ \A\B\Foo(), \Bar, Baz, Corge(4, \'baz qux\'), A\B\Qux(prop1: [1, 2, 4], prop2: true, prop3: \'foo bar\', prop4: new P\R()), ] class X {} ', ['use_parentheses' => true], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/ModernizeTypesCastingFixerTest.php
tests/Fixer/CastNotation/ModernizeTypesCastingFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\AbstractFunctionReferenceFixer * @covers \PhpCsFixer\Fixer\CastNotation\ModernizeTypesCastingFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\ModernizeTypesCastingFixer> * * @author Vladimir Reznichenko <kalessil@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ModernizeTypesCastingFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { $multiLinePatternToFix = <<<'FIX' <?php $x = intval ( mt_rand ( 0, 100 ) ) ; FIX; $multiLinePatternFixed = <<<'FIXED' <?php $x = (int) ( mt_rand ( 0, 100 ) ) ; FIXED; $overriddenFunction = <<<'OVERRIDDEN' <?php class overridesIntval { public function intval($x) { return \intval($x); } public function usesInval() { // that's why it is risky return intval(mt_rand(0, 100)); } } OVERRIDDEN; $overriddenFunctionFixed = <<<'OVERRIDDEN' <?php class overridesIntval { public function intval($x) { return (int) $x; } public function usesInval() { // that's why it is risky return (int) (mt_rand(0, 100)); } } OVERRIDDEN; yield ['<?php $x = "intval";']; yield ['<?php $x = ClassA::intval(mt_rand(0, 100));']; yield ['<?php $x = ScopeA\intval(mt_rand(0, 100));']; yield ['<?php $x = namespace\intval(mt_rand(0, 100));']; yield ['<?php $x = $object->intval(mt_rand(0, 100));']; yield ['<?php $x = new \intval(mt_rand(0, 100));']; yield ['<?php $x = new intval(mt_rand(0, 100));']; yield ['<?php $x = new ScopeB\intval(mt_rand(0, 100));']; yield ['<?php intvalSmth(mt_rand(0, 100));']; yield ['<?php smth_intval(mt_rand(0, 100));']; yield ['<?php "SELECT ... intval(mt_rand(0, 100)) ...";']; yield ['<?php "test" . "intval" . "in concatenation";']; yield ['<?php $x = intval($x, 16);']; yield ['<?php $x = intval($x, $options["base"]);']; yield ['<?php $x = intval($x, $options->get("base", 16));']; yield ['<?php $x = (int) $x;', '<?php $x = intval($x);']; yield ['<?php $x = (float) $x;', '<?php $x = floatval($x);']; yield ['<?php $x = (float) $x;', '<?php $x = doubleval($x);']; yield ['<?php $x = (string) $x;', '<?php $x = strval($x);']; yield ['<?php $x = (bool) $x;', '<?php $x = boolval ( $x );']; yield ['<?php $x = (int) (mt_rand(0, 100));', '<?php $x = intval(mt_rand(0, 100));']; yield ['<?php $x = (int) (mt_rand(0, 100));', '<?php $x = \intval(mt_rand(0, 100));']; yield ['<?php $x = (int) (mt_rand(0, 100)).".dist";', '<?php $x = intval(mt_rand(0, 100)).".dist";']; yield ['<?php $x = (int) (mt_rand(0, 100)).".dist";', '<?php $x = \intval(mt_rand(0, 100)).".dist";']; yield [$multiLinePatternFixed, $multiLinePatternToFix]; yield [$overriddenFunctionFixed, $overriddenFunction]; yield [ '<?php $a = (string) ($b . $c);', '<?php $a = strval($b . $c);', ]; yield [ '<?php $x = /**/(int) /**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;', '<?php $x = /**/intval/**/ /** x*/(/**//** */mt_rand(0, 100)/***/)/*xx*/;', ]; yield [ '<?php $x = (string) ((int) ((int) $x + (float) $x));', '<?php $x = strval(intval(intval($x) + floatval($x)));', ]; yield [ '<?php intval();intval(1,2,3);', ]; yield [ '<?php interface Test { public function floatval($a); public function &doubleval($a); }', ]; yield [ '<?php $foo = ((int) $x)**2;', '<?php $foo = intval($x)**2;', ]; yield [ '<?php $foo = ((string) $x)[0];', '<?php $foo = strval($x)[0];', ]; yield [ '<?php $foo = ((string) ($x + $y))[0];', '<?php $foo = strval($x + $y)[0];', ]; yield [ '<?php $a = (int) $b;', '<?php $a = intval($b, );', ]; yield [ '<?php $a = (int) $b;', '<?php $a = intval($b , );', ]; yield [ '<?php $a = (string) ($b . $c);', '<?php $a = strval($b . $c, );', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php $foo = ((string) ($x + $y)){0};', '<?php $foo = strval($x + $y){0};', ]; yield [ '<?php $a = # # # (int) # ( # $b# )# ;#', '<?php $a = # # \ # intval# ( # $b# )# ;#', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php $x = intval(...);', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/NoShortBoolCastFixerTest.php
tests/Fixer/CastNotation/NoShortBoolCastFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\CastNotation\NoShortBoolCastFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\NoShortBoolCastFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoShortBoolCastFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixCases(): iterable { yield [ '<?php $c = // lala // cc (bool)$content;', '<?php $c = ! // lala // cc !$content;', ]; yield [ '<?php $a = \'0\'; $b = /* */(bool)$a;', '<?php $a = \'0\'; $b = !/* */!$a;', ]; yield [ '<?php function foo($a, $b) { $c = (bool)$a; $d = !$a; $d1 = ! $a; $d2 = !$a; $b = !(!$foo); echo \'!!\'; // !! ! ! $c = (bool) $b; $e = (bool) $d1; return (bool) $a; } ', '<?php function foo($a, $b) { $c = !!$a; $d = !$a; $d1 = ! $a; $d2 = !$a; $b = !(!$foo); echo \'!!\'; // !! ! ! $c = ! ! $b; $e = ! ! $d1; return !! $a; } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/CastSpacesFixerTest.php
tests/Fixer/CastNotation/CastSpacesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\CastSpacesFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class CastSpacesFixerTest extends AbstractFixerTestCase { /** * @param array<string, mixed> $config * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfiguration(array $config, string $expectedMessage): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($expectedMessage); $this->fixer->configure($config); } /** * @return iterable<string, array{array<string, int|string>, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'missing key' => [ ['a' => 1], '#^\[cast_spaces\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#', ]; yield 'invalid value' => [ ['space' => 'double'], '#^\[cast_spaces\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php echo "( int ) $foo";', ]; yield [ '<?php $bar = (int) $foo;', '<?php $bar = ( int)$foo;', ]; yield [ '<?php $bar = (int) $foo;', '<?php $bar = ( int)$foo;', ]; yield [ '<?php $bar = (int) $foo;', '<?php $bar = (int) $foo;', ]; yield [ '<?php $bar = (string) (int) $foo;', '<?php $bar = ( string )( int )$foo;', ]; yield [ '<?php $bar = (string) (int) $foo;', '<?php $bar = (string)(int)$foo;', ]; yield [ '<?php $bar = (string) (int) $foo;', '<?php $bar = ( string ) ( int )$foo;', ]; yield [ '<?php $bar = (string) $foo;', '<?php $bar = ( string ) $foo;', ]; yield [ '<?php $bar = (float) Foo::bar();', '<?php $bar = (float )Foo::bar();', ]; yield [ '<?php $bar = Foo::baz((float) Foo::bar());', '<?php $bar = Foo::baz((float )Foo::bar());', ]; yield [ '<?php $bar = $query["params"] = (array) $query["params"];', '<?php $bar = $query["params"] = (array)$query["params"];', ]; yield [ "<?php \$bar = (int)\n \$foo;", ]; yield [ "<?php \$bar = (int)\r\n \$foo;", ]; yield [ '<?php echo "( int ) $foo";', null, ['space' => 'none'], ]; yield [ '<?php $bar = (int)$foo;', '<?php $bar = ( int)$foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (int)$foo;', '<?php $bar = ( int)$foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (int)$foo;', '<?php $bar = (int) $foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (string)(int)$foo;', '<?php $bar = ( string )( int )$foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (string)(int)$foo;', null, ['space' => 'none'], ]; yield [ '<?php $bar = (string)(int)$foo;', '<?php $bar = ( string ) ( int )$foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (string)$foo;', '<?php $bar = ( string ) $foo;', ['space' => 'none'], ]; yield [ '<?php $bar = (float)Foo::bar();', '<?php $bar = (float )Foo::bar();', ['space' => 'none'], ]; yield [ '<?php $bar = Foo::baz((float)Foo::bar());', '<?php $bar = Foo::baz((float )Foo::bar());', ['space' => 'none'], ]; yield [ '<?php $bar = $query["params"] = (array)$query["params"];', null, ['space' => 'none'], ]; yield [ '<?php $bar = (int)$foo;', "<?php \$bar = (int)\n \$foo;", ['space' => 'none'], ]; yield [ '<?php $bar = (int)$foo;', "<?php \$bar = (int)\r\n \$foo;", ['space' => 'none'], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix85Cases * * @requires PHP 8.5 */ public function testFix85(string $expected, ?string $input = null, array $configuration = []): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}> */ public static function provideFix85Cases(): iterable { yield [ '<?php (void) foo();', '<?php (void) foo();', ]; yield [ '<?php (void)foo();', '<?php (void) foo();', ['space' => 'none'], ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/LowercaseCastFixerTest.php
tests/Fixer/CastNotation/LowercaseCastFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\CastNotation\LowercaseCastFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\LowercaseCastFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LowercaseCastFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}> */ public static function provideFixCases(): iterable { $types = ['boolean', 'bool', 'integer', 'int', 'double', 'float', 'string', 'array', 'object', 'binary']; foreach ($types as $from) { yield from self::createCasesFor($from); } } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}> */ public static function provideFixPre80Cases(): iterable { yield from self::createCasesFor('unset'); yield from self::createCasesFor('real'); } /** * @return iterable<array{0: non-empty-string, 1?: non-empty-string}> */ private static function createCasesFor(string $type): iterable { yield [ \sprintf('<?php $b= (%s)$d;', $type), \sprintf('<?php $b= (%s)$d;', strtoupper($type)), ]; yield [ \sprintf('<?php $b=( %s) $d;', $type), \sprintf('<?php $b=( %s) $d;', ucfirst($type)), ]; yield [ \sprintf('<?php $b=(%s ) $d;', $type), \sprintf('<?php $b=(%s ) $d;', strtoupper($type)), ]; yield [ \sprintf('<?php $b=( %s ) $d;', $type), \sprintf('<?php $b=( %s ) $d;', ucfirst($type)), ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/NoUnsetCastFixerTest.php
tests/Fixer/CastNotation/NoUnsetCastFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\CastNotation\NoUnsetCastFixer * * @requires PHP <8.0 * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\NoUnsetCastFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoUnsetCastFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple form I' => [ "<?php\n\$a = null;", "<?php\n\$a =(unset)\$z;", ]; yield 'simple form II' => [ "<?php\n\$b = null;", "<?php\n\$b = (unset)\$z;", ]; yield 'simple form III' => [ "<?php\n\$c = null?>", "<?php\n\$c = (unset)\$z?>", ]; yield 'lot of spaces' => [ "<?php\n\$d = \t \t \t null;", "<?php\n\$d = \t (unset)\$z\t \t ;", ]; yield 'comments' => [ '<?php #0 $a#1 #2 = null#3 #4 #5 #6 #7 #8 ; ', '<?php #0 $a#1 #2 =#3 #4 (unset)#5 #6 $b#7 #8 ; ', ]; yield [ "<?php\n(unset) \$b;", ]; yield [ '<?php $r = (unset) f(1);', ]; yield [ '<?php $r = (unset) (new C())->mf(3);', ]; yield [ '<?php $r = (unset) $f(1);', ]; yield [ '<?php $r = (unset) $c::sf(2) ?>', ]; yield [ '<?php $r = (unset) $a[0];', ]; yield [ '<?php $r = (unset) $n**f($n);', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/CastNotation/ShortScalarCastFixerTest.php
tests/Fixer/CastNotation/ShortScalarCastFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\CastNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class ShortScalarCastFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) { yield from self::createCasesFor($from, $to); } $types = ['string', 'array', 'object']; foreach ($types as $cast) { yield [\sprintf('<?php $b=(%s) $d;', $cast)]; yield [\sprintf('<?php $b=( %s ) $d;', $cast)]; yield [\sprintf('<?php $b=(%s ) $d;', ucfirst($cast))]; yield [\sprintf('<?php $b=(%s ) $d;', strtoupper($cast))]; } } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string}> */ public static function provideFixPre80Cases(): iterable { yield ['<?php $b=(unset) $d;']; yield ['<?php $b=( unset ) $d;']; yield ['<?php $b=(Unset ) $d;']; yield ['<?php $b=(UNSET ) $d;']; yield from self::createCasesFor('real', 'float'); } /** * @return iterable<array{0: non-empty-string, 1?: non-empty-string}> */ private static function createCasesFor(string $from, string $to): iterable { yield [ \sprintf('<?php echo ( %s )$a;', $to), \sprintf('<?php echo ( %s )$a;', $from), ]; yield [ \sprintf('<?php $b=(%s) $d;', $to), \sprintf('<?php $b=(%s) $d;', $from), ]; yield [ \sprintf('<?php $b= (%s)$d;', $to), \sprintf('<?php $b= (%s)$d;', strtoupper($from)), ]; yield [ \sprintf('<?php $b=( %s) $d;', $to), \sprintf('<?php $b=( %s) $d;', ucfirst($from)), ]; yield [ \sprintf('<?php $b=(%s ) $d;', $to), \sprintf('<?php $b=(%s ) $d;', ucfirst($from)), ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/GlobalNamespaceImportFixerTest.php
tests/Fixer/Import/GlobalNamespaceImportFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer> * * @author Gregor Harlan <gharlan@web.de> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class GlobalNamespaceImportFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{string, null|string, _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'import_constants (true) - non-global names' => [ <<<'EXPECTED' <?php namespace Test; echo FOO, \Bar\BAZ, namespace\FOO2; EXPECTED, null, ['import_constants' => true], ]; yield 'import_constants (true) - name already used [1]' => [ <<<'EXPECTED' <?php namespace Test; echo \FOO, FOO, \FOO; EXPECTED, null, ['import_constants' => true], ]; yield 'import_constants (true) - name already used [2]' => [ <<<'EXPECTED' <?php namespace Test; use const Bar\FOO; echo \FOO; EXPECTED, null, ['import_constants' => true], ]; yield 'import_constants (true) - name already used [3]' => [ <<<'EXPECTED' <?php namespace Test; const FOO = 1; echo \FOO; EXPECTED, null, ['import_constants' => true], ]; yield 'import_constants (true) - without namespace / do not import' => [ <<<'INPUT' <?php echo \FOO, \BAR, \FOO; INPUT, null, ['import_constants' => true], ]; yield 'import_constants (true) - with namespace' => [ <<<'EXPECTED' <?php namespace Test; use const BAR; use const FOO; echo FOO, BAR; EXPECTED, <<<'INPUT' <?php namespace Test; echo \FOO, \BAR; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - with namespace with {} syntax' => [ <<<'EXPECTED' <?php namespace Test { use const BAR; use const FOO; echo FOO, BAR; } EXPECTED, <<<'INPUT' <?php namespace Test { echo \FOO, \BAR; } INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - ignore other imported types' => [ <<<'EXPECTED' <?php namespace Test; use BAR; use const BAR; use const FOO; echo FOO, BAR; EXPECTED, <<<'INPUT' <?php namespace Test; use BAR; echo \FOO, \BAR; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - respect already imported names [1]' => [ <<<'EXPECTED' <?php namespace Test; use const BAR; use const FOO; echo FOO, BAR; EXPECTED, <<<'INPUT' <?php namespace Test; use const BAR; echo \FOO, \BAR; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - respect already imported names [2]' => [ <<<'EXPECTED' <?php namespace Test; use const \BAR; use const FOO; echo FOO, BAR, BAR; EXPECTED, <<<'INPUT' <?php namespace Test; use const \BAR; echo \FOO, \BAR, BAR; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - handle case sensitivity' => [ <<<'EXPECTED' <?php namespace Test; use const fOO; use const FOO; use const Foo; const foO = 1; echo FOO, Foo; EXPECTED, <<<'INPUT' <?php namespace Test; use const fOO; const foO = 1; echo \FOO, \Foo; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - handle aliased imports' => [ <<<'EXPECTED' <?php namespace Test; use const BAR as BAZ; use const FOO; echo FOO, BAZ; EXPECTED, <<<'INPUT' <?php namespace Test; use const BAR as BAZ; echo \FOO, \BAR; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - ignore class constants' => [ <<<'EXPECTED' <?php namespace Test; use const FOO; class Bar { const FOO = 1; } echo FOO; EXPECTED, <<<'INPUT' <?php namespace Test; class Bar { const FOO = 1; } echo \FOO; INPUT, ['import_constants' => true], ]; yield 'import_constants (true) - global namespace' => [ <<<'INPUT' <?php echo \FOO, \BAR; INPUT, null, ['import_constants' => true], ]; yield [ <<<'INPUT' <?php namespace { echo \FOO, \BAR; } INPUT, null, ['import_constants' => true], ]; yield 'import_functions (true) - non-global names' => [ <<<'EXPECTED' <?php namespace Test; foo(); Bar\baz(); namespace\foo2(); EXPECTED, null, ['import_functions' => true], ]; yield 'import_functions (true) - name already used [1]' => [ <<<'EXPECTED' <?php namespace Test; \foo(); Foo(); \foo(); EXPECTED, null, ['import_functions' => true], ]; yield 'import_functions (true) - name already used [2]' => [ <<<'EXPECTED' <?php namespace Test; use function Bar\foo; \Foo(); EXPECTED, null, ['import_functions' => true], ]; yield 'import_functions (true) - name already used [3]' => [ <<<'EXPECTED' <?php namespace Test; function foo() {} \Foo(); EXPECTED, null, ['import_functions' => true], ]; yield 'import_functions (true) - without namespace / do not import' => [ <<<'INPUT' <?php \foo(); \bar(); \Foo(); INPUT, null, ['import_functions' => true], ]; yield 'import_functions (true) - with namespace' => [ <<<'EXPECTED' <?php namespace Test; use function bar; use function foo; foo(); bar(); EXPECTED, <<<'INPUT' <?php namespace Test; \foo(); \bar(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - with namespace with {} syntax' => [ <<<'EXPECTED' <?php namespace Test { use function bar; use function foo; foo(); bar(); } EXPECTED, <<<'INPUT' <?php namespace Test { \foo(); \bar(); } INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - ignore other imported types' => [ <<<'EXPECTED' <?php namespace Test; use bar; use function bar; use function foo; foo(); bar(); EXPECTED, <<<'INPUT' <?php namespace Test; use bar; \foo(); \bar(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - respect already imported names [1]' => [ <<<'EXPECTED' <?php namespace Test; use function bar; use function foo; foo(); Bar(); EXPECTED, <<<'INPUT' <?php namespace Test; use function bar; \foo(); \Bar(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - respect already imported names [2]' => [ <<<'EXPECTED' <?php namespace Test; use function \bar; use function foo; foo(); Bar(); bar(); EXPECTED, <<<'INPUT' <?php namespace Test; use function \bar; \foo(); \Bar(); bar(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - handle aliased imports' => [ <<<'EXPECTED' <?php namespace Test; use function bar as baz; use function foo; foo(); baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use function bar as baz; \foo(); \Bar(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - ignore class methods' => [ <<<'EXPECTED' <?php namespace Test; use function foo; class Bar { function foo() {} } foo(); EXPECTED, <<<'INPUT' <?php namespace Test; class Bar { function foo() {} } \foo(); INPUT, ['import_functions' => true], ]; yield 'import_functions (true) - name already used' => [ <<<'EXPECTED' <?php namespace Test; class Bar { function baz() { new class() { function baz() { function foo() {} } }; } } \foo(); EXPECTED, null, ['import_functions' => true], ]; yield 'import_classes (true) - non-global names' => [ <<<'EXPECTED' <?php namespace Test; new Foo(); new Bar\Baz(); new namespace\Foo2(); /** @var Foo|Bar\Baz $x */ $x = x(); EXPECTED, null, ['import_classes' => true], ]; yield 'import_classes (true) - name already used [1]' => [ <<<'EXPECTED' <?php namespace Test; new \Foo(); new foo(); /** @var \Foo $foo */ $foo = new \Foo(); EXPECTED, null, ['import_classes' => true], ]; yield 'import_classes (true) - name already used [2]' => [ <<<'EXPECTED' <?php namespace Test; use Bar\foo; /** @var \Foo $foo */ $foo = new \Foo(); EXPECTED, null, ['import_classes' => true], ]; yield 'import_classes (true) - name already used [3]' => [ <<<'EXPECTED' <?php namespace Test; class foo {} /** @var \Foo $foo */ $foo = new \Foo(); EXPECTED, null, ['import_classes' => true], ]; yield 'import_classes (true) - name already used [4]' => [ <<<'EXPECTED' <?php namespace Test; /** @return array<string, foo> */ function x() {} /** @var \Foo $foo */ $foo = new \Foo(); EXPECTED, null, ['import_classes' => true], ]; yield 'import_classes (true) - without namespace / do not import' => [ <<<'INPUT' <?php /** @var \Foo $foo */ $foo = new \foo(); new \Bar(); \FOO::baz(); INPUT, null, ['import_classes' => true], ]; yield 'import_classes (true) - with namespace' => [ <<<'EXPECTED' <?php namespace Test; use Bar; use Baz; use Foo; new Foo(); Bar::baz(); /** @return Baz<string, foo> */ function x() {} EXPECTED, <<<'INPUT' <?php namespace Test; new \Foo(); \Bar::baz(); /** @return \Baz<string, \foo> */ function x() {} INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - with namespace with {} syntax' => [ <<<'EXPECTED' <?php namespace Test { use Bar; use Foo; new Foo(); Bar::baz(); } EXPECTED, <<<'INPUT' <?php namespace Test { new \Foo(); \Bar::baz(); } INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - phpdoc only' => [ <<<'EXPECTED' <?php namespace Test; use Throwable; /** @throws Throwable */ function x() {} EXPECTED, <<<'INPUT' <?php namespace Test; /** @throws \Throwable */ function x() {} INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - ignore other imported types' => [ <<<'EXPECTED' <?php namespace Test; use function Bar; use Bar; use Foo; new Foo(); Bar::baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use function Bar; new \Foo(); \Bar::baz(); INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - respect already imported names [1]' => [ <<<'EXPECTED' <?php namespace Test; use Bar; use Foo; new Foo(); bar::baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use Bar; new \Foo(); \bar::baz(); INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - respect already imported names [2]' => [ <<<'EXPECTED' <?php namespace Test; use \Bar; use Foo; new Foo(); new bar(); new Bar(); EXPECTED, <<<'INPUT' <?php namespace Test; use \Bar; new \Foo(); new \bar(); new Bar(); INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - respect already imported names [3]' => [ <<<'EXPECTED' <?php namespace Test; use Throwable; /** @throws Throwable */ function x() {} /** @throws Throwable */ function y() {} EXPECTED, <<<'INPUT' <?php namespace Test; use Throwable; /** @throws Throwable */ function x() {} /** @throws \Throwable */ function y() {} INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - handle aliased imports' => [ <<<'EXPECTED' <?php namespace Test; use Bar as Baz; use Foo; new Foo(); /** @var Baz $bar */ $bar = new Baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use Bar as Baz; new \Foo(); /** @var \bar $bar */ $bar = new \bar(); INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - handle typehints' => [ <<<'EXPECTED' <?php namespace Test; use Bar; use Baz; use Foo; class Abc { function bar(Foo $a, Bar $b, foo &$c, Baz ...$d) {} } EXPECTED, <<<'INPUT' <?php namespace Test; class Abc { function bar(\Foo $a, \Bar $b, \foo &$c, \Baz ...$d) {} } INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - handle typehints 2' => [ <<<'EXPECTED' <?php namespace Test; use Bar; use Foo; class Abc { function bar(?Foo $a): ?Bar {} } EXPECTED, <<<'INPUT' <?php namespace Test; class Abc { function bar(?\Foo $a): ?\Bar {} } INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - try catch' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (Exception $e) { } EXPECTED, <<<'INPUT' <?php namespace Test; try { } catch (\Exception $e) { } INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - try catch with comments' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (/* ... */ Exception $e /* ... */) { } EXPECTED, <<<'INPUT' <?php namespace Test; try { } catch (/* ... */ \Exception $e /* ... */) { } INPUT, ['import_classes' => true], ]; yield 'import_constants (false) - already fqn or sub namespace' => [ <<<'EXPECTED' <?php use const FOO; use const BAR; echo \FOO, Baz\BAR; EXPECTED, null, ['import_constants' => false], ]; yield 'import_constants (false) - handle all occurrences' => [ <<<'EXPECTED' <?php namespace X; use const FOO; use const BAR; echo \FOO, \BAR, \FOO; EXPECTED, <<<'INPUT' <?php namespace X; use const FOO; use const BAR; echo FOO, BAR, FOO; INPUT, ['import_constants' => false], ]; yield 'import_constants (false) - ignore other imports and non-imported names' => [ <<<'EXPECTED' <?php namespace Test; use FOO; use const BAR; use const Baz; echo FOO, \BAR, BAZ, QUX; EXPECTED, <<<'INPUT' <?php namespace Test; use FOO; use const BAR; use const Baz; echo FOO, BAR, BAZ, QUX; INPUT, ['import_constants' => false], ]; yield 'import_functions (false) - already fqn or sub namespace' => [ <<<'EXPECTED' <?php use function foo; use function bar; \foo(); Baz\bar(); EXPECTED, null, ['import_functions' => false], ]; yield 'import_functions (false) - handle all occurrences' => [ <<<'EXPECTED' <?php namespace X; use function foo; use function bar; \foo(); \bar(); \Foo(); EXPECTED, <<<'INPUT' <?php namespace X; use function foo; use function bar; foo(); bar(); Foo(); INPUT, ['import_functions' => false], ]; yield 'import_functions (false) - ignore other imports and non-imported names' => [ <<<'EXPECTED' <?php namespace Test; use foo; use function bar; foo(); \bar(); baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use foo; use function bar; foo(); bar(); baz(); INPUT, ['import_functions' => false], ]; yield 'import_classes (false) - already fqn or sub namespace' => [ <<<'EXPECTED' <?php use Foo; use Bar; new \Foo(); Baz\Bar::baz(); /** * @param \Foo $foo * @param Baz\Bar $bar */ function abc(\Foo $foo, Baz\Bar $bar = null) {} EXPECTED, null, ['import_classes' => false], ]; yield 'import_classes (false) - handle all occurrences' => [ <<<'EXPECTED' <?php namespace X; use Foo; use Bar; use IteratorAggregate; new \Foo(); new \Bar(); \foo::baz(); /** * @param \Foo|string $foo * @param null|\Bar[] $bar * @param-out \Foo $foo * @return array<string, ?\Bar<int, \foo>>|null */ function abc($foo, \Bar $bar = null) {} /** * @extends \Foo<\Bar> * @implements \IteratorAggregate<\Foo> */ class Qux implements \IteratorAggregate {} EXPECTED, <<<'INPUT' <?php namespace X; use Foo; use Bar; use IteratorAggregate; new Foo(); new Bar(); foo::baz(); /** * @param Foo|string $foo * @param null|Bar[] $bar * @param-out Foo $foo * @return array<string, ?Bar<int, foo>>|null */ function abc($foo, Bar $bar = null) {} /** * @extends Foo<Bar> * @implements IteratorAggregate<Foo> */ class Qux implements IteratorAggregate {} INPUT, ['import_classes' => false], ]; yield 'import_classes (false) - ignore other imports and non-imported names' => [ <<<'EXPECTED' <?php namespace Test; use function Foo; use Bar; new Foo(); new \Bar(); new Baz(); EXPECTED, <<<'INPUT' <?php namespace Test; use function Foo; use Bar; new Foo(); new Bar(); new Baz(); INPUT, ['import_classes' => false], ]; yield 'import_classes (false) - try catch' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (\Exception $e) { } EXPECTED, <<<'INPUT' <?php namespace Test; use Exception; try { } catch (Exception $e) { } INPUT, ['import_classes' => false], ]; yield 'import_classes (false) - try catch with comments' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (/* ... */ \Exception $e /* ... */) { } EXPECTED, <<<'INPUT' <?php namespace Test; use Exception; try { } catch (/* ... */ Exception $e /* ... */) { } INPUT, ['import_classes' => false], ]; yield 'import_classes (false) - key in PHPDoc\'s array shape matching class name' => [ '<?php namespace Foo; use Exception; class Bar { /** * @return array{code: int, exception: \Exception} */ public function f1(): array {} /** * @return array{exception: \Exception} */ public function f2(): array {} /** * @return array{exceptions: array<\Exception>} */ public function f3(): array {} }', '<?php namespace Foo; use Exception; class Bar { /** * @return array{code: int, exception: Exception} */ public function f1(): array {} /** * @return array{exception: Exception} */ public function f2(): array {} /** * @return array{exceptions: array<Exception>} */ public function f3(): array {} }', ['import_classes' => false], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null, array $configuration = []): void { $this->testFix($expected, $input, $configuration); } /** * @return iterable<array{string, null|string, _AutogeneratedInputConfiguration}> */ public static function provideFix80Cases(): iterable { yield 'import_classes (true) - try catch without variable' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (Exception) { } EXPECTED, <<<'INPUT' <?php namespace Test; try { } catch (\Exception) { } INPUT, ['import_classes' => true], ]; yield 'import_classes (true) - try catch without variable and comments' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (/* non-capturing catch */ Exception /* just because! */) { } EXPECTED, <<<'INPUT' <?php namespace Test; try { } catch (/* non-capturing catch */ \Exception /* just because! */) { } INPUT, ['import_classes' => true], ]; yield 'import_classes (false) - try catch without variable' => [ <<<'EXPECTED' <?php namespace Test; use Exception; try { } catch (\Exception) {
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/OrderedImportsFixerTest.php
tests/Fixer/Import/OrderedImportsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\Import\OrderedImportsFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\OrderedImportsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\OrderedImportsFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\OrderedImportsFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class OrderedImportsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<string, array{0: string, 1?:null|string, 2?:_AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'multiple namespaces' => [ <<<'EOF' <?php namespace FooRoo { use Foo\Bar; use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz; use Foo\Bir as FBB; use Foo\Zar\Baz; use SomeClass; use Symfony\Annotation\Template, Zoo\Bar as ZooBar; use Zoo\Tar1; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); use Zoo\Tar2; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } } namespace BlaRoo { use Foo\Zar\Baz; use SomeClass; use Symfony\Annotation\Template; use Symfony\Doctrine\Entities\Entity, Zoo\Bar; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } } EOF, <<<'EOF' <?php namespace FooRoo { use Foo\Bar\FooBar as FooBaz; use Zoo\Bar as ZooBar, Zoo\Tar1; use Foo\Bar; use Foo\Zar\Baz; use Symfony\Annotation\Template; use Foo\Bar\Foo as Fooo, Foo\Bir as FBB; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); use Zoo\Tar2; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } } namespace BlaRoo { use Foo\Zar\Baz; use Zoo\Bar; use SomeClass; use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } } EOF, ]; yield 'multiple namespaces II' => [ <<<'EOF' <?php namespace Space1 { use Foo\Bar\Foo; use Symfony\Annotation\Template; } namespace Space2 { use A,B; } namespace Space3 { use Symfony\Annotation\Template; use Symfony\Doctrine\Entities\Entity0, Zoo\Bar; echo Bar::C; use A\B; } namespace Space4{} EOF, <<<'EOF' <?php namespace Space1 { use Symfony\Annotation\Template; use Foo\Bar\Foo; } namespace Space2 { use B,A; } namespace Space3 { use Zoo\Bar; use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity0; echo Bar::C; use A\B; } namespace Space4{} EOF, ]; yield 'with imports after logic' => [ '<?php use B; use C; $foo = new C(); use A; ', '<?php use C; use B; $foo = new C(); use A; ', ]; yield 'open-close groups' => [ ' <?php use X ?> <?php use Z ?> <?php echo X::class ?> <?php use E ?> output <?php use F ?><?php echo E::class; use A; ?> ', ' <?php use Z ?> <?php use X ?> <?php echo X::class ?> <?php use F ?> output <?php use E ?><?php echo E::class; use A; ?> ', ]; yield 'with comment' => [ <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar; use Foo\Bar; use Foo\Zar\Baz; <?php use Foo\Bar; use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar /* He there */ as FooBaz; use Foo\Bir as FBB; use Foo\Zar\Baz; use SomeClass; use /* check */Symfony\Annotation\Template, Zoo\Bar as ZooBar; use Zoo\Tar; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar; use Foo\Bar; use Foo\Zar\Baz; <?php use Foo\Bar\FooBar /* He there */ as FooBaz; use Zoo\Bar as ZooBar, Zoo\Tar; use Foo\Bar; use Foo\Zar\Baz; use /* check */Symfony\Annotation\Template; use Foo\Bar\Foo as Fooo, Foo\Bir as FBB; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, ]; yield 'with traits' => [ <<<'EOF' <?php use Foo\Bar; use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz; use Foo\Bir as FBB; use Foo\Zar\Baz; use SomeClass; use Symfony\Annotation\Template, Zoo\Bar as ZooBar; use Zoo\Tar; trait Foo {} trait Zoo {} class AnnotatedClass { use Foo, Bar; /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, <<<'EOF' <?php use Foo\Bar\FooBar as FooBaz; use Zoo\Bar as ZooBar, Zoo\Tar; use Foo\Bar; use Foo\Zar\Baz; use Symfony\Annotation\Template; use Foo\Bar\Foo as Fooo, Foo\Bir as FBB; use SomeClass; trait Foo {} trait Zoo {} class AnnotatedClass { use Foo, Bar; /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, ]; yield 'with trait imports' => [ <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar; use Foo\Bar; use Foo\Zar\Baz; <?php use Acme\MyReusableTrait; use Foo\Bar, Foo\Bar\Foo as Fooo; use Foo\Bar\FooBar as FooBaz; use Foo\Bir as FBB; use Foo\Zar\Baz; use SomeClass; use Symfony\Annotation\Template, Zoo\Bar as ZooBar; use Zoo\Tar; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { use MyReusableTrait; /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $baz) {}; } } EOF, <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar; use Foo\Bar; use Foo\Zar\Baz; <?php use Foo\Bar\FooBar as FooBaz; use Zoo\Bar as ZooBar, Zoo\Tar; use Foo\Bar; use Foo\Zar\Baz; use Acme\MyReusableTrait; use Symfony\Annotation\Template; use Foo\Bar\Foo as Fooo, Foo\Bir as FBB; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { use MyReusableTrait; /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $baz) {}; } } EOF, ]; yield 'with different cases' => [ <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Baz; use abc\Bar; <?php use abc\Bar; use Zoo\Baz; class Test { } EOF, <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Baz; use abc\Bar; <?php use Zoo\Baz; use abc\Bar; class Test { } EOF, ]; yield 'without uses' => [ <<<'EOF' <?php $c = 1; EOF, ]; yield 'with trailing digits' => [ <<<'EOF' <?php use abc\Bar; use abc2\Bar2; use xyz\abc\Bar6; use xyz\abc2\Bar7; use xyz\xyz\Bar4; use xyz\xyz\Bar5; class Test { } EOF, <<<'EOF' <?php use abc2\Bar2; use abc\Bar; use xyz\abc2\Bar7; use xyz\abc\Bar6; use xyz\xyz\Bar4; use xyz\xyz\Bar5; class Test { } EOF, ]; yield 'with imports only' => [ <<<'EOF' <?php use Aaa; use Bbb; EOF, <<<'EOF' <?php use Bbb; use Aaa; EOF, ]; yield 'with close tag' => [ '<?php use A\C1; use A\D?><?php use B\C2; use E\F ?>', '<?php use A\C1; use B\C2?><?php use A\D; use E\F ?>', ]; yield 'with comments' => [ '<?php use A\C1 /* A */; use /* B */ B\C2;', '<?php use /* B */ B\C2; use A\C1 /* A */;', ]; yield 'with text before opening tag' => [ <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar as ZooBar; use Foo\Bar; use Foo\Zar\Baz; <?php use Foo\Bar; use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz; use Foo\Bir as FBB; use Foo\Zar\Baz; use SomeClass; use Symfony\Annotation\Template, Zoo\Bar as ZooBar; use Zoo\Tar; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, <<<'EOF' The normal use of this fixer should not change this sentence nor those statements below use Zoo\Bar as ZooBar; use Foo\Bar; use Foo\Zar\Baz; <?php use Foo\Bar\FooBar as FooBaz; use Zoo\Bar as ZooBar, Zoo\Tar; use Foo\Bar; use Foo\Zar\Baz; use Symfony\Annotation\Template; use Foo\Bar\Foo as Fooo, Foo\Bir as FBB; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, ]; yield 'with grouped import' => [ '<?php use A\B; use some\a\{ClassA, ClassB, ClassC as C}; use some\b\{ ClassF, ClassG }; use const some\a\{ConstA, ConstB, ConstC}; use const some\b\{ ConstX, ConstY, ConstZ }; use function some\a\{fn_a, fn_b, fn_c}; use function some\b\{ fn_x, fn_y, fn_z }; ', '<?php use some\a\{ClassA, ClassB, ClassC as C}; use function some\b\{ fn_y, fn_z, fn_x }; use function some\a\{fn_a, fn_b, fn_c}; use A\B; use const some\b\{ ConstZ, ConstX, ConstY }; use const some\a\{ConstA, ConstB, ConstC}; use some\b\{ ClassG, ClassF }; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['class', 'const', 'function'], ], ]; yield 'with grouped import and comment' => [ '<?php use A\B; use some\a\{ClassA as A /*z*/, ClassB, ClassC}; use const some\a\{ ConstA, ConstB, ConstC }; use function some\a\{fn_a, fn_b, fn_c}; ', '<?php use some\a\{ ClassB,ClassC, /*z*/ ClassA as A}; use function some\a\{fn_c, fn_a,fn_b }; use A\B; use const some\a\{ ConstA, ConstB, ConstC }; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['class', 'const', 'function'], ], ]; yield 'with grouped import for classes, constants and functions' => [ '<?php use A\B; use some\a\{ClassA, ClassB, ClassC as C}; use const some\a\{ConstA, ConstB, ConstC}; use function some\a\{fn_a, fn_b, fn_c}; use some\b\{ ClassF, ClassG }; use const some\b\{ ConstX, ConstY, ConstZ }; use function some\b\{ fn_x, fn_y, fn_z }; ', '<?php use some\a\{ClassA, ClassB, ClassC as C}; use function some\b\{ fn_y, fn_z, fn_x }; use function some\a\{fn_a, fn_b, fn_c}; use A\B; use const some\b\{ ConstZ, ConstX, ConstY }; use const some\a\{ConstA, ConstB, ConstC}; use some\b\{ ClassG, ClassF }; ', ]; yield 'with grouped imports' => [ '<?php use A\B; use const some\a\{ ConstA, ConstB, ConstC }; use some\a\{ClassA as A /*z2*/, ClassB, ClassC}; use function some\a\{fn_a, fn_b, fn_c}; ', '<?php use some\a\{ ClassB,ClassC, /*z2*/ ClassA as A}; use function some\a\{fn_c, fn_a,fn_b }; use A\B; use const some\a\{ ConstA, ConstB, ConstC }; ', ]; yield 'with order of types' => [ '<?php use C\B; use function B\fn_a; use const A\ConstA; ', '<?php use const A\ConstA; use function B\fn_a; use C\B; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['class', 'function', 'const'], ], ]; yield 'with imports in the same line and in the new lines' => [ '<?php use Foo\Bar\Baz;use Foo\Bar\{ClassA, ClassB, ClassC}; use Foo\Bir; ', '<?php use Foo\Bar\Baz, Foo\Bir; use Foo\Bar\{ClassC, ClassB, ClassA}; ', ]; yield 'many import in single line' => [ '<?php use A\A;use Foo3\Bar\{ClassA};use G\G;use H\H;use Ioo2\Bar\{ClassB};use J\J;use K\K;use Loo1\Bar\{ClassC};use M\M; ', '<?php use A\A,G\G;use Foo3\Bar\{ClassA};use H\H,J\J;use Ioo2\Bar\{ClassB};use K\K,M\M;use Loo1\Bar\{ClassC}; ', ]; yield 'grouped import changing location to comma separated imports' => [ <<<'PHP' <?php use A; use B;use C\{C1, C2, C3};use D; PHP, <<<'PHP' <?php use C\{C1, C2, C3}; use D, A, B; PHP, ]; yield 'many grouped imports' => [ '<?php use Foo\Bar\{ClassA, ClassB, ClassC}; use Foo\Bir\{ ClassD, ClassE, ClassF }; use Foo\Bor\{ ClassG, ClassH, ClassI, ClassJ }; ', '<?php use Foo\Bar\{ClassC, ClassB, ClassA}; use Foo\Bir\{ClassE, ClassF, ClassD}; use Foo\Bor\{ ClassJ, ClassI, ClassH, ClassG }; ', ]; yield 'alpha - [\'class\', \'function\', \'const\']' => [ '<?php use Z\Z; use function X\X; use const Y\Y; ', '<?php use const Y\Y; use function X\X; use Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['class', 'function', 'const'], ], ]; yield 'alpha - [\'class\', \'const\', \'function\']' => [ '<?php use Z\Z; use const Y\Y; use function X\X; ', '<?php use function X\X; use const Y\Y; use Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['class', 'const', 'function'], ], ]; yield 'alpha - [\'function\', \'class\', \'const\']' => [ '<?php use function Z\Z; use Y\Y; use const X\X; ', '<?php use const X\X; use Y\Y; use function Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['function', 'class', 'const'], ], ]; yield 'alpha - [\'function\', \'const\', \'class\']' => [ '<?php use function Z\Z; use const Y\Y; use X\X; ', '<?php use X\X; use const Y\Y; use function Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['function', 'const', 'class'], ], ]; yield 'alpha - [\'const\', \'function\', \'class\']' => [ '<?php use const Z\Z; use function Y\Y; use X\X; ', '<?php use X\X; use function Y\Y; use const Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['const', 'function', 'class'], ], ]; yield 'alpha - [\'const\', \'class\', \'function\']' => [ '<?php use const Z\Z; use Y\Y; use function X\X; ', '<?php use function X\X; use Y\Y; use const Z\Z; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => ['const', 'class', 'function'], ], ]; yield '"strcasecmp" vs. "strnatcasecmp"' => [ '<?php use A\A1; use A\A10; use A\A2; use A\A20; ', '<?php use A\A20; use A\A2; use A\A10; use A\A1; ', [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, ], ]; yield 'with trailing comma in single-line grouped import' => [ '<?php use A\{B,}; use C\{D,E,}; ', '<?php use C\{D,E,}; use A\{B,}; ', ]; yield 'with trailing comma in multi-line grouped import' => [ '<?php use Foo\{ Aaa, Bbb, };', '<?php use Foo\{ Bbb, Aaa, };', ]; yield 'with trailing comma in multi-line grouped import with comments' => [ '<?php use Foo\{ Aaa /* 3 *//* 4 *//* 5 */, Bbb /* 1 *//* 2 */, };', '<?php use Foo\{ /* 1 */Bbb/* 2 */,/* 3 */ /* 4 */Aaa/* 5 */,/* 6 */ };', ]; $input = '<?php use A\{B,}; use some\y\{ClassA, ClassB, ClassC as C,}; use function some\a\{fn_a, fn_b, fn_c,}; use const some\Z\{ConstAA,ConstBB,ConstCC,}; use const some\X\{ConstA,ConstB,ConstC,ConstF}; use C\{D,E,}; '; yield 'with trailing comma in multi-line grouped import - sorting alphabetically' => [ '<?php use A\{B,}; use C\{D,E,}; use some\y\{ClassA, ClassB, ClassC as C,}; use const some\X\{ConstA,ConstB,ConstC,ConstF}; use const some\Z\{ConstAA,ConstBB,ConstCC,}; use function some\a\{fn_a, fn_b, fn_c,}; ', $input, [ 'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA, 'imports_order' => [OrderedImportsFixer::IMPORT_TYPE_CLASS, OrderedImportsFixer::IMPORT_TYPE_CONST, OrderedImportsFixer::IMPORT_TYPE_FUNCTION], ], ]; yield 'with trailing comma in multi-line grouped import - sorting by length' => [ '<?php use A\{B,}; use C\{D,E,}; use some\y\{ClassA, ClassB, ClassC as C,}; use const some\Z\{ConstAA,ConstBB,ConstCC,}; use const some\X\{ConstA,ConstB,ConstC,ConstF}; use function some\a\{fn_a, fn_b, fn_c,}; ', $input, [ 'sort_algorithm' => OrderedImportsFixer::SORT_LENGTH, 'imports_order' => [OrderedImportsFixer::IMPORT_TYPE_CLASS, OrderedImportsFixer::IMPORT_TYPE_CONST, OrderedImportsFixer::IMPORT_TYPE_FUNCTION], ], ]; yield 'with trailing comma in multi-line grouped import - no sorting' => [ '<?php use A\{B,}; use some\y\{ClassA, ClassB, ClassC as C,}; use C\{D,E,}; use const some\Z\{ConstAA,ConstBB,ConstCC,}; use const some\X\{ConstA,ConstB,ConstC,ConstF}; use function some\a\{fn_a, fn_b, fn_c,}; ', $input, [ 'sort_algorithm' => OrderedImportsFixer::SORT_NONE, 'imports_order' => [OrderedImportsFixer::IMPORT_TYPE_CLASS, OrderedImportsFixer::IMPORT_TYPE_CONST, OrderedImportsFixer::IMPORT_TYPE_FUNCTION], ], ]; yield 'only constant imports' => [ '<?php use const CONST_A, CONST_B, CONST_C;', '<?php use const CONST_C, CONST_B, CONST_A;', ]; yield 'only function imports' => [ '<?php use function Foo\A, Foo\B, Foo\C;', '<?php use function Foo\B, Foo\C, Foo\A;', ]; yield 'by length with same length' => [ <<<'EOF' <?php use Acme; use Bar1; use Barr; use Fooo; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, <<<'EOF' <?php use Acme; use Fooo; use Barr; use Bar1; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } EOF, [ 'sort_algorithm' => OrderedImportsFixer::SORT_LENGTH, 'imports_order' => null, ], ]; yield 'by length with same length and case sensitive' => [ <<<'EOF' <?php use Acme; use BaRr; use Bar1; use Fooo; class AnnotatedClass { } EOF, <<<'EOF' <?php use Acme; use Fooo; use Bar1; use BaRr; class AnnotatedClass { } EOF, [ 'sort_algorithm' => OrderedImportsFixer::SORT_LENGTH, 'imports_order' => null, 'case_sensitive' => true, ], ]; yield 'by length with multiple namespace' => [ <<<'EOF' <?php namespace FooRoo { use Foo\Bar; use Zoo\Tar1, Zoo\Tar2; use SomeClass; use Foo\Zar\Baz; use Foo\Bir as FBB; use Zoo\Bar as ZooBar, Foo\Bar\Foo as Fooo; use Foo\Bar\FooBar as FooBaz; use Symfony\Annotation\Template; $a = new Bar(); $a = new FooBaz(); $a = new someclass(); class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ return function () use ($bar, $foo) {}; } } } namespace BlaRoo { use Zoo\Bar; use SomeClass; use Foo\Zar\Baz; use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\FullyQualifiedStrictTypesFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\FullyQualifiedStrictTypesFixer> * * @author VeeWee <toonverwerft@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\FullyQualifiedStrictTypesFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class FullyQualifiedStrictTypesFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix( string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null ): void { $this->fixer->configure($configuration); if (null !== $whitespacesConfig) { $this->fixer->setWhitespacesConfig($whitespacesConfig); $expected = str_replace("\n", $whitespacesConfig->getLineEnding(), $expected); if (null !== $input) { $input = str_replace("\n", $whitespacesConfig->getLineEnding(), $input); } } $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield 'namespace === type name' => [ '<?php namespace Foo\Bar; function test(\Foo\Bar $x) {}', ]; yield 'reserved type' => [ '<?php function test(int $x): void {}', ]; yield 'ignore class resolution shortening when imported symbol is a function' => [ '<?php use function Symfony\Component\String\u; echo Symfony\Component\String\u::class;', ]; yield 'namespace cases' => [ '<?php namespace A\B\C\D { class Foo {} } namespace A\B\C\D\E { class Bar {} } namespace A\B\C\D { function A(Foo $fix, \X\B\C\D\E\Bar $doNotFix) {} } ', '<?php namespace A\B\C\D { class Foo {} } namespace A\B\C\D\E { class Bar {} } namespace A\B\C\D { function A(\A\B\C\D\Foo $fix, \X\B\C\D\E\Bar $doNotFix) {} } ', ]; yield 'simple use' => [ '<?php use A\Exception; function foo(Exception $e) {}', '<?php use A\Exception; function foo(\A\Exception $e) {}', ]; yield 'simple use with global' => [ '<?php use A\Exception; function foo(Exception $e, \Exception $e2) {}', '<?php use A\Exception; function foo(\A\Exception $e, \Exception $e2) {}', ]; yield 'no backslash with global' => [ '<?php use A\Exception; function foo(Exception $e, Foo $e2) {}', '<?php use A\Exception; function foo(A\Exception $e, \Foo $e2) {}', ]; yield 'leading backslash in global namespace' => [ '<?php use A\Exception; function foo(Exception $e, \Foo $e2) {}', '<?php use A\Exception; function foo(A\Exception $e, Foo $e2) {}', ['leading_backslash_in_global_namespace' => true], ]; yield 'backslash must be kept when conflicts with other use with global' => [ '<?php use A\Exception; function foo(Exception $e, \Exception $e2) {}', ]; yield 'simple use as' => [ '<?php use A\Exception as C; function foo(C $e) {}', '<?php use A\Exception as C; function foo(\A\Exception $e) {}', ]; yield 'simple use as casing' => [ '<?php use A\Exception as C; function foo(C $e) {}', '<?php use A\Exception as C; function foo(\A\EXCEPTION $e) {}', ]; yield 'simple use 2' => [ '<?php use \A\Exception; function foo(Exception $e) {}', '<?php use \A\Exception; function foo(\A\Exception $e) {}', ]; yield 'common prefix 1' => [ '<?php namespace Foo; function foo(\FooBar $v): \FooBar {}', ]; yield 'common prefix 2' => [ '<?php namespace Foo; function foo(\FooBar\Baz $v): \FooBar {}', ]; yield 'issue #7025 - non-empty namespace, import and FQCN in argument' => [ '<?php namespace foo\bar\baz; use foo\baz\buzz; class A { public function b(buzz $buzz): void { } }', '<?php namespace foo\bar\baz; use foo\baz\buzz; class A { public function b(\foo\baz\buzz $buzz): void { } }', ]; yield 'interface multiple extends' => [ '<?php namespace Foo\Bar; use D\E; use IIII\G; use Foo\Bar\C; interface NakanoInterface extends IzumiInterface, A, E, \C, EZ { }', '<?php namespace Foo\Bar; use D\E; use IIII\G; use Foo\Bar\C; interface NakanoInterface extends \Foo\Bar\IzumiInterface, \Foo\Bar\A, \D\E, \C, EZ { }', ]; yield 'interface in global namespace with global extend' => [ '<?php interface Foo1 extends \ArrayAccess2{}', '<?php interface Foo1 extends ArrayAccess2{}', ['leading_backslash_in_global_namespace' => true], ]; yield 'interface in global namespace with multiple extend' => [ '<?php use B\Exception; interface Foo extends \ArrayAccess, \Exception, Exception {}', '<?php use B\Exception; interface Foo extends \ArrayAccess, \Exception, \B\Exception {}', ['leading_backslash_in_global_namespace' => true], ]; yield 'class implements' => [ '<?php namespace Foo\Bar; class SomeClass implements Izumi { }', '<?php namespace Foo\Bar; class SomeClass implements \Foo\Bar\Izumi { }', ]; yield 'anonymous class implements, shorten to namespace' => [ '<?php namespace Foo\Bar; $a = new class implements Izumi {};', '<?php namespace Foo\Bar; $a = new class implements \Foo\Bar\Izumi {};', ]; yield 'anonymous class implements, shorten to imported name' => [ '<?php use Foo\Bar\Izumi; $a = new class implements Izumi {};', '<?php use Foo\Bar\Izumi; $a = new class implements \Foo\Bar\Izumi {};', ]; yield 'class extends and implements' => [ '<?php namespace Foo\Bar; class SomeClass extends A implements Izumi { }', '<?php namespace Foo\Bar; class SomeClass extends \Foo\Bar\A implements \Foo\Bar\Izumi { }', ]; yield 'class extends and implements multiple' => [ '<?php namespace Foo\Bar; class SomeClass extends A implements Izumi, A, \A\B, C { }', '<?php namespace Foo\Bar; class SomeClass extends \Foo\Bar\A implements \Foo\Bar\Izumi, A, \A\B, \Foo\Bar\C { }', ]; yield 'single caught exception' => [ '<?php use A\B; echo 1; try{ foo(999); } catch (B $z) {}', '<?php use A\B; echo 1; try{ foo(999); } catch (\A\B $z) {}', ]; yield 'single caught exception namespaced' => [ '<?php namespace B; try{ foo(999); } catch (A $z) {}', '<?php namespace B; try{ foo(999); } catch (\B\A $z) {}', ]; yield 'multiple caught exceptions' => [ '<?php namespace D; use A\B; try{ foo(); } catch (B | \A\C | /* 1 */ \A\D $z) {}', '<?php namespace D; use A\B; try{ foo(); } catch (\A\B | \A\C | /* 1 */ \A\D $z) {}', ]; yield 'catch in multiple namespaces' => [ '<?php namespace { try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (\B\Z $z) {} try{ foo(); } catch (\B\Z $z) {} } namespace A { try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (X $z) {} try{ foo(); } catch (\B\Z $z) {} } namespace B { try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (Z $z) {} } ', '<?php namespace { try{ foo(); } catch (Exception $z) {} try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (A\X $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (B\Z $z) {} try{ foo(); } catch (\B\Z $z) {} } namespace A { try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (\B\Z $z) {} } namespace B { try{ foo(); } catch (\Exception $z) {} try{ foo(); } catch (\A\X $z) {} try{ foo(); } catch (\B\Z $z) {} } ', ['leading_backslash_in_global_namespace' => true], ]; yield 'new class' => [ '<?php use A\B; new B();', '<?php use A\B; new \A\B();', ]; yield 'new class namespaced' => [ '<?php namespace B; new A();', '<?php namespace B; new \B\A();', ]; yield 'new class not imported' => [ '<?php new A\B(); new A\B();', '<?php new \A\B(); new A\B();', ]; yield 'instanceof' => [ '<?php use A\B; $res = $v instanceof B;', '<?php use A\B; $res = $v instanceof \A\B;', ]; yield 'instanceof namespaced' => [ '<?php namespace B; $res = ($v->obj()) instanceof A;', '<?php namespace B; $res = ($v->obj()) instanceof \B\A;', ]; yield 'use trait simple' => [ '<?php use A\B; class Foo { use B; };', '<?php use A\B; class Foo { use \A\B; };', ]; yield 'use trait complex' => [ '<?php use A\B; class Foo { use A\C; use D; use B { B::bar as baz; } };', '<?php use A\B; class Foo { use \A\C; use \D; use \A\B { \A\B::bar as baz; } };', ]; yield 'typed property in class' => [ '<?php use A\B; class Cl { public B $p; var B $p2; }', '<?php use A\B; class Cl { public \A\B $p; var \A\B $p2; }', ]; yield 'typed property in anonymous class' => [ '<?php use A\B; new class() { public B $p; };', '<?php use A\B; new class() { public \A\B $p; };', ]; yield 'typed nullable property in class' => [ '<?php use A\B; class Cl { public ?B $p = null, $r; }', '<?php use A\B; class Cl { public ?\A\B $p = null, $r; }', ]; yield 'starts with but not full name extends' => [ '<?php namespace a\abcd; class Foo extends \a\abcdTest { }', null, ]; yield 'starts with but not full name function arg' => [ '<?php namespace Z\B\C\D { function A(\Z\B\C\DE\Foo $fix) {} } ', null, ]; yield 'static class reference' => [ '<?php use ZXY\A; echo A::class; echo A::B(); echo A::class; foo(A::B,A::C); echo $a[A::class]; echo A::class?> ', '<?php use ZXY\A; echo \ZXY\A::class; echo \ZXY\A::B(); echo \ZXY\A::class; foo(\ZXY\A::B,\ZXY\A::C); echo $a[\ZXY\A::class]; echo \ZXY\A::class?> ', ]; yield [ '<?php namespace Foo\Test; $this->assertSame($names, \Foo\TestMyThing::zxy(1,2)); ', null, ]; yield [ '<?php use ZXY\A; use D; echo $D::CONST_VALUE; echo parent::CONST_VALUE; echo self::$abc; echo Z::F; echo X\Z::F; ', null, ]; yield 'import new symbols from all supported places' => [ '<?php namespace Foo\Test; use Other\BaseClass; use Other\CaughtThrowable; use Other\FunctionArgument; use Other\FunctionReturnType; use Other\InstanceOfClass; use Other\Interface1; use Other\Interface2; use Other\NewClass; use Other\PropertyPhpDoc; use Other\StaticFunctionCall; class Foo extends BaseClass implements Interface1, Interface2 { /** @var PropertyPhpDoc */ private $array; public function __construct(FunctionArgument $arg) {} public function foo(): FunctionReturnType { try { StaticFunctionCall::bar(); } catch (CaughtThrowable $e) {} } } new NewClass(); if ($a instanceof InstanceOfClass) { return false; } ', '<?php namespace Foo\Test; class Foo extends \Other\BaseClass implements \Other\Interface1, \Other\Interface2 { /** @var \Other\PropertyPhpDoc */ private $array; public function __construct(\Other\FunctionArgument $arg) {} public function foo(): \Other\FunctionReturnType { try { \Other\StaticFunctionCall::bar(); } catch (\Other\CaughtThrowable $e) {} } } new \Other\NewClass(); if ($a instanceof \Other\InstanceOfClass) { return false; } ', ['import_symbols' => true], ]; yield 'import new symbols under already existing imports' => [ '<?php namespace Foo\Test; use Other\A; use Other\B; use Other\C; use Other\D; use Other\E; function foo(A $a, B $b) {} function bar(C $c, D $d): E {} ', '<?php namespace Foo\Test; use Other\A; use Other\B; function foo(A $a, B $b) {} function bar(\Other\C $c, \Other\D $d): \Other\E {} ', ['import_symbols' => true], ]; yield 'import new symbols within multiple namespaces' => [ '<?php namespace Foo\Bar { use Other\A; use Other\B; function foo(A $a, B $b) {} } namespace Foo\Baz { use Other\A; use Other\C; function foo(A $a, C $c) {} } ', '<?php namespace Foo\Bar { use Other\A; function foo(A $a, \Other\B $b) {} } namespace Foo\Baz { use Other\A; function foo(A $a, \Other\C $c) {} } ', ['import_symbols' => true], ]; yield 'import new symbols with no existing imports nor namespace /wo declare' => [ <<<'EOD' <?php use Ns\A; // comment foo(); function foo(A $v) {} EOD, <<<'EOD' <?php // comment foo(); function foo(\Ns\A $v) {} EOD, ['import_symbols' => true], ]; yield 'import new symbols with no existing imports nor namespace /w declare' => [ <<<'EOD' <?php // comment declare(strict_types=1); use Ns\A; function foo(A $v) {} EOD, <<<'EOD' <?php // comment declare(strict_types=1); function foo(\Ns\A $v) {} EOD, ['import_symbols' => true], ]; yield 'import new symbols with custom whitespace config' => [ '<?php namespace Foo\Bar; use Other\A; use Other\B; function foo(A $a, B $b) {} ', '<?php namespace Foo\Bar; use Other\A; function foo(A $a, \Other\B $b) {} ', ['import_symbols' => true], new WhitespacesFixerConfig("\t", "\r\n"), ]; yield 'ignore importing if there is name conflict' => [ '<?php namespace Foo\Test; use Other\A; function foo(A $a, \YetAnother\A $b) {}', null, ['import_symbols' => true], ]; yield 'ignore importing if symbol is not a FQN' => [ '<?php namespace Foo\Test; use Foo\Test\Sub\Symbol1; function foo(Symbol1 $a, Sub\Symbol2 $b) {}', null, ['import_symbols' => true], ]; yield 'ignore global FQNs (there is GlobalNamespaceImportFixer for that)' => [ '<?php namespace Foo\Test; function foo(\Symbol $a, \OtherSymbol $b) {}', null, ['import_symbols' => true], ]; yield '@link shall not crash fixer' => [ '<?php use Symfony\Component\Validator\Constraints\Valid; /** * {@link Valid} is assumed. * * @return void */ function validate(): void {} ', '<?php /** * {@link \Symfony\Component\Validator\Constraints\Valid} is assumed. * * @return void */ function validate(): void {} ', ['import_symbols' => true, 'phpdoc_tags' => ['link']], ]; yield 'import short name only once (ignore consequent same-name, different-namespace symbols)' => [ '<?php namespace Test; use A\A; class Foo extends A implements \B\A, \C\A { /** @var \D\A */ private $array; public function __construct(\E\A $arg) {} public function foo(): \F\A { try { \G\A::bar(); } catch (\H\A $e) {} } }', '<?php namespace Test; class Foo extends \A\A implements \B\A, \C\A { /** @var \D\A */ private $array; public function __construct(\E\A $arg) {} public function foo(): \F\A { try { \G\A::bar(); } catch (\H\A $e) {} } }', ['import_symbols' => true], ]; yield 'do not import if already implicitly used by class declaration' => [ <<<'EOD' <?php namespace Ns; class City { public \Ns2\City $city; } EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by interface declaration' => [ <<<'EOD' <?php namespace Ns; interface City { public function f(\Ns2\City $city); } EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by trait declaration' => [ <<<'EOD' <?php namespace Ns; trait City { public \Ns2\City $city; } EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by short name usage in class instantiation' => [ <<<'EOD' <?php namespace Ns; new \Ns2\MyCl(); new MyCl(); EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by short name usage in attribute' => [ <<<'EOD' <?php namespace Ns; new \Ns2\MyCl(); #[MyCl] class Cl {} EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by short name usage in phpdoc' => [ <<<'EOD' <?php namespace Ns; new \Ns2\MyCl(); /** @var MyCl */; EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by relative name first part' => [ <<<'EOD' <?php namespace Ns; new \Ns2\MyCl(); new MyCl\Sub(); EOD, null, ['import_symbols' => true], ]; yield 'do not import if already implicitly used by relative name first part (with more backslashes than other FQCN)' => [ <<<'EOD' <?php namespace Ns; new \Ns2\MyCl(); new MyCl\A\B\C(); EOD, null, ['import_symbols' => true], ]; yield 'prevent import if implicitly used by generics template' => [ <<<'EOD' <?php namespace Ns; use Foo\T; class Cl { /** * @return T */ public function before() { return new T(); } /** * @template T of \Exception * @param \Closure(\Foo\T): T $fx * @return T */ public function makeException(\Closure $fx) { $arg = new \Foo\T(); return $fx($arg); } /** * @return T */ public function after() { return new T(); } /** * @return T */ public function anony() { $anony = new /** * @template T of \Exception */ class(new RuntimeException()) { /** @var T */ public \Exception $e; /** * @param T $e */ public function __construct(\Exception $e) { $this->e = $e; } public function before(): void { new \Foo\T(); } /** * @return T */ public function returnT() { new \Foo\T(); return $this->e; } public function after(): void { new \Foo\T(); } }; return new T(); } } EOD, <<<'EOD' <?php namespace Ns; class Cl { /** * @return \Foo\T */ public function before() { return new \Foo\T(); } /** * @template T of \Exception * @param \Closure(\Foo\T): T $fx * @return T */ public function makeException(\Closure $fx) { $arg = new \Foo\T(); return $fx($arg); } /** * @return \Foo\T */ public function after() { return new \Foo\T(); } /** * @return \Foo\T */ public function anony() { $anony = new /** * @template T of \Exception */ class(new RuntimeException()) { /** @var T */ public \Exception $e; /** * @param T $e */ public function __construct(\Exception $e) { $this->e = $e; } public function before(): void { new \Foo\T(); } /** * @return T */ public function returnT() { new \Foo\T(); return $this->e; } public function after(): void { new \Foo\T(); } }; return new \Foo\T(); } } EOD, ['import_symbols' => true], ]; yield 'prevent import if implicitly used by local type' => [ <<<'EOD' <?php namespace Ns; use Ns2\Bar; use Ns2\Foo; /** * @phpstan-type Foo array{int, int} * @phpstan-import-type Bar from OtherCl */ class Cl { /** * @param \Ns2\Foo $v * * @return Foo */ public function foo($v) { return [1, 2]; } /** * @param \Ns2\Bar $v * * @return Bar */ public function bar($v) { return null; } } class Cl2 { /** * @param Foo $v */ public function foo($v): void {} /** * @param Bar $v */ public function bar($v): void {} } EOD, <<<'EOD' <?php namespace Ns; /** * @phpstan-type Foo array{int, int} * @phpstan-import-type Bar from OtherCl */ class Cl { /** * @param \Ns2\Foo $v * * @return Foo */ public function foo($v) { return [1, 2]; } /** * @param \Ns2\Bar $v * * @return Bar */ public function bar($v) { return null; } } class Cl2 { /** * @param \Ns2\Foo $v */ public function foo($v): void {} /** * @param \Ns2\Bar $v */ public function bar($v): void {} } EOD, ['import_symbols' => true], ]; yield 'prevent import if implicitly used by generics template - psalm/phpstan prefix' => [ <<<'EOD' <?php /** @psalm-template T1 */ /** @phpstan-template T2 */ class Foo { /** @var T1 */ public $v1; /** @var T2 */ public $v2; /** @var \T3 */ public $v3; } EOD, <<<'EOD' <?php /** @psalm-template T1 */ /** @phpstan-template T2 */ class Foo { /** @var T1 */ public $v1; /** @var T2 */ public $v2; /** @var T3 */ public $v3; } EOD, ['leading_backslash_in_global_namespace' => true], ]; yield 'prevent import if implicitly used by generics template - covariant/contravariant suffix' => [ <<<'EOD' <?php /** @template-covariant T1 */ /** @psalm-template-contravariant T2 */ class Foo { /** @var T1 */ public $v1; /** @var T2 */ public $v2; } EOD, null, ['leading_backslash_in_global_namespace' => true], ]; yield 'import with relative and absolute symbols - global' => [ <<<'EOD' <?php use Foo\Bar; new Exception(); new Exception(); new Bar(); EOD, <<<'EOD' <?php new \Exception(); new Exception(); new Foo\Bar(); EOD, ['import_symbols' => true], ]; yield 'import with relative and absolute symbols - global and leading backslash' => [ <<<'EOD' <?php use Foo\Bar; new \Exception(); new \Exception(); new Bar(); EOD, <<<'EOD' <?php new \Exception(); new Exception(); new Foo\Bar(); EOD, ['import_symbols' => true, 'leading_backslash_in_global_namespace' => true], ]; yield 'import with relative and absolute symbols - namespaced' => [ <<<'EOD' <?php namespace Ns; use Ns2\Foo4; use Ns\Foo3\Sub3; use Ns\Foo\Sub2; new Foo(); new Foo\Sub(); new Foo(); new Foo2(); new Sub2(); new Sub3(); new \Ns2\Foo(); new Foo4(); EOD, <<<'EOD' <?php namespace Ns; new Foo(); new Foo\Sub(); new \Ns\Foo(); new \Ns\Foo2(); new \Ns\Foo\Sub2(); new \Ns\Foo3\Sub3(); new \Ns2\Foo(); new \Ns2\Foo4(); EOD, ['import_symbols' => true], ]; yield 'shorten relative reference to already imported, direct short name' => [ <<<'EOD' <?php namespace Foo\Bar\Baz; use Foo\Bar; use Foo\Bar\A\B; final class Buzz extends Bar implements B {} final class Fuzz extends Bar implements B {} EOD, <<<'EOD' <?php namespace Foo\Bar\Baz; use Foo\Bar; use Foo\Bar\A\B; final class Buzz extends Bar implements Bar\A\B {} final class Fuzz extends Bar implements B {} EOD, ]; yield 'fix to longest imported name' => [ <<<'EOD' <?php use A\B; use A\X as Y; use S as R; use S\T; new B(); new B\C(); new Y(); new Y\Z(); new T(); EOD, <<<'EOD' <?php use A\B; use A\X as Y; use S as R; use S\T; new \A\B();
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/NoLeadingImportSlashFixerTest.php
tests/Fixer/Import/NoLeadingImportSlashFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer> * * @author Carlos Cirello <carlos.cirello.nl@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoLeadingImportSlashFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php use A\B; ', '<?php use \A\B; ', ]; yield [ '<?php use/*1*/A\C; ', '<?php use/*1*/\A\C; ', ]; yield [ '<?php $a = function(\B\C $a) use ($b){ }; ', ]; yield [ '<?php namespace NS; use A\B; ', '<?php namespace NS; use \A\B; ', ]; yield [ '<?php namespace NS{ use A\B; } namespace NS2{ use C\D; } ', '<?php namespace NS{ use \A\B; } namespace NS2{ use \C\D; } ', ]; yield [ '<?php namespace Foo { use A; use A\X; new X(); } namespace Bar { use B; use B\X; new X(); } ', '<?php namespace Foo { use \A; use \A\X; new X(); } namespace Bar { use \B; use \B\X; new X(); } ', ]; yield [ '<?php namespace Foo\Bar; use Baz; class Foo implements Baz {} ', '<?php namespace Foo\Bar; use \Baz; class Foo implements Baz {} ', ]; yield [ '<?php trait SomeTrait { use \A; } ', ]; yield [ '<?php namespace NS{ use A\B; trait Tr8A{ use \B, \C; } } namespace NS2{ use C\D; } ', '<?php namespace NS{ use \A\B; trait Tr8A{ use \B, \C; } } namespace NS2{ use \C\D; } ', ]; yield [ '<?php trait Foo {} class Bar { use \Foo; } ', ]; yield [ '<?php use function a\b; use const d\e; ', '<?php use function \a\b; use const \d\e; ', ]; yield [ '<?php namespace AAA; use some\a\{ClassA, ClassB, ClassC as C,}; use function some\a\{fn_a, fn_b, fn_c,}; use const some\a\{ConstA,ConstB,ConstC , }; use const some\Z\{ConstX,ConstY,ConstZ,}; ', '<?php namespace AAA; use \some\a\{ClassA, ClassB, ClassC as C,}; use function \some\a\{fn_a, fn_b, fn_c,}; use const \some\a\{ConstA,ConstB,ConstC , }; use const \some\Z\{ConstX,ConstY,ConstZ,}; ', ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php use /*1*/A\D;', '<?php use\/*1*/A\D;', ]; yield 'no space case' => [ '<?php use Events\Payment\Base as PaymentEvent; use const d\e; ', '<?php use\Events\Payment\Base as PaymentEvent; use const\d\e; ', ]; yield [ '<?php use C; use C\X; namespace Foo { use A; use A\X; new X(); } namespace Bar { use B; use B\X; new X(); } ', '<?php use \C; use \C\X; namespace Foo { use \A; use \A\X; new X(); } namespace Bar { use \B; use \B\X; new X(); } ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/SingleLineAfterImportsFixerTest.php
tests/Fixer/Import/SingleLineAfterImportsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer> * * @author Ceeram <ceeram@cakephp.org> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SingleLineAfterImportsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php use D; use E; use DP; /**/ use EZ; // use DAZ; use EGGGG; /**/ use A\B; use C\DE; use E\F; use G\H; ', '<?php use D; use E; use DP; /**/ use EZ; // use DAZ; use EGGGG; /**/ use A\B; use C\DE; use E\F; use G\H; ', ]; yield [ '<?php use \Exception; ?> <?php $a = new Exception(); ', '<?php use \Exception?> <?php $a = new Exception(); ', ]; yield [ '<?php use \stdClass; use \DateTime; ?> <?php $a = new DateTime(); ', '<?php use \stdClass; use \DateTime?> <?php $a = new DateTime(); ', ]; yield [ '<?php namespace Foo; '.' use Bar\Baz; /** * Foo. */', '<?php namespace Foo; '.' use Bar\Baz; /** * Foo. */', ]; yield [ '<?php namespace A\B; use D; class C {} ', '<?php namespace A\B; use D; class C {} ', ]; yield [ '<?php namespace A\B; use D; class C {} ', '<?php namespace A\B; use D; class C {} ', ]; yield [ '<?php namespace A\B; use D; use E; class C {} ', '<?php namespace A\B; use D; use E; class C {} ', ]; yield [ '<?php namespace A\B; use D; class C {} ', '<?php namespace A\B; use D; class C {} ', ]; yield [ '<?php namespace A\B; use D; use E; { class C {} }', '<?php namespace A\B; use D; use E; { class C {} }', ]; yield [ '<?php namespace A\B; use D; use E; { class C {} }', '<?php namespace A\B; use D; use E; { class C {} }', ]; yield [ '<?php namespace A\B { use D; use E; class C {} }', '<?php namespace A\B { use D; use E; class C {} }', ]; yield [ '<?php namespace A\B; class C { use SomeTrait; }', ]; yield [ '<?php $lambda = function () use ( $arg ){ return true; };', ]; yield [ '<?php namespace A\B; use D, E; class C { }', '<?php namespace A\B; use D, E; class C { }', ]; yield [ '<?php namespace A1; use B1; // need to import this ! use B2; class C1 {} ', ]; yield [ '<?php namespace A2; use B2;// need to import this ! use B3; class C4 {} ', ]; yield [ '<?php namespace A1; use B1; // need to import this ! use B2; class C1 {} ', ]; yield [ '<?php namespace A1; use B1;// need to import this ! use B2; class C1 {} ', ]; yield [ '<?php namespace A1; use B1; /** need to import this !*/ use B2; class C1 {} ', ]; yield [ '<?php namespace A1; use B1;# need to import this ! use B2; class C1 {} ', ]; yield [ '<?php namespace Foo; use Bar; use Baz; class Hello {} ', '<?php namespace Foo; use Bar; use Baz; class Hello {} ', ]; yield [ '<?php class HelloTrait { use SomeTrait; use Another;// ensure use statements for traits are not touched } ', ]; yield [ '<?php namespace Foo {} namespace Bar { class Baz { use Aaa; } } ', ]; yield [ '<?php use A\B; ?>', '<?php use A\B?>', ]; yield [ '<?php use A\B; ', '<?php use A\B;', ]; yield [ str_replace("\n", "\r\n", '<?php use Foo; use Bar; class Baz {} '), ]; yield [ '<?php use some\test\{ClassA, ClassB, ClassC as C}; ?> test 123 ', '<?php use some\test\{ClassA, ClassB, ClassC as C} ?> test 123 ', ]; yield [ '<?php use some\test\{CA, Cl, ClassC as C}; class Test {} ', '<?php use some\test\{CA, Cl, ClassC as C}; class Test {} ', ]; yield [ '<?php use function some\test\{fn_g, fn_f, fn_e}; fn_a();', '<?php use function some\test\{fn_g, fn_f, fn_e}; fn_a();', ]; yield [ '<?php use const some\test\{ConstA, ConstB, ConstD}; ', '<?php use const some\test\{ConstA, ConstB, ConstD}; ', ]; yield [ '<?php namespace Z\B; use const some\test\{ConstA, ConstB, ConstC}; use A\B\C; ', '<?php namespace Z\B; use const some\test\{ConstA, ConstB, ConstC}; use A\B\C; ', ]; yield [ ' <?php use some\a\ClassA; use function some\a\fn_a; use const some\c; ', ' <?php use some\a\ClassA; use function some\a\fn_a; use const some\c; ', ]; yield [ "<?php use some\\a\\{ClassA,};\n\n", '<?php use some\a\{ClassA,};', ]; yield [ "<?php use some\\a\\{ClassA};\nuse some\\b\\{ClassB};\n\n", '<?php use some\a\{ClassA};use some\b\{ClassB};', ]; yield [ "<?php use some\\a\\{ClassA};\nuse const some\\b\\{ClassB};\n\n", '<?php use some\a\{ClassA};use const some\b\{ClassB};', ]; yield [ "<?php use some\\a\\{ClassA, ClassZ};\nuse const some\\b\\{ClassB, ClassX};\nuse function some\\d;\n\n", '<?php use some\a\{ClassA, ClassZ};use const some\b\{ClassB, ClassX};use function some\d;', ]; $imports = [ 'some\a\{ClassA, ClassB, ClassC as C,};', 'function some\a\{fn_a, fn_b, fn_c,};', 'const some\a\{ConstA,ConstB,ConstC,};', 'const some\Z\{ConstX,ConstY,ConstZ,};', ]; yield 'group types with trailing comma' => [ "<?php\nuse ".implode("\nuse ", $imports)."\n\necho 1;", "<?php\nuse ".implode('use ', $imports).' echo 1;', ]; foreach ($imports as $import) { $case = [ "<?php\nuse ".$import."\n\necho 1;", "<?php\nuse ".$import.' echo 1;', ]; yield [ str_replace('some', '\some', $case[0]), str_replace('some', '\some', $case[1]), ]; yield $case; } } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php namespace A\\B;\r\n use D;\r\n\r\n class C {}", "<?php namespace A\\B;\r\n use D;\r\n\r\n\r\n class C {}", ]; yield [ "<?php namespace A\\B;\r\n use D;\r\n\r\n class C {}", "<?php namespace A\\B;\r\n use D;\r\n class C {}", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/NoUnneededImportAliasFixerTest.php
tests/Fixer/Import/NoUnneededImportAliasFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\NoUnneededImportAliasFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\NoUnneededImportAliasFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoUnneededImportAliasFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php use some\ns\{ClassA, ClassB, ClassC };', '<?php use some\ns\{ClassA, ClassB, ClassC as ClassC};', ]; yield [ '<?php use A\B\C , D\E\F as G; use const X\Y\Z as Z1, U\V\W ; use function U\V\W\FX , U\V\W\FY ; ', '<?php use A\B\C as C, D\E\F as G; use const X\Y\Z as Z1, U\V\W as W; use function U\V\W\FX as FX, U\V\W\FY as FY; ', ]; yield [ '<?php use F ; use X as x; use const CA ; use function FW ; ', '<?php use F as F; use X as x; use const CA as CA; use function FW as FW; ', ]; yield [ '<?php use /* 1 */\F ; use const \CA/* 2 */ /* 3 */; use /* 4 */ function/* 5 */ \FW /* 6 */ /* 7 */ ; ', '<?php use /* 1 */\F as F; use const \CA/* 2 */ as CA/* 3 */; use /* 4 */ function/* 5 */ \FW /* 6 */ as /* 7 */ FW; ', ]; yield [ '<?php use \F\B\C ; use const \X\Y\CA ; use function \U\V\FW ; ', '<?php use \F\B\C as C; use const \X\Y\CA as CA; use function \U\V\FW as FW; ', ]; yield [ '<?php use A\B ?> X <?php use C\D; use E\F ?>', '<?php use A\B as B ?> X <?php use C\D; use E\F as F ?>', ]; yield [ '<?php use A\B ?>', '<?php use A\B as B ?>', ]; yield [ '<?php foreach ($a as $a) {}', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/GroupImportFixerTest.php
tests/Fixer/Import/GroupImportFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Fixer\Import\GroupImportFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\GroupImportFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\GroupImportFixer> * * @author Volodymyr Kupriienko <vldmr.kuprienko@gmail.com> * @author Greg Korba <greg@codito.dev> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\GroupImportFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class GroupImportFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @phpstan-param _AutogeneratedInputConfiguration $configuration */ public function testFix(string $expected, ?string $input = null, ?array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php namespace X1\Y1; use A\{B as X, C as Y}; use B\C; ', '<?php namespace X1\Y1; use A\B as X; use A\C as Y; use B\C; ', ]; yield [ '<?php namespace Foo\Bar; use A\{B as X, C as Y}; ', '<?php namespace Foo\Bar; use A\B as X; use A\C as Y; ', ]; yield [ '<?php namespace Test; use Foo\{Bar, Baz, Test}; ', '<?php namespace Test; use Foo\Bar; use Foo\Baz; use Foo\Test; ', ]; yield [ '<?php use App\First; use Foo\{Bar, Baz}; use Test\Second; ', '<?php use App\First; use Foo\Bar; use Test\Second; use Foo\Baz; ', ]; yield [ '<?php use App\{First, Second}; use Foo\{Bar, Baz}; ', '<?php use Foo\Bar; use Foo\Baz; use App\First; use App\Second; ', ]; yield [ '<?php use App\{First, Second}; use Foo\{Bar, Baz}; ', '<?php use Foo\Bar; use App\First; use Foo\Baz; use App\Second; ', ]; yield [ '<?php use Foo\{Bar as Test, Baz}; use App; ', '<?php use Foo\Bar as Test; use Foo\Baz; use App; ', ]; yield [ '<?php use App\Repository\{Customer as Client, Profile, User}; ', '<?php use App\Repository\User; use App\Repository\Profile; use App\Repository\Customer as Client; ', ]; yield [ '<?php use function Foo\{Bar, Baz, Test as Alias}; ', '<?php use function Foo\Bar; use function Foo\Baz; use function Foo\Test as Alias; ', ]; yield [ '<?php use const Some\Place\{A, B, C as D}; ', '<?php use const Some\Place\A; use const Some\Place\B; use const Some\Place\C as D; ', ]; yield [ '<?php use Foo\Bar; use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\{One, Two}; ', '<?php use Foo\Bar; use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\One; use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\Two; ', ]; yield [ '<?php use Foo\{Bar, Baz}; use Foo\Baz\John\{Doe, Smith}; use Foo\Baz\Johnny\{DoeSecond, SmithSecond}; use Foo\Baz\John\Smith\Junior; ', '<?php use Foo\Bar; use Foo\Baz; use Foo\Baz\John\Doe; use Foo\Baz\John\Smith; use Foo\Baz\John\Smith\Junior; use Foo\Baz\Johnny\DoeSecond; use Foo\Baz\Johnny\SmithSecond; ', ]; yield [ '<?php use PhpCsFixer\Tokenizer\{AbstractTransformer, CT, Token, Tokens}; ', '<?php use PhpCsFixer\Tokenizer\AbstractTransformer; use PhpCsFixer\Tokenizer\CT; use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; ', ]; yield [ '<?php use Foo\{Bar, Baz}; ', '<?php use Foo\Bar;use Foo\Baz; ', ]; yield [ '<?php use Foo\{Bar, Baz}; \DontTouch::me(); ', '<?php use Foo\Bar;use Foo\Baz;\DontTouch::me(); ', ]; yield [ '<?php use Foo\{Bar, Baz}; use ReflectionClass; use ReflectionMethod; ', '<?php use Foo\Bar; use Foo\Baz; use ReflectionClass; use ReflectionMethod; ', ]; yield [ '<?php use Foo\{Bar, Baz}; use \ReflectionClass; use \ReflectionMethod; ', '<?php use Foo\Bar; use Foo\Baz; use \ReflectionClass; use \ReflectionMethod; ', ]; yield [ '<?php use Framework\Database\Query\JoinClause; use Framework\Support\{Arr, Collection}; use Framework\Database\ORM\{Model, SoftDeletes}; use Framework\Notifications\{Notifiable, Notification}; use Framework\Support\Facades\{DB, Log}; use Framework\Database\ORM\Relations\{BelongsTo, HasOne}; ', '<?php use Framework\Database\ORM\Model; use Framework\Database\ORM\Relations\BelongsTo; use Framework\Database\ORM\Relations\HasOne; use Framework\Database\ORM\SoftDeletes; use Framework\Database\Query\JoinClause; use Framework\Notifications\Notifiable; use Framework\Notifications\Notification; use Framework\Support\Arr; use Framework\Support\Collection; use Framework\Support\Facades\DB; use Framework\Support\Facades\Log; ', ]; yield [ '<?php use Framework\Bar\{Class3, Class4, Class5}; use Framework\Foo\{Class1, Class2, Class7}; use Framework\Baz\Class6; ', '<?php use Framework\Foo\Class1; use Framework\Foo\Class2; use Framework\Bar\Class3; use Framework\Bar\Class4; use Framework\Bar\Class5; use Framework\Baz\Class6; use Framework\Foo\Class7; ', ]; yield [ '<?php use Foo\{Bar, Baz}; use function Foo\baz; ', '<?php use Foo\Bar; use function Foo\baz; use Foo\Baz; ', ]; yield [ '<?php use Foo\Bar\C; use Foo\{D, E}; use Foo\Bar\Baz\{A, B}; use function Foo\{a, b}; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use Foo\Bar\C; use Foo\D; use Foo\E; use function Foo\a; use function Foo\b; ', ]; yield 'only class-like' => [ '<?php use Foo\Bar\C; use Foo\{D, E}; use Foo\Bar\Baz\{A, B}; use function Foo\a; use function Foo\b; use const Foo\X; use const Foo\Y; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use Foo\Bar\C; use Foo\D; use Foo\E; use function Foo\a; use function Foo\b; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_CLASSY]], ]; yield 'only functions' => [ '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\{a, b}; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS]], ]; yield 'only constants' => [ '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\{X, Y}; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_CONSTANTS]], ]; yield 'class-likes and functions' => [ '<?php use function Foo\{a, b}; use Foo\Bar\C; use Foo\{D, E}; use Foo\Bar\Baz\{A, B}; use const Foo\X; use const Foo\Y; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_FUNCTIONS]], ]; yield 'class-likes and constants' => [ '<?php use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\{D, E}; use Foo\Bar\Baz\{A, B}; use const Foo\{X, Y}; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_CONSTANTS]], ]; yield 'functions and constants' => [ '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\{a, b}; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\{X, Y}; ', '<?php use Foo\Bar\Baz\A; use Foo\Bar\Baz\B; use function Foo\a; use function Foo\b; use Foo\Bar\C; use Foo\D; use Foo\E; use const Foo\X; use const Foo\Y; ', ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS, GroupImportFixer::GROUP_CONSTANTS]], ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php /*1*/use A\{B, C, D}; /*2*//*3*//*4*//*5*//*6*/ /*7*//*8*//*9*//*10*//*11*//*12*/ /*13*//*14*//*15*//*16*//*17*//*18*/ ', '<?php /*1*/use/*2*/A/*3*/\/*4*/B/*5*/;/*6*/ /*7*/use/*8*/A/*9*/\/*10*/C/*11*/;/*12*/ /*13*/use/*14*/A/*15*/\/*16*/D/*17*/;/*18*/ ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/NoUnusedImportsFixerTest.php
tests/Fixer/Import/NoUnusedImportsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\NoUnusedImportsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\NoUnusedImportsFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoUnusedImportsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield 'simple' => [ <<<'EOF' <?php use Foo\Bar; use Foo\Bar\FooBar as FooBaz; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new SomeClass(); use Symfony\Annotation\Template; use Symfony\Doctrine\Entities\Entity; use Symfony\Array123\ArrayInterface; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ } } EOF, <<<'EOF' <?php use Foo\Bar; use Foo\Bar\Baz; use Foo\Bar\FooBar as FooBaz; use Foo\Bar\Foo as Fooo; use Foo\Bar\Baar\Baar; use SomeClass; $a = new Bar(); $a = new FooBaz(); $a = new SomeClass(); use Symfony\Annotation\Template; use Symfony\Doctrine\Entities\Entity; use Symfony\Array123\ArrayInterface; class AnnotatedClass { /** * @Template(foobar=21) * @param Entity $foo */ public function doSomething($foo) { $bar = $foo->toArray(); /** @var ArrayInterface $bar */ } } EOF, ]; yield 'with_indents' => [ <<<'EOF' <?php use Foo\Bar; $foo = 1; use Foo\Bar\FooBar as FooBaz; use SomeClassIndented; $a = new Bar(); $a = new FooBaz(); $a = new SomeClassIndented(); EOF, <<<'EOF' <?php use Foo\Bar; use Foo\Bar\Baz; $foo = 1; use Foo\Bar\FooBar as FooBaz; use Foo\Bar\Foo as Fooo; use Foo\Bar\Baar\Baar; use SomeClassIndented; $a = new Bar(); $a = new FooBaz(); $a = new SomeClassIndented(); EOF, ]; yield 'in_same_namespace_1' => [ <<<'EOF' <?php namespace Foo\Bar\FooBar; use Foo\Bar\FooBar\Foo as Fooz; use Foo\Bar\FooBar\Aaa\Bbb; use XYZ\FQCN_XYZ; $a = new Baz(); $b = new Fooz(); $c = new Bar\Fooz(); $d = new Bbb(); $e = new FQCN_Babo(); $f = new FQCN_XYZ(); EOF, <<<'EOF' <?php namespace Foo\Bar\FooBar; use Foo\Bar\FooBar\Baz; use Foo\Bar\FooBar\Foo as Fooz; use Foo\Bar\FooBar\Bar; use Foo\Bar\FooBar\Aaa\Bbb; use \Foo\Bar\FooBar\FQCN_Babo; use XYZ\FQCN_XYZ; $a = new Baz(); $b = new Fooz(); $c = new Bar\Fooz(); $d = new Bbb(); $e = new FQCN_Babo(); $f = new FQCN_XYZ(); EOF, ]; yield 'in_same_namespace_2' => [ <<<'EOF' <?php namespace App\Http\Controllers; EOF, <<<'EOF' <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; EOF, ]; yield 'in_same_namespace_multiple_1' => [ <<<'EOF' <?php namespace Foooooooo; namespace Foo; use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); EOF, <<<'EOF' <?php namespace Foooooooo; namespace Foo; use Foo\Bar; use Foo\Baz; use Foooooooo\Baaaar; use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); EOF, ]; yield 'in_same_namespace_multiple_2' => [ <<<'EOF' <?php namespace Foooooooo; use Foo\Bar; $a = new Baaaar(); $b = new Baaaaz(); $c = new Bar(); namespace Foo; use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); EOF, <<<'EOF' <?php namespace Foooooooo; use Foo\Bar; use Foo\Baz; use Foooooooo\Baaaar; use Foooooooo\Baaaaz; $a = new Baaaar(); $b = new Baaaaz(); $c = new Bar(); namespace Foo; use Foo\Bar; use Foo\Baz; use Foooooooo\Baaaar; use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); EOF, ]; yield 'in_same_namespace_multiple_braces' => [ <<<'EOF' <?php namespace Foooooooo { use Foo\Bar; $a = new Baaaar(); $b = new Baaaaz(); $c = new Bar(); } namespace Foo { use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); } EOF, <<<'EOF' <?php namespace Foooooooo { use Foo\Bar; use Foo\Baz; use Foooooooo\Baaaar; use Foooooooo\Baaaaz; $a = new Baaaar(); $b = new Baaaaz(); $c = new Bar(); } namespace Foo { use Foo\Bar; use Foo\Baz; use Foooooooo\Baaaar; use Foooooooo\Baaaaz; $a = new Bar(); $b = new Baz(); $c = new Baaaaz(); } EOF, ]; yield 'multiple_use' => [ <<<'EOF' <?php namespace Foo; use BarE; $c = new D(); $e = new BarE(); EOF, <<<'EOF' <?php namespace Foo; use Bar; use BarA; use BarB, BarC as C, BarD; use BarB2; use BarB\B2; use BarE; use function fun_a, fun_b, fun_c; use const CONST_A, CONST_B, CONST_C; $c = new D(); $e = new BarE(); EOF, ]; yield 'with_braces' => [ <<<'EOF' <?php namespace Foo\Bar\FooBar { use Foo\Bar\FooBar\Foo as Fooz; use Foo\Bar\FooBar\Aaa\Bbb; $a = new Baz(); $b = new Fooz(); $c = new Bar\Fooz(); $d = new Bbb(); } EOF, <<<'EOF' <?php namespace Foo\Bar\FooBar { use Foo\Bar\FooBar\Baz; use Foo\Bar\FooBar\Foo as Fooz; use Foo\Bar\FooBar\Bar; use Foo\Bar\FooBar\Aaa\Bbb; $a = new Baz(); $b = new Fooz(); $c = new Bar\Fooz(); $d = new Bbb(); } EOF, ]; yield 'trailing_spaces' => [ <<<'EOF' <?php use Foo\Bar ; use Foo\Bar\FooBar as FooBaz ; $a = new Bar(); $a = new FooBaz(); EOF, <<<'EOF' <?php use Foo\Bar ; use Foo\Bar\FooBar as FooBaz ; use Foo\Bar\Foo as Fooo ; use SomeClass ; $a = new Bar(); $a = new FooBaz(); EOF, ]; yield 'traits' => [ <<<'EOF' <?php use Foo as Bar; use A\MyTrait1; class MyParent { use MyTrait1; use MyTrait2; use Bar; } EOF, <<<'EOF' <?php use Foo; use Foo as Bar; use A\MyTrait1; class MyParent { use MyTrait1; use MyTrait2; use Bar; } EOF, ]; yield 'function_use' => [ <<<'EOF' <?php use Foo; $f = new Foo(); $a = function ($item) use ($f) { return !in_array($item, $f); }; EOF, <<<'EOF' <?php use Foo; use Bar; $f = new Foo(); $a = function ($item) use ($f) { return !in_array($item, $f); }; EOF, ]; yield 'similar_names' => [ <<<'EOF' <?php use SomeEntityRepository; class SomeService { public function __construct(SomeEntityRepository $repo) { $this->repo = $repo; } } EOF, <<<'EOF' <?php use SomeEntityRepository; use SomeEntity; class SomeService { public function __construct(SomeEntityRepository $repo) { $this->repo = $repo; } } EOF, ]; yield 'variable_name' => [ <<<'EOF' <?php $bar = null; EOF, <<<'EOF' <?php use Foo\Bar; $bar = null; EOF, ]; yield 'property name, method name, static method call, static property' => [ <<<'EOF' <?php $foo->bar = null; $foo->bar(); $foo::bar(); $foo::bar; EOF, <<<'EOF' <?php use Foo\Bar; $foo->bar = null; $foo->bar(); $foo::bar(); $foo::bar; EOF, ]; yield 'constant_name' => [ <<<'EOF' <?php class Baz { const BAR = 0; } EOF, <<<'EOF' <?php use Foo\Bar; class Baz { const BAR = 0; } EOF, ]; yield 'namespace_part' => [ <<<'EOF' <?php new \Baz\Bar(); EOF, <<<'EOF' <?php use Foo\Bar; new \Baz\Bar(); EOF, ]; yield 'use_in_string_1' => [ <<<'EOF' <?php $x=<<<'EOA' use a; use b; EOA; EOF, ]; yield 'use_in_string_2' => [ <<<'EOF' <?php $x=' use a; use b; '; EOF, ]; yield 'use_in_string_3' => [ <<<'EOF' <?php $x=" use a; use b; "; EOF, ]; yield 'import_in_global_namespace' => [ <<<'EOF' <?php namespace A; use \SplFileInfo; new SplFileInfo(__FILE__); EOF, ]; yield 'no_import_in_global_namespace' => [ <<<'EOF' <?php namespace A; new \SplFileInfo(__FILE__); EOF, <<<'EOF' <?php namespace A; use SplFileInfo; new \SplFileInfo(__FILE__); EOF, ]; yield 'no_import_attribute_in_global_namespace' => [ <<<'EOF' <?php namespace A; #[\Attribute(\Attribute::TARGET_PROPERTY)] final class B {} EOF, <<<'EOF' <?php namespace A; use Attribute; #[\Attribute(\Attribute::TARGET_PROPERTY)] final class B {} EOF, ]; yield 'use_as_last_statement' => [ <<<'EOF' <?php EOF, <<<'EOF' <?php use Bar\Finder; EOF, ]; yield 'use_with_same_last_part_that_is_in_namespace' => [ <<<'EOF' <?php namespace Foo\Finder; EOF, <<<'EOF' <?php namespace Foo\Finder; use Bar\Finder; EOF, ]; yield 'used_use_with_same_last_part_that_is_in_namespace' => [ <<<'EOF' <?php namespace Foo\Finder; use Bar\Finder; class Baz extends Finder { } EOF, ]; yield 'foo' => [ <<<'EOF' <?php namespace Aaa; class Ddd { } EOF, <<<'EOF' <?php namespace Aaa; use Aaa\Bbb; use Ccc; class Ddd { } EOF, ]; yield 'close_tag_1' => [ '<?php ?>inline content<?php ?>', '<?php use A\AA; use B\C?>inline content<?php use A\D; use E\F ?>', ]; yield 'close_tag_2' => [ '<?php ?>', '<?php use A\B;?>', ]; yield 'close_tag_3' => [ '<?php ?>', '<?php use A\B?>', ]; yield 'case_mismatch_typo' => [ '<?php use Foo\exception; // must be kept by non-risky fixer try { x(); } catch (Exception $e) { echo \'Foo\Exception caught\'; } catch (\Exception $e) { echo \'Exception caught\'; } ', ]; yield 'with_matches_in_comments' => [ '<?php use Foo; use Bar; use Baz; //Foo #Bar /*Baz*/', ]; yield 'with_case_insensitive_matches_in_comments' => [ '<?php use Foo; use Bar; use Baz; //foo #bar /*baz*/', ]; yield 'with_same_namespace_import_and_unused_import' => [ <<<'EOF' <?php namespace Foo; use Bar\C; /* test */ abstract class D extends A implements C { } EOF, <<<'EOF' <?php namespace Foo; use Bar\C; use Foo\A; use Foo\Bar\B /* test */ ; abstract class D extends A implements C { } EOF, ]; yield 'with_same_namespace_import_and_unused_import_after_namespace_statement' => [ <<<'EOF' <?php namespace Foo; use Foo\Bar\C; abstract class D extends A implements C { } EOF, <<<'EOF' <?php namespace Foo; use Foo\A; use Foo\Bar\B; use Foo\Bar\C; abstract class D extends A implements C { } EOF, ]; yield 'wrong_casing' => [ <<<'EOF' <?php use Foo\Foo; use Bar\Bar; $a = new FOO(); $b = new bar(); EOF, ]; yield 'phpdoc_unused' => [ <<<'EOF' <?php class Foo extends \PHPUnit_Framework_TestCase { /** * @expectedException \Exception */ public function testBar() { } } EOF, <<<'EOF' <?php use Some\Exception; class Foo extends \PHPUnit_Framework_TestCase { /** * @expectedException \Exception */ public function testBar() { } } EOF, ]; yield 'imported_class_is_used_for_constants_1' => [ '<?php use A\ABC; $a = 5-ABC::Test; $a = 5-ABC::Test-5; $a = ABC::Test-5; ', ]; yield 'imported_class_is_used_for_constants_2' => [ '<?php use A\ABC; $a = 5-ABC::Test; $a = 5-ABC::Test-5; ', ]; yield 'imported_class_is_used_for_constants_3' => [ '<?php use A\ABC; $a = 5-ABC::Test; ', ]; yield 'imported_class_is_used_for_constants_4' => ['<?php use A\ABC; $a = ABC::Test-5; ', ]; yield 'imported_class_is_used_for_constants_5' => ['<?php use A\ABC; $a = 5-ABC::Test-5; ', ]; yield 'imported_class_is_used_for_constants_6' => ['<?php use A\ABC; $b = $a-->ABC::Test; ', ]; yield 'imported_class_name_is_prefix_with_dash_of_constant' => [ <<<'EOF' <?php class Dummy { const C = 'bar-bados'; } EOF, <<<'EOF' <?php use Foo\Bar; class Dummy { const C = 'bar-bados'; } EOF, ]; yield 'imported_class_name_is_suffix_with_dash_of_constant' => [ <<<'EOF' <?php class Dummy { const C = 'tool-bar'; } EOF, <<<'EOF' <?php use Foo\Bar; class Dummy { const C = 'tool-bar'; } EOF, ]; yield 'imported_class_name_is_inside_with_dash_of_constant' => [ <<<'EOF' <?php class Dummy { const C = 'tool-bar-bados'; } EOF, <<<'EOF' <?php use Foo\Bar; class Dummy { const C = 'tool-bar-bados'; } EOF, ]; yield 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_new_lines_and_is_uppercase' => [ <<<'EOF' <?php namespace Foo; use function is_int; is_int(1); EOF, <<<'EOF' <?php namespace Foo; use function is_int; use function is_float; is_int(1); EOF, ]; yield 'constants_in_the_global_namespace_should_not_be_removed' => [ <<<'EOF' <?php namespace Foo; use const PHP_INT_MAX; echo PHP_INT_MAX; EOF, <<<'EOF' <?php namespace Foo; use const PHP_INT_MAX; use const PHP_INT_MIN; echo PHP_INT_MAX; EOF, ]; yield 'functions_in_the_global_namespace_should_not_be_removed_even_when_declaration_has_ne_lines_and_is_uppercase' => [ <<<'EOF' <?php namespace Foo;use/**/FUNCTION#1 is_int;#2 is_int(1); EOF, <<<'EOF' <?php namespace Foo;use/**/FUNCTION#1 is_int;#2 use function is_float; use const PHP_INT_MIN; is_int(1); EOF, ]; yield 'use_trait should never be removed' => [ <<<'EOF' <?php class UsesTraits { /** * @see #4086 */ private function withComplexStringVariable() { $name = 'World'; return "Hello, {$name}!"; } use MyTrait; } EOF, ]; yield 'imported_name_is_part_of_namespace' => [ <<<'EOF' <?php namespace App\Foo; class Baz { } EOF, <<<'EOF' <?php namespace App\Foo; use Foo\Bar\App; class Baz { } EOF, ]; yield 'imported_name_is_part_of_namespace with closing tag' => [ <<<'EOF' <?php namespace A\B {?> <?php require_once __DIR__.'/test2.php' ?> <?php use X\Z\Y ?> <?php $y = new Y() ?> <?php var_dump($y);} EOF, ]; yield [ '<?php use App\Http\Requests\StoreRequest; class StoreController { /** * @param \App\Http\Requests\StoreRequest $request */ public function __invoke(StoreRequest $request) {} }', '<?php use App\Http\Requests\StoreRequest; use Illuminate\Http\Request; class StoreController { /** * @param \App\Http\Requests\StoreRequest $request */ public function __invoke(StoreRequest $request) {} }', ]; yield 'unused import matching function call' => [ '<?php namespace Foo; bar();', '<?php namespace Foo; use Bar; bar();', ]; yield 'unused import matching function declaration' => [ '<?php namespace Foo; function bar () {}', '<?php namespace Foo; use Bar; function bar () {}', ]; yield 'unused import matching method declaration' => [ '<?php namespace Foo; class Foo { public function bar () {} }', '<?php namespace Foo; use Bar; class Foo { public function bar () {} }', ]; yield 'unused import matching constant usage' => [ '<?php namespace Foo; echo BAR;', '<?php namespace Foo; use Bar; echo BAR;', ]; yield 'unused import matching class constant' => [ '<?php namespace Foo; class Foo { const BAR = 1; }', '<?php namespace Foo; use Bar; class Foo { const BAR = 1; }', ]; yield 'unused function import matching class usage' => [ '<?php namespace Foo; new Bar(); Baz::method();', '<?php namespace Foo; use function bar; use function baz; new Bar(); Baz::method();', ]; yield 'unused function import matching method call' => [ '<?php namespace Foo; Foo::bar();', '<?php namespace Foo; use function bar; Foo::bar();', ]; yield 'unused function import matching method declaration' => [ '<?php namespace Foo; class Foo { public function bar () {} }', '<?php namespace Foo; use function bar; class Foo { public function bar () {} }', ]; yield 'unused function import matching constant usage' => [ '<?php namespace Foo; echo BAR;', '<?php namespace Foo; use function bar; echo BAR;', ]; yield 'unused function import matching class constant' => [ '<?php namespace Foo; class Foo { const BAR = 1; }', '<?php namespace Foo; use function bar; class Foo { const BAR = 1; }', ]; yield 'unused constant import matching function call' => [ '<?php namespace Foo; bar();', '<?php namespace Foo; use const BAR; bar();', ]; yield 'unused constant import matching function declaration' => [ '<?php namespace Foo; function bar () {}', '<?php namespace Foo; use const BAR; function bar () {}', ]; yield 'unused constant import matching method declaration' => [ '<?php namespace Foo; class Foo { public function bar () {} }', '<?php namespace Foo; use const BAR; class Foo { public function bar () {} }', ]; yield 'unused constant import matching class constant' => [ '<?php namespace Foo; class Foo { const BAR = 1; }', '<?php namespace Foo; use const BAR; class Foo { const BAR = 1; }', ]; yield 'attribute without braces' => [ '<?php use Foo; class Controller { #[Foo] public function foo() {} }', ]; yield 'attribute with braces' => [ '<?php use Foo; class Controller { #[Foo()] public function foo() {} }', ]; yield 'go to' => [ '<?php Bar1: Bar2: Bar3: ', '<?php use Bar1; use const Bar2; use function Bar3; Bar1: Bar2: Bar3: ', ]; yield [ <<<'EOF' <?php use some\a\{ClassD}; use function some\c\{fn_a}; use const some\d\{ConstB}; new CLassD(); echo fn_a(ConstB); EOF, <<<'EOF' <?php use some\a\{ClassD}; use some\b\{ClassA, ClassB, ClassC as C}; use function some\c\{fn_a, fn_b, fn_c}; use const some\d\{ConstA, ConstB, ConstC}; new CLassD(); echo fn_a(ConstB); EOF, ]; yield 'grouped imports' => [ <<<'EOF' <?php use some\y\{ClassA, ClassC as C,}; use function some\a\{ fn_b, }; use const some\Z\{ConstA,ConstC,}; echo ConstA.ConstC; fn_b(ClassA::test, new C()); EOF, <<<'EOF' <?php use A\{B,}; use some\y\{ClassA, ClassB, ClassC as C,}; use function some\a\{ fn_a, fn_b, fn_c, }; use function some\b\{fn_x, fn_y, fn_z,}; use const some\Z\{ConstA,ConstB,ConstC,}; use const some\X\{ConstX,ConstY,ConstZ}; use C\{D,E,}; use Z; echo ConstA.ConstC; fn_b(ClassA::test, new C()); EOF, ]; yield 'multiline grouped imports with comments' => [ <<<'EOF' <?php use function some\a\{ // Foo fn_b, /* Bar *//** Baz */ # Buzz }; fn_b(); EOF, <<<'EOF' <?php use function some\a\{ fn_a, // Foo fn_b, /* Bar */ fn_c, /** Baz */ fn_d, # Buzz }; fn_b(); EOF, ]; yield 'comma-separated imports' => [ <<<'EOF' <?php use A; use function fn_b; use const ConstC; fn_b(new A(), ConstC); EOF, <<<'EOF' <?php use A, B, C; use function fn_a, fn_b, fn_c; use const ConstA, ConstB, ConstC; fn_b(new A(), ConstC); EOF, ]; yield 'only unused comma-separated imports in single line' => [ '<?php ', '<?php use A, B, C;', ]; yield 'only unused grouped imports in single line' => [ '<?php ', '<?php use A\{B, C};', ]; yield 'unused comma-separated imports right after open tag, with consecutive lines' => [ "<?php \n# Comment", "<?php use A, B, C;\n\n# Comment", ]; yield 'unused grouped imports right after open tag, with consecutive lines' => [ "<?php \n# Comment", "<?php use A\\{B, C};\n\n# Comment", ]; yield 'unused comma-separated imports right after open tag with a non-empty token after it, and with consecutive lines' => [ "<?php # Comment\n\n# Another comment", "<?php use A, B, C; # Comment\n\n# Another comment", ]; yield 'unused grouped imports right after open tag with a non-empty token after it, and with consecutive lines' => [ "<?php # Comment\n\n# Another comment", "<?php use A\\{B, C}; # Comment\n\n# Another comment", ];
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
true
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Import/SingleImportPerStatementFixerTest.php
tests/Fixer/Import/SingleImportPerStatementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class SingleImportPerStatementFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void { $this->fixer->configure($configuration); $this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig()); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?php /**/use Foo; use FooB; ', '<?php /**/use Foo,FooB; ', ]; yield [ <<<'EOF' use Some, Not, PHP, Like, Use, Statement; <?php use Foo; use FooA; use FooB; use FooC; use FooD as D; use FooE; use FooF; use FooG as G; use FooH; use FooI; use FooJ; use FooZ; EOF, <<<'EOF' use Some, Not, PHP, Like, Use, Statement; <?php use Foo; use FooA, FooB; use FooC, FooD as D, FooE; use FooF, FooG as G, FooH, FooI, FooJ; use FooZ; EOF, ]; yield [ <<<'EOF' <?php namespace { use Foo; use FooA; use FooB; use FooC; use FooD as D; use FooE; use FooF; use FooG as G; use FooH; use FooI; use FooJ; use FooZ; } namespace Boo { use Bar; use BarA; use BarB; use BarC; use BarD as D; use BarE; use BarF; use BarG as G; use BarH; use BarI; use BarJ; use BarZ; } EOF, <<<'EOF' <?php namespace { use Foo; use FooA, FooB; use FooC, FooD as D, FooE; use FooF, FooG as G, FooH, FooI, FooJ; use FooZ; } namespace Boo { use Bar; use BarA, BarB; use BarC, BarD as D, BarE; use BarF, BarG as G, BarH, BarI, BarJ; use BarZ; } EOF, ]; yield [ '<?php use FooA; use FooB; ', '<?php use FooA, FooB; ', ]; yield [ '<?php use FooA; use FooB?>', '<?php use FooA, FooB?>', ]; yield [ '<?php use B; use C; use E; use F; use G; use H; ', '<?php use B,C; use E,F; use G,H; ', ]; yield [ '<?php use B; /* */use C; ', '<?php use B, /* */C; ', ]; yield [ '<?php use A; use B; //,{} use ; : #,{} use ; : /*,{} use ; :*/ use C ; ', '<?php use A,B, //,{} use ; : #,{} use ; : /*,{} use ; :*/ C ; ', ]; yield [ '<?php use Z ; use X ?><?php new X(); // run before white space around semicolon', '<?php use Z , X ?><?php new X(); // run before white space around semicolon', ]; yield [ '<?php use FooA# ;# # use FooB;', '<?php use FooA# ,# # FooB;', ]; yield [ '<?php use some\b\ClassB; use function some\b\CC as C; use function some\b\D; use const some\b\E; use function some\b\A\B;', '<?php use some\b\{ClassB, function CC as C, function D, const E, function A\B};', ]; yield [ '<?php use Foo\Bar; use Foo\Baz;', '<?php use Foo\ { Bar, Baz };', ]; yield [ '<?php use Foo\Bar; use Foo\Baz;', '<?php use Foo\ { Bar, Baz };', ]; yield [ '<?php use function md5; use function str_repeat; use const true; use const false; use A; use B; ', '<?php use function md5, str_repeat; use const true, false; use A,B; ', ]; yield [ '<?php use D\E; use D\F; use G\H; use G\I/*1*//*2*/; ', '<?php use D\{E,F,}; use G\{H,I/*1*/,/*2*/}; ', ]; yield [ '<?php use A\B; ', '<?php use A\{B}; ', ]; yield [ '<?php use Space\Models\TestModelA; use Space\Models\TestModelB; use Space\Models\TestModel;', '<?php use Space\Models\ { TestModelA, TestModelB, TestModel, };', ]; yield [ '<?php use Space\Models\ { TestModelA, TestModelB, TestModel, };', null, ['group_to_single_imports' => false], ]; yield [ "<?php\r\n use FooA;\r\n use FooB;", "<?php\r\n use FooA, FooB;", [], new WhitespacesFixerConfig("\t", "\r\n"), ]; } /** * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<array{string, string}> */ public static function provideFixPre80Cases(): iterable { yield [ '<?php use some\a\ClassA; use some\a\ClassB; use some\a\ClassC as C; use function some\b\fn_a; use function some\b\fn_b; use function some\b\fn_c; use const some\c\ConstA/**/as/**/E; /* group comment */ use const some\c\ConstB as D; use const some\c\// use.,{} ConstC; use A\B; use D\E; use D\F; ', '<?php use some\a\{ClassA, ClassB, ClassC as C}; use function some\b\{fn_a, fn_b, fn_c}; use const/* group comment */some\c\{ConstA/**/as/**/ E , ConstB AS D, '.' // use.,{} ConstC}; use A\{B}; use D\{E,F}; ', ]; yield 'messy comments' => [ '<?php use D\/*1*//*2*//*3*/E; use D\/*4*//*5*//*6*//*7*//*8*//*9*/F/*10*//*11*//*12*/; ', '<?php use D\{ /*1*//*2*//*3*/E,/*4*//*5*//*6*/ /*7*//*8*//*9*/F/*10*//*11*//*12*/ }; ', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpTag/FullOpeningTagFixerTest.php
tests/Fixer/PhpTag/FullOpeningTagFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\PhpTag; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\PhpTag\FullOpeningTagFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\FullOpeningTagFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class FullOpeningTagFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield ['<?php echo \'Foo\';', '<? echo \'Foo\';']; yield ['<?php echo \'Foo\';', '<?pHp echo \'Foo\';']; yield ['<?= \'Foo\';']; yield ['<?php echo \'Foo\'; ?> PLAIN TEXT']; yield ['PLAIN TEXT<?php echo \'Foo\'; ?>']; yield ['<?php $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";', '<? $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";']; yield ['<?php echo \'Foo\'; ', '<? echo \'Foo\'; ', ]; yield [ "<?php if ('<?php' === '<?') { }", "<? if ('<?php' === '<?') { }", ]; yield [ '<?php // <?php', '<?pHP // <?php', ]; yield [ "<?php '<? ';", ]; yield [ '<?php // Replace all <? with <?php !', ]; yield [ '<?php // Replace all <? with <?pHp !', ]; yield [ '<?php /** * Convert <?= ?> to long-form <?php echo ?> and <?php ?> to <?php ?> * */', ]; yield [ "<?php \$this->data = preg_replace('/<\\?(?!xml|php)/s', '<?php ', \$this->data);", ]; yield [ 'foo <?php echo "-"; echo "aaa <?php bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <?php echo "<? ";', ]; yield [ '<?php $a = <<< "TEST" <?Php <? TEST;?> TEST; ?> <?php $a = <<< \'TEST\' <?PHP <? TEST;?> TEST; ?> ', ]; yield 'binary string' => [ '<?php echo b\'Foo\';', '<? echo b\'Foo\';', ]; yield ['<?php', '<?']; yield ["<?php\n", "<?\n"]; yield ["<?php \n", "<? \n"]; yield ["<?php \n?><?= 1?>", "<? \n?><?= 1?>"]; yield [ 'foo <?php echo "-"; echo "aaa <? bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <?php echo "<? ";', 'foo <? echo "-"; echo "aaa <? bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <? echo "<? ";', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpTag/NoClosingTagFixerTest.php
tests/Fixer/PhpTag/NoClosingTagFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\PhpTag; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\PhpTag\NoClosingTagFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\NoClosingTagFixer> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoClosingTagFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php echo \'Foo\';', '<?php echo \'Foo\'; ?>', ]; yield [ '<?php echo \'Foo\';', '<?php echo \'Foo\';?>', ]; yield [ '<?php echo \'Foo\'; ?> PLAIN TEXT', ]; yield [ 'PLAIN TEXT<?php echo \'Foo\'; ?>', ]; yield [ '<?php echo \'Foo\';', '<?php echo \'Foo\'; ?>', ]; yield [ '<?php echo \'Foo\'; ?> <p><?php echo \'this is a template\'; ?></p> <?php echo \'Foo\'; ?>', ]; yield [ '<?php echo "foo";', '<?php echo "foo" ?>', ]; yield [ '<?php class foo { public function bar() { echo "Here I am!"; } }', '<?php class foo { public function bar() { echo "Here I am!"; } }?>', ]; yield [ '<?php function bar() { echo "Here I am!"; }', '<?php function bar() { echo "Here I am!"; }?>', ]; yield [ '<?php if (true) { echo "Here I am!"; }', '<?php if (true) { echo "Here I am!"; }?>', ]; yield 'Trailing linebreak, priority issue with SingleBlankLineAtEofFixer.' => [ '<?php echo 1;', "<?php echo 1;\n?>\n", ]; yield 'Trailing comment.' => [ '<?php echo 1;// test', "<?php echo 1;// test\n?>", ]; yield 'No code' => [ '<?php ', '<?php ?>', ]; yield 'No code, only comment' => [ '<?php /* license */', '<?php /* license */ ?>', ]; yield [ '<?php ?>aa', ]; } /** * @dataProvider provideWithShortOpenTagCases */ public function testWithShortOpenTag(string $expected, ?string $input = null): void { if ('1' !== \ini_get('short_open_tag')) { self::markTestSkipped('The short_open_tag option is required to be enabled.'); } $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideWithShortOpenTagCases(): iterable { yield [ '<? echo \'Foo\';', '<? echo \'Foo\'; ?>', ]; yield [ '<? echo \'Foo\';', '<? echo \'Foo\';?>', ]; yield [ '<? echo \'Foo\'; ?> <p><? echo \'this is a template\'; ?></p> <? echo \'Foo\'; ?>', ]; yield [ '<? /**/', '<? /**/?>', ]; yield [ '<?= "somestring"; ?> <?= "anotherstring"; ?>', ]; yield [ '<?= 1;', '<?= 1; ?>', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php
tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\PhpTag; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\PhpTag\LinebreakAfterOpeningTagFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\LinebreakAfterOpeningTagFixer> * * @author Ceeram <ceeram@cakephp.org> * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class LinebreakAfterOpeningTagFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php $a = function(){ echo 1; };', '<?php $a = function(){ echo 1; };', ]; yield [ '<?php $foo = true; ?>', ]; yield [ '<?php $foo = true; ?> ', ]; yield [ '<?php $foo = true; ?>', ]; yield [ '<?php $foo = true; $bar = false; ?>', '<?php $foo = true; $bar = false; ?>', ]; yield [ '<?php $foo = true; ?> Html here <?php $bar = false; ?>', ]; yield [ '<?= $bar; $foo = $bar; ?>', ]; yield [ str_replace("\n", "\r\n", '<?php // linebreak already present in file with Windows line endings '), ]; yield 'file with shebang' => [ <<<'EOD' #!x <?php echo 1; echo 2; EOD, <<<'EOD' #!x <?php echo 1; echo 2; EOD, ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\n\$foo = true;\n", "<?php \$foo = true;\n", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpTag/EchoTagSyntaxFixerTest.php
tests/Fixer/PhpTag/EchoTagSyntaxFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\PhpTag; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\PhpTag\EchoTagSyntaxFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\EchoTagSyntaxFixer> * * @author Michele Locati <michele@locati.it> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpTag\EchoTagSyntaxFixer * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class EchoTagSyntaxFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}> */ public static function provideFixCases(): iterable { yield [ '<?= \'Foo\';', '<?php echo \'Foo\';', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\';', '<?php print \'Foo\';', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\'; ?> PLAIN TEXT', '<?php echo \'Foo\'; ?> PLAIN TEXT', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\'; ?> PLAIN TEXT', '<?php print \'Foo\'; ?> PLAIN TEXT', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ 'PLAIN TEXT<?= \'Foo\'; ?>', 'PLAIN TEXT<?php echo \'Foo\'; ?>', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ 'PLAIN TEXT<?= \'Foo\'; ?>', 'PLAIN TEXT<?php print \'Foo\'; ?>', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\'; ?> <?= \'Bar\'; ?>', '<?php echo \'Foo\'; ?> <?php echo \'Bar\'; ?>', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\'; ?> <?= \'Bar\'; ?>', '<?php print \'Foo\'; ?> <?php echo \'Bar\'; ?>', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?php echo \'Foo\'; someThingElse();', null, ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ '<?= \'Foo\'; someThingElse();', '<?php echo \'Foo\'; someThingElse();', ['format' => 'short', 'shorten_simple_statements_only' => false], ]; yield [ '<?=/*this */ /** should be in the result*/ \'Foo\';', '<?php /*this */ /** should be in the result*/ echo \'Foo\';', ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ <<<'EOT' <?=/*comment*/ 1 ?> EOT, <<<'EOT' <?php /*comment*/ echo 1 ?> EOT, ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ <<<'EOT' <?=/*comment*/ 1 ?> EOT, <<<'EOT' <?php /*comment*/ echo 1 ?> EOT, ['format' => 'short', 'shorten_simple_statements_only' => true], ]; yield [ <<<'EOT' <?=/*comment*/ 1 ?> EOT, <<<'EOT' <?php /*comment*/ echo 1 ?> EOT, ['format' => 'short', 'shorten_simple_statements_only' => true], ]; $shortToLongFormatCases = [ ['<?php <fn> 1;', '<?= 1;'], ['<?php <fn> 1;', '<?=1;'], ['<?php <fn> /**/1;', '<?=/**/1;'], ['<?php <fn> /**/ 1;', '<?=/**/ 1;'], ['<?php <fn> \'Foo\';', '<?= \'Foo\';'], ['<?php <fn> \'Foo\'; ?> PLAIN TEXT', '<?= \'Foo\'; ?> PLAIN TEXT'], ['PLAIN TEXT<?php <fn> \'Foo\'; ?>', 'PLAIN TEXT<?= \'Foo\'; ?>'], ['<?php <fn> \'Foo\'; ?> <?php <fn> \'Bar\'; ?>', '<?= \'Foo\'; ?> <?= \'Bar\'; ?>'], ['<?php <fn> foo();', '<?=foo();'], ]; foreach (['echo', 'print'] as $function) { foreach ($shortToLongFormatCases as $case) { yield [ str_replace('<fn>', $function, $case[0]), str_replace('<fn>', $function, $case[1]), ['format' => 'long', 'long_function' => $function], ]; } } } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpTag/BlankLineAfterOpeningTagFixerTest.php
tests/Fixer/PhpTag/BlankLineAfterOpeningTagFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\PhpTag; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer> * * @author Ceeram <ceeram@cakephp.org> * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class BlankLineAfterOpeningTagFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php echo 1;', '<?php echo 1;', ]; yield [ '<?php $b = 2; echo 3;', '<?php $b = 2; echo 3;', ]; yield [ '<?php '.' $c = 4; echo 5;', ]; yield [ '<?php $a = function(){ echo 1; };', '<?php $a = function(){ echo 1; };', ]; yield [ '<?php class SomeClass { const VERSION = "1.1.1"; const FOO = "bar"; } ', ]; yield [ '<?php $foo = true; ?>', ]; yield [ '<?php $foo = true; ?> ', ]; yield [ '<?php $foo = true; ?>', '<?php $foo = true; ?>', ]; yield [ '<?php $foo = true; $bar = false; ', '<?php $foo = true; $bar = false; ', ]; yield [ '<?php $foo = true; ?> Html here <?php $bar = false;', ]; yield [ '<?php $foo = true; ?> Html here <?php $bar = false; ', ]; yield [ '<?= $bar; $foo = $bar; ?>', ]; yield 'empty file with open tag without new line' => [ '<?php', ]; yield 'empty file with open tag with new line' => [ "<?php\n", ]; yield 'file with shebang' => [ <<<'EOD' #!x <?php echo 1; EOD, <<<'EOD' #!x <?php echo 1; EOD, ]; yield 'file starting with multi-line comment' => [ <<<'PHP' <?php /** * @author yes */ PHP, <<<'PHP' <?php /** * @author yes */ PHP, ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "<?php\r\n\r\n\$foo = true;\r\n", "<?php \$foo = true;\r\n", ]; yield [ "<?php\r\n\r\n\$foo = true;\r\n", "<?php\r\n\$foo = true;\r\n", ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Semicolon/NoSinglelineWhitespaceBeforeSemicolonsFixerTest.php
tests/Fixer/Semicolon/NoSinglelineWhitespaceBeforeSemicolonsFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Semicolon; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer> * * @author John Kelly <wablam@gmail.com> * @author Graham Campbell <hello@gjcampbell.co.uk> * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoSinglelineWhitespaceBeforeSemicolonsFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php for ($uu = 0; ; ++$uu) {}', '<?php for ($uu = 0 ; ; ++$uu) {}', ]; yield [ '<?php $this ->setName(\'readme1\') ->setDescription(\'Generates the README content, based on the fix command help\') ;', ]; yield [ '<?php $this ->setName(\'readme2\') ->setDescription(\'Generates the README content, based on the fix command help\') ;', ]; yield [ '<?php echo "$this->foo(\'with param containing ;\') ;";', '<?php echo "$this->foo(\'with param containing ;\') ;" ;', ]; yield [ '<?php $this->foo();', '<?php $this->foo() ;', ]; yield [ '<?php $this->foo(\'with param containing ;\');', '<?php $this->foo(\'with param containing ;\') ;', ]; yield [ '<?php $this->foo(\'with param containing ) ; \');', '<?php $this->foo(\'with param containing ) ; \') ;', ]; yield [ '<?php $this->foo("with param containing ) ; ");', '<?php $this->foo("with param containing ) ; ") ;', ]; yield [ '<?php $foo ->bar(1) ->baz(2) ;', ]; yield [ '<?php $foo ->bar(1) //->baz(2) ;', ]; yield [ '<?php $this->foo("with semicolon in string) ; ");', ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false
PHP-CS-Fixer/PHP-CS-Fixer
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Semicolon/NoEmptyStatementFixerTest.php
tests/Fixer/Semicolon/NoEmptyStatementFixerTest.php
<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien Potencier <fabien@symfony.com> * Dariusz Rumiński <dariusz.ruminski@gmail.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Semicolon; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\Semicolon\NoEmptyStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Semicolon\NoEmptyStatementFixer> * * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> * * @no-named-arguments Parameter names are not covered by the backward compatibility promise. */ final class NoEmptyStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{0: string, 1?: string}> */ public static function provideFixCases(): iterable { yield [ '<?php abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2 { } ', '<?php abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2 { }; ', ]; yield [ '<?php abstract class TestClass1 EXTENDS Test implements TestInterface { } ', '<?php abstract class TestClass1 EXTENDS Test implements TestInterface { }; ', ]; yield [ '<?php CLASS TestClass2 extends Test { } ', '<?php CLASS TestClass2 extends Test { }; ', ]; yield [ '<?php class TestClass3 implements TestInterface1 { } ', '<?php class TestClass3 implements TestInterface1 { }; ', ]; yield [ '<?php class TestClass4 { } ', '<?php class TestClass4 { }; ', ]; yield [ '<?php interface TestInterface1 { } ', '<?php interface TestInterface1 { }; ', ]; yield [ '<?php interface TestExtendingInterface extends TestInterface2, TestInterface3 { } ', '<?php interface TestExtendingInterface extends TestInterface2, TestInterface3 { }; ', ]; yield [ '<?php namespace Two { $a = 1; { } } ', '<?php namespace Two {;; $a = 1; { }; } ', ]; yield [ '<?php { '.' } echo 1; ', '<?php { ; }; echo 1; ', ]; yield [ '<?php while($time < $a) ; echo "done waiting."; $b = \Test; ', ]; yield [ '<?php if($a>1){ } ', '<?php if($a>1){ }; ', ]; yield [ '<?php if($a>1) { } else { } ', '<?php if($a>1) { } else { }; ', ]; yield [ '<?php try{ }catch (\Exception $e) { } ', '<?php try{ }catch (\Exception $e) { }; ', ]; yield [ '<?php ', '<?php ;', ]; yield [ '<?php function foo() { // a } ', '<?php function foo() { ; // a } ', ]; yield [ '<?php function foo(){}', '<?php function foo(){;;}', ]; yield [ '<?php class Test{}', '<?php class Test{};', ]; yield [ '<?php for(;;) { } ', '<?php for(;;) { }; ', ]; yield [ '<?php foreach($a as $b) { } foreach($a as $b => $c) { } ', '<?php foreach($a as $b) { }; foreach($a as $b => $c) { }; ', ]; yield [ '<?php while($a > 1){ } do { } while($a>1); // 1 ', '<?php while($a > 1){ }; do { } while($a>1); 1; // 1 ', ]; yield [ '<?php switch($a) { default : {echo 1;} } ', '<?php switch($a) { default : {echo 1;} }; ', ]; yield [ '<?php function test($a, $b) { } ', '<?php function test($a, $b) { }; ', ]; yield [ '<?php function foo($n) { '.' $a = function(){}; $b = function() use ($a) {}; ++${"a"}; switch(fooBar()) { case 5;{ } } return $n->{$o}; } ', '<?php function foo($n) { ; $a = function(){}; $b = function() use ($a) {}; ++${"a"}; switch(fooBar()) { case 5;{ } }; return $n->{$o}; }; ', ]; yield [ '<?php declare(ticks=1) { // entire script here } declare(ticks=1); ', '<?php declare(ticks=1) { // entire script here }; declare(ticks=1); ', ]; yield [ '<?php namespace A\B\C; use D; ', '<?php namespace A\B\C;;;; use D;;;; ', ]; yield [ '<?php namespace A\B\C; use D; ', '<?php namespace A\B\C; use D;;;; ', ]; yield [ '<?php namespace A\B\C;use D; ', '<?php namespace A\B\C;;use D; ', ]; yield [ '<?php trait TestTrait { } ', '<?php trait TestTrait { }; ', ]; yield [ '<?php try { throw new \Exception("Foo."); } catch (\Exception $e){ // } finally { } '.' ', '<?php try { throw new \Exception("Foo."); } catch (\Exception $e){ // } finally { } ; ', ]; foreach (['break', 'continue'] as $ops) { yield [ \sprintf('<?php while(true) {%s ;}', $ops), \sprintf('<?php while(true) {%s 1;}', $ops), ]; } foreach (['1', '1.0', '"foo"', '$foo'] as $noop) { yield [ '<?php echo "foo"; ', \sprintf('<?php echo "foo"; %s ;', $noop), ]; } yield [ '<?php /* 1 */ /* 2 */ /* 3 */ ', '<?php /* 1 */ ; /* 2 */ 1 /* 3 */ ;', ]; yield [ '<?php while(true) {while(true) {break 2;}} while(true) {continue;} ', ]; yield [ '<?php if ($foo1) {} ', '<?php if ($foo1) {} 1;', ]; yield [ '<?php if ($foo2) {}', '<?php if ($foo2) {1;}', ]; yield [ '<?php use function Functional\map; $a = new class { public function log($msg) { } }; ', ]; yield [ '<?php use function Functional\map; $a = new class extends A { }; ', ]; yield [ '<?php use function Functional\map; $a = new class implements B { }; ', ]; yield [ '<?php use function Functional\map; $a = new class extends A implements B { }; ', ]; yield [ '<?php $a = new class extends \A implements B\C { }; ', ]; yield [ '<?php {{}}', '<?php {{}};', ]; yield [ '<?php namespace A\B\C { } ', '<?php namespace A\B\C { }; ', ]; yield [ '<?php namespace A{ } ', '<?php namespace A{ }; ', ]; yield [ '<?php namespace{ } ', '<?php namespace{ }; ', ]; yield [ '<?php $foo = 2 ; // '.' ', '<?php $foo = 2 ; // ; ', ]; yield [ '<?php $foo = 3; /**/ ', '<?php $foo = 3; /**/; ;', ]; yield [ '<?php $foo = 1;', '<?php $foo = 1;;;', ]; yield [ '<?php $foo = 4; ', '<?php $foo = 4;; ;;', ]; yield [ '<?php $foo = 5; ', '<?php $foo = 5;; ; ;', ]; yield [ '<?php $foo = 6; ', '<?php $foo = 6;; ', ]; yield [ '<?php for ($i = 7; ; ++$i) {}', ]; yield [ '<?php switch($a){ case 8; echo 9; } ', '<?php switch($a){ case 8;; echo 9; } ', ]; } /** * @dataProvider provideWithShortOpenTagCases */ public function testWithShortOpenTag(string $expected, ?string $input = null): void { if ('1' !== \ini_get('short_open_tag')) { self::markTestSkipped('No short tag tests possible.'); } $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideWithShortOpenTagCases(): iterable { yield [ '<? ', '<? ;', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, string $input): void { $this->doTest($expected, $input); } /** * @return iterable<int, array{string, string}> */ public static function provideFix81Cases(): iterable { yield [ '<?php enum Foo{}', '<?php enum Foo{};', ]; } /** * @dataProvider provideFix84Cases * * @requires PHP 8.4 */ public function testFix84(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable<string, array{string, 1?: string}> */ public static function provideFix84Cases(): iterable { yield 'interface with property hooks' => [ <<<'PHP' <?php interface I { public bool $a { get; } public bool $b { get; set; } public bool $c { set; } public bool $d { set; get; } public bool $e {/* hello1 */set/* hello2 */;/* hello3 */get/* hello4 */;/* hello5 */} } PHP, ]; yield 'abstract class with property hooks' => [ <<<'PHP' <?php abstract class A { abstract public bool $a { get; set; } abstract public bool $b { get{} set; } abstract public bool $c { get; set{} } } PHP, ]; } }
php
MIT
63b024eaaf8a48445494964bd21a6c38c788f40f
2026-01-04T15:02:54.911792Z
false