123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- <?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
- */
- 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);
- }
- 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 _AutogeneratedInputConfiguration $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<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 $config
- *
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input, array $config): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1: null|string, 2: array{consider_absent_docblock_as_internal_class? : bool, exclude?: list<string>, include?: list<string>}}>
- */
- 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 $config
- *
- * @dataProvider provideFix82Cases
- *
- * @requires PHP 8.2
- */
- public function testFix82(string $expected, ?string $input, array $config): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1: null|string, 2: array{consider_absent_docblock_as_internal_class? : bool, exclude?: list<string>, include?: list<string>}}>
- */
- 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],
- ];
- }
- }
|