Skip to content

Commit bea1d39

Browse files
committed
cleanup
1 parent bd169df commit bea1d39

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;
6+
7+
use PhpParser\Node\Expr\Assign;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\Variable;
10+
use PHPStan\Type\ObjectType;
11+
use PHPStan\Type\TypeCombinator;
12+
use Rector\NodeNameResolver\NodeNameResolver;
13+
use Rector\NodeTypeResolver\NodeTypeResolver;
14+
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;
15+
16+
final class NullableObjectAssignCollector
17+
{
18+
public function __construct(
19+
private readonly NodeNameResolver $nodeNameResolver,
20+
private readonly NodeTypeResolver $nodeTypeResolver,
21+
) {
22+
}
23+
24+
public function collect(Assign $assign): ?VariableNameToType
25+
{
26+
if (! $assign->expr instanceof MethodCall) {
27+
return null;
28+
}
29+
30+
if (! $assign->var instanceof Variable) {
31+
return null;
32+
}
33+
34+
$variableName = $this->nodeNameResolver->getName($assign->var);
35+
$variableType = $this->nodeTypeResolver->getType($assign);
36+
37+
$bareVariableType = TypeCombinator::removeNull($variableType);
38+
if (! $bareVariableType instanceof ObjectType) {
39+
return null;
40+
}
41+
42+
return new VariableNameToType($variableName, $bareVariableType->getClassName());
43+
}
44+
}

rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
use PhpParser\Node\Stmt\ClassMethod;
1616
use PhpParser\Node\Stmt\Expression;
1717
use PhpParser\Node\Stmt\Foreach_;
18-
use PHPStan\Type\ObjectType;
1918
use PHPStan\Type\Type;
2019
use PHPStan\Type\TypeCombinator;
2120
use PHPStan\Type\UnionType;
21+
use Rector\PHPUnit\CodeQuality\NodeAnalyser\NullableObjectAssignCollector;
2222
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;
2323
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2424
use 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 = [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\ValueObject;
6+
7+
final class VariableNameToTypeCollection
8+
{
9+
/**
10+
* @param VariableNameToType[] $variableNameToType
11+
*/
12+
public function __construct(
13+
private array $variableNameToType
14+
) {
15+
}
16+
}

0 commit comments

Comments
 (0)