> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Node { $hasChanged = \false; foreach ($this->typeMethodWraps as $typeMethodWrap) { if (!$this->isObjectType($node, $typeMethodWrap->getObjectType())) { continue; } foreach ($node->getMethods() as $classMethod) { if (!$this->isName($classMethod, $typeMethodWrap->getMethod())) { continue; } if ($node->stmts === null) { continue; } $this->wrap($classMethod, $typeMethodWrap->isArrayWrap()); $hasChanged = \true; } } if ($hasChanged) { return $node; } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, WrapReturn::class); $this->typeMethodWraps = $configuration; } private function wrap(ClassMethod $classMethod, bool $isArrayWrap) : ?ClassMethod { if (!\is_iterable($classMethod->stmts)) { return null; } foreach ($classMethod->stmts as $key => $stmt) { if ($stmt instanceof Return_ && $stmt->expr instanceof Expr) { if ($isArrayWrap && !$stmt->expr instanceof Array_) { $stmt->expr = new Array_([new ArrayItem($stmt->expr)]); } $classMethod->stmts[$key] = $stmt; } } return $classMethod; } }