1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213 |
- <?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
- */
- final class ClassDefinitionFixerTest extends AbstractFixerTestCase
- {
- public function testConfigure(): void
- {
- $defaultConfig = [
- 'inline_constructor_arguments' => true,
- 'multi_line_extends_each_single_line' => false,
- 'single_item_single_line' => false,
- 'single_line' => false,
- 'space_before_parenthesis' => false,
- ];
- $fixer = new ClassDefinitionFixer();
- $fixer->configure($defaultConfig);
- self::assertConfigurationSame($defaultConfig, $fixer);
- $fixer->configure([]);
- self::assertConfigurationSame($defaultConfig, $fixer);
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideInvalidConfigurationCases
- */
- public function testInvalidConfiguration(array $config, string $exceptionExpression): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches($exceptionExpression);
- $this->fixer->configure($config);
- }
- /**
- * @return iterable<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);
- }
- 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 array<string, mixed> $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);
- }
- 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,
- 'numberOfExtends' => 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,
- 'numberOfExtends' => 3,
- 'multiLine' => false,
- ],
- 'implements' => false,
- 'anonymousClass' => false,
- 'final' => false,
- 'abstract' => false,
- 'readonly' => false,
- ],
- ];
- }
- /**
- * @param array<string, mixed> $expected
- *
- * @dataProvider provideClassyInheritanceInfoCases
- */
- public function testClassyInheritanceInfo(string $source, string $label, array $expected): void
- {
- $this->doTestClassyInheritanceInfo($source, $label, $expected);
- }
- public static function provideClassyInheritanceInfoCases(): iterable
- {
- yield '1' => [
- '<?php
- class X11 implements Z , T,R
- {
- }',
- 'numberOfImplements',
- ['start' => 5, 'numberOfImplements' => 3, 'multiLine' => false],
- ];
- yield '2' => [
- '<?php
- class X10 implements Z , T,R //
- {
- }',
- 'numberOfImplements',
- ['start' => 5, 'numberOfImplements' => 3, 'multiLine' => false],
- ];
- yield '3' => [
- '<?php class A implements B {}',
- 'numberOfImplements',
- ['start' => 5, 'numberOfImplements' => 1, 'multiLine' => false],
- ];
- yield '4' => [
- "<?php class A implements B,\n I{}",
- 'numberOfImplements',
- ['start' => 5, 'numberOfImplements' => 2, 'multiLine' => true],
- ];
- yield '5' => [
- "<?php class A implements Z\\C\\B,C,D {\n\n\n}",
- 'numberOfImplements',
- ['start' => 5, 'numberOfImplements' => 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();
- }',
- 'numberOfImplements',
- ['start' => 36, 'numberOfImplements' => 2, 'multiLine' => false],
- ];
- yield [
- "<?php \$a = new class(3) extends\nSomeClass\timplements SomeInterface, D {};",
- 'numberOfExtends',
- ['start' => 12, 'numberOfExtends' => 1, 'multiLine' => true],
- ];
- yield [
- "<?php \$a = new class(4) extends\nSomeClass\timplements SomeInterface, D\n\n{};",
- 'numberOfImplements',
- ['start' => 16, 'numberOfImplements' => 2, 'multiLine' => false],
- ];
- yield [
- "<?php \$a = new class(5) extends SomeClass\nimplements SomeInterface, D {};",
- 'numberOfExtends',
- ['start' => 12, 'numberOfExtends' => 1, 'multiLine' => true],
- ];
- }
- /**
- * @param array<string, mixed> $expected
- *
- * @dataProvider provideClassyInheritanceInfoPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testClassyInheritanceInfoPre80(string $source, string $label, array $expected): void
- {
- $this->doTestClassyInheritanceInfo($source, $label, $expected);
- }
- 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();
- }',
- 'numberOfImplements',
- ['start' => 36, 'numberOfImplements' => 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<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<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 array<string, mixed> $expected
- */
- private function doTestClassyInheritanceInfo(string $source, string $label, 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'], $label), null, ClassDefinitionFixer::class)($this->fixer);
- self::assertSame($expected, $result);
- }
- /**
- * @param array<string, mixed> $expected
- */
- private static function assertConfigurationSame(array $expected, ClassDefinitionFixer $fixer): void
- {
- self::assertSame(
- $expected,
- \Closure::bind(static fn (ClassDefinitionFixer $fixer): array => $fixer->configuration, null, ClassDefinitionFixer::class)($fixer),
- );
- }
- /**
- * @return iterable<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<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<int|string, 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
- {
- }',
- ],
- ];
- }
- }
|