mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 04:19:44 +01:00
Updated Rector to commit eb7d73b432478f48f5e10248f7c20b981734a382
eb7d73b432
[DeadCode][CodeQualityStrict] Add SideEffectNodeDetector::detectCallExpr() (#543)
This commit is contained in:
parent
74fd096ad7
commit
b777b6eb45
@ -7,13 +7,9 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Do_;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\ElseIf_;
|
||||
@ -26,9 +22,8 @@ use PhpParser\Node\Stmt\Switch_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\SideEffect\PureFunctionDetector;
|
||||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
||||
use Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder;
|
||||
use Rector\NodeNestingScope\ParentFinder;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -44,9 +39,9 @@ final class MoveVariableDeclarationNearReferenceRector extends \Rector\Core\Rect
|
||||
*/
|
||||
private $scopeAwareNodeFinder;
|
||||
/**
|
||||
* @var \Rector\DeadCode\SideEffect\PureFunctionDetector
|
||||
* @var \Rector\DeadCode\SideEffect\SideEffectNodeDetector
|
||||
*/
|
||||
private $pureFunctionDetector;
|
||||
private $sideEffectNodeDetector;
|
||||
/**
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
@ -55,10 +50,10 @@ final class MoveVariableDeclarationNearReferenceRector extends \Rector\Core\Rect
|
||||
* @var \Rector\NodeNestingScope\ParentFinder
|
||||
*/
|
||||
private $parentFinder;
|
||||
public function __construct(\Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder $scopeAwareNodeFinder, \Rector\DeadCode\SideEffect\PureFunctionDetector $pureFunctionDetector, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\NodeNestingScope\ParentFinder $parentFinder)
|
||||
public function __construct(\Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder $scopeAwareNodeFinder, \Rector\DeadCode\SideEffect\SideEffectNodeDetector $sideEffectNodeDetector, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\NodeNestingScope\ParentFinder $parentFinder)
|
||||
{
|
||||
$this->scopeAwareNodeFinder = $scopeAwareNodeFinder;
|
||||
$this->pureFunctionDetector = $pureFunctionDetector;
|
||||
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->parentFinder = $parentFinder;
|
||||
}
|
||||
@ -229,29 +224,10 @@ CODE_SAMPLE
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function isClassCallerThrowable(\PhpParser\Node\Expr\StaticCall $staticCall) : bool
|
||||
{
|
||||
$class = $staticCall->class;
|
||||
if (!$class instanceof \PhpParser\Node\Name) {
|
||||
return \false;
|
||||
}
|
||||
$throwableType = new \PHPStan\Type\ObjectType('Throwable');
|
||||
$type = new \PHPStan\Type\ObjectType($class->toString());
|
||||
return $throwableType->isSuperTypeOf($type)->yes();
|
||||
}
|
||||
private function hasCall(\PhpParser\Node $node) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst($node, function (\PhpParser\Node $n) : bool {
|
||||
if ($n instanceof \PhpParser\Node\Expr\StaticCall && !$this->isClassCallerThrowable($n)) {
|
||||
return \true;
|
||||
}
|
||||
if ($n instanceof \PhpParser\Node\Expr\MethodCall) {
|
||||
return \true;
|
||||
}
|
||||
if (!$n instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||
return \false;
|
||||
}
|
||||
return !$this->pureFunctionDetector->detect($n);
|
||||
return $this->sideEffectNodeDetector->detectCallExpr($n);
|
||||
});
|
||||
}
|
||||
private function getCountFound(\PhpParser\Node $node, \PhpParser\Node\Expr\Variable $variable) : int
|
||||
|
@ -3,16 +3,22 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\SideEffect;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\NullsafeMethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
use PHPStan\Type\ConstantType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
final class SideEffectNodeDetector
|
||||
{
|
||||
@ -20,6 +26,10 @@ final class SideEffectNodeDetector
|
||||
* @var array<class-string<Expr>>
|
||||
*/
|
||||
private const SIDE_EFFECT_NODE_TYPES = [\PhpParser\Node\Scalar\Encapsed::class, \PhpParser\Node\Expr\New_::class, \PhpParser\Node\Expr\BinaryOp\Concat::class, \PhpParser\Node\Expr\PropertyFetch::class];
|
||||
/**
|
||||
* @var array<class-string<Expr>>
|
||||
*/
|
||||
private const CALL_EXPR_SIDE_EFFECT_NODE_TYPES = [\PhpParser\Node\Expr\MethodCall::class, \PhpParser\Node\Expr\NullsafeMethodCall::class, \PhpParser\Node\Expr\StaticCall::class];
|
||||
/**
|
||||
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
||||
*/
|
||||
@ -57,6 +67,33 @@ final class SideEffectNodeDetector
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
public function detectCallExpr(\PhpParser\Node $node) : bool
|
||||
{
|
||||
if (!$node instanceof \PhpParser\Node\Expr) {
|
||||
return \false;
|
||||
}
|
||||
if ($node instanceof \PhpParser\Node\Expr\StaticCall && $this->isClassCallerThrowable($node)) {
|
||||
return \false;
|
||||
}
|
||||
$exprClass = \get_class($node);
|
||||
if (\in_array($exprClass, self::CALL_EXPR_SIDE_EFFECT_NODE_TYPES, \true)) {
|
||||
return \true;
|
||||
}
|
||||
if ($node instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||
return !$this->pureFunctionDetector->detect($node);
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function isClassCallerThrowable(\PhpParser\Node\Expr\StaticCall $staticCall) : bool
|
||||
{
|
||||
$class = $staticCall->class;
|
||||
if (!$class instanceof \PhpParser\Node\Name) {
|
||||
return \false;
|
||||
}
|
||||
$throwableType = new \PHPStan\Type\ObjectType('Throwable');
|
||||
$type = new \PHPStan\Type\ObjectType($class->toString());
|
||||
return $throwableType->isSuperTypeOf($type)->yes();
|
||||
}
|
||||
private function resolveVariable(\PhpParser\Node\Expr $expr) : ?\PhpParser\Node\Expr\Variable
|
||||
{
|
||||
while ($expr instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
|
||||
|
@ -16,11 +16,11 @@ final class VersionResolver
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '46a6ab042e2e522c3c2b4b11aa09414df374387b';
|
||||
public const PACKAGE_VERSION = 'eb7d73b432478f48f5e10248f7c20b981734a382';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2021-07-29 16:25:28';
|
||||
public const RELEASE_DATE = '2021-07-29 19:17:22';
|
||||
public static function resolvePackageVersion() : string
|
||||
{
|
||||
$process = new \RectorPrefix20210729\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -4,4 +4,4 @@
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db::getLoader();
|
||||
return ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8::getLoader();
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db
|
||||
class ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,15 +22,15 @@ class ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8::getInitializer($loader));
|
||||
} else {
|
||||
$classMap = require __DIR__ . '/autoload_classmap.php';
|
||||
if ($classMap) {
|
||||
@ -42,19 +42,19 @@ class ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire0f90a3bd9b784cbd6786368bfc4945db($fileIdentifier, $file);
|
||||
composerRequire4f561c0bfd40d5c00e90bf48c1a76bf8($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequire0f90a3bd9b784cbd6786368bfc4945db($fileIdentifier, $file)
|
||||
function composerRequire4f561c0bfd40d5c00e90bf48c1a76bf8($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db
|
||||
class ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8
|
||||
{
|
||||
public static $files = array (
|
||||
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
||||
@ -3852,9 +3852,9 @@ class ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit0f90a3bd9b784cbd6786368bfc4945db::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit4f561c0bfd40d5c00e90bf48c1a76bf8::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
||||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20210729\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db', false) && !interface_exists('ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db', false) && !trait_exists('ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db', false)) {
|
||||
spl_autoload_call('RectorPrefix20210729\ComposerAutoloaderInit0f90a3bd9b784cbd6786368bfc4945db');
|
||||
if (!class_exists('ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8', false) && !interface_exists('ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8', false) && !trait_exists('ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8', false)) {
|
||||
spl_autoload_call('RectorPrefix20210729\ComposerAutoloaderInit4f561c0bfd40d5c00e90bf48c1a76bf8');
|
||||
}
|
||||
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
|
||||
spl_autoload_call('RectorPrefix20210729\Doctrine\Inflector\Inflector');
|
||||
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
|
||||
return \RectorPrefix20210729\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequire0f90a3bd9b784cbd6786368bfc4945db')) {
|
||||
function composerRequire0f90a3bd9b784cbd6786368bfc4945db() {
|
||||
return \RectorPrefix20210729\composerRequire0f90a3bd9b784cbd6786368bfc4945db(...func_get_args());
|
||||
if (!function_exists('composerRequire4f561c0bfd40d5c00e90bf48c1a76bf8')) {
|
||||
function composerRequire4f561c0bfd40d5c00e90bf48c1a76bf8() {
|
||||
return \RectorPrefix20210729\composerRequire4f561c0bfd40d5c00e90bf48c1a76bf8(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('parseArgs')) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user