-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathDisallowedEmptyRuleFixerRector.php
137 lines (135 loc) · 4.86 KB
/
DisallowedEmptyRuleFixerRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare (strict_types=1);
namespace Rector\Strict\Rector\Empty_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Isset_;
use PHPStan\Analyser\Scope;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PHPStan\ScopeFetcher;
use Rector\Strict\NodeAnalyzer\UnitializedPropertyAnalyzer;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\DisallowedEmptyRuleFixerRectorTest
*/
final class DisallowedEmptyRuleFixerRector extends AbstractFalsyScalarRuleFixerRector implements ConfigurableRectorInterface
{
/**
* @readonly
*/
private ExactCompareFactory $exactCompareFactory;
/**
* @readonly
*/
private ExprAnalyzer $exprAnalyzer;
/**
* @readonly
*/
private UnitializedPropertyAnalyzer $unitializedPropertyAnalyzer;
public function __construct(ExactCompareFactory $exactCompareFactory, ExprAnalyzer $exprAnalyzer, UnitializedPropertyAnalyzer $unitializedPropertyAnalyzer)
{
$this->exactCompareFactory = $exactCompareFactory;
$this->exprAnalyzer = $exprAnalyzer;
$this->unitializedPropertyAnalyzer = $unitializedPropertyAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
$errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\DisallowedConstructs\\DisallowedEmptyRule');
return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class SomeEmptyArray
{
public function run(array $items)
{
return empty($items);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeEmptyArray
{
public function run(array $items)
{
return $items === [];
}
}
CODE_SAMPLE
, [self::TREAT_AS_NON_EMPTY => \false])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Empty_::class, BooleanNot::class];
}
/**
* @param Empty_|BooleanNot $node
*/
public function refactor(Node $node) : ?\PhpParser\Node\Expr
{
$scope = ScopeFetcher::fetch($node);
if ($node instanceof BooleanNot) {
return $this->refactorBooleanNot($node, $scope);
}
if ($node->expr instanceof ArrayDimFetch) {
return null;
}
return $this->refactorEmpty($node, $scope, $this->treatAsNonEmpty);
}
private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope) : ?\PhpParser\Node\Expr
{
if (!$booleanNot->expr instanceof Empty_) {
return null;
}
$empty = $booleanNot->expr;
if ($empty->expr instanceof ArrayDimFetch) {
return $this->createDimFetchBooleanAnd($empty->expr);
}
if ($this->exprAnalyzer->isNonTypedFromParam($empty->expr)) {
return null;
}
$emptyExprType = $scope->getNativeType($empty->expr);
$result = $this->exactCompareFactory->createNotIdenticalFalsyCompare($emptyExprType, $empty->expr, $this->treatAsNonEmpty);
if (!$result instanceof Expr) {
return null;
}
if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) {
return new BooleanAnd(new Isset_([$empty->expr]), $result);
}
return $result;
}
private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpty) : ?\PhpParser\Node\Expr
{
if ($this->exprAnalyzer->isNonTypedFromParam($empty->expr)) {
return null;
}
$exprType = $scope->getNativeType($empty->expr);
$result = $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty);
if (!$result instanceof Expr) {
return null;
}
if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) {
return new BooleanOr(new BooleanNot(new Isset_([$empty->expr])), $result);
}
return $result;
}
private function createDimFetchBooleanAnd(ArrayDimFetch $arrayDimFetch) : ?BooleanAnd
{
$exprType = $this->nodeTypeResolver->getNativeType($arrayDimFetch);
$isset = new Isset_([$arrayDimFetch]);
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $arrayDimFetch, \false);
if (!$compareExpr instanceof Expr) {
return null;
}
return new BooleanAnd($isset, $compareExpr);
}
}