From a1f482b41bf7d38eeb773d22a3e790b7dbb926cb Mon Sep 17 00:00:00 2001 From: fix-issue-bot Date: Wed, 27 May 2026 06:48:12 +0200 Subject: [PATCH] fix: drop PreventParentMethodVisibilityOverrideRule (fixes #227) --- README.md | 49 -------- config/static-rules.neon | 1 - src/Enum/RuleIdentifier.php | 2 - ...ventParentMethodVisibilityOverrideRule.php | 113 ------------------ .../Fixture/ClassWithOverridingVisibility.php | 12 -- .../Fixture/GoodVisibility.php | 13 -- ...ParentMethodVisibilityOverrideRuleTest.php | 47 -------- .../config/configured_rule.neon | 5 - 8 files changed, 242 deletions(-) delete mode 100644 src/Rules/PreventParentMethodVisibilityOverrideRule.php delete mode 100644 tests/Rules/PreventParentMethodVisibilityOverrideRule/Fixture/ClassWithOverridingVisibility.php delete mode 100644 tests/Rules/PreventParentMethodVisibilityOverrideRule/Fixture/GoodVisibility.php delete mode 100644 tests/Rules/PreventParentMethodVisibilityOverrideRule/PreventParentMethodVisibilityOverrideRuleTest.php delete mode 100644 tests/Rules/PreventParentMethodVisibilityOverrideRule/config/configured_rule.neon diff --git a/README.md b/README.md index eeb688b68..ff48188a3 100644 --- a/README.md +++ b/README.md @@ -903,55 +903,6 @@ class SomeClass
-### PreventParentMethodVisibilityOverrideRule - -Change `"%s()"` method visibility to "%s" to respect parent method visibility. - -```yaml -rules: - - Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule -``` - -```php -class SomeParentClass -{ - public function run() - { - } -} - -class SomeClass extends SomeParentClass -{ - protected function run() - { - } -} -``` - -:x: - -
- -```php -class SomeParentClass -{ - public function run() - { - } -} - -class SomeClass extends SomeParentClass -{ - public function run() - { - } -} -``` - -:+1: - -
- ### RequiredOnlyInAbstractRule `@required` annotation should be used only in abstract classes, to child classes can use clean `__construct()` service injection. diff --git a/config/static-rules.neon b/config/static-rules.neon index d76909435..af63c23e2 100644 --- a/config/static-rules.neon +++ b/config/static-rules.neon @@ -9,7 +9,6 @@ rules: - Symplify\PHPStanRules\Rules\RequireAttributeNameRule - Symplify\PHPStanRules\Rules\Enum\RequireUniqueEnumConstantRule - - Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule - Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule - Symplify\PHPStanRules\Rules\NoReferenceRule diff --git a/src/Enum/RuleIdentifier.php b/src/Enum/RuleIdentifier.php index dba6c5210..15aca3755 100644 --- a/src/Enum/RuleIdentifier.php +++ b/src/Enum/RuleIdentifier.php @@ -14,8 +14,6 @@ final class RuleIdentifier public const string PHP_PARSER_NO_LEADING_BACKSLASH_IN_NAME = 'phpParser.noLeadingBackslashInName'; - public const string PARENT_METHOD_VISIBILITY_OVERRIDE = 'symplify.parentMethodVisibilityOverride'; - public const string NO_RETURN_SETTER_METHOD = 'symplify.noReturnSetterMethod'; public const string FORBIDDEN_STATIC_CLASS_CONST_FETCH = 'symplify.forbiddenStaticClassConstFetch'; diff --git a/src/Rules/PreventParentMethodVisibilityOverrideRule.php b/src/Rules/PreventParentMethodVisibilityOverrideRule.php deleted file mode 100644 index be44b963b..000000000 --- a/src/Rules/PreventParentMethodVisibilityOverrideRule.php +++ /dev/null @@ -1,113 +0,0 @@ - - * @see \Symplify\PHPStanRules\Tests\Rules\PreventParentMethodVisibilityOverrideRule\PreventParentMethodVisibilityOverrideRuleTest - */ -final readonly class PreventParentMethodVisibilityOverrideRule implements Rule -{ - public const string ERROR_MESSAGE = 'Change "%s()" method visibility to "%s" to respect parent method visibility.'; - - public function __construct( - private ReflectionProvider $reflectionProvider - ) { - } - - public function getNodeType(): string - { - return ClassMethod::class; - } - - /** - * @param ClassMethod $node - */ - public function processNode(Node $node, Scope $scope): array - { - if (! $scope->getClassReflection() instanceof ClassReflection) { - return []; - } - - $classReflection = $scope->getClassReflection(); - $parentClassNames = $classReflection->getParentClassesNames(); - if ($parentClassNames === []) { - return []; - } - - $methodName = (string) $node->name; - foreach ($parentClassNames as $parentClassName) { - if (! $this->reflectionProvider->hasClass($parentClassName)) { - continue; - } - - $parentClassReflection = $this->reflectionProvider->getClass($parentClassName); - - if (! $parentClassReflection->hasMethod($methodName)) { - continue; - } - - $parentReflectionMethod = $parentClassReflection->getMethod($methodName, $scope); - if ($this->isClassMethodCompatibleWithParentReflectionMethod($node, $parentReflectionMethod)) { - return []; - } - - $methodVisibility = $this->resolveReflectionMethodVisibilityAsStrings($parentReflectionMethod); - - $errorMessage = sprintf(self::ERROR_MESSAGE, $methodName, $methodVisibility); - - return [RuleErrorBuilder::message($errorMessage) - ->identifier(RuleIdentifier::PARENT_METHOD_VISIBILITY_OVERRIDE) - ->build()]; - } - - return []; - } - - private function isClassMethodCompatibleWithParentReflectionMethod( - ClassMethod $classMethod, - ExtendedMethodReflection $extendedMethodReflection - ): bool { - if ($extendedMethodReflection->isPublic() && $classMethod->isPublic()) { - return true; - } - - // see https://github.com/phpstan/phpstan/discussions/7456#discussioncomment-2927978 - $isProtectedMethod = ! $extendedMethodReflection->isPublic() && ! $extendedMethodReflection->isPrivate(); - if ($isProtectedMethod && $classMethod->isProtected()) { - return true; - } - - if (! $extendedMethodReflection->isPrivate()) { - return false; - } - - return $classMethod->isPrivate(); - } - - private function resolveReflectionMethodVisibilityAsStrings( - ExtendedMethodReflection $extendedMethodReflection - ): string { - if ($extendedMethodReflection->isPublic()) { - return 'public'; - } - - if ($extendedMethodReflection->isPrivate()) { - return 'private'; - } - - return 'protected'; - } -} diff --git a/tests/Rules/PreventParentMethodVisibilityOverrideRule/Fixture/ClassWithOverridingVisibility.php b/tests/Rules/PreventParentMethodVisibilityOverrideRule/Fixture/ClassWithOverridingVisibility.php deleted file mode 100644 index cc9e78ec7..000000000 --- a/tests/Rules/PreventParentMethodVisibilityOverrideRule/Fixture/ClassWithOverridingVisibility.php +++ /dev/null @@ -1,12 +0,0 @@ -> $expectedErrorMessagesWithLines - */ - #[DataProvider('provideData')] - public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void - { - $this->analyse([$filePath], $expectedErrorMessagesWithLines); - } - - /** - * @return Iterator<(array>>|array>>|array)> - */ - public static function provideData(): Iterator - { - $errorMessage = sprintf(PreventParentMethodVisibilityOverrideRule::ERROR_MESSAGE, 'run', 'protected'); - yield [__DIR__ . '/Fixture/ClassWithOverridingVisibility.php', [[$errorMessage, 9]]]; - } - - /** - * @return string[] - */ - #[Override] - public static function getAdditionalConfigFiles(): array - { - return [__DIR__ . '/config/configured_rule.neon']; - } - - protected function getRule(): Rule - { - return self::getContainer()->getByType(PreventParentMethodVisibilityOverrideRule::class); - } -} diff --git a/tests/Rules/PreventParentMethodVisibilityOverrideRule/config/configured_rule.neon b/tests/Rules/PreventParentMethodVisibilityOverrideRule/config/configured_rule.neon deleted file mode 100644 index a8f519503..000000000 --- a/tests/Rules/PreventParentMethodVisibilityOverrideRule/config/configured_rule.neon +++ /dev/null @@ -1,5 +0,0 @@ -includes: - - ../../../config/included_services.neon - -rules: - - Symplify\PHPStanRules\Rules\PreventParentMethodVisibilityOverrideRule