@@ -49,8 +49,8 @@ public function getNodeType(): string
4949public function processNode (Node $ node , Scope $ scope ): array
5050{
5151$ method = $ node ->getMethodReflection ();
52- $ prototype = $ this ->findPrototype ($ node ->getClassReflection (), $ method ->getName ());
53- if ($ prototype === null ) {
52+ $ prototypeData = $ this ->findPrototype ($ node ->getClassReflection (), $ method ->getName ());
53+ if ($ prototypeData === null ) {
5454if (strtolower ($ method ->getName ()) === '__construct ' ) {
5555$ parent = $ method ->getDeclaringClass ()->getParentClass ();
5656if ($ parent !== null && $ parent ->hasConstructor ()) {
@@ -93,6 +93,8 @@ public function processNode(Node $node, Scope $scope): array
9393return [];
9494}
9595
96+ [$ prototype , $ checkVisibility ] = $ prototypeData ;
97+
9698$ messages = [];
9799if ($ prototype ->isFinalByKeyword ()->yes ()) {
98100$ messages [] = RuleErrorBuilder::message (sprintf (
@@ -132,19 +134,19 @@ public function processNode(Node $node, Scope $scope): array
132134))->nonIgnorable ()->build ();
133135}
134136
135- if ($ prototype -> isPublic () ) {
136- if (! $ method ->isPublic ()) {
137- $ messages [] = RuleErrorBuilder:: message ( sprintf (
138- ' %s method %s::%s() overriding public method %s::%s() should also be public. ' ,
139- $ method-> isPrivate () ? ' Private ' : ' Protected ' ,
140- $ method ->getDeclaringClass ()-> getDisplayName () ,
141- $ method ->getName (),
142- $ prototype -> getDeclaringClass ()-> getDisplayName ( $ this -> genericPrototypeMessage ),
143- $ prototype ->getName ( ),
144- ))-> nonIgnorable ()-> build ();
145- }
146- } elseif ( $ method -> isPrivate ()) {
147- if (! $ prototype ->isPrivate ()) {
137+ if ($ checkVisibility ) {
138+ if ($ prototype ->isPublic ()) {
139+ if (! $ method -> isPublic ()) {
140+ $ messages [] = RuleErrorBuilder:: message ( sprintf (
141+ ' %s method %s::%s () overriding public method %s::%s() should also be public. ' ,
142+ $ method ->isPrivate () ? ' Private ' : ' Protected ' ,
143+ $ method ->getDeclaringClass ()-> getDisplayName (),
144+ $ method -> getName ( ),
145+ $ prototype ->getDeclaringClass ()-> getDisplayName ( $ this -> genericPrototypeMessage ),
146+ $ prototype -> getName (),
147+ ))-> nonIgnorable ()-> build ();
148+ }
149+ } elseif ( $ method ->isPrivate ()) {
148150$ messages [] = RuleErrorBuilder::message (sprintf (
149151'Private method %s::%s() overriding protected method %s::%s() should be protected or public. ' ,
150152$ method ->getDeclaringClass ()->getDisplayName (),
@@ -293,11 +295,14 @@ private function hasOverrideAttribute(Node\Stmt\ClassMethod $method): bool
293295return false ;
294296}
295297
296- private function findPrototype (ClassReflection $ classReflection , string $ methodName ): ?ExtendedMethodReflection
298+ /**
299+ * @return array{ExtendedMethodReflection, bool}|null
300+ */
301+ private function findPrototype (ClassReflection $ classReflection , string $ methodName ): ?array
297302{
298303foreach ($ classReflection ->getImmediateInterfaces () as $ immediateInterface ) {
299304if ($ immediateInterface ->hasNativeMethod ($ methodName )) {
300- return $ immediateInterface ->getNativeMethod ($ methodName );
305+ return [ $ immediateInterface ->getNativeMethod ($ methodName ), true ] ;
301306}
302307}
303308
@@ -311,10 +316,10 @@ private function findPrototype(ClassReflection $classReflection, string $methodN
311316$ isAbstract = $ method ->isAbstract ();
312317if (is_bool ($ isAbstract )) {
313318if ($ isAbstract ) {
314- return $ method ;
319+ return [ $ method, false ] ;
315320}
316321} elseif ($ isAbstract ->yes ()) {
317- return $ method ;
322+ return [ $ method, false ] ;
318323}
319324}
320325}
@@ -350,7 +355,7 @@ private function findPrototype(ClassReflection $classReflection, string $methodN
350355}
351356}
352357
353- return $ method ;
358+ return [ $ method, true ] ;
354359}
355360
356361}
0 commit comments