mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 12:20:52 +00:00
Updated Rector to commit 9c64c2a7ef9aa19592349b3928173adc9b9eb394
9c64c2a7ef
[TypeDeclaration] Fix TypedPropertyFromStrictConstructorRector to avoid conflict with default type (#3269)
This commit is contained in:
parent
6b7133ac92
commit
29be2b6a0f
|
@ -4,13 +4,15 @@ declare (strict_types=1);
|
|||
namespace Rector\TypeDeclaration\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\PropertyProperty;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
||||
use Rector\Core\Php\PhpVersionProvider;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
|
@ -47,23 +49,17 @@ final class TypedPropertyFromStrictConstructorRector extends AbstractRector impl
|
|||
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
|
||||
*/
|
||||
private $constructorAssignDetector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Php\PhpVersionProvider
|
||||
*/
|
||||
private $phpVersionProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard
|
||||
*/
|
||||
private $propertyTypeOverrideGuard;
|
||||
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, PhpDocTypeChanger $phpDocTypeChanger, ConstructorAssignDetector $constructorAssignDetector, PhpVersionProvider $phpVersionProvider, PropertyTypeOverrideGuard $propertyTypeOverrideGuard)
|
||||
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, PhpDocTypeChanger $phpDocTypeChanger, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard)
|
||||
{
|
||||
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
|
||||
$this->varTagRemover = $varTagRemover;
|
||||
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
||||
$this->constructorAssignDetector = $constructorAssignDetector;
|
||||
$this->phpVersionProvider = $phpVersionProvider;
|
||||
$this->propertyTypeOverrideGuard = $propertyTypeOverrideGuard;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
|
@ -104,25 +100,22 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$hasChanged = \false;
|
||||
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
|
||||
if (!$constructClassMethod instanceof ClassMethod) {
|
||||
return null;
|
||||
}
|
||||
$hasChanged = \false;
|
||||
foreach ($node->getProperties() as $property) {
|
||||
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($property, $constructClassMethod);
|
||||
if ($propertyType instanceof MixedType) {
|
||||
continue;
|
||||
}
|
||||
if ($propertyType instanceof ObjectType && $propertyType->isInstanceOf('Doctrine\\Common\\Collections\\Collection')->yes()) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->propertyTypeOverrideGuard->isLegal($property)) {
|
||||
continue;
|
||||
}
|
||||
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($property, $constructClassMethod);
|
||||
if ($this->shouldSkipPropertyType($propertyType)) {
|
||||
continue;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
||||
// public property can be anything
|
||||
if ($this->isVarDocPreffered($property)) {
|
||||
if ($property->isPublic()) {
|
||||
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType);
|
||||
$hasChanged = \true;
|
||||
continue;
|
||||
|
@ -131,14 +124,18 @@ CODE_SAMPLE
|
|||
if (!$propertyTypeNode instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
if (!$property->isPublic()) {
|
||||
$property->type = $propertyTypeNode;
|
||||
}
|
||||
$propertyProperty = $property->props[0];
|
||||
$propertyName = $this->nodeNameResolver->getName($property);
|
||||
if ($this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
|
||||
$property->props[0]->default = null;
|
||||
$propertyProperty->default = null;
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($this->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
|
||||
continue;
|
||||
}
|
||||
$property->type = $propertyTypeNode;
|
||||
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $property);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
|
@ -149,11 +146,28 @@ CODE_SAMPLE
|
|||
{
|
||||
return PhpVersionFeature::TYPED_PROPERTIES;
|
||||
}
|
||||
private function isVarDocPreffered(Property $property) : bool
|
||||
private function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType) : bool
|
||||
{
|
||||
if ($property->isPublic()) {
|
||||
if (!$propertyProperty->default instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
// the defaults can be in conflict
|
||||
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
|
||||
// type is not matching, skip it
|
||||
return !$defaultType->isSuperTypeOf($propertyType)->yes();
|
||||
}
|
||||
private function isDoctrineCollectionType(Type $type) : bool
|
||||
{
|
||||
if (!$type instanceof ObjectType) {
|
||||
return \false;
|
||||
}
|
||||
return $type->isInstanceOf('Doctrine\\Common\\Collections\\Collection')->yes();
|
||||
}
|
||||
private function shouldSkipPropertyType(Type $propertyType) : bool
|
||||
{
|
||||
if ($propertyType instanceof MixedType) {
|
||||
return \true;
|
||||
}
|
||||
return !$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES);
|
||||
return $this->isDoctrineCollectionType($propertyType);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'd43f7498b821cf58437f622bce3b3aae290fa89e';
|
||||
public const PACKAGE_VERSION = '9c64c2a7ef9aa19592349b3928173adc9b9eb394';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-01-05 03:38:37';
|
||||
public const RELEASE_DATE = '2023-01-08 17:09:26';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitae2e5b9026f29723b3d16d0bfb6cde2d::getLoader();
|
||||
return ComposerAutoloaderInit077e2f6e03226968aabf48f42e9fa232::getLoader();
|
||||
|
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitae2e5b9026f29723b3d16d0bfb6cde2d
|
||||
class ComposerAutoloaderInit077e2f6e03226968aabf48f42e9fa232
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInitae2e5b9026f29723b3d16d0bfb6cde2d
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitae2e5b9026f29723b3d16d0bfb6cde2d', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit077e2f6e03226968aabf48f42e9fa232', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitae2e5b9026f29723b3d16d0bfb6cde2d', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit077e2f6e03226968aabf48f42e9fa232', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit077e2f6e03226968aabf48f42e9fa232::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit077e2f6e03226968aabf48f42e9fa232::$files;
|
||||
$requireFile = static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d
|
||||
class ComposerStaticInit077e2f6e03226968aabf48f42e9fa232
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3063,9 +3063,9 @@ class ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitae2e5b9026f29723b3d16d0bfb6cde2d::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit077e2f6e03226968aabf48f42e9fa232::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit077e2f6e03226968aabf48f42e9fa232::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit077e2f6e03226968aabf48f42e9fa232::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user