Skip to content

Commit 15f1720

Browse files
committed
Updated Rector to commit da52751a6d28053f1dfa87abe1645f2cbdc262be
rectorphp/rector-src@da52751 [Php80] Add more exact Expr node that always bool for switch(true) usage on ChangeSwitchToMatchRector (#6965)
1 parent 2248513 commit 15f1720

4 files changed

Lines changed: 57 additions & 69 deletions

File tree

rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\Assign;
9-
use PhpParser\Node\Expr\CallLike;
109
use PhpParser\Node\Expr\Cast;
1110
use PhpParser\Node\Expr\Cast\Int_;
1211
use PhpParser\Node\Expr\Cast\String_;
13-
use PhpParser\Node\Expr\Instanceof_;
1412
use PhpParser\Node\Expr\Match_;
1513
use PhpParser\Node\Stmt\Expression;
1614
use PhpParser\Node\Stmt\Return_;
1715
use PhpParser\Node\Stmt\Switch_;
1816
use PHPStan\Type\ObjectType;
1917
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
18+
use Rector\NodeAnalyzer\ExprAnalyzer;
2019
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
2120
use Rector\Php80\NodeFactory\MatchFactory;
2221
use Rector\Php80\NodeResolver\SwitchExprsResolver;
@@ -49,12 +48,17 @@ final class ChangeSwitchToMatchRector extends AbstractRector implements MinPhpVe
4948
* @readonly
5049
*/
5150
private ValueResolver $valueResolver;
52-
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory, ValueResolver $valueResolver)
51+
/**
52+
* @readonly
53+
*/
54+
private ExprAnalyzer $exprAnalyzer;
55+
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory, ValueResolver $valueResolver, ExprAnalyzer $exprAnalyzer)
5356
{
5457
$this->switchExprsResolver = $switchExprsResolver;
5558
$this->matchSwitchAnalyzer = $matchSwitchAnalyzer;
5659
$this->matchFactory = $matchFactory;
5760
$this->valueResolver = $valueResolver;
61+
$this->exprAnalyzer = $exprAnalyzer;
5862
}
5963
public function getRuleDefinition() : RuleDefinition
6064
{
@@ -187,7 +191,9 @@ private function castMatchCond(Match_ $match) : void
187191
}
188192
private function mirrorDynamicBoolExpr(Match_ $match) : void
189193
{
190-
if ($this->valueResolver->isTrue($match->cond)) {
194+
// switch(true) already just use
195+
// switch(false) is dead code that can be on purpose
196+
if ($this->valueResolver->isTrueOrFalse($match->cond)) {
191197
return;
192198
}
193199
$isChanged = \false;
@@ -196,14 +202,11 @@ private function mirrorDynamicBoolExpr(Match_ $match) : void
196202
continue;
197203
}
198204
foreach ($arm->conds as $cond) {
199-
if ($cond instanceof Instanceof_ || $cond instanceof CallLike) {
200-
$type = $this->nodeTypeResolver->getNativeType($cond);
201-
if ($type->isBoolean()->yes()) {
202-
// dont' stop lookup for dynamic conditions
203-
// continue verify other condition, in case of mixed condition
204-
$isChanged = \true;
205-
continue;
206-
}
205+
if ($this->exprAnalyzer->isBoolExpr($cond) || $this->exprAnalyzer->isCallLikeReturnNativeBool($cond)) {
206+
// dont' stop lookup for dynamic conditions
207+
// continue verify other condition, in case of mixed condition
208+
$isChanged = \true;
209+
continue;
207210
}
208211
// return early here, as condition is mixed
209212
// we need another real use case for mixed conditions of dynamic + non-dynamic case expr

rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@
55

66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
8-
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
9-
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
10-
use PhpParser\Node\Expr\BinaryOp\Equal;
11-
use PhpParser\Node\Expr\BinaryOp\Greater;
12-
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
13-
use PhpParser\Node\Expr\BinaryOp\Identical;
14-
use PhpParser\Node\Expr\BinaryOp\NotEqual;
15-
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
16-
use PhpParser\Node\Expr\BinaryOp\Smaller;
17-
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
18-
use PhpParser\Node\Expr\BooleanNot;
198
use PhpParser\Node\Expr\Closure;
209
use PhpParser\Node\Expr\ConstFetch;
21-
use PhpParser\Node\Expr\Empty_;
2210
use PhpParser\Node\Expr\FuncCall;
23-
use PhpParser\Node\Expr\Isset_;
2411
use PhpParser\Node\Identifier;
2512
use PhpParser\Node\Name;
2613
use PhpParser\Node\Stmt\ClassMethod;
2714
use PhpParser\Node\Stmt\Function_;
2815
use PhpParser\Node\Stmt\Return_;
2916
use PHPStan\Analyser\Scope;
3017
use PHPStan\Reflection\ReflectionProvider;
18+
use Rector\NodeAnalyzer\ExprAnalyzer;
3119
use Rector\PhpParser\Node\BetterNodeFinder;
3220
use Rector\PhpParser\Node\Value\ValueResolver;
3321
use Rector\PHPStan\ScopeFetcher;
@@ -63,13 +51,18 @@ final class BoolReturnTypeFromBooleanStrictReturnsRector extends AbstractRector
6351
* @readonly
6452
*/
6553
private ReturnAnalyzer $returnAnalyzer;
66-
public function __construct(ReflectionProvider $reflectionProvider, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnAnalyzer $returnAnalyzer)
54+
/**
55+
* @readonly
56+
*/
57+
private ExprAnalyzer $exprAnalyzer;
58+
public function __construct(ReflectionProvider $reflectionProvider, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnAnalyzer $returnAnalyzer, ExprAnalyzer $exprAnalyzer)
6759
{
6860
$this->reflectionProvider = $reflectionProvider;
6961
$this->valueResolver = $valueResolver;
7062
$this->betterNodeFinder = $betterNodeFinder;
7163
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
7264
$this->returnAnalyzer = $returnAnalyzer;
65+
$this->exprAnalyzer = $exprAnalyzer;
7366
}
7467
public function getRuleDefinition() : RuleDefinition
7568
{
@@ -148,7 +141,7 @@ private function hasOnlyBoolScalarReturnExprs(array $returns) : bool
148141
if (!$return->expr instanceof Expr) {
149142
return \false;
150143
}
151-
if ($this->isBooleanOp($return->expr)) {
144+
if ($this->exprAnalyzer->isBoolExpr($return->expr)) {
152145
continue;
153146
}
154147
if ($return->expr instanceof FuncCall && $this->isNativeBooleanReturnTypeFuncCall($return->expr)) {
@@ -179,46 +172,6 @@ private function isNativeBooleanReturnTypeFuncCall(FuncCall $funcCall) : bool
179172
}
180173
return \true;
181174
}
182-
private function isBooleanOp(Expr $expr) : bool
183-
{
184-
if ($expr instanceof Smaller) {
185-
return \true;
186-
}
187-
if ($expr instanceof SmallerOrEqual) {
188-
return \true;
189-
}
190-
if ($expr instanceof Greater) {
191-
return \true;
192-
}
193-
if ($expr instanceof GreaterOrEqual) {
194-
return \true;
195-
}
196-
if ($expr instanceof BooleanOr) {
197-
return \true;
198-
}
199-
if ($expr instanceof BooleanAnd) {
200-
return \true;
201-
}
202-
if ($expr instanceof Identical) {
203-
return \true;
204-
}
205-
if ($expr instanceof NotIdentical) {
206-
return \true;
207-
}
208-
if ($expr instanceof Equal) {
209-
return \true;
210-
}
211-
if ($expr instanceof NotEqual) {
212-
return \true;
213-
}
214-
if ($expr instanceof Empty_) {
215-
return \true;
216-
}
217-
if ($expr instanceof Isset_) {
218-
return \true;
219-
}
220-
return $expr instanceof BooleanNot;
221-
}
222175
/**
223176
* @param Return_[] $returns
224177
*/

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'dd7a14e4bbffd44014b6341c9f0a22ec6bfe8b74';
22+
public const PACKAGE_VERSION = 'da52751a6d28053f1dfa87abe1645f2cbdc262be';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-06-08 13:28:34';
27+
public const RELEASE_DATE = '2025-06-09 17:02:53';
2828
/**
2929
* @var int
3030
*/

src/NodeAnalyzer/ExprAnalyzer.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Array_;
1010
use PhpParser\Node\Expr\BinaryOp;
11+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
12+
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
13+
use PhpParser\Node\Expr\BinaryOp\Equal;
14+
use PhpParser\Node\Expr\BinaryOp\Greater;
15+
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
16+
use PhpParser\Node\Expr\BinaryOp\Identical;
17+
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
18+
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
19+
use PhpParser\Node\Expr\BinaryOp\LogicalXor;
20+
use PhpParser\Node\Expr\BinaryOp\NotEqual;
21+
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
22+
use PhpParser\Node\Expr\BinaryOp\Smaller;
23+
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
1124
use PhpParser\Node\Expr\BitwiseNot;
1225
use PhpParser\Node\Expr\BooleanNot;
26+
use PhpParser\Node\Expr\CallLike;
1327
use PhpParser\Node\Expr\Cast;
28+
use PhpParser\Node\Expr\Cast\Bool_;
1429
use PhpParser\Node\Expr\ClassConstFetch;
1530
use PhpParser\Node\Expr\Clone_;
1631
use PhpParser\Node\Expr\ConstFetch;
@@ -20,6 +35,7 @@
2035
use PhpParser\Node\Expr\Exit_;
2136
use PhpParser\Node\Expr\Include_;
2237
use PhpParser\Node\Expr\Instanceof_;
38+
use PhpParser\Node\Expr\Isset_;
2339
use PhpParser\Node\Expr\Print_;
2440
use PhpParser\Node\Expr\Throw_;
2541
use PhpParser\Node\Expr\UnaryMinus;
@@ -40,6 +56,22 @@
4056
use Rector\NodeTypeResolver\Node\AttributeKey;
4157
final class ExprAnalyzer
4258
{
59+
public function isBoolExpr(Expr $expr) : bool
60+
{
61+
return $expr instanceof BooleanNot || $expr instanceof Empty_ || $expr instanceof Isset_ || $expr instanceof Instanceof_ || $expr instanceof Bool_ || $expr instanceof Equal || $expr instanceof NotEqual || $expr instanceof Identical || $expr instanceof NotIdentical || $expr instanceof Greater || $expr instanceof GreaterOrEqual || $expr instanceof Smaller || $expr instanceof SmallerOrEqual || $expr instanceof BooleanAnd || $expr instanceof BooleanOr || $expr instanceof LogicalAnd || $expr instanceof LogicalOr || $expr instanceof LogicalXor;
62+
}
63+
public function isCallLikeReturnNativeBool(Expr $expr) : bool
64+
{
65+
if (!$expr instanceof CallLike) {
66+
return \false;
67+
}
68+
$scope = $expr->getAttribute(AttributeKey::SCOPE);
69+
if (!$scope instanceof Scope) {
70+
return \false;
71+
}
72+
$nativeType = $scope->getNativeType($expr);
73+
return $nativeType->isBoolean()->yes();
74+
}
4375
/**
4476
* Verify that Expr has ->expr property that can be wrapped by parentheses
4577
*/

0 commit comments

Comments
 (0)