mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-17 08:32:22 +00:00
add WrapReturnRector
This commit is contained in:
parent
925b046f0c
commit
2e3634f4ae
|
@ -64,21 +64,21 @@ services:
|
|||
Rector\Rector\MethodCall\MethodNameReplacerRector:
|
||||
Symfony\Component\Cache\CacheItem:
|
||||
getPreviousTags: 'getMetadata'
|
||||
|
||||
# todo use: "Symfony\Component\Form\AbstractTypeExtension"
|
||||
Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension:
|
||||
Symfony\Component\Form\AbstractTypeExtension:
|
||||
getExtendedType: 'getExtendedTypes'
|
||||
|
||||
Rector\Rector\Typehint\ReturnTypehintRector:
|
||||
# todo use: "Symfony\Component\Form\AbstractTypeExtension"
|
||||
Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension:
|
||||
Symfony\Component\Form\AbstractTypeExtension:
|
||||
getExtendedTypes: 'iterable'
|
||||
|
||||
Rector\Rector\Visibility\ChangeMethodVisibilityRector:
|
||||
# todo use: "Symfony\Component\Form\AbstractTypeExtension"
|
||||
Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension:
|
||||
Symfony\Component\Form\AbstractTypeExtension:
|
||||
getExtendedTypes: 'static'
|
||||
|
||||
Rector\Rector\ClassMethod\WrapReturnRector:
|
||||
Symfony\Component\Form\AbstractTypeExtension:
|
||||
getExtendedTypes: 'array'
|
||||
|
||||
Rector\Rector\Argument\ArgumentDefaultValueReplacerRector:
|
||||
# https://github.com/symfony/symfony/commit/9493cfd5f2366dab19bbdde0d0291d0575454567
|
||||
Symfony\Component\HttpFoundation\Cookie:
|
||||
|
|
|
@ -73,7 +73,6 @@ final class VisibilityMaintainer
|
|||
if ($visibility === 'static') {
|
||||
$node->flags |= Class_::MODIFIER_STATIC;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function ensureIsClassMethodOrProperty(Node $node, string $location): void
|
||||
|
|
105
src/Rector/ClassMethod/WrapReturnRector.php
Normal file
105
src/Rector/ClassMethod/WrapReturnRector.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class WrapReturnRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var mixed[][]
|
||||
*/
|
||||
private $typeToMethodToWrap = [];
|
||||
|
||||
/**
|
||||
* @param mixed[][] $typeToMethodToWrap
|
||||
*/
|
||||
public function __construct(array $typeToMethodToWrap)
|
||||
{
|
||||
$this->typeToMethodToWrap = $typeToMethodToWrap;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Wrap return value of specificx method', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function getItem()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function getItem()
|
||||
{
|
||||
return [1];
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
foreach ($this->typeToMethodToWrap as $type => $methodToType) {
|
||||
if (! $this->isType($node, $type)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($methodToType as $method => $type) {
|
||||
if (! $this->isName($node, $method)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $node->stmts) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->wrap($node, $type);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function wrap(ClassMethod $classMethodNode, string $type): void
|
||||
{
|
||||
if (! is_iterable($classMethodNode->stmts)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($classMethodNode->stmts as $i => $stmt) {
|
||||
if ($stmt instanceof Return_) {
|
||||
if ($type === 'array') {
|
||||
$stmt->expr = new Array_([$stmt->expr]);
|
||||
}
|
||||
|
||||
$classMethodNode->stmts[$i] = $stmt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\ClassMethod\WrapReturnRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\ClassMethod\WrapReturnRector\Source\SomeReturnClass;
|
||||
|
||||
final class SomeClass extends SomeReturnClass
|
||||
{
|
||||
public function getItem()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\ClassMethod\WrapReturnRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\ClassMethod\WrapReturnRector\Source\SomeReturnClass;
|
||||
|
||||
final class SomeClass extends SomeReturnClass
|
||||
{
|
||||
public function getItem()
|
||||
{
|
||||
return [1];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,8 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\ClassMethod\WrapReturnRector\Source;
|
||||
|
||||
class SomeReturnClass
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\ClassMethod\WrapReturnRector;
|
||||
|
||||
use Rector\Rector\ClassMethod\WrapReturnRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Tests\Rector\ClassMethod\WrapReturnRector\Source\SomeReturnClass;
|
||||
|
||||
final class WrapReturnRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return WrapReturnRector::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
protected function getRectorConfiguration(): ?array
|
||||
{
|
||||
return [
|
||||
SomeReturnClass::class => [
|
||||
'getItem' => 'array',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
2
tests/Rector/ClassMethod/WrapReturnRector/config.yml
Normal file
2
tests/Rector/ClassMethod/WrapReturnRector/config.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
services:
|
||||
Rector\Rector\Rector\ClassMethod\WrapReturnRector: ~
|
Loading…
Reference in New Issue
Block a user