classMethodVisibilityGuard = $classMethodVisibilityGuard; $this->visibilityManipulator = $visibilityManipulator; $this->overrideByParentClassGuard = $overrideByParentClassGuard; $this->betterNodeFinder = $betterNodeFinder; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Change protected class method to private if possible', [new CodeSample(<<<'CODE_SAMPLE' final class SomeClass { protected function someMethod() { } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class SomeClass { private function someMethod() { } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { if (!$node->isFinal()) { return null; } if (!$this->overrideByParentClassGuard->isLegal($node)) { return null; } $classReflection = $scope->getClassReflection(); if (!$classReflection instanceof ClassReflection) { return null; } $hasChanged = \false; foreach ($node->getMethods() as $classMethod) { if ($this->shouldSkipClassMethod($classMethod)) { continue; } if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByParent($classMethod, $classReflection)) { continue; } if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByTrait($classMethod, $classReflection)) { continue; } $this->visibilityManipulator->makePrivate($classMethod); $hasChanged = \true; } if ($hasChanged) { return $node; } return null; } private function shouldSkipClassMethod(ClassMethod $classMethod) : bool { // edge case in nette framework /** @var string $methodName */ $methodName = $this->getName($classMethod->name); if (\strncmp($methodName, 'createComponent', \strlen('createComponent')) === 0) { return \true; } if (!$classMethod->isProtected()) { return \true; } // if has parent call, its probably overriding parent one → skip it $hasParentCall = (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) : bool { if (!$node instanceof StaticCall) { return \false; } return $this->isName($node->class, 'parent'); }); return $hasParentCall; } }