From 48bf682fd90f31b407452deb4e960467024dad11 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 18 Dec 2024 11:52:41 +0100 Subject: [PATCH] use rule identifiers --- README.md | 22 +++++++++++++++++++ composer-dependency-analyser.php | 1 - config/services/services.neon | 3 +++ .../DoctrineEntityDocumentAnalyser.php | 2 +- src/Enum/ClassName.php | 2 +- src/Enum/RuleIdentifier.php | 22 +++++++++++++++++++ .../NoGetRepositoryOutsideServiceRule.php | 18 ++++++++------- .../Rules/Doctrine/NoParentRepositoryRule.php | 8 +++---- .../NoRepositoryCallInDataFixtureRule.php | 4 ++-- .../DataProviderMethodResolver.php | 2 +- .../Complexity/NoConstructorOverrideRule.php | 10 +++++---- src/Rules/Doctrine/NoEntityMockingRule.php | 9 ++++---- src/Rules/NoDynamicNameRule.php | 4 ++-- src/Rules/NoReferenceRule.php | 6 ++--- src/Rules/PHPUnit/NoMockOnlyTestRule.php | 15 +++++++------ .../PHPUnit/PublicStaticDataProviderRule.php | 11 +++++----- src/Rules/PreferredClassRule.php | 4 ++-- .../NoAbstractControllerConstructorRule.php | 10 +++++---- .../Symfony/NoListenerWithoutContractRule.php | 17 +++++++------- .../Symfony/NoRequiredOutsideClassRule.php | 4 ++-- .../NoStringInGetSubscribedEventsRule.php | 8 ++++--- .../Symfony/SingleArgEventDispatchRule.php | 10 +++++---- src/Testing/PHPUnitTestAnalyser.php | 5 ++++- .../config/configured_rule.neon | 2 +- .../NoMockOnlyTestRuleTest.php | 2 +- .../PublicStaticDataProviderRuleTest.php | 2 +- .../RequireInvokableControllerRuleTest.php | 2 +- 27 files changed, 134 insertions(+), 71 deletions(-) rename src/{Testing => PHPUnit}/DataProviderMethodResolver.php (94%) diff --git a/README.md b/README.md index e0b17607..65172d98 100644 --- a/README.md +++ b/README.md @@ -1028,6 +1028,28 @@ final class SomeClass
+## Doctrine-specific Rules + +### NoGetRepositoryOutsideServiceRule + +Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean + +```yaml +rules: + - Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule +``` + +```php +class SomeClass +{ + public function run(EntityManagerInterface $entityManager) + { + return $entityManager->getRepository(SomeEntity::class); + } +} +``` + +
diff --git a/composer-dependency-analyser.php b/composer-dependency-analyser.php index d1a5e112..a24219e4 100644 --- a/composer-dependency-analyser.php +++ b/composer-dependency-analyser.php @@ -8,7 +8,6 @@ return (new Configuration())->addPathToScan(__DIR__ . '/src', false) ->addPathToExclude(__DIR__ . '/tests/Rules/Rector/NoInstanceOfStaticReflectionRule/Fixture') ->addPathToExclude(__DIR__ . '/tests/Rules/Enum/RequireUniqueEnumConstantRule/Fixture') - ->addPathToExclude(__DIR__ . '/tests/Rules/NoReturnArrayVariableListRule/Fixture') ->addPathToExclude(__DIR__ . '/tests/Rules/ForbiddenExtendOfNonAbstractClassRule/Fixture') ->addPathToExclude(__DIR__ . '/tests/Rules/PHPUnit/NoTestMocksRule/Fixture') diff --git a/config/services/services.neon b/config/services/services.neon index bf0f9853..a36f500c 100644 --- a/config/services/services.neon +++ b/config/services/services.neon @@ -13,3 +13,6 @@ services: - Symplify\PHPStanRules\PhpDoc\BarePhpDocParser - Symplify\PHPStanRules\PhpDoc\PhpDocResolver - Symplify\PHPStanRules\TypeAnalyzer\CallableTypeAnalyzer + + # doctrine + - Symplify\PHPStanRules\Doctrine\DoctrineEntityDocumentAnalyser diff --git a/src/Doctrine/DoctrineEntityDocumentAnalyser.php b/src/Doctrine/DoctrineEntityDocumentAnalyser.php index 36650f1d..ae6fec32 100644 --- a/src/Doctrine/DoctrineEntityDocumentAnalyser.php +++ b/src/Doctrine/DoctrineEntityDocumentAnalyser.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\PHPStanRules\PHPStan; +namespace Symplify\PHPStanRules\Doctrine; use PHPStan\PhpDoc\ResolvedPhpDocBlock; use PHPStan\Reflection\ClassReflection; diff --git a/src/Enum/ClassName.php b/src/Enum/ClassName.php index 99568a49..8f6fe268 100644 --- a/src/Enum/ClassName.php +++ b/src/Enum/ClassName.php @@ -24,7 +24,7 @@ final class ClassName /** * @var string */ - public const EVENT_DISPATCHER_INTERFACE = 'Symfony\Component\EventDispatcher\EventSubscriberInterface'; + public const EVENT_DISPATCHER_INTERFACE = 'Symfony\Component\EventDispatcher\EventDispatcherInterface'; /** * @var string diff --git a/src/Enum/RuleIdentifier.php b/src/Enum/RuleIdentifier.php index 98ad5cc3..6c3ce298 100644 --- a/src/Enum/RuleIdentifier.php +++ b/src/Enum/RuleIdentifier.php @@ -177,4 +177,26 @@ final class RuleIdentifier public const NO_DYNAMIC_NAME = 'symplify.noDynamicName'; public const NO_REFERENCE = 'symplify.noReference'; + + public const PHPUNIT_NO_MOCK_ONLY = 'phpunit.noMockOnly'; + + public const SINGLE_ARG_EVENT_DISPATCH = 'symfony.singleArgEventDispatch'; + + public const NO_ENTITY_MOCKING = 'doctrine.noEntityMocking'; + + public const NO_STRING_IN_GET_SUBSCRIBED_EVENTS = 'symfony.noStringInGetSubscribedEvents'; + + public const NO_LISTENER_WITHOUT_CONTRACT = 'symfony.noListenerWithoutContract'; + + public const DOCTRINE_NO_PARENT_REPOSITORY = 'doctrine.noParentRepository'; + + public const DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE = 'doctrine.noGetRepositoryOutsideService'; + + public const SYMFONY_NO_REQUIRED_OUTSIDE_CLASS = 'symfony.noRequiredOutsideClass'; + + public const NO_CONSTRUCTOR_OVERRIDE = 'symplify.noConstructorOverride'; + + public const SYMFONY_NO_ABSTRACT_CONTROLLER_CONSTRUCTOR = 'symfony.noAbstractControllerConstructor'; + + public const PHPUNIT_PUBLIC_STATIC_DATA_PROVIDER = 'phpunit.publicStaticDataProvider'; } diff --git a/src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php b/src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php index 4b5e9e3d..7a2ea3cd 100644 --- a/src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php +++ b/src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php @@ -9,14 +9,11 @@ use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** - * Check if abstract controller has constructor, as it should use - * #[Require] instead to avoid parent constructor override - * - * @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest + * @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest * * @implements Rule */ @@ -34,7 +31,6 @@ public function getNodeType(): string /** * @param MethodCall $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -47,7 +43,10 @@ public function processNode(Node $node, Scope $scope): array } if (! $scope->isInClass()) { - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); + $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE) + ->build(); + return [$ruleError]; } @@ -57,7 +56,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); + $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE) + ->build(); + return [$ruleError]; } } diff --git a/src/PHPStan/Rules/Doctrine/NoParentRepositoryRule.php b/src/PHPStan/Rules/Doctrine/NoParentRepositoryRule.php index a672c7a8..969081f6 100644 --- a/src/PHPStan/Rules/Doctrine/NoParentRepositoryRule.php +++ b/src/PHPStan/Rules/Doctrine/NoParentRepositoryRule.php @@ -9,8 +9,8 @@ use PhpParser\Node\Stmt\Class_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * Check if class extends repository class, @@ -39,7 +39,6 @@ public function getNodeType(): string /** * @param Class_ $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -52,9 +51,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::DOCTRINE_NO_PARENT_REPOSITORY) ->build(); - return [$ruleError]; + return [$identifierRuleError]; } } diff --git a/src/PHPStan/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php b/src/PHPStan/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php index 81eca63f..ce6e9810 100644 --- a/src/PHPStan/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php +++ b/src/PHPStan/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php @@ -55,11 +55,11 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) ->identifier(RuleIdentifier::DOCTRINE_NO_REPOSITORY_CALL_IN_DATA_FIXTURES) ->build(); - return [$ruleError]; + return [$identifierRuleError]; } private function isDataFixtureClass(Scope $scope): bool diff --git a/src/Testing/DataProviderMethodResolver.php b/src/PHPUnit/DataProviderMethodResolver.php similarity index 94% rename from src/Testing/DataProviderMethodResolver.php rename to src/PHPUnit/DataProviderMethodResolver.php index 86b568e4..409fa996 100644 --- a/src/Testing/DataProviderMethodResolver.php +++ b/src/PHPUnit/DataProviderMethodResolver.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\PHPStanRules\PHPStan; +namespace Symplify\PHPStanRules\PHPUnit; use PhpParser\Comment\Doc; use PhpParser\Node\Stmt\ClassMethod; diff --git a/src/Rules/Complexity/NoConstructorOverrideRule.php b/src/Rules/Complexity/NoConstructorOverrideRule.php index a5f29e91..702869c5 100644 --- a/src/Rules/Complexity/NoConstructorOverrideRule.php +++ b/src/Rules/Complexity/NoConstructorOverrideRule.php @@ -10,8 +10,8 @@ use PhpParser\NodeFinder; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * @implements Rule @@ -35,7 +35,6 @@ public function getNodeType(): string /** * @param ClassMethod $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -69,7 +68,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); - return [$ruleError]; + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::NO_CONSTRUCTOR_OVERRIDE) + ->build(); + + return [$identifierRuleError]; } } diff --git a/src/Rules/Doctrine/NoEntityMockingRule.php b/src/Rules/Doctrine/NoEntityMockingRule.php index 18be7a58..2ac35889 100644 --- a/src/Rules/Doctrine/NoEntityMockingRule.php +++ b/src/Rules/Doctrine/NoEntityMockingRule.php @@ -10,15 +10,16 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; -use Symplify\PHPStanRules\PHPStan\DoctrineEntityDocumentAnalyser; +use Symplify\PHPStanRules\Doctrine\DoctrineEntityDocumentAnalyser; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * The ORM entities and ODM documents should never be mocked, as it leads to typeless code. * Use them directly instead. * - * @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoEntityMockingRule\NoEntityMockingRuleTest + * @see \Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoEntityMockingRule\NoEntityMockingRuleTest + * * @implements Rule */ final readonly class NoEntityMockingRule implements Rule @@ -40,7 +41,6 @@ public function getNodeType(): string /** * @param MethodCall $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -62,6 +62,7 @@ public function processNode(Node $node, Scope $scope): array } $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::NO_ENTITY_MOCKING) ->build(); return [$ruleError]; diff --git a/src/Rules/NoDynamicNameRule.php b/src/Rules/NoDynamicNameRule.php index e5f7ceb4..abba040d 100644 --- a/src/Rules/NoDynamicNameRule.php +++ b/src/Rules/NoDynamicNameRule.php @@ -24,7 +24,7 @@ * * @implements Rule */ -final class NoDynamicNameRule implements Rule +final readonly class NoDynamicNameRule implements Rule { /** * @var string @@ -32,7 +32,7 @@ final class NoDynamicNameRule implements Rule public const ERROR_MESSAGE = 'Use explicit names over dynamic ones'; public function __construct( - private readonly CallableTypeAnalyzer $callableTypeAnalyzer, + private CallableTypeAnalyzer $callableTypeAnalyzer, ) { } diff --git a/src/Rules/NoReferenceRule.php b/src/Rules/NoReferenceRule.php index 5c824add..21d69fd6 100644 --- a/src/Rules/NoReferenceRule.php +++ b/src/Rules/NoReferenceRule.php @@ -24,9 +24,9 @@ /** * @see \Symplify\PHPStanRules\Tests\Rules\NoReferenceRule\NoReferenceRuleTest * - * @implements Rule<\PhpParser\Node> + * @implements Rule */ -final class NoReferenceRule implements Rule +final readonly class NoReferenceRule implements Rule { /** * @var string @@ -34,7 +34,7 @@ final class NoReferenceRule implements Rule public const ERROR_MESSAGE = 'Use explicit return value over magic &reference'; public function __construct( - private readonly ParentClassMethodNodeResolver $parentClassMethodNodeResolver, + private ParentClassMethodNodeResolver $parentClassMethodNodeResolver, ) { } diff --git a/src/Rules/PHPUnit/NoMockOnlyTestRule.php b/src/Rules/PHPUnit/NoMockOnlyTestRule.php index 4b6a2142..04af1d30 100644 --- a/src/Rules/PHPUnit/NoMockOnlyTestRule.php +++ b/src/Rules/PHPUnit/NoMockOnlyTestRule.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\PHPStanRules\PHPStan\Rule; +namespace Symplify\PHPStanRules\Rules\PHPUnit; use PhpParser\Node; use PhpParser\Node\Name; @@ -10,12 +10,12 @@ use PHPStan\Analyser\Scope; use PHPStan\Node\InClassNode; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; -use Symplify\PHPStanRules\PHPStan\PHPUnitTestAnalyser; +use Symplify\PHPStanRules\Enum\RuleIdentifier; +use Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser; /** - * @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoMockOnlyTestRule\NoMockOnlyTestRuleTest + * @see \Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockOnlyTestRule\NoMockOnlyTestRuleTest * * @implements Rule */ @@ -38,7 +38,6 @@ public function getNodeType(): string /** * @param InClassNode $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -73,8 +72,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::PHPUNIT_NO_MOCK_ONLY) ->build(); - return [$ruleError]; + + return [$identifierRuleError]; } } diff --git a/src/Rules/PHPUnit/PublicStaticDataProviderRule.php b/src/Rules/PHPUnit/PublicStaticDataProviderRule.php index 3629ff47..bc8fe418 100644 --- a/src/Rules/PHPUnit/PublicStaticDataProviderRule.php +++ b/src/Rules/PHPUnit/PublicStaticDataProviderRule.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\PHPStanRules\PHPStan\Rule; +namespace Symplify\PHPStanRules\Rules\PHPUnit; use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; @@ -10,8 +10,9 @@ use PHPStan\Node\InClassNode; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; -use Symplify\PHPStanRules\PHPStan\DataProviderMethodResolver; -use Symplify\PHPStanRules\PHPStan\PHPUnitTestAnalyser; +use Symplify\PHPStanRules\Enum\RuleIdentifier; +use Symplify\PHPStanRules\PHPUnit\DataProviderMethodResolver; +use Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser; /** * PHPUnit data provider have to be public and static @@ -50,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array $classLike = $node->getOriginalNode(); foreach ($classLike->getMethods() as $classMethod) { - if (! PHPUnitTestAnalyser::isTestClassMethod($classMethod)) { + if (! PHPUnitTestAnalyser::class::isTestClassMethod($classMethod)) { continue; } @@ -75,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array if (! $dataProviderClassMethod->isStatic()) { $errorMessage = sprintf(self::PUBLIC_ERROR_MESSAGE, $dataProviderMethodName); $ruleErrors[] = RuleErrorBuilder::message($errorMessage) - ->identifier('phpunit.publicDataProvider') + ->identifier(RuleIdentifier::PHPUNIT_PUBLIC_STATIC_DATA_PROVIDER) ->line($dataProviderClassMethod->getLine()) ->build(); } diff --git a/src/Rules/PreferredClassRule.php b/src/Rules/PreferredClassRule.php index 98855f81..63b93866 100644 --- a/src/Rules/PreferredClassRule.php +++ b/src/Rules/PreferredClassRule.php @@ -24,7 +24,7 @@ * * @implements Rule */ -final class PreferredClassRule implements Rule +final readonly class PreferredClassRule implements Rule { /** * @var string @@ -35,7 +35,7 @@ final class PreferredClassRule implements Rule * @param string[] $oldToPreferredClasses */ public function __construct( - private readonly array $oldToPreferredClasses + private array $oldToPreferredClasses ) { } diff --git a/src/Rules/Symfony/NoAbstractControllerConstructorRule.php b/src/Rules/Symfony/NoAbstractControllerConstructorRule.php index 8fd51ddf..08c333f4 100644 --- a/src/Rules/Symfony/NoAbstractControllerConstructorRule.php +++ b/src/Rules/Symfony/NoAbstractControllerConstructorRule.php @@ -9,8 +9,8 @@ use PhpParser\Node\Stmt\Class_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * Check if abstract controller has constructor, as it should use @@ -34,7 +34,6 @@ public function getNodeType(): string /** * @param Class_ $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -55,7 +54,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); - return [$ruleError]; + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::SYMFONY_NO_ABSTRACT_CONTROLLER_CONSTRUCTOR) + ->build(); + + return [$identifierRuleError]; } } diff --git a/src/Rules/Symfony/NoListenerWithoutContractRule.php b/src/Rules/Symfony/NoListenerWithoutContractRule.php index 3b157b3e..c8b7cfa5 100644 --- a/src/Rules/Symfony/NoListenerWithoutContractRule.php +++ b/src/Rules/Symfony/NoListenerWithoutContractRule.php @@ -9,8 +9,8 @@ use PHPStan\Analyser\Scope; use PHPStan\Node\InClassNode; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * Based on https://tomasvotruba.com/blog/2019/07/22/how-to-convert-listeners-to-subscribers-and-reduce-your-configs @@ -23,7 +23,7 @@ final class NoListenerWithoutContractRule implements Rule /** * @var string */ - public const ERROR_MESSAGE = 'There should be no listeners defined in yaml config, use contract + PHP instead'; + public const ERROR_MESSAGE = 'There should be no listeners modified in config. Use EventSubscriberInterface contract and PHP instead'; public function getNodeType(): string { @@ -32,7 +32,6 @@ public function getNodeType(): string /** * @param InClassNode $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -45,17 +44,19 @@ public function processNode(Node $node, Scope $scope): array return []; } - $class = $node->getOriginalNode(); - if (! $class instanceof Class_) { + $classLike = $node->getOriginalNode(); + if (! $classLike instanceof Class_) { return []; } - if ($class->implements !== []) { + if ($classLike->implements !== []) { return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::NO_LISTENER_WITHOUT_CONTRACT) + ->build(); - return [$ruleError]; + return [$identifierRuleError]; } } diff --git a/src/Rules/Symfony/NoRequiredOutsideClassRule.php b/src/Rules/Symfony/NoRequiredOutsideClassRule.php index 93bf69d3..22aa07ec 100644 --- a/src/Rules/Symfony/NoRequiredOutsideClassRule.php +++ b/src/Rules/Symfony/NoRequiredOutsideClassRule.php @@ -10,8 +10,8 @@ use PhpParser\Node\Stmt\Trait_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoRequiredOutsideClassRule\NoRequiredOutsideClassRuleTest @@ -37,7 +37,6 @@ public function getNodeType(): string /** * @param Trait_ $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -47,6 +46,7 @@ public function processNode(Node $node, Scope $scope): array if ($this->isAutowiredClassMethod($classMethod)) { $ruleErrors[] = RuleErrorBuilder::message(self::ERROR_MESSAGE) ->file($scope->getFile()) + ->identifier(RuleIdentifier::SYMFONY_NO_REQUIRED_OUTSIDE_CLASS) ->line($classMethod->getLine()) ->build(); } diff --git a/src/Rules/Symfony/NoStringInGetSubscribedEventsRule.php b/src/Rules/Symfony/NoStringInGetSubscribedEventsRule.php index c6640577..c74e1f88 100644 --- a/src/Rules/Symfony/NoStringInGetSubscribedEventsRule.php +++ b/src/Rules/Symfony/NoStringInGetSubscribedEventsRule.php @@ -13,9 +13,9 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; use Symplify\PHPStanRules\Enum\ClassName; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * @implements Rule @@ -39,7 +39,6 @@ public function getNodeType(): string /** * @param ClassMethod $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -95,7 +94,10 @@ public function processNode(Node $node, Scope $scope): array continue; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); + $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::NO_STRING_IN_GET_SUBSCRIBED_EVENTS) + ->build(); + return [$ruleError]; } diff --git a/src/Rules/Symfony/SingleArgEventDispatchRule.php b/src/Rules/Symfony/SingleArgEventDispatchRule.php index 6bc7f787..fdceba3d 100644 --- a/src/Rules/Symfony/SingleArgEventDispatchRule.php +++ b/src/Rules/Symfony/SingleArgEventDispatchRule.php @@ -9,10 +9,10 @@ use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\ObjectType; use Symplify\PHPStanRules\Enum\ClassName; +use Symplify\PHPStanRules\Enum\RuleIdentifier; /** * @implements Rule @@ -31,7 +31,6 @@ public function getNodeType(): string /** * @param MethodCall $node - * @return RuleError[] */ public function processNode(Node $node, Scope $scope): array { @@ -57,7 +56,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build(); - return [$ruleError]; + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) + ->identifier(RuleIdentifier::SINGLE_ARG_EVENT_DISPATCH) + ->build(); + + return [$identifierRuleError]; } } diff --git a/src/Testing/PHPUnitTestAnalyser.php b/src/Testing/PHPUnitTestAnalyser.php index 6a7b5947..152f4659 100644 --- a/src/Testing/PHPUnitTestAnalyser.php +++ b/src/Testing/PHPUnitTestAnalyser.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\PHPStanRules\PHPStan; +namespace Symplify\PHPStanRules\Testing; use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Analyser\Scope; @@ -25,6 +25,9 @@ public static function isTestClass(Scope $scope): bool return $classReflection->isSubclassOf(self::TEST_CASE_CLASS); } + /** + * @api is used + */ public static function isTestClassMethod(ClassMethod $classMethod): bool { if (! $classMethod->isPublic()) { diff --git a/tests/Rules/PHPUnit/NoEntityMockingRule/config/configured_rule.neon b/tests/Rules/PHPUnit/NoEntityMockingRule/config/configured_rule.neon index 3430a70b..48e37fff 100644 --- a/tests/Rules/PHPUnit/NoEntityMockingRule/config/configured_rule.neon +++ b/tests/Rules/PHPUnit/NoEntityMockingRule/config/configured_rule.neon @@ -1,5 +1,5 @@ services: - - Symplify\PHPStanRules\PHPStan\DoctrineEntityDocumentAnalyser + - Symplify\PHPStanRules\Doctrine\DoctrineEntityDocumentAnalyser rules: - Symplify\PHPStanRules\Rules\Doctrine\NoEntityMockingRule diff --git a/tests/Rules/PHPUnit/NoMockOnlyTestRule/NoMockOnlyTestRuleTest.php b/tests/Rules/PHPUnit/NoMockOnlyTestRule/NoMockOnlyTestRuleTest.php index 0b540bcc..1f7e7f85 100644 --- a/tests/Rules/PHPUnit/NoMockOnlyTestRule/NoMockOnlyTestRuleTest.php +++ b/tests/Rules/PHPUnit/NoMockOnlyTestRule/NoMockOnlyTestRuleTest.php @@ -8,7 +8,7 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\DataProvider; -use Symplify\PHPStanRules\PHPStan\Rule\NoMockOnlyTestRule; +use Symplify\PHPStanRules\Rules\PHPUnit\NoMockOnlyTestRule; final class NoMockOnlyTestRuleTest extends RuleTestCase { diff --git a/tests/Rules/PHPUnit/PublicStaticDataProviderRule/PublicStaticDataProviderRuleTest.php b/tests/Rules/PHPUnit/PublicStaticDataProviderRule/PublicStaticDataProviderRuleTest.php index 2a729e2f..4f42c146 100644 --- a/tests/Rules/PHPUnit/PublicStaticDataProviderRule/PublicStaticDataProviderRuleTest.php +++ b/tests/Rules/PHPUnit/PublicStaticDataProviderRule/PublicStaticDataProviderRuleTest.php @@ -6,7 +6,7 @@ use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\DataProvider; -use Symplify\PHPStanRules\PHPStan\Rule\PublicStaticDataProviderRule; +use Symplify\PHPStanRules\Rules\PHPUnit\PublicStaticDataProviderRule; final class PublicStaticDataProviderRuleTest extends RuleTestCase { diff --git a/tests/Rules/Symfony/RequireInvokableControllerRule/RequireInvokableControllerRuleTest.php b/tests/Rules/Symfony/RequireInvokableControllerRule/RequireInvokableControllerRuleTest.php index 79c42b2d..92917ae5 100644 --- a/tests/Rules/Symfony/RequireInvokableControllerRule/RequireInvokableControllerRuleTest.php +++ b/tests/Rules/Symfony/RequireInvokableControllerRule/RequireInvokableControllerRuleTest.php @@ -26,7 +26,7 @@ public static function provideData(): Iterator yield [__DIR__ . '/Fixture/SkipInvokableController.php', []]; yield [__DIR__ . '/Fixture/SkipRandomPublicMethodController.php', []]; - yield [__DIR__ . '/Fixture/MissnamedController.php', [[RequireInvokableControllerRule::ERROR_MESSAGE, 15]]]; + yield [__DIR__ . '/Fixture/MissnamedController.php', [[RequireInvokableControllerRule::ERROR_MESSAGE, 14]]]; } /**