-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathDisallowedShortTernaryRuleFixerRector.php
106 lines (104 loc) · 3.5 KB
/
DisallowedShortTernaryRuleFixerRector.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
<?php
declare (strict_types=1);
namespace Rector\Strict\Rector\Ternary;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Analyser\Scope;
use Rector\PHPStan\ScopeFetcher;
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 rule:
* https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php
*
* @see \Rector\Tests\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector\DisallowedShortTernaryRuleFixerRectorTest
*/
final class DisallowedShortTernaryRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
{
/**
* @readonly
*/
private ExactCompareFactory $exactCompareFactory;
private bool $hasChanged = \false;
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\\DisallowedConstructs\\DisallowedShortTernaryRule');
return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class ShortTernaryArray
{
public function run(array $array)
{
return $array ?: 2;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class ShortTernaryArray
{
public function run(array $array)
{
return $array !== [] ? $array : 2;
}
}
CODE_SAMPLE
, [self::TREAT_AS_NON_EMPTY => \false])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Ternary::class];
}
/**
* @param Ternary $node
*/
public function refactor(Node $node) : ?Ternary
{
$this->hasChanged = \false;
// skip non-short ternary
if ($node->if instanceof Expr) {
return null;
}
$scope = ScopeFetcher::fetch($node);
// special case for reset() function
if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'reset')) {
$this->refactorResetFuncCall($node, $node->cond, $scope);
if (!$this->hasChanged) {
return null;
}
return $node;
}
$exprType = $scope->getNativeType($node->cond);
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $node->cond, $this->treatAsNonEmpty);
if (!$compareExpr instanceof Expr) {
return null;
}
$node->if = $node->cond;
$node->cond = $compareExpr;
return $node;
}
private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall, Scope $scope) : void
{
$ternary->if = $ternary->cond;
if ($resetFuncCall->isFirstClassCallable()) {
return;
}
$firstArgValue = $resetFuncCall->getArgs()[0]->value;
$firstArgType = $scope->getNativeType($firstArgValue);
$falsyCompareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($firstArgType, $firstArgValue, $this->treatAsNonEmpty);
if (!$falsyCompareExpr instanceof Expr) {
return;
}
$ternary->cond = $falsyCompareExpr;
$this->hasChanged = \true;
}
}