-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
BooleanInIfConditionRuleFixerRector.php
98 lines (94 loc) · 3.04 KB
/
BooleanInIfConditionRuleFixerRector.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
<?php
declare (strict_types=1);
namespace Rector\Strict\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Strict\NodeFactory\ExactCompareFactory;
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Fixer Rector for PHPStan rules:
* https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/BooleansInConditions/BooleanInIfConditionRule.php
* https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php
*
* @see \Rector\Tests\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector\BooleanInIfConditionRuleFixerRectorTest
*/
final class BooleanInIfConditionRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
/**
* @readonly
* @var \Rector\Strict\NodeFactory\ExactCompareFactory
*/
private $exactCompareFactory;
public function __construct(ExactCompareFactory $exactCompareFactory)
{
$this->exactCompareFactory = $exactCompareFactory;
}
public function getRuleDefinition() : RuleDefinition
{
$errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\BooleansInConditions\\BooleanInIfConditionRule');
return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class NegatedString
{
public function run(string $name)
{
if ($name) {
return 'name';
}
return 'no name';
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class NegatedString
{
public function run(string $name)
{
if ($name !== '') {
return 'name';
}
return 'no name';
}
}
CODE_SAMPLE
, [self::TREAT_AS_NON_EMPTY => \false])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?If_
{
$hasChanged = \false;
// 1. if
$ifCondExprType = $scope->getNativeType($node->cond);
$notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($ifCondExprType, $node->cond, $this->treatAsNonEmpty);
if ($notIdentical !== null) {
$node->cond = $notIdentical;
$hasChanged = \true;
}
// 2. elseifs
foreach ($node->elseifs as $elseif) {
$elseifCondExprType = $scope->getNativeType($elseif->cond);
$notIdentical = $this->exactCompareFactory->createNotIdenticalFalsyCompare($elseifCondExprType, $elseif->cond, $this->treatAsNonEmpty);
if (!$notIdentical instanceof Expr) {
continue;
}
$elseif->cond = $notIdentical;
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
}