1
0
mirror of https://github.com/rectorphp/rector.git synced 2025-04-25 09:55:28 +02:00

Updated Rector to commit a394e5ea44ef09812ec1224aa0f402a483400d73

a394e5ea44 Split instance of check to 2 rules ()
This commit is contained in:
Tomas Votruba 2023-06-10 18:53:37 +00:00
parent bc5f802deb
commit 99469d703d
8 changed files with 247 additions and 120 deletions

@ -29,6 +29,7 @@ use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\DeadCode\Rector\If_\RemoveTypedPropertyDeadInstanceOfRector;
use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
use Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector;
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
@ -78,6 +79,7 @@ return static function (RectorConfig $rectorConfig) : void {
RemoveDeadConditionAboveReturnRector::class,
RemoveUnusedConstructorParamRector::class,
RemoveDeadInstanceOfRector::class,
RemoveTypedPropertyDeadInstanceOfRector::class,
RemoveDeadLoopRector::class,
RemoveUnusedPrivateMethodParameterRector::class,
// docblock

@ -5,25 +5,22 @@ namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use PHPStan\Type\MixedType;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -36,56 +33,26 @@ final class RemoveDeadInstanceOfRector extends AbstractRector
* @var \Rector\Core\NodeManipulator\IfManipulator
*/
private $ifManipulator;
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
/**
* @readonly
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @readonly
* @var \Rector\Php80\NodeAnalyzer\PromotedPropertyResolver
*/
private $promotedPropertyResolver;
/**
* @readonly
* @var \Rector\NodeNestingScope\ContextAnalyzer
*/
private $contextAnalyzer;
public function __construct(IfManipulator $ifManipulator, PropertyFetchAnalyzer $propertyFetchAnalyzer, ConstructorAssignDetector $constructorAssignDetector, PromotedPropertyResolver $promotedPropertyResolver, ContextAnalyzer $contextAnalyzer)
public function __construct(IfManipulator $ifManipulator)
{
$this->ifManipulator = $ifManipulator;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->promotedPropertyResolver = $promotedPropertyResolver;
$this->contextAnalyzer = $contextAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove dead instanceof check on type hinted variable', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
function run(stdClass $stdClass)
{
public function go(stdClass $stdClass)
{
if (! $stdClass instanceof stdClass) {
return false;
}
return true;
if (! $stdClass instanceof stdClass) {
return false;
}
return true;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
function run(stdClass $stdClass)
{
public function go(stdClass $stdClass)
{
return true;
}
return true;
}
CODE_SAMPLE
)]);
@ -95,18 +62,19 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [If_::class];
return [If_::class, For_::class, Foreach_::class, While_::class, Do_::class];
}
/**
* @param If_ $node
* @param If_|For_|Foreach_|While_|Do_ $node
* @return Stmt[]|null|int
*/
public function refactor(Node $node)
{
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($node)) {
return null;
// avoid ifs in a loop, as unexpected behavior
if (!$node instanceof If_) {
return NodeTraverser::STOP_TRAVERSAL;
}
if ($this->contextAnalyzer->isInLoop($node)) {
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($node)) {
return null;
}
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) {
@ -125,15 +93,16 @@ CODE_SAMPLE
if (!$instanceof->class instanceof Name) {
return null;
}
// handle in another rule
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
return null;
}
$classType = $this->nodeTypeResolver->getType($instanceof->class);
$exprType = $this->nodeTypeResolver->getType($instanceof->expr);
$isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType)->yes();
if (!$isSameStaticTypeOrSubtype) {
return null;
}
if (!$instanceof->expr instanceof Variable && !$this->isInPropertyPromotedParams($instanceof->expr) && $this->isSkippedPropertyFetch($instanceof->expr)) {
return null;
}
if ($this->shouldSkipFromNotTypedParam($instanceof)) {
return null;
}
@ -143,66 +112,19 @@ CODE_SAMPLE
if ($if->stmts === []) {
return NodeTraverser::REMOVE_NODE;
}
// unwrap stmts
return $if->stmts;
}
private function shouldSkipFromNotTypedParam(Instanceof_ $instanceof) : bool
{
$functionLike = $this->betterNodeFinder->findParentType($instanceof, FunctionLike::class);
if (!$functionLike instanceof FunctionLike) {
return \false;
}
$variable = $instanceof->expr;
$isReAssign = (bool) $this->betterNodeFinder->findFirstPrevious($instanceof, function (Node $subNode) use($variable) : bool {
return $subNode instanceof Assign && $this->nodeComparator->areNodesEqual($subNode->var, $variable);
});
if ($isReAssign) {
return \false;
}
$params = $functionLike->getParams();
foreach ($params as $param) {
if ($this->nodeComparator->areNodesEqual($param->var, $instanceof->expr)) {
return $param->type === null;
}
}
return \false;
$nativeParamType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
return $nativeParamType instanceof MixedType;
}
private function isSkippedPropertyFetch(Expr $expr) : bool
private function isPropertyFetch(Expr $expr) : bool
{
if (!$this->propertyFetchAnalyzer->isPropertyFetch($expr)) {
if ($expr instanceof PropertyFetch) {
return \true;
}
/** @var PropertyFetch|StaticPropertyFetch $propertyFetch */
$propertyFetch = $expr;
$classLike = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);
if (!$classLike instanceof Class_) {
return \true;
}
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($propertyFetch);
$property = $classLike->getProperty($propertyName);
if (!$property instanceof Property) {
return \true;
}
$isPropertyAssignedInConstuctor = $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
return $property->type === null && !$isPropertyAssignedInConstuctor;
}
private function isInPropertyPromotedParams(Expr $expr) : bool
{
if (!$expr instanceof PropertyFetch) {
return \false;
}
$classLike = $this->betterNodeFinder->findParentType($expr, Class_::class);
if (!$classLike instanceof Class_) {
return \false;
}
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($expr);
$params = $this->promotedPropertyResolver->resolveFromClass($classLike);
foreach ($params as $param) {
if ($this->nodeNameResolver->isName($param, $propertyName)) {
return \true;
}
}
return \false;
return $expr instanceof StaticPropertyFetch;
}
}

