2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-12 19:34:32 +00:00
|
|
|
|
2021-02-20 23:21:19 +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;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-02-20 23:21:19 +00:00
|
|
|
use Rector\Transform\ValueObject\WrapReturn;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-08-25 19:05:58 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-12-12 19:34:32 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +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
|
|
|
*/
|
2020-08-25 19:05:58 +00:00
|
|
|
public const TYPE_METHOD_WRAPS = 'type_method_wraps';
|
2018-12-12 19:34:32 +00:00
|
|
|
|
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var WrapReturn[]
|
2018-12-12 19:34:32 +00:00
|
|
|
*/
|
2020-08-25 19:05:58 +00:00
|
|
|
private $typeMethodWraps = [];
|
2018-12-12 19:34:32 +00:00
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-12-12 19:34:32 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Wrap return value of specific method', [
|
2018-12-14 19:38:47 +00:00
|
|
|
new ConfiguredCodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-12 19:34:32 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function getItem()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-12-12 19:34:32 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-12 19:34:32 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function getItem()
|
|
|
|
{
|
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-12-14 19:38:47 +00:00
|
|
|
,
|
|
|
|
[
|
2020-09-12 21:19:08 +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
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +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
|
|
|
|
{
|
2020-08-25 19:05:58 +00:00
|
|
|
foreach ($this->typeMethodWraps as $typeMethodWrap) {
|
2021-02-27 02:13:22 +00:00
|
|
|
if (! $this->isObjectType($node, $typeMethodWrap->getObjectType())) {
|
2018-12-12 19:34:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:05:58 +00:00
|
|
|
if (! $this->isName($node, $typeMethodWrap->getMethod())) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-12 19:34:32 +00:00
|
|
|
|
2020-08-25 19:05:58 +00:00
|
|
|
if (! $node->stmts) {
|
|
|
|
continue;
|
2018-12-12 19:34:32 +00:00
|
|
|
}
|
2020-08-25 19:05:58 +00:00
|
|
|
|
|
|
|
return $this->wrap($node, $typeMethodWrap->isArrayWrap());
|
2018-12-12 19:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:39:41 +00:00
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
2020-08-25 19:05:58 +00:00
|
|
|
$typeMethodWraps = $configuration[self::TYPE_METHOD_WRAPS] ?? [];
|
2020-09-12 21:19:08 +00:00
|
|
|
Assert::allIsInstanceOf($typeMethodWraps, WrapReturn::class);
|
2020-08-25 19:05:58 +00:00
|
|
|
$this->typeMethodWraps = $typeMethodWraps;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 19:05:58 +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)) {
|
2020-08-25 19:05:58 +00:00
|
|
|
return null;
|
2018-12-12 19:34:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 00:22:45 +00:00
|
|
|
foreach ($classMethod->stmts as $key => $stmt) {
|
2019-01-14 18:21:23 +00:00
|
|
|
if ($stmt instanceof Return_ && $stmt->expr !== null) {
|
2020-08-25 19:05:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-25 19:05:58 +00:00
|
|
|
$classMethod->stmts[$key] = $stmt;
|
2018-12-12 19:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 19:05:58 +00:00
|
|
|
|
|
|
|
return $classMethod;
|
2018-12-12 19:34:32 +00:00
|
|
|
}
|
|
|
|
}
|