mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
Updated Rector to commit 02b93319906ffa9842cbab8f80d58d8b5576cf43
02b9331990
[Traverser] Add BreakingRemovalGuardNodeVisitor to reduce parent attribute usage on BreakingRemovalGuard (#3978)
This commit is contained in:
parent
cbde507496
commit
7b17dd2d5b
|
@ -4,10 +4,6 @@ declare (strict_types=1);
|
|||
namespace Rector\NodeRemoval;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
final class BreakingRemovalGuard
|
||||
|
@ -20,47 +16,15 @@ final class BreakingRemovalGuard
|
|||
if ($this->isLegalNodeRemoval($node)) {
|
||||
return;
|
||||
}
|
||||
// validate the node can be removed
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof Node) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
throw new ShouldNotHappenException(\sprintf('Node "%s" on line %d is child of "%s", so it cannot be removed as it would break PHP code. Change or remove the parent node instead.', \get_class($node), $node->getLine(), \get_class($parentNode)));
|
||||
/** @var string $childOfNodeType */
|
||||
$childOfNodeType = $node->getAttribute(AttributeKey::CHILD_OF_NODE_TYPE);
|
||||
throw new ShouldNotHappenException(\sprintf('Node "%s" on line %d is child of "%s", so it cannot be removed as it would break PHP code. Change or remove the parent node instead.', \get_class($node), $node->getLine(), $childOfNodeType));
|
||||
}
|
||||
/**
|
||||
* @api
|
||||
*/
|
||||
public function isLegalNodeRemoval(Node $node) : bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof If_ && $parentNode->cond === $node) {
|
||||
return \false;
|
||||
}
|
||||
if ($parentNode instanceof BooleanNot) {
|
||||
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
}
|
||||
if ($parentNode instanceof Assign) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->isIfCondition($node)) {
|
||||
return \false;
|
||||
}
|
||||
return !$this->isWhileCondition($node);
|
||||
}
|
||||
private function isIfCondition(Node $node) : bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof If_) {
|
||||
return \false;
|
||||
}
|
||||
return $parentNode->cond === $node;
|
||||
}
|
||||
private function isWhileCondition(Node $node) : bool
|
||||
{
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof While_) {
|
||||
return \false;
|
||||
}
|
||||
return $parentNode->cond === $node;
|
||||
return $node->getAttribute(AttributeKey::IS_BREAKING_REMOVAL_NODE) !== \true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -200,4 +200,12 @@ final class AttributeKey
|
|||
* @var string
|
||||
*/
|
||||
public const STMT_KEY = 'stmt_key';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const IS_BREAKING_REMOVAL_NODE = 'is_breaking_removal_node';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CHILD_OF_NODE_TYPE = 'child_of_node_type';
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
|
||||
final class BreakingRemovalGuardNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
|
||||
{
|
||||
public function enterNode(Node $node) : ?Node
|
||||
{
|
||||
if ($node instanceof If_) {
|
||||
$node->cond->setAttribute(AttributeKey::IS_BREAKING_REMOVAL_NODE, \true);
|
||||
$node->cond->setAttribute(AttributeKey::CHILD_OF_NODE_TYPE, If_::class);
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof BooleanNot) {
|
||||
$node->expr->setAttribute(AttributeKey::IS_BREAKING_REMOVAL_NODE, \true);
|
||||
$node->expr->setAttribute(AttributeKey::CHILD_OF_NODE_TYPE, BooleanNot::class);
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof While_) {
|
||||
$node->cond->setAttribute(AttributeKey::IS_BREAKING_REMOVAL_NODE, \true);
|
||||
$node->cond->setAttribute(AttributeKey::CHILD_OF_NODE_TYPE, While_::class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '919c052d712c4069a6b0c244744dfd124fb565eb';
|
||||
public const PACKAGE_VERSION = '02b93319906ffa9842cbab8f80d58d8b5576cf43';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-06-04 11:09:45';
|
||||
public const RELEASE_DATE = '2023-06-04 11:10:01';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitc11cbbb526989df739f5965a88ff027f::getLoader();
|
||||
return ComposerAutoloaderInit74ec1fce4be9fa253a71bae4bf69b3c0::getLoader();
|
||||
|
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
|
@ -1965,6 +1965,7 @@ return array(
|
|||
'Rector\\NodeTypeResolver\\PHPStan\\ParametersAcceptorSelectorVariantsWrapper' => $baseDir . '/packages/NodeTypeResolver/PHPStan/ParametersAcceptorSelectorVariantsWrapper.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\Contract\\NodeVisitor\\ScopeResolverNodeVisitorInterface' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/Contract/NodeVisitor/ScopeResolverNodeVisitorInterface.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\AssignedToNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/AssignedToNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\BreakingRemovalGuardNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/BreakingRemovalGuardNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefReturnNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefVariableNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
|
||||
|
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitc11cbbb526989df739f5965a88ff027f
|
||||
class ComposerAutoloaderInit74ec1fce4be9fa253a71bae4bf69b3c0
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInitc11cbbb526989df739f5965a88ff027f
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitc11cbbb526989df739f5965a88ff027f', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit74ec1fce4be9fa253a71bae4bf69b3c0', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitc11cbbb526989df739f5965a88ff027f', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit74ec1fce4be9fa253a71bae4bf69b3c0', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitc11cbbb526989df739f5965a88ff027f::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitc11cbbb526989df739f5965a88ff027f::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitc11cbbb526989df739f5965a88ff027f
|
||||
class ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -2207,6 +2207,7 @@ class ComposerStaticInitc11cbbb526989df739f5965a88ff027f
|
|||
'Rector\\NodeTypeResolver\\PHPStan\\ParametersAcceptorSelectorVariantsWrapper' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/ParametersAcceptorSelectorVariantsWrapper.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\Contract\\NodeVisitor\\ScopeResolverNodeVisitorInterface' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/Contract/NodeVisitor/ScopeResolverNodeVisitorInterface.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\AssignedToNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/AssignedToNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\BreakingRemovalGuardNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/BreakingRemovalGuardNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefReturnNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefVariableNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php',
|
||||
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
|
||||
|
@ -3061,9 +3062,9 @@ class ComposerStaticInitc11cbbb526989df739f5965a88ff027f
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitc11cbbb526989df739f5965a88ff027f::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitc11cbbb526989df739f5965a88ff027f::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitc11cbbb526989df739f5965a88ff027f::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit74ec1fce4be9fa253a71bae4bf69b3c0::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
8
vendor/composer/installed.json
vendored
8
vendor/composer/installed.json
vendored
|
@ -1852,12 +1852,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/rectorphp\/rector-doctrine.git",
|
||||
"reference": "e79ad0529fe431d018df95195874544e2828b7f1"
|
||||
"reference": "c6666a43f71d4922da41a5da42770f2523844286"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-doctrine\/zipball\/e79ad0529fe431d018df95195874544e2828b7f1",
|
||||
"reference": "e79ad0529fe431d018df95195874544e2828b7f1",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-doctrine\/zipball\/c6666a43f71d4922da41a5da42770f2523844286",
|
||||
"reference": "c6666a43f71d4922da41a5da42770f2523844286",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1882,7 +1882,7 @@
|
|||
"tomasvotruba\/type-coverage": "^0.2",
|
||||
"tomasvotruba\/unused-public": "^0.1"
|
||||
},
|
||||
"time": "2023-06-02T10:15:10+00:00",
|
||||
"time": "2023-06-04T10:42:02+00:00",
|
||||
"default-branch": true,
|
||||
"type": "rector-extension",
|
||||
"extra": {
|
||||
|
|
2
vendor/composer/installed.php
vendored
2
vendor/composer/installed.php
vendored
File diff suppressed because one or more lines are too long
|
@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
|
|||
*/
|
||||
final class GeneratedConfig
|
||||
{
|
||||
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main e79ad05'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 7e64cfd'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 71fabfb'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 5c0d61c'));
|
||||
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main c6666a4'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 7e64cfd'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 71fabfb'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 5c0d61c'));
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -167,8 +167,12 @@ CODE_SAMPLE
|
|||
if (!$this->isName($staticCall->class, 'parent')) {
|
||||
continue;
|
||||
}
|
||||
if ($staticCall->isFirstClassCallable()) {
|
||||
continue;
|
||||
}
|
||||
unset($classMethod->stmts[$key]);
|
||||
return $staticCall->getArgs()[1]->value;
|
||||
$args = $staticCall->getArgs();
|
||||
return $args[1]->value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -65,6 +65,9 @@ CODE_SAMPLE
|
|||
if (!$this->nodeTypeResolver->isObjectType($node->var, new ObjectType('Doctrine\\DBAL\\Query\\Expression\\CompositeExpression'))) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
$node->name = new Identifier('with');
|
||||
$firstArg = $node->getArgs()[0];
|
||||
$firstArg->value = new ArrayItem($firstArg->value, null, \false, [], \true);
|
||||
|
|
|
@ -65,6 +65,9 @@ CODE_SAMPLE
|
|||
if (!$this->isName($node->name, 'getRepository')) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0] ?? null;
|
||||
if (!$firstArg instanceof Arg) {
|
||||
return null;
|
||||
|
|
|
@ -78,6 +78,9 @@ CODE_SAMPLE
|
|||
if (!$this->isNames($node->name, self::ENTITY_REPOSITORY_PUBLIC_METHODS)) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
// is it getRepository(), replace it with DI property
|
||||
if ($node->var instanceof MethodCall && $this->isName($node->var->name, 'getRepository')) {
|
||||
return $this->refactorGetRepositoryMethodCall($node);
|
||||
|
|
Loading…
Reference in New Issue
Block a user