@ -0,0 +1,201 @@
<?php
declare (strict_types=1);
namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveTypedPropertyDeadInstanceOfRector\RemoveTypedPropertyDeadInstanceOfRectorTest
*/
final class RemoveTypedPropertyDeadInstanceOfRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\IfManipulator
*/
private $ifManipulator;
/**
* @readonly
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @readonly
* @var \Rector\Php80\NodeAnalyzer\PromotedPropertyResolver
*/
private $promotedPropertyResolver;
public function __construct(IfManipulator $ifManipulator, ConstructorAssignDetector $constructorAssignDetector, PromotedPropertyResolver $promotedPropertyResolver)
{
$this->ifManipulator = $ifManipulator;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->promotedPropertyResolver = $promotedPropertyResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove dead instanceof check on type hinted property', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
private $someObject;
public function __construct($someObject)
{
$this->someObject = $someObject;
}
public function run()
{
if ($this->someObject instanceof SomeObject) {
return true;
}
return false;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
private $someObject;
public function __construct($someObject)
{
$this->someObject = $someObject;
}
public function run()
{
return true;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Class_
{
$hasChanged = \false;
$class = $node;
$this->traverseNodesWithCallable($node->getMethods(), function (Node $node) use(&$hasChanged, $class) {
// avoid loop ifs
if ($node instanceof While_ || $node instanceof Foreach_ || $node instanceof For_ || $node instanceof Do_) {
return NodeTraverser::STOP_TRAVERSAL;
}
if (!$node instanceof If_) {
return null;
}
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($node)) {
return null;
}
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) {
$result = $this->refactorStmtAndInstanceof($class, $node, $node->cond->expr);
if ($result !== null) {
$hasChanged = \true;
return $result;
}
}
if ($node->cond instanceof Instanceof_) {
$result = $this->refactorStmtAndInstanceof($class, $node, $node->cond);
if ($result !== null) {
$hasChanged = \true;
return $result;
}
}
return null;
});
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @return null|Stmt[]|int
*/
private function refactorStmtAndInstanceof(Class_ $class, If_ $if, Instanceof_ $instanceof)
{
// check local property only
if (!$instanceof->expr instanceof PropertyFetch || !$this->isName($instanceof->expr->var, 'this')) {
return null;
}
if (!$instanceof->class instanceof Name) {
return null;
}
$classType = $this->nodeTypeResolver->getType($instanceof->class);
$exprType = $this->nodeTypeResolver->getType($instanceof->expr);
$isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType)->yes();
if (!$isSameStaticTypeOrSubtype) {
return null;
}
if (!$this->isInPropertyPromotedParams($class, $instanceof->expr) && $this->isSkippedPropertyFetch($class, $instanceof->expr)) {
return null;
}
if ($if->cond !== $instanceof) {
return NodeTraverser::REMOVE_NODE;
}
if ($if->stmts === []) {
return NodeTraverser::REMOVE_NODE;
}
return $if->stmts;
}
/**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch
*/
private function isSkippedPropertyFetch(Class_ $class, $propertyFetch) : bool
{
$propertyName = $this->getName($propertyFetch->name);
if ($propertyName === null) {
return \true;
}
if ($this->constructorAssignDetector->isPropertyAssigned($class, $propertyName)) {
return \false;
}
$property = $class->getProperty($propertyName);
if (!$property instanceof Property) {
return \false;
}
return $property->type === null;
}
/**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch
*/
private function isInPropertyPromotedParams(Class_ $class, $propertyFetch) : bool
{
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($propertyFetch);
$params = $this->promotedPropertyResolver->resolveFromClass($class);
foreach ($params as $param) {
if ($this->nodeNameResolver->isName($param, $propertyName)) {
return \true;
}
}
return \false;
}
}

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'd3e37c45f144a5a100c94847cd1324f972f05089';
public const PACKAGE_VERSION = 'a394e5ea44ef09812ec1224aa0f402a483400d73';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-10 13:00:58';
public const RELEASE_DATE = '2023-06-10 18:48:27';
/**
* @var int
*/

