breakingVariableRenameGuard = $breakingVariableRenameGuard; $this->expectedNameResolver = $expectedNameResolver; $this->namingConventionAnalyzer = $namingConventionAnalyzer; $this->variableRenamer = $variableRenamer; $this->foreachMatcher = $foreachMatcher; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Renames value variable name in foreach loop to match method type', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $array = []; foreach ($object->getMethods() as $property) { $array[] = $property; } } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $array = []; foreach ($object->getMethods() as $method) { $array[] = $method; } } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Foreach_::class]; } /** * @param Foreach_ $node */ public function refactor(Node $node) : ?Node { $variableAndCallForeach = $this->foreachMatcher->match($node); if (!$variableAndCallForeach instanceof VariableAndCallForeach) { return null; } $expectedName = $this->expectedNameResolver->resolveForForeach($variableAndCallForeach->getCall()); if ($expectedName === null) { return null; } if ($this->isName($variableAndCallForeach->getVariable(), $expectedName)) { return null; } if ($this->shouldSkip($variableAndCallForeach, $expectedName)) { return null; } $this->renameVariable($variableAndCallForeach, $expectedName); return $node; } private function shouldSkip(VariableAndCallForeach $variableAndCallForeach, string $expectedName) : bool { if ($this->namingConventionAnalyzer->isCallMatchingVariableName($variableAndCallForeach->getCall(), $variableAndCallForeach->getVariableName(), $expectedName)) { return \true; } return $this->breakingVariableRenameGuard->shouldSkipVariable($variableAndCallForeach->getVariableName(), $expectedName, $variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariable()); } private function renameVariable(VariableAndCallForeach $variableAndCallForeach, string $expectedName) : void { $this->variableRenamer->renameVariableInFunctionLike($variableAndCallForeach->getFunctionLike(), $variableAndCallForeach->getVariableName(), $expectedName, null); } }