rector/rules/CodingStyle/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector.php
Tomas Votruba e9e85a1300 Updated Rector to commit a8b01f217b
a8b01f217b [automated] Re-Generate Nodes/Rectors Documentation (#619)
2021-08-08 00:26:40 +00:00

160 lines
6.0 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\NodeTransformer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20210808\Webmozart\Assert\Assert;
/**
* @changelog https://medium.com/tech-tajawal/use-memory-gently-with-yield-in-php-7e62e2480b8d
* @see https://3v4l.org/5PJid
*
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector\ReturnArrayClassMethodToYieldRectorTest
*/
final class ReturnArrayClassMethodToYieldRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string
*/
public const METHODS_TO_YIELDS = 'methods_to_yields';
/**
* @var ReturnArrayClassMethodToyield[]
*/
private $methodsToYields = [];
/**
* @var \Rector\Core\PhpParser\NodeTransformer
*/
private $nodeTransformer;
/**
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
*/
private $commentsMerger;
public function __construct(\Rector\Core\PhpParser\NodeTransformer $nodeTransformer, \Rector\BetterPhpDocParser\Comment\CommentsMerger $commentsMerger)
{
$this->nodeTransformer = $nodeTransformer;
$this->commentsMerger = $commentsMerger;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Turns array return to yield return in specific type and method', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
{
public static function provideData()
{
return [['some text']];
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
{
public static function provideData()
{
yield ['some text'];
}
}
CODE_SAMPLE
, [self::METHODS_TO_YIELDS => [new \Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield('PHPUnit\\Framework\\TestCase', '*provide*')]])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
$hasChanged = \false;
foreach ($this->methodsToYields as $methodToYield) {
if (!$this->isObjectType($node, $methodToYield->getObjectType())) {
continue;
}
if (!$this->isName($node, $methodToYield->getMethod())) {
continue;
}
$arrayNode = $this->collectReturnArrayNodesFromClassMethod($node);
if (!$arrayNode instanceof \PhpParser\Node\Expr\Array_) {
continue;
}
$this->transformArrayToYieldsOnMethodNode($node, $arrayNode);
$this->commentsMerger->keepParent($node, $arrayNode);
$hasChanged = \true;
}
if (!$hasChanged) {
return null;
}
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
$methodsToYields = $configuration[self::METHODS_TO_YIELDS] ?? [];
\RectorPrefix20210808\Webmozart\Assert\Assert::allIsInstanceOf($methodsToYields, \Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield::class);
$this->methodsToYields = $methodsToYields;
}
private function collectReturnArrayNodesFromClassMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod) : ?\PhpParser\Node\Expr\Array_
{
if ($classMethod->stmts === null) {
return null;
}
foreach ($classMethod->stmts as $statement) {
if ($statement instanceof \PhpParser\Node\Stmt\Return_) {
$returnedExpr = $statement->expr;
if (!$returnedExpr instanceof \PhpParser\Node\Expr\Array_) {
continue;
}
return $returnedExpr;
}
}
return null;
}
private function transformArrayToYieldsOnMethodNode(\PhpParser\Node\Stmt\ClassMethod $classMethod, \PhpParser\Node\Expr\Array_ $array) : void
{
$yieldNodes = $this->nodeTransformer->transformArrayToYields($array);
// remove whole return node
$parentNode = $array->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if (!$parentNode instanceof \PhpParser\Node) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
$this->removeReturnTag($classMethod);
// change return typehint
$classMethod->returnType = new \PhpParser\Node\Name\FullyQualified('Iterator');
foreach ((array) $classMethod->stmts as $key => $classMethodStmt) {
if (!$classMethodStmt instanceof \PhpParser\Node\Stmt\Return_) {
continue;
}
unset($classMethod->stmts[$key]);
}
$classMethod->stmts = \array_merge((array) $classMethod->stmts, $yieldNodes);
}
private function removeReturnTag(\PhpParser\Node\Stmt\ClassMethod $classMethod) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$phpDocInfo->removeByType(\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode::class);
}
}