From a4e557403b747a8a2e6c588f921550bb468627a8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 17 Mar 2025 11:18:32 +0000 Subject: [PATCH] Updated Rector to commit 3fc24e1460dcf672b75bbe7d374a9bea4a403573 https://github.com/rectorphp/rector-src/commit/3fc24e1460dcf672b75bbe7d374a9bea4a403573 [naming] Skip mock object in RenamePropertyToMatchTypeRector to keep original "mock" suffix (#6786) --- .../RemoveTypedPropertyNonMockDocblockRector.php | 8 ++------ .../Class_/RenamePropertyToMatchTypeRector.php | 16 ++++++++++++++++ .../ReturnTypeFromMockObjectRector.php | 10 +++------- .../TypedPropertyFromCreateMockAssignRector.php | 6 +----- .../TypedPropertyFromStrictSetUpRector.php | 5 +++-- src/Application/VersionResolver.php | 4 ++-- src/Enum/ClassName.php | 4 ++++ 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php b/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php index 0d611959862..80e872c8548 100644 --- a/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php +++ b/rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php @@ -38,10 +38,6 @@ final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector impl * @readonly */ private PhpDocInfoFactory $phpDocInfoFactory; - /** - * @var string - */ - private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject'; public function __construct(VarTagRemover $varTagRemover, StaticTypeMapper $staticTypeMapper, PhpDocInfoFactory $phpDocInfoFactory) { $this->varTagRemover = $varTagRemover; @@ -97,7 +93,7 @@ CODE_SAMPLE if (!$property->type instanceof FullyQualified) { continue; } - if ($this->isObjectType($property->type, new ObjectType(self::MOCK_OBJECT_CLASS))) { + if ($this->isObjectType($property->type, new ObjectType(ClassName::MOCK_OBJECT))) { continue; } $propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); @@ -132,7 +128,7 @@ CODE_SAMPLE return \false; } foreach ($varTagType->getTypes() as $unionedType) { - if ($unionedType->isSuperTypeOf(new ObjectType(self::MOCK_OBJECT_CLASS))->yes()) { + if ($unionedType->isSuperTypeOf(new ObjectType(ClassName::MOCK_OBJECT))->yes()) { return \true; } } diff --git a/rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php b/rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php index cca8bcbf631..a94a632c683 100644 --- a/rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php +++ b/rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php @@ -4,10 +4,12 @@ declare (strict_types=1); namespace Rector\Naming\Rector\Class_; use PhpParser\Node; +use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; +use Rector\Enum\ClassName; use Rector\Naming\ExpectedNameResolver\MatchPropertyTypeExpectedNameResolver; use Rector\Naming\PropertyRenamer\MatchTypePropertyRenamer; use Rector\Naming\PropertyRenamer\PropertyPromotionRenamer; @@ -111,6 +113,9 @@ CODE_SAMPLE if (!$propertyRename instanceof PropertyRename) { continue; } + if ($this->skipMockObjectProperty($property)) { + continue; + } $renameProperty = $this->matchTypePropertyRenamer->rename($propertyRename); if (!$renameProperty instanceof Property) { continue; @@ -118,4 +123,15 @@ CODE_SAMPLE $this->hasChanged = \true; } } + /** + * Such properties can have "xMock" names that are not compatible with "MockObject" suffix + * They should be kept and handled by another naming rule that deals with mocks + */ + private function skipMockObjectProperty(Property $property) : bool + { + if (!$property->type instanceof Name) { + return \false; + } + return $this->isName($property->type, ClassName::MOCK_OBJECT); + } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php index 380a84cb076..107ab2e1c32 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php @@ -39,10 +39,6 @@ final class ReturnTypeFromMockObjectRector extends AbstractRector implements Min * @readonly */ private ReturnAnalyzer $returnAnalyzer; - /** - * @var string - */ - private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject'; public function __construct(BetterNodeFinder $betterNodeFinder, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnAnalyzer $returnAnalyzer) { $this->betterNodeFinder = $betterNodeFinder; @@ -107,7 +103,7 @@ CODE_SAMPLE if (!$this->isMockObjectType($returnType)) { return null; } - $node->returnType = new FullyQualified(self::MOCK_OBJECT_CLASS); + $node->returnType = new FullyQualified(ClassName::MOCK_OBJECT); return $node; } public function provideMinPhpVersion() : int @@ -122,11 +118,11 @@ CODE_SAMPLE if (\count($type->getTypes()) !== 2) { return \false; } - return \in_array(self::MOCK_OBJECT_CLASS, $type->getObjectClassNames()); + return \in_array(ClassName::MOCK_OBJECT, $type->getObjectClassNames()); } private function isMockObjectType(Type $returnType) : bool { - if ($returnType instanceof ObjectType && $returnType->isInstanceOf(self::MOCK_OBJECT_CLASS)->yes()) { + if ($returnType instanceof ObjectType && $returnType->isInstanceOf(ClassName::MOCK_OBJECT)->yes()) { return \true; } return $this->isIntersectionWithMockObjectType($returnType); diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php index fca0589286a..477657dcddf 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php @@ -35,10 +35,6 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple * @readonly */ private ConstructorAssignDetector $constructorAssignDetector; - /** - * @var string - */ - private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject'; public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInferer, StaticTypeMapper $staticTypeMapper, ConstructorAssignDetector $constructorAssignDetector) { $this->assignToPropertyTypeInferer = $assignToPropertyTypeInferer; @@ -105,7 +101,7 @@ CODE_SAMPLE if (!$propertyType instanceof Node) { continue; } - if (!$this->isObjectType($propertyType, new ObjectType(self::MOCK_OBJECT_CLASS))) { + if (!$this->isObjectType($propertyType, new ObjectType(ClassName::MOCK_OBJECT))) { continue; } if (!$this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) { diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php index 4cc8adbbf69..e123da34142 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php @@ -12,6 +12,7 @@ use PHPStan\Type\ObjectType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode; use Rector\Comments\NodeDocBlock\DocBlockUpdater; +use Rector\Enum\ClassName; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; @@ -110,11 +111,11 @@ CODE_SAMPLE if (!$propertyTypeNode instanceof Node) { continue; } - if ($propertyType instanceof ObjectType && $propertyType->getClassName() === 'PHPUnit\\Framework\\MockObject\\MockObject') { + if ($propertyType instanceof ObjectType && $propertyType->getClassName() === ClassName::MOCK_OBJECT) { $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); $varTag = $phpDocInfo->getVarTagValueNode(); $varType = $phpDocInfo->getVarType(); - if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== 'PHPUnit\\Framework\\MockObject\\MockObject') { + if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== ClassName::MOCK_OBJECT) { $varTag->type = new IntersectionTypeNode([new FullyQualifiedIdentifierTypeNode($propertyType->getClassName()), new FullyQualifiedIdentifierTypeNode($varType->getClassName())]); $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 76a9266bf5d..de7727fe15a 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'd1e896cd58204f76a1dea43a86e8f119a8c71c9c'; + public const PACKAGE_VERSION = '3fc24e1460dcf672b75bbe7d374a9bea4a403573'; /** * @api * @var string */ - public const RELEASE_DATE = '2025-03-16 00:36:29'; + public const RELEASE_DATE = '2025-03-17 12:15:54'; /** * @var int */ diff --git a/src/Enum/ClassName.php b/src/Enum/ClassName.php index 80b446eca14..97069a64bf4 100644 --- a/src/Enum/ClassName.php +++ b/src/Enum/ClassName.php @@ -9,4 +9,8 @@ final class ClassName * @var string */ public const TEST_CASE_CLASS = 'PHPUnit\\Framework\\TestCase'; + /** + * @var string + */ + public const MOCK_OBJECT = 'PHPUnit\\Framework\\MockObject\\MockObject'; }