44namespace Rector \TypeDeclaration \Rector \Class_ ;
55
66use PhpParser \Node ;
7- use PhpParser \Node \Expr \MethodCall ;
87use PhpParser \Node \Name \FullyQualified ;
98use PhpParser \Node \Stmt \Class_ ;
109use PhpParser \Node \Stmt \ClassMethod ;
1110use PHPStan \Type \IntersectionType ;
12- use PHPStan \Type \NeverType ;
1311use PHPStan \Type \ObjectType ;
1412use PHPStan \Type \Type ;
15- use Rector \NodeManipulator \ClassMethodPropertyFetchManipulator ;
1613use Rector \Rector \AbstractRector ;
14+ use Rector \TypeDeclaration \TypeInferer \PropertyTypeInferer \TrustedClassMethodPropertyTypeInferer ;
1715use Rector \ValueObject \MethodName ;
1816use Rector \ValueObject \PhpVersionFeature ;
1917use Rector \VersionBonding \Contract \MinPhpVersionInterface ;
@@ -26,9 +24,9 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
2624{
2725 /**
2826 * @readonly
29- * @var \Rector\NodeManipulator\ClassMethodPropertyFetchManipulator
27+ * @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer
3028 */
31- private $ classMethodPropertyFetchManipulator ;
29+ private $ trustedClassMethodPropertyTypeInferer ;
3230 /**
3331 * @var string
3432 */
@@ -37,9 +35,9 @@ final class TypedPropertyFromCreateMockAssignRector extends AbstractRector imple
3735 * @var string
3836 */
3937 private const MOCK_OBJECT_CLASS = 'PHPUnit \\Framework \\MockObject \\MockObject ' ;
40- public function __construct (ClassMethodPropertyFetchManipulator $ classMethodPropertyFetchManipulator )
38+ public function __construct (TrustedClassMethodPropertyTypeInferer $ trustedClassMethodPropertyTypeInferer )
4139 {
42- $ this ->classMethodPropertyFetchManipulator = $ classMethodPropertyFetchManipulator ;
40+ $ this ->trustedClassMethodPropertyTypeInferer = $ trustedClassMethodPropertyTypeInferer ;
4341 }
4442 public function getRuleDefinition () : RuleDefinition
4543 {
@@ -89,16 +87,12 @@ public function refactor(Node $node) : ?Node
8987 if ($ property ->type instanceof Node) {
9088 continue ;
9189 }
92- $ propertyName = $ this ->getName ($ property );
9390 $ setUpClassMethod = $ node ->getMethod (MethodName::SET_UP );
9491 if (!$ setUpClassMethod instanceof ClassMethod) {
9592 continue ;
9693 }
97- $ assignedType = $ this ->resolveSingleAssignedExprType ($ setUpClassMethod , $ propertyName );
98- if (!$ assignedType instanceof Type) {
99- continue ;
100- }
101- if (!$ this ->isMockObjectType ($ assignedType )) {
94+ $ type = $ this ->trustedClassMethodPropertyTypeInferer ->inferProperty ($ node , $ property , $ setUpClassMethod );
95+ if (!$ this ->isMockObjectType ($ type )) {
10296 continue ;
10397 }
10498 $ property ->type = new FullyQualified (self ::MOCK_OBJECT_CLASS );
@@ -130,18 +124,4 @@ private function isIntersectionWithMockObjectType(Type $type) : bool
130124 }
131125 return \in_array (self ::MOCK_OBJECT_CLASS , $ type ->getObjectClassNames ());
132126 }
133- private function resolveSingleAssignedExprType (ClassMethod $ setUpClassMethod , string $ propertyName ) : ?Type
134- {
135- $ assignedExprs = $ this ->classMethodPropertyFetchManipulator ->findAssignsToPropertyName ($ setUpClassMethod , $ propertyName );
136- if (\count ($ assignedExprs ) !== 1 ) {
137- return null ;
138- }
139- $ assignedExpr = $ assignedExprs [0 ];
140- $ exprType = $ this ->getType ($ assignedExpr );
141- // work around finalized class mock
142- if ($ exprType instanceof NeverType && $ assignedExpr instanceof MethodCall && $ this ->isName ($ assignedExpr ->name , 'createMock ' )) {
143- return new ObjectType (self ::MOCK_OBJECT_CLASS );
144- }
145- return $ exprType ;
146- }
147127}
0 commit comments