Updated Rector to commit 2970fa114ef632768f4704c8eefd2f5d0310703c

2970fa114e [TypeDeclaration] Skip in conditional on AddParamTypeFromPropertyTypeRector (#4779)
This commit is contained in:
Tomas Votruba 2023-08-13 07:18:25 +00:00
parent 68e8b4f8d1
commit 5034b0a7f0
8 changed files with 101 additions and 64 deletions

View File

@ -0,0 +1,68 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Guard;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeTraverser;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final class ParamTypeAddGuard
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(NodeNameResolver $nodeNameResolver, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, BetterNodeFinder $betterNodeFinder)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->betterNodeFinder = $betterNodeFinder;
}
public function isLegal(Param $param, ClassMethod $classMethod) : bool
{
$paramName = $this->nodeNameResolver->getName($param->var);
if ($paramName === null) {
return \false;
}
$isLegal = \true;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $subNode) use(&$isLegal, $paramName) : ?int {
if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->nodeNameResolver->isName($subNode->var, $paramName)) {
$isLegal = \false;
return NodeTraverser::STOP_TRAVERSAL;
}
if ($subNode instanceof If_ && (bool) $this->betterNodeFinder->findFirst($subNode->cond, function (Node $node) use($paramName) : bool {
return $node instanceof Variable && $this->nodeNameResolver->isName($node, $paramName);
})) {
$isLegal = \false;
return NodeTraverser::STOP_TRAVERSAL;
}
if ($subNode instanceof Ternary && (bool) $this->betterNodeFinder->findFirst($subNode, function (Node $node) use($paramName) : bool {
return $node instanceof Variable && $this->nodeNameResolver->isName($node, $paramName);
})) {
$isLegal = \false;
return NodeTraverser::STOP_TRAVERSAL;
}
return null;
});
return $isLegal;
}
}

View File

