rector/rules/Transform/Rector/ClassMethod/WrapReturnRector.php

99 lines
2.8 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\ClassMethod;
2018-12-12 19:34:32 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\Transform\ValueObject\WrapReturn;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202403\Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Transform\Rector\ClassMethod\WrapReturnRector\WrapReturnRectorTest
2019-09-03 09:11:45 +00:00
*/
final class WrapReturnRector extends AbstractRector implements ConfigurableRectorInterface
2018-12-12 19:34:32 +00:00
{
/**
* @var WrapReturn[]
2018-12-12 19:34:32 +00:00
*/
private $typeMethodWraps = [];
public function getRuleDefinition() : RuleDefinition
2018-12-12 19:34:32 +00:00
{
return new RuleDefinition('Wrap return value of specific method', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
2018-12-12 19:34:32 +00:00
final class SomeClass
{
public function getItem()
{
return 1;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2018-12-12 19:34:32 +00:00
final class SomeClass
{
public function getItem()
{
return [1];
}
}
CODE_SAMPLE
, [new WrapReturn('SomeClass', 'getItem', \true)])]);
2018-12-12 19:34:32 +00:00
}
/**
* @return array<class-string<Node>>
2018-12-12 19:34:32 +00:00
*/
public function getNodeTypes() : array
2018-12-12 19:34:32 +00:00
{
return [ClassMethod::class];
2018-12-12 19:34:32 +00:00
}
/**
* @param ClassMethod $node
2018-12-12 19:34:32 +00:00
*/
public function refactor(Node $node) : ?Node
2018-12-12 19:34:32 +00:00
{
foreach ($this->typeMethodWraps as $typeMethodWrap) {
if (!$this->isName($node, $typeMethodWrap->getMethod())) {
2018-12-12 19:34:32 +00:00
continue;
}
if (!$this->isObjectType($node, $typeMethodWrap->getObjectType())) {
continue;
}
if ($node->stmts === null) {
continue;
2018-12-12 19:34:32 +00:00
}
return $this->wrap($node, $typeMethodWrap->isArrayWrap());
2018-12-12 19:34:32 +00:00
}
return null;
2018-12-12 19:34:32 +00:00
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 23:39:41 +00:00
{
Assert::allIsAOf($configuration, WrapReturn::class);
$this->typeMethodWraps = $configuration;
2020-07-29 23:39:41 +00:00
}
private function wrap(ClassMethod $classMethod, bool $isArrayWrap) : ?ClassMethod
2018-12-12 19:34:32 +00:00
{
if (!\is_iterable($classMethod->stmts)) {
return null;
2018-12-12 19:34:32 +00:00
}
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)]);
2018-12-12 19:34:32 +00:00
}
$classMethod->stmts[$key] = $stmt;
2018-12-12 19:34:32 +00:00
}
}
return $classMethod;
2018-12-12 19:34:32 +00:00
}
}