Skip to content

Commit 8929abc

Browse files
authored
allow coalesce not on full variable, but dim fetch in StrictArrayParamDimFetchRector (#7404)
1 parent 155b0f3 commit 8929abc

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

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 DirectCoalesce
6+
{
7+
public function run($productOptions)
8+
{
9+
return $productOptions['list'] ?? 'default';
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
18+
19+
final class DirectCoalesce
20+
{
21+
public function run(array $productOptions)
22+
{
23+
return $productOptions['list'] ?? 'default';
24+
}
25+
}
26+
27+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_coalesce.php.inc renamed to rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_coalesce_without_dim_fetch.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
44

5-
final class SkipCoalesce
5+
final class SkipCoalesceWithoutDimFetch
66
{
77
public function run($productOptions)
88
{

rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private function shouldStop(Node $node, Param $param, string $paramName): bool
236236
$nodeToCheck = $node->var;
237237
}
238238

239-
if ($this->isMethodCallOrArrayDimFetch($paramName, $nodeToCheck)) {
239+
if ($this->isMethodCall($paramName, $nodeToCheck)) {
240240
return true;
241241
}
242242

@@ -295,16 +295,12 @@ private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool
295295
return $node instanceof Array_ && $node->expr instanceof Variable && $this->isName($node->expr, $paramName);
296296
}
297297

298-
private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bool
298+
private function isMethodCall(string $paramName, ?Node $node): bool
299299
{
300300
if ($node instanceof MethodCall) {
301301
return $node->var instanceof Variable && $this->isName($node->var, $paramName);
302302
}
303303

304-
if ($node instanceof ArrayDimFetch) {
305-
return $node->var instanceof Variable && $this->isName($node->var, $paramName);
306-
}
307-
308304
return false;
309305
}
310306

0 commit comments

Comments
 (0)