1515use PhpParser \Node \Stmt \ClassMethod ;
1616use PhpParser \Node \Stmt \Expression ;
1717use PhpParser \Node \Stmt \Foreach_ ;
18- use PHPStan \Type \ObjectType ;
1918use PHPStan \Type \Type ;
2019use PHPStan \Type \TypeCombinator ;
2120use PHPStan \Type \UnionType ;
21+ use Rector \PHPUnit \CodeQuality \NodeAnalyser \NullableObjectAssignCollector ;
2222use Rector \PHPUnit \CodeQuality \ValueObject \VariableNameToType ;
2323use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
2424use Rector \Rector \AbstractRector ;
@@ -32,6 +32,7 @@ final class AddInstanceofAssertForNullableInstanceRector extends AbstractRector
3232{
3333 public function __construct (
3434 private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
35+ private readonly NullableObjectAssignCollector $ nullableObjectAssignCollector ,
3536 ) {
3637 }
3738
@@ -121,7 +122,7 @@ public function refactor(Node $node): ?Node
121122
122123 foreach ($ node ->stmts as $ key => $ stmt ) {
123124 if ($ stmt instanceof Expression && $ stmt ->expr instanceof Assign) {
124- $ variableNameToType = $ this ->collectVariableFromAssign ($ stmt ->expr );
125+ $ variableNameToType = $ this ->nullableObjectAssignCollector -> collect ($ stmt ->expr );
125126 if ($ variableNameToType instanceof VariableNameToType) {
126127 $ nullableVariableNamesToTypes [] = $ variableNameToType ;
127128 continue ;
@@ -172,32 +173,6 @@ private function isNullableType(Type $type): bool
172173 return count ($ type ->getTypes ()) === 2 ;
173174 }
174175
175- private function collectVariableFromAssign (Assign $ assign ): ?VariableNameToType
176- {
177- if (! $ assign ->expr instanceof MethodCall) {
178- return null ;
179- }
180-
181- if (! $ assign ->var instanceof Variable) {
182- return null ;
183- }
184-
185- $ variableType = $ this ->getType ($ assign );
186- if (! $ this ->isNullableType ($ variableType )) {
187- return null ;
188- }
189-
190- // $fullType = TypeCombinator::removeNull($variableType);
191- $ variableName = $ this ->getName ($ assign ->var );
192-
193- $ bareVariableType = TypeCombinator::removeNull ($ variableType );
194- if (! $ bareVariableType instanceof ObjectType) {
195- return null ;
196- }
197-
198- return new VariableNameToType ($ variableName , $ bareVariableType ->getClassName ());
199- }
200-
201176 private function createAssertInstanceof (VariableNameToType $ variableNameToType ): Expression
202177 {
203178 $ args = [
0 commit comments