Updated Rector to commit c504b2f11f2b732a744ad804b3a4b5afc30e2841

c504b2f11f Refactor PARENT_NODE away from GetClassOnNullRector (#3944)
This commit is contained in:
Tomas Votruba 2023-05-24 09:36:58 +00:00
parent a32354e5eb
commit d7eced92e2
10 changed files with 55 additions and 110 deletions

View File

@ -5,11 +5,11 @@ namespace Rector\Php72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use Rector\Core\Rector\AbstractScopeAwareRector;
@ -58,55 +58,47 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
return [Class_::class];
}
/**
* @param FuncCall $node
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if (!$this->isName($node, 'get_class')) {
return null;
}
if (!isset($node->getArgs()[0])) {
return null;
}
$firstArg = $node->getArgs()[0];
$firstArgValue = $firstArg->value;
if (!$scope->isInClass()) {
return null;
}
// possibly already changed
if ($this->shouldSkip($node)) {
return null;
}
$firstArgType = $this->getType($firstArgValue);
if (!$this->nodeTypeResolver->isNullableType($firstArgValue) && !$firstArgType instanceof NullType) {
return null;
}
$notIdentical = new NotIdentical($firstArgValue, $this->nodeFactory->createNull());
$funcCall = $this->createGetClassFuncCall($node);
$selfClassConstFetch = $this->nodeFactory->createClassConstReference('self');
return new Ternary($notIdentical, $funcCall, $selfClassConstFetch);
}
private function shouldSkip(FuncCall $funcCall) : bool
{
$isJustAdded = (bool) $funcCall->getAttribute(AttributeKey::DO_NOT_CHANGE);
if ($isJustAdded) {
return \true;
}
$classLike = $this->betterNodeFinder->findParentType($funcCall, Class_::class);
if (!$classLike instanceof Class_) {
return \true;
}
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Ternary) {
if ($this->isIdenticalToNotNull($funcCall, $parentNode)) {
return \true;
$hasChanged = \false;
$this->traverseNodesWithCallable($node, function (Node $node) use(&$hasChanged) {
if ($node instanceof Ternary) {
return NodeTraverser::STOP_TRAVERSAL;
}
return $this->isNotIdenticalToNull($funcCall, $parentNode);
if (!$node instanceof FuncCall) {
return null;
}
// just created func call
if ($node->getAttribute(AttributeKey::DO_NOT_CHANGE) === \true) {
return null;
}
if (!$this->isName($node, 'get_class')) {
return null;
}
$firstArg = $node->getArgs()[0] ?? null;
if (!$firstArg instanceof Arg) {
return null;
}
$firstArgValue = $firstArg->value;
$firstArgType = $this->getType($firstArgValue);
if (!$this->nodeTypeResolver->isNullableType($firstArgValue) && !$firstArgType instanceof NullType) {
return null;
}
$notIdentical = new NotIdentical($firstArgValue, $this->nodeFactory->createNull());
$funcCall = $this->createGetClassFuncCall($node);
$selfClassConstFetch = $this->nodeFactory->createClassConstReference('self');
$hasChanged = \true;
return new Ternary($notIdentical, $funcCall, $selfClassConstFetch);
});
if ($hasChanged) {
return $node;
}
return \false;
return null;
}
private function createGetClassFuncCall(FuncCall $oldFuncCall) : FuncCall
{
@ -114,45 +106,4 @@ CODE_SAMPLE
$funcCall->setAttribute(AttributeKey::DO_NOT_CHANGE, \true);
return $funcCall;
}
/**
* E.g. "$value === [!null] ? get_class($value)"
*/
private function isIdenticalToNotNull(FuncCall $funcCall, Ternary $ternary) : bool
{
if (!$ternary->cond instanceof Identical) {
return \false;
}
if (!isset($funcCall->getArgs()[0])) {
return \false;
}
if ($this->nodeComparator->areNodesEqual($ternary->cond->left, $funcCall->getArgs()[0]->value) && !$this->valueResolver->isNull($ternary->cond->right)) {
return \true;
}
if (!$this->nodeComparator->areNodesEqual($ternary->cond->right, $funcCall->getArgs()[0]->value)) {
return \false;
}
return !$this->valueResolver->isNull($ternary->cond->left);
}
/**
* E.g. "$value !== null ? get_class($value)"
*/
private function isNotIdenticalToNull(FuncCall $funcCall, Ternary $ternary) : bool
{
if (!$ternary->cond instanceof NotIdentical) {
return \false;
}
if (!isset($funcCall->getArgs()[0])) {
return \false;
}
if (!$funcCall->args[0] instanceof Arg) {
return \false;
}
if ($this->nodeComparator->areNodesEqual($ternary->cond->left, $funcCall->args[0]->value) && $this->valueResolver->isNull($ternary->cond->right)) {
return \true;
}
if (!$this->nodeComparator->areNodesEqual($ternary->cond->right, $funcCall->args[0]->value)) {
return \false;
}
return $this->valueResolver->isNull($ternary->cond->left);
}
}