2
vendor/autoload.php vendored

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

@ -1725,6 +1725,7 @@ return array(
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => $baseDir . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveUnusedNonEmptyArrayBeforeForeachRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
'Rector\\DeadCode\\Rector\\If_\\SimplifyIfElseWithSameContentRector' => $baseDir . '/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php',
'Rector\\DeadCode\\Rector\\If_\\UnwrapFutureCompatibleIfPhpVersionRector' => $baseDir . '/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php',

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

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit5d565228be4e06f3ac2724ff516ec321
class ComposerStaticInitc03e7b0bcdec35890eb8daf230e94f16
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1976,6 +1976,7 @@ class ComposerStaticInit5d565228be4e06f3ac2724ff516ec321
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveUnusedNonEmptyArrayBeforeForeachRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
'Rector\\DeadCode\\Rector\\If_\\SimplifyIfElseWithSameContentRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php',
'Rector\\DeadCode\\Rector\\If_\\UnwrapFutureCompatibleIfPhpVersionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php',
@ -3126,9 +3127,9 @@ class ComposerStaticInit5d565228be4e06f3ac2724ff516ec321
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit5d565228be4e06f3ac2724ff516ec321::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5d565228be4e06f3ac2724ff516ec321::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5d565228be4e06f3ac2724ff516ec321::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitc03e7b0bcdec35890eb8daf230e94f16::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc03e7b0bcdec35890eb8daf230e94f16::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc03e7b0bcdec35890eb8daf230e94f16::$classMap;
}, null, ClassLoader::class);
}