Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

68 changes: 8 additions & 60 deletions rules/Transform/Rector/ClassMethod/WrapReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,20 @@
namespace Rector\Transform\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\WrapReturn;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\ClassMethod\WrapReturnRector\WrapReturnRectorTest
* @deprecated This rule is deprecated, as only used for 1 case now covered by dedicated GetExtendedTypesReturnArrayRector in rector-symfony. Write a custom rule for the exact project case instead.
*/
final class WrapReturnRector extends AbstractRector implements ConfigurableRectorInterface
final class WrapReturnRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var WrapReturn[]
*/
private array $typeMethodWraps = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Wrap return value of specific method', [
Expand Down Expand Up @@ -70,59 +61,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;

foreach ($this->typeMethodWraps as $typeMethodWrap) {
foreach ($node->getMethods() as $classMethod) {
if (! $this->isName($classMethod, $typeMethodWrap->getMethod())) {
continue;
}

if ($node->stmts === null) {
continue;
}

if (! $this->isObjectType($node, $typeMethodWrap->getObjectType())) {
continue;
}

if ($typeMethodWrap->isArrayWrap() && $this->wrap($classMethod)) {
$hasChanged = true;
}
}
}

if ($hasChanged) {
return $node;
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as only used for 1 case now covered by a dedicated rule; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, WrapReturn::class);
$this->typeMethodWraps = $configuration;
}

private function wrap(ClassMethod $classMethod): bool
{
if (! is_iterable($classMethod->stmts)) {
return false;
}

$hasChanged = false;
foreach ($classMethod->stmts as $stmt) {
if ($stmt instanceof Return_ && $stmt->expr instanceof Expr
&& ! $stmt->expr instanceof Array_) {
$stmt->expr = new Array_([new ArrayItem($stmt->expr)]);
$hasChanged = true;
}
}

return $hasChanged;
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/WrapReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Type\ObjectType;
use Rector\Validation\RectorAssert;

/**
* @api used in deprecated WrapReturnRector configs
*/
final readonly class WrapReturn
{
public function __construct(
Expand Down
Loading