@ -5,7 +5,6 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
@ -17,6 +16,7 @@ use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\Guard\ParamTypeAddGuard;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -46,16 +46,22 @@ final class AddParamTypeFromPropertyTypeRector extends AbstractRector implements
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
*/
private $parentClassMethodTypeOverrideGuard;
/**
* @readonly
* @var \Rector\TypeDeclaration\Guard\ParamTypeAddGuard
*/
private $paramTypeAddGuard;
/**
* @var string
*/
private const ERROR_MESSAGE = 'Adds param type declaration based on property type the value is assigned to PHPUnit provider return type declaration';
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, TypeFactory $typeFactory, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard)
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, TypeFactory $typeFactory, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, ParamTypeAddGuard $paramTypeAddGuard)
{
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->typeFactory = $typeFactory;
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
$this->paramTypeAddGuard = $paramTypeAddGuard;
}
public function getRuleDefinition() : RuleDefinition
{
@ -101,20 +107,10 @@ CODE_SAMPLE
if ($param->type instanceof Node) {
continue;
}
$paramName = $this->getName($param);
// has param override? skip it
$hasParamOverride = (bool) $this->betterNodeFinder->findFirst($node, function (Node $node) use($paramName) : bool {
if (!$node instanceof Assign) {
return \false;
}
if (!$node->var instanceof Variable) {
return \false;
}
return $this->isName($node->var, $paramName);
});
if ($hasParamOverride) {
if (!$this->paramTypeAddGuard->isLegal($param, $node)) {
continue;
}
$paramName = $this->getName($param);
$propertyStaticTypes = $this->resolvePropertyStaticTypesByParamName($node, $paramName);
$possibleParamType = $this->typeFactory->createMixedPassedOrUnionType($propertyStaticTypes);
$paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($possibleParamType, TypeKind::PARAM);

View File

@ -5,24 +5,20 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\UnionType;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\Guard\ParamTypeAddGuard;
use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -42,10 +38,16 @@ final class ParamTypeByMethodCallTypeRector extends AbstractScopeAwareRector
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
*/
private $parentClassMethodTypeOverrideGuard;
public function __construct(CallerParamMatcher $callerParamMatcher, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard)
/**
* @readonly
* @var \Rector\TypeDeclaration\Guard\ParamTypeAddGuard
*/
private $paramTypeAddGuard;
public function __construct(CallerParamMatcher $callerParamMatcher, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, ParamTypeAddGuard $paramTypeAddGuard)
{
$this->callerParamMatcher = $callerParamMatcher;
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
$this->paramTypeAddGuard = $paramTypeAddGuard;
}
public function getRuleDefinition() : RuleDefinition
{
@ -152,40 +154,9 @@ CODE_SAMPLE
});
$decoratedParam->type = $newParamType;
}
/**
* Should skip param because one of them is conditional types?
*/
private function isParamConditioned(Param $param, ClassMethod $classMethod) : bool
{
$paramName = $this->nodeNameResolver->getName($param->var);
if ($paramName === null) {
return \false;
}
$isParamConditioned = \false;
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $subNode) use(&$isParamConditioned, $paramName) : ?int {
if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->isName($subNode->var, $paramName)) {
$isParamConditioned = \true;
return NodeTraverser::STOP_TRAVERSAL;
}
if ($subNode instanceof If_ && (bool) $this->betterNodeFinder->findFirst($subNode->cond, function (Node $node) use($paramName) : bool {
return $node instanceof Variable && $this->isName($node, $paramName);
})) {
$isParamConditioned = \true;
return NodeTraverser::STOP_TRAVERSAL;
}
if ($subNode instanceof Ternary && (bool) $this->betterNodeFinder->findFirst($subNode, function (Node $node) use($paramName) : bool {
return $node instanceof Variable && $this->isName($node, $paramName);
})) {
$isParamConditioned = \true;
return NodeTraverser::STOP_TRAVERSAL;
}
return null;
});
return $isParamConditioned;
}
private function shouldSkipParam(Param $param, ClassMethod $classMethod) : bool
{
if ($this->isParamConditioned($param, $classMethod)) {
if (!$this->paramTypeAddGuard->isLegal($param, $classMethod)) {
return \true;
}
if ($param->variadic) {

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '697ce5de0230bd4aeddd17288fca625bcfebfa47';
public const PACKAGE_VERSION = '2970fa114ef632768f4704c8eefd2f5d0310703c';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-08-13 00:03:17';
public const RELEASE_DATE = '2023-08-13 07:15:02';
/**
* @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 ComposerAutoloaderInit2661f7532710fa1a125636b14e6b741d::getLoader();
return ComposerAutoloaderInit22cf5cfc1cfbfa321d3634568d4c2a88::getLoader();

View File

@ -2588,6 +2588,7 @@ return array(
'Rector\\TypeDeclaration\\AlreadyAssignDetector\\PropertyDefaultAssignDetector' => $baseDir . '/rules/TypeDeclaration/AlreadyAssignDetector/PropertyDefaultAssignDetector.php',
'Rector\\TypeDeclaration\\Contract\\PHPStan\\TypeWithClassTypeSpecifierInterface' => $baseDir . '/rules/TypeDeclaration/Contract/PHPStan/TypeWithClassTypeSpecifierInterface.php',
'Rector\\TypeDeclaration\\FunctionLikeReturnTypeResolver' => $baseDir . '/rules/TypeDeclaration/FunctionLikeReturnTypeResolver.php',
'Rector\\TypeDeclaration\\Guard\\ParamTypeAddGuard' => $baseDir . '/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php',
'Rector\\TypeDeclaration\\Guard\\PropertyTypeOverrideGuard' => $baseDir . '/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php',
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => $baseDir . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit2661f7532710fa1a125636b14e6b741d
class ComposerAutoloaderInit22cf5cfc1cfbfa321d3634568d4c2a88
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit2661f7532710fa1a125636b14e6b741d
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit2661f7532710fa1a125636b14e6b741d', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit22cf5cfc1cfbfa321d3634568d4c2a88', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit2661f7532710fa1a125636b14e6b741d', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit22cf5cfc1cfbfa321d3634568d4c2a88', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit2661f7532710fa1a125636b14e6b741d::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit2661f7532710fa1a125636b14e6b741d::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88::$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 ComposerStaticInit2661f7532710fa1a125636b14e6b741d
class ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2841,6 +2841,7 @@ class ComposerStaticInit2661f7532710fa1a125636b14e6b741d
'Rector\\TypeDeclaration\\AlreadyAssignDetector\\PropertyDefaultAssignDetector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/AlreadyAssignDetector/PropertyDefaultAssignDetector.php',
'Rector\\TypeDeclaration\\Contract\\PHPStan\\TypeWithClassTypeSpecifierInterface' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Contract/PHPStan/TypeWithClassTypeSpecifierInterface.php',
'Rector\\TypeDeclaration\\FunctionLikeReturnTypeResolver' => __DIR__ . '/../..' . '/rules/TypeDeclaration/FunctionLikeReturnTypeResolver.php',
'Rector\\TypeDeclaration\\Guard\\ParamTypeAddGuard' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Guard/ParamTypeAddGuard.php',
'Rector\\TypeDeclaration\\Guard\\PropertyTypeOverrideGuard' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Guard/PropertyTypeOverrideGuard.php',
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',
@ -2959,9 +2960,9 @@ class ComposerStaticInit2661f7532710fa1a125636b14e6b741d
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit2661f7532710fa1a125636b14e6b741d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2661f7532710fa1a125636b14e6b741d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2661f7532710fa1a125636b14e6b741d::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit22cf5cfc1cfbfa321d3634568d4c2a88::$classMap;
}, null, ClassLoader::class);
}