Skip to content

Commit 7a73e88

Browse files
authored
[type-declarations] Open isset check in StrictArrayParamDimFetchRectorTest, move to last position in type-declaration level (#7388)
1 parent 385c8ee commit 7a73e88

File tree

10 files changed

+91
-34
lines changed

10 files changed

+91
-34
lines changed

rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/different_usage.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ final class DifferentUsage {
5151
}
5252
}
5353

54-
?>
54+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector/Fixture/with_array_filter_and_array_walk.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ final class WithArrayFilterAndArrayWalk
5151
}
5252
}
5353

54-
?>
54+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeFromTryCatchTypeRector/Fixture/try_catch_finally_same_type.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ final class TryCatchFinallySameType
3636
}
3737
}
3838

39-
?>
39+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/func_call_found.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ class FuncCallFound
2424
}
2525
}
2626

27-
?>
27+
?>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
4+
5+
final class CoverIssetWithDimFetch
6+
{
7+
public function run($param, $item): bool
8+
{
9+
if (! isset($param[$item])) {
10+
return false;
11+
}
12+
13+
if ($param[$item] === 100) {
14+
return true;
15+
}
16+
17+
return false;
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
26+
27+
final class CoverIssetWithDimFetch
28+
{
29+
public function run(array $param, $item): bool
30+
{
31+
if (! isset($param[$item])) {
32+
return false;
33+
}
34+
35+
if ($param[$item] === 100) {
36+
return true;
37+
}
38+
39+
return false;
40+
}
41+
}
42+
43+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
4+
5+
final class IncludeIssetIndex
6+
{
7+
public function run($param): void
8+
{
9+
echo isset($param['index']) ? $param['index'] : 'test';
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
18+
19+
final class IncludeIssetIndex
20+
{
21+
public function run(array $param): void
22+
{
23+
echo isset($param['index']) ? $param['index'] : 'test';
24+
}
25+
}
26+
27+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_isset_index.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PhpParser\Node\Expr\Closure;
1616
use PhpParser\Node\Expr\Empty_;
1717
use PhpParser\Node\Expr\FuncCall;
18-
use PhpParser\Node\Expr\Isset_;
1918
use PhpParser\Node\Expr\MethodCall;
2019
use PhpParser\Node\Expr\Variable;
2120
use PhpParser\Node\FunctionLike;
@@ -28,6 +27,7 @@
2827
use PhpParser\Node\Stmt\Function_;
2928
use PhpParser\NodeVisitor;
3029
use PHPStan\Type\ObjectType;
30+
use PHPStan\Type\Type;
3131
use PHPStan\Type\UnionType;
3232
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
3333
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
@@ -182,10 +182,7 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
182182
return NodeVisitor::STOP_TRAVERSAL;
183183
}
184184

185-
if ($variableType instanceof ObjectType && $this->typeComparator->isSubtype(
186-
$variableType,
187-
new ObjectType('ArrayAccess')
188-
)) {
185+
if ($this->isArrayAccess($variableType)) {
189186
$isParamAccessedArrayDimFetch = false;
190187
return NodeVisitor::STOP_TRAVERSAL;
191188
}
@@ -216,18 +213,8 @@ private function shouldStop(Node $node, Param $param, string $paramName): bool
216213
{
217214
$nodeToCheck = null;
218215

219-
if (! $param->default instanceof Expr) {
220-
if ($node instanceof Isset_) {
221-
foreach ($node->vars as $var) {
222-
if ($var instanceof ArrayDimFetch && $var->var instanceof Variable && $var->var->name === $paramName) {
223-
return true;
224-
}
225-
}
226-
}
227-
228-
if ($node instanceof Empty_ && $node->expr instanceof ArrayDimFetch && $node->expr->var instanceof Variable && $node->expr->var->name === $paramName) {
229-
return true;
230-
}
216+
if (! $param->default instanceof Expr && ($node instanceof Empty_ && $node->expr instanceof ArrayDimFetch && $node->expr->var instanceof Variable && $node->expr->var->name === $paramName)) {
217+
return true;
231218
}
232219

233220
if ($node instanceof FuncCall
@@ -320,4 +307,13 @@ private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bo
320307

321308
return false;
322309
}
310+
311+
private function isArrayAccess(Type $type): bool
312+
{
313+
if (! $type instanceof ObjectType) {
314+
return false;
315+
}
316+
317+
return $this->typeComparator->isSubtype($type, new ObjectType('ArrayAccess'));
318+
}
323319
}

rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
public function getRuleDefinition(): RuleDefinition
3535
{
36-
return new RuleDefinition('Add function/closure param array type, if dim fetch is inside', [
36+
return new RuleDefinition('Add closure and arrow function param array type, if dim fetch is used inside', [
3737
new CodeSample(
3838
<<<'CODE_SAMPLE'
3939
$array = [['name' => 'John']];

src/Config/Level/TypeDeclarationLevel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ final class TypeDeclarationLevel
137137
AddReturnTypeDeclarationBasedOnParentClassMethodRector::class,
138138
ReturnTypeFromStrictFluentReturnRector::class,
139139
ReturnNeverTypeRector::class,
140-
StrictArrayParamDimFetchRector::class,
141140
StrictStringParamConcatRector::class,
142141
TypedPropertyFromJMSSerializerAttributeTypeRector::class,
143142

143+
// array parameter from dim fetch assign inside
144+
StrictArrayParamDimFetchRector::class,
145+
144146
// possibly based on docblocks, but also helpful, intentionally last
145147
AddArrayFunctionClosureParamTypeRector::class,
146148
TypedPropertyFromDocblockSetUpDefinedRector::class,

0 commit comments

Comments
 (0)