Skip to content

Commit a4e5574

Browse files
committed
Updated Rector to commit 3fc24e1460dcf672b75bbe7d374a9bea4a403573
rectorphp/rector-src@3fc24e1 [naming] Skip mock object in RenamePropertyToMatchTypeRector to keep original "mock" suffix (#6786)
1 parent 32e3939 commit a4e5574

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

rules/DeadCode/Rector/ClassLike/RemoveTypedPropertyNonMockDocblockRector.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector impl
3838
* @readonly
3939
*/
4040
private PhpDocInfoFactory $phpDocInfoFactory;
41-
/**
42-
* @var string
43-
*/
44-
private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject';
4541
public function __construct(VarTagRemover $varTagRemover, StaticTypeMapper $staticTypeMapper, PhpDocInfoFactory $phpDocInfoFactory)
4642
{
4743
$this->varTagRemover = $varTagRemover;
@@ -97,7 +93,7 @@ public function refactor(Node $node) : ?Node
9793
if (!$property->type instanceof FullyQualified) {
9894
continue;
9995
}
100-
if ($this->isObjectType($property->type, new ObjectType(self::MOCK_OBJECT_CLASS))) {
96+
if ($this->isObjectType($property->type, new ObjectType(ClassName::MOCK_OBJECT))) {
10197
continue;
10298
}
10399
$propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
@@ -132,7 +128,7 @@ private function isVarTagUnionTypeMockObject(PhpDocInfo $phpDocInfo, Property $p
132128
return \false;
133129
}
134130
foreach ($varTagType->getTypes() as $unionedType) {
135-
if ($unionedType->isSuperTypeOf(new ObjectType(self::MOCK_OBJECT_CLASS))->yes()) {
131+
if ($unionedType->isSuperTypeOf(new ObjectType(ClassName::MOCK_OBJECT))->yes()) {
136132
return \true;
137133
}
138134
}

rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php

+16
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
namespace Rector\Naming\Rector\Class_;
55

66
use PhpParser\Node;
7+
use PhpParser\Node\Name;
78
use PhpParser\Node\Stmt\Class_;
89
use PhpParser\Node\Stmt\ClassLike;
910
use PhpParser\Node\Stmt\Interface_;
1011
use PhpParser\Node\Stmt\Property;
12+
use Rector\Enum\ClassName;
1113
use Rector\Naming\ExpectedNameResolver\MatchPropertyTypeExpectedNameResolver;
1214
use Rector\Naming\PropertyRenamer\MatchTypePropertyRenamer;
1315
use Rector\Naming\PropertyRenamer\PropertyPromotionRenamer;
@@ -111,11 +113,25 @@ private function refactorClassProperties(ClassLike $classLike) : void
111113
if (!$propertyRename instanceof PropertyRename) {
112114
continue;
113115
}
116+
if ($this->skipMockObjectProperty($property)) {
117+
continue;
118+
}
114119
$renameProperty = $this->matchTypePropertyRenamer->rename($propertyRename);
115120
if (!$renameProperty instanceof Property) {
116121
continue;
117122
}
118123
$this->hasChanged = \true;
119124
}
120125
}
126+
/**
127+
* Such properties can have "xMock" names that are not compatible with "MockObject" suffix
128+
* They should be kept and handled by another naming rule that deals with mocks
129+
*/
130+
private function skipMockObjectProperty(Property $property) : bool
131+
{
132+
if (!$property->type instanceof Name) {
133+
return \false;
134+
}
135+
return $this->isName($property->type, ClassName::MOCK_OBJECT);
136+
}
121137
}

rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromMockObjectRector.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ final class ReturnTypeFromMockObjectRector extends AbstractRector implements Min
3939
* @readonly
4040
*/
4141
private ReturnAnalyzer $returnAnalyzer;
42-
/**
43-
* @var string
44-
*/
45-
private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject';
4642
public function __construct(BetterNodeFinder $betterNodeFinder, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnAnalyzer $returnAnalyzer)
4743
{
4844
$this->betterNodeFinder = $betterNodeFinder;
@@ -107,7 +103,7 @@ public function refactor(Node $node) : ?Node
107103
if (!$this->isMockObjectType($returnType)) {
108104
return null;
109105
}
110-
$node->returnType = new FullyQualified(self::MOCK_OBJECT_CLASS);
106+
$node->returnType = new FullyQualified(ClassName::MOCK_OBJECT);
111107
return $node;
112108
}
113109
public function provideMinPhpVersion() : int
@@ -122,11 +118,11 @@ private function isIntersectionWithMockObjectType(Type $type) : bool
122118
if (\count($type->getTypes()) !== 2) {
123119
return \false;
124120
}
125-
return \in_array(self::MOCK_OBJECT_CLASS, $type->getObjectClassNames());
121+
return \in_array(ClassName::MOCK_OBJECT, $type->getObjectClassNames());
126122
}
127123
private function isMockObjectType(Type $returnType) : bool
128124
{
129-
if ($returnType instanceof ObjectType && $returnType->isInstanceOf(self::MOCK_OBJECT_CLASS)->yes()) {
125+
if ($returnType instanceof ObjectType && $returnType->isInstanceOf(ClassName::MOCK_OBJECT)->yes()) {
130126
return \true;
131127
}
132128
return $this->isIntersectionWithMockObjectType($returnType);

rules/TypeDeclaration/Rector/Class_/TypedPropertyFromCreateMockAssignRector.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
3535
* @readonly
3636
*/
3737
private ConstructorAssignDetector $constructorAssignDetector;
38-
/**
39-
* @var string
40-
*/
41-
private const MOCK_OBJECT_CLASS = 'PHPUnit\\Framework\\MockObject\\MockObject';
4238
public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInferer, StaticTypeMapper $staticTypeMapper, ConstructorAssignDetector $constructorAssignDetector)
4339
{
4440
$this->assignToPropertyTypeInferer = $assignToPropertyTypeInferer;
@@ -105,7 +101,7 @@ public function refactor(Node $node) : ?Node
105101
if (!$propertyType instanceof Node) {
106102
continue;
107103
}
108-
if (!$this->isObjectType($propertyType, new ObjectType(self::MOCK_OBJECT_CLASS))) {
104+
if (!$this->isObjectType($propertyType, new ObjectType(ClassName::MOCK_OBJECT))) {
109105
continue;
110106
}
111107
if (!$this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {

rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1313
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
1414
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
15+
use Rector\Enum\ClassName;
1516
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1617
use Rector\Rector\AbstractRector;
1718
use Rector\StaticTypeMapper\StaticTypeMapper;
@@ -110,11 +111,11 @@ public function refactor(Node $node) : ?Node
110111
if (!$propertyTypeNode instanceof Node) {
111112
continue;
112113
}
113-
if ($propertyType instanceof ObjectType && $propertyType->getClassName() === 'PHPUnit\\Framework\\MockObject\\MockObject') {
114+
if ($propertyType instanceof ObjectType && $propertyType->getClassName() === ClassName::MOCK_OBJECT) {
114115
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
115116
$varTag = $phpDocInfo->getVarTagValueNode();
116117
$varType = $phpDocInfo->getVarType();
117-
if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== 'PHPUnit\\Framework\\MockObject\\MockObject') {
118+
if ($varTag instanceof VarTagValueNode && $varType instanceof ObjectType && $varType->getClassName() !== ClassName::MOCK_OBJECT) {
118119
$varTag->type = new IntersectionTypeNode([new FullyQualifiedIdentifierTypeNode($propertyType->getClassName()), new FullyQualifiedIdentifierTypeNode($varType->getClassName())]);
119120
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
120121
}

src/Application/VersionResolver.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'd1e896cd58204f76a1dea43a86e8f119a8c71c9c';
22+
public const PACKAGE_VERSION = '3fc24e1460dcf672b75bbe7d374a9bea4a403573';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-03-16 00:36:29';
27+
public const RELEASE_DATE = '2025-03-17 12:15:54';
2828
/**
2929
* @var int
3030
*/

src/Enum/ClassName.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ final class ClassName
99
* @var string
1010
*/
1111
public const TEST_CASE_CLASS = 'PHPUnit\\Framework\\TestCase';
12+
/**
13+
* @var string
14+
*/
15+
public const MOCK_OBJECT = 'PHPUnit\\Framework\\MockObject\\MockObject';
1216
}

0 commit comments

Comments
 (0)