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

126 lines
3.1 KiB
PHP
Raw Normal View History

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