Updated Rector to commit 59722f15af9ff1205ff0fafb425995932af7261f

59722f15af [Php74] Do not add default value property that always filled by __construct() param (#187)
This commit is contained in:
Tomas Votruba 2021-06-09 12:37:40 +00:00
parent 6fa9c12855
commit c8118ff445
7 changed files with 90 additions and 21 deletions

View File

@ -4,8 +4,13 @@ declare (strict_types=1);
namespace Rector\Php74\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Reflection\ReflectionProvider;
@ -15,9 +20,12 @@ use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;
@ -64,13 +72,18 @@ final class TypedPropertyRector extends \Rector\Core\Rector\AbstractRector imple
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
public function __construct(\Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer $propertyTypeInferer, \Rector\VendorLocker\VendorLockResolver $vendorLockResolver, \Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer $doctrineTypeAnalyzer, \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover $varTagRemover, \PHPStan\Reflection\ReflectionProvider $reflectionProvider)
/**
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
public function __construct(\Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer $propertyTypeInferer, \Rector\VendorLocker\VendorLockResolver $vendorLockResolver, \Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer $doctrineTypeAnalyzer, \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover $varTagRemover, \PHPStan\Reflection\ReflectionProvider $reflectionProvider, \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer $propertyFetchAnalyzer)
{
$this->propertyTypeInferer = $propertyTypeInferer;
$this->vendorLockResolver = $vendorLockResolver;
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
$this->varTagRemover = $varTagRemover;
$this->reflectionProvider = $reflectionProvider;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
@ -203,6 +216,9 @@ CODE_SAMPLE
if ($onlyProperty->default !== null) {
return;
}
if ($this->propertyFetchAnalyzer->isFilledByConstructParam($property)) {
return;
}
$onlyProperty->default = $this->nodeFactory->createNull();
}
private function shouldSkipProperty(\PhpParser\Node\Stmt\Property $property) : bool

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '4f26e3643d638a69edaf1064cf59c13f373d557d';
public const PACKAGE_VERSION = '59722f15af9ff1205ff0fafb425995932af7261f';
/**
* @var string
*/
public const RELEASE_DATE = '2021-06-09 02:30:39';
public const RELEASE_DATE = '2021-06-09 14:26:25';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210609\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

View File

@ -10,9 +10,14 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class PropertyFetchAnalyzer
@ -25,10 +30,15 @@ final class PropertyFetchAnalyzer
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder)
/**
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
*/
private $nodeComparator;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeComparator = $nodeComparator;
}
public function isLocalPropertyFetch(\PhpParser\Node $node) : bool
{
@ -100,6 +110,29 @@ final class PropertyFetchAnalyzer
}
return $this->isLocalPropertyFetch($node->var);
}
public function isFilledByConstructParam(\PhpParser\Node\Stmt\Property $property) : bool
{
$class = $property->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
if (!$class instanceof \PhpParser\Node\Stmt\Class_) {
return \false;
}
$classMethod = $class->getMethod(\Rector\Core\ValueObject\MethodName::CONSTRUCT);
if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
return \false;
}
$params = $classMethod->params;
if ($params === []) {
return \false;
}
$stmts = (array) $classMethod->stmts;
if ($stmts === []) {
return \false;
}
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($property->props[0]->name);
$kindPropertyFetch = $property->isStatic() ? \PhpParser\Node\Expr\StaticPropertyFetch::class : \PhpParser\Node\Expr\PropertyFetch::class;
return $this->isParamFilledStmts($params, $stmts, $propertyName, $kindPropertyFetch);
}
/**
* @param string[] $propertyNames
*/
@ -111,4 +144,24 @@ final class PropertyFetchAnalyzer
/** @var PropertyFetch $node */
return $this->nodeNameResolver->isNames($node->name, $propertyNames);
}
/**
* @param Param[] $params
* @param Stmt[] $stmts
*/
private function isParamFilledStmts(array $params, array $stmts, string $propertyName, string $kindPropertyFetch) : bool
{
foreach ($params as $param) {
$paramVariable = $param->var;
$isAssignWithParamVarName = $this->betterNodeFinder->findFirst($stmts, function (\PhpParser\Node $node) use($propertyName, $paramVariable, $kindPropertyFetch) : bool {
if (!$node instanceof \PhpParser\Node\Expr\Assign) {
return \false;
}
return $kindPropertyFetch === \get_class($node->var) && $this->nodeNameResolver->isName($node->var, $propertyName) && $this->nodeComparator->areNodesEqual($node->expr, $paramVariable);
});
if ($isAssignWithParamVarName !== null) {
return \true;
}
}
return \false;
}
}

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3::getLoader();
return ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3
class ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097', '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\ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticIniteeed15c479df00a183271214d2bb1097::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3::$files;
$includeFiles = Composer\Autoload\ComposerStaticIniteeed15c479df00a183271214d2bb1097::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire37bc1e1a1076dfb4810ab74bc2f5ecb3($fileIdentifier, $file);
composerRequireeeed15c479df00a183271214d2bb1097($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire37bc1e1a1076dfb4810ab74bc2f5ecb3($fileIdentifier, $file)
function composerRequireeeed15c479df00a183271214d2bb1097($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3
class ComposerStaticIniteeed15c479df00a183271214d2bb1097
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3879,9 +3879,9 @@ class ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit37bc1e1a1076dfb4810ab74bc2f5ecb3::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticIniteeed15c479df00a183271214d2bb1097::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticIniteeed15c479df00a183271214d2bb1097::$prefixDirsPsr4;
$loader->classMap = ComposerStaticIniteeed15c479df00a183271214d2bb1097::$classMap;
}, null, ClassLoader::class);
}

View File

@ -21,8 +21,8 @@ if (!class_exists('SomeTestCase', false) && !interface_exists('SomeTestCase', fa
if (!class_exists('CheckoutEntityFactory', false) && !interface_exists('CheckoutEntityFactory', false) && !trait_exists('CheckoutEntityFactory', false)) {
spl_autoload_call('RectorPrefix20210609\CheckoutEntityFactory');
}
if (!class_exists('ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3', false) && !interface_exists('ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3', false) && !trait_exists('ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3', false)) {
spl_autoload_call('RectorPrefix20210609\ComposerAutoloaderInit37bc1e1a1076dfb4810ab74bc2f5ecb3');
if (!class_exists('ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097', false) && !interface_exists('ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097', false) && !trait_exists('ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097', false)) {
spl_autoload_call('RectorPrefix20210609\ComposerAutoloaderIniteeed15c479df00a183271214d2bb1097');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210609\Doctrine\Inflector\Inflector');
@ -3323,9 +3323,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210609\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire37bc1e1a1076dfb4810ab74bc2f5ecb3')) {
function composerRequire37bc1e1a1076dfb4810ab74bc2f5ecb3() {
return \RectorPrefix20210609\composerRequire37bc1e1a1076dfb4810ab74bc2f5ecb3(...func_get_args());
if (!function_exists('composerRequireeeed15c479df00a183271214d2bb1097')) {
function composerRequireeeed15c479df00a183271214d2bb1097() {
return \RectorPrefix20210609\composerRequireeeed15c479df00a183271214d2bb1097(...func_get_args());
}
}
if (!function_exists('parseArgs')) {