View File

@ -6,7 +6,6 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\ClassReflection;
@ -17,7 +16,6 @@ use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnVendorLockResolver;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '3eadce164860c1d4465e40fd5e07fb11d3aa1b99';
public const PACKAGE_VERSION = 'c504b2f11f2b732a744ad804b3a4b5afc30e2841';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-24 04:15:17';
public const RELEASE_DATE = '2023-05-24 09:31:55';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -88,8 +88,7 @@ if (\PHP_VERSION_ID < 80000) {
}
}
if (\function_exists('stream_get_wrappers') && \in_array('phpvfscomposer', \stream_get_wrappers(), \true) || \function_exists('stream_wrapper_register') && \stream_wrapper_register('phpvfscomposer', 'RectorPrefix202305\\Composer\\BinProxyWrapper')) {
include "phpvfscomposer://" . __DIR__ . '/..' . '/nette/neon/bin/neon-lint';
exit(0);
return include "phpvfscomposer://" . __DIR__ . '/..' . '/nette/neon/bin/neon-lint';
}
}
include __DIR__ . '/..' . '/nette/neon/bin/neon-lint';
return include __DIR__ . '/..' . '/nette/neon/bin/neon-lint';

View File

@ -88,8 +88,7 @@ if (\PHP_VERSION_ID < 80000) {
}
}
if (\function_exists('stream_get_wrappers') && \in_array('phpvfscomposer', \stream_get_wrappers(), \true) || \function_exists('stream_wrapper_register') && \stream_wrapper_register('phpvfscomposer', 'RectorPrefix202305\\Composer\\BinProxyWrapper')) {
include "phpvfscomposer://" . __DIR__ . '/..' . '/nikic/php-parser/bin/php-parse';
exit(0);
return include "phpvfscomposer://" . __DIR__ . '/..' . '/nikic/php-parser/bin/php-parse';
}
}
include __DIR__ . '/..' . '/nikic/php-parser/bin/php-parse';
return include __DIR__ . '/..' . '/nikic/php-parser/bin/php-parse';

5
vendor/bin/phpstan vendored
View File

@ -88,8 +88,7 @@ if (\PHP_VERSION_ID < 80000) {
}
}
if (\function_exists('stream_get_wrappers') && \in_array('phpvfscomposer', \stream_get_wrappers(), \true) || \function_exists('stream_wrapper_register') && \stream_wrapper_register('phpvfscomposer', 'RectorPrefix202305\\Composer\\BinProxyWrapper')) {
include "phpvfscomposer://" . __DIR__ . '/..' . '/phpstan/phpstan/phpstan';
exit(0);
return include "phpvfscomposer://" . __DIR__ . '/..' . '/phpstan/phpstan/phpstan';
}
}
include __DIR__ . '/..' . '/phpstan/phpstan/phpstan';
return include __DIR__ . '/..' . '/phpstan/phpstan/phpstan';

View File

@ -112,9 +112,8 @@ if (PHP_VERSION_ID < 80000) {
(function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true))
|| (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper'))
) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/phpstan/phpstan/phpstan.phar');
exit(0);
return include("phpvfscomposer://" . __DIR__ . '/..'.'/phpstan/phpstan/phpstan.phar');
}
}
include __DIR__ . '/..'.'/phpstan/phpstan/phpstan.phar';
return include __DIR__ . '/..'.'/phpstan/phpstan/phpstan.phar';

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitb7b74474b87e58ef937ad854d6d77003
class ComposerAutoloaderInitd1e79f67f253438ff69fc93ef401a7af
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitb7b74474b87e58ef937ad854d6d77003
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitb7b74474b87e58ef937ad854d6d77003', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitd1e79f67f253438ff69fc93ef401a7af', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitb7b74474b87e58ef937ad854d6d77003', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitd1e79f67f253438ff69fc93ef401a7af', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitb7b74474b87e58ef937ad854d6d77003::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitb7b74474b87e58ef937ad854d6d77003::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af::$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 ComposerStaticInitb7b74474b87e58ef937ad854d6d77003
class ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3106,9 +3106,9 @@ class ComposerStaticInitb7b74474b87e58ef937ad854d6d77003
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitb7b74474b87e58ef937ad854d6d77003::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb7b74474b87e58ef937ad854d6d77003::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb7b74474b87e58ef937ad854d6d77003::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd1e79f67f253438ff69fc93ef401a7af::$classMap;
}, null, ClassLoader::class);
}