anonymousFunctionFactory = $anonymousFunctionFactory; $this->reflectionResolver = $reflectionResolver; $this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Convert [$this, "method"] to proper anonymous function', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { $values = [1, 5, 3]; usort($values, [$this, 'compareSize']); return $values; } private function compareSize($first, $second) { return $first <=> $second; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $values = [1, 5, 3]; usort($values, function ($first, $second) { return $this->compareSize($first, $second); }); return $values; } private function compareSize($first, $second) { return $first <=> $second; } } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [Array_::class]; } /** * @param Array_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { if ($this->shouldSkipTwigExtension($scope)) { return null; } $arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope); if (!$arrayCallable instanceof ArrayCallable) { return null; } $phpMethodReflection = $this->reflectionResolver->resolveMethodReflection($arrayCallable->getClass(), $arrayCallable->getMethod(), $scope); if (!$phpMethodReflection instanceof PhpMethodReflection) { return null; } return $this->anonymousFunctionFactory->createFromPhpMethodReflection($phpMethodReflection, $arrayCallable->getCallerExpr()); } private function shouldSkipTwigExtension(Scope $scope) : bool { if (!$scope->isInClass()) { return \false; } $classReflection = $scope->getClassReflection(); return $classReflection->isSubclassOf('Twig\\Extension\\ExtensionInterface'); } }