Updated Rector to commit 8f24c73f1894356dd9ad3745924d027fa65a5602

8f24c73f18 Remove PARENT_NODE from CommentsMerger (#4011)
This commit is contained in:
Tomas Votruba 2023-05-29 08:09:04 +00:00
parent 969127f9a6
commit b6aa01ccb7
7 changed files with 33 additions and 87 deletions

View File

@ -3,21 +3,10 @@
declare (strict_types=1);
namespace Rector\BetterPhpDocParser\Comment;
use PhpParser\Comment;
use PhpParser\Node;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final class CommentsMerger
{
/**
* @readonly
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
{
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
}
/**
* @param Node[] $mergedNodes
*/
@ -34,51 +23,4 @@ final class CommentsMerger
// remove so comments "win"
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, null);
}
/**
* @api
*/
public function keepParent(Node $newNode, Node $oldNode) : void
{
$parentNode = $oldNode->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) {
return;
}
$phpDocInfo = $parentNode->getAttribute(AttributeKey::PHP_DOC_INFO);
$comments = $parentNode->getComments();
if ($phpDocInfo === null && $comments === []) {
return;
}
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
$newNode->setAttribute(AttributeKey::COMMENTS, $comments);
}
/**
* @api
*/
public function keepChildren(Node $newNode, Node $oldNode) : void
{
$childrenComments = $this->collectChildrenComments($oldNode);
if ($childrenComments === []) {
return;
}
$commentContent = '';
foreach ($childrenComments as $childComment) {
$commentContent .= $childComment->getText() . \PHP_EOL;
}
$newNode->setAttribute(AttributeKey::COMMENTS, [new Comment($commentContent)]);
}
/**
* @return Comment[]
*/
private function collectChildrenComments(Node $node) : array
{
$childrenComments = [];
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($node, static function (Node $node) use(&$childrenComments) {
$comments = $node->getComments();
if ($comments !== []) {
$childrenComments = \array_merge($childrenComments, $comments);
}
return null;
});
return $childrenComments;
}
}

View File

@ -9,11 +9,11 @@ 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\PhpParser\NodeTransformer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202305\Webmozart\Assert\Assert;
@ -34,15 +34,9 @@ final class ReturnArrayClassMethodToYieldRector extends AbstractRector implement
* @var \Rector\Core\PhpParser\NodeTransformer
*/
private $nodeTransformer;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
*/
private $commentsMerger;
public function __construct(NodeTransformer $nodeTransformer, CommentsMerger $commentsMerger)
public function __construct(NodeTransformer $nodeTransformer)
{
$this->nodeTransformer = $nodeTransformer;
$this->commentsMerger = $commentsMerger;
}
public function getRuleDefinition() : RuleDefinition
{
@ -53,7 +47,9 @@ final class SomeTest implements TestCase
{
public static function provideData()
{
return [['some text']];
return [
['some text']
];
}
}
CODE_SAMPLE
@ -82,6 +78,9 @@ CODE_SAMPLE
*/
public function refactor(Node $node) : ?Node
{
if ($node->stmts === null) {
return null;
}
$hasChanged = \false;
foreach ($this->methodsToYields as $methodToYield) {
if (!$this->isName($node, $methodToYield->getMethod())) {
@ -94,8 +93,12 @@ CODE_SAMPLE
if (!$arrayNode instanceof Array_) {
continue;
}
// keep comments of 1st array item
$firstComment = $node->stmts[0]->getAttribute(AttributeKey::COMMENTS);
$this->transformArrayToYieldsOnMethodNode($node, $arrayNode);
$this->commentsMerger->keepParent($node, $arrayNode);
if (\is_array($firstComment)) {
$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, \array_merge($firstComment, (array) $node->stmts[0]->getAttribute(AttributeKey::COMMENTS)));
}
$hasChanged = \true;
}
if (!$hasChanged) {
@ -129,7 +132,7 @@ CODE_SAMPLE
}
private function transformArrayToYieldsOnMethodNode(ClassMethod $classMethod, Array_ $array) : void
{
$yieldNodes = $this->nodeTransformer->transformArrayToYields($array);
$yields = $this->nodeTransformer->transformArrayToYields($array);
$this->removeReturnTag($classMethod);
// change return typehint
$classMethod->returnType = new FullyQualified('Iterator');
@ -139,7 +142,7 @@ CODE_SAMPLE
}
unset($classMethod->stmts[$key]);
}
$classMethod->stmts = \array_merge((array) $classMethod->stmts, $yieldNodes);
$classMethod->stmts = \array_merge((array) $classMethod->stmts, $yields);
}
private function removeReturnTag(ClassMethod $classMethod) : void
{

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'f429e2811c97689001f45c420d1d674322d00822';
public const PACKAGE_VERSION = '8f24c73f1894356dd9ad3745924d027fa65a5602';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-29 06:28:14';
public const RELEASE_DATE = '2023-05-29 09:05:07';
/**
* @var int
*/

View File

@ -59,19 +59,20 @@ final class NodeTransformer
*/
public function transformArrayToYields(Array_ $array) : array
{
$yieldNodes = [];
$yields = [];
foreach ($array->items as $arrayItem) {
if (!$arrayItem instanceof ArrayItem) {
continue;
}
$expressionNode = new Expression(new Yield_($arrayItem->value, $arrayItem->key));
$yield = new Yield_($arrayItem->value, $arrayItem->key);
$expression = new Expression($yield);
$arrayItemComments = $arrayItem->getComments();
if ($arrayItemComments !== []) {
$expressionNode->setAttribute(AttributeKey::COMMENTS, $arrayItemComments);
$expression->setAttribute(AttributeKey::COMMENTS, $arrayItemComments);
}
$yieldNodes[] = $expressionNode;
$yields[] = $expression;
}
return $yieldNodes;
return $yields;
}
/**
* @api symfony

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitde431eba275ceb2d397b23a8d00be279::getLoader();
return ComposerAutoloaderInit7977ba338c257f0b70710b3404e69e00::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitde431eba275ceb2d397b23a8d00be279
class ComposerAutoloaderInit7977ba338c257f0b70710b3404e69e00
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitde431eba275ceb2d397b23a8d00be279
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitde431eba275ceb2d397b23a8d00be279', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit7977ba338c257f0b70710b3404e69e00', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitde431eba275ceb2d397b23a8d00be279', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit7977ba338c257f0b70710b3404e69e00', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitde431eba275ceb2d397b23a8d00be279::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit7977ba338c257f0b70710b3404e69e00::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitde431eba275ceb2d397b23a8d00be279::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit7977ba338c257f0b70710b3404e69e00::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitde431eba275ceb2d397b23a8d00be279
class ComposerStaticInit7977ba338c257f0b70710b3404e69e00
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3086,9 +3086,9 @@ class ComposerStaticInitde431eba275ceb2d397b23a8d00be279
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitde431eba275ceb2d397b23a8d00be279::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitde431eba275ceb2d397b23a8d00be279::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitde431eba275ceb2d397b23a8d00be279::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit7977ba338c257f0b70710b3404e69e00::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit7977ba338c257f0b70710b3404e69e00::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit7977ba338c257f0b70710b3404e69e00::$classMap;
}, null, ClassLoader::class);
}