Skip to content

Commit d0b22d4

Browse files
authored
[CodeQuality] Skip next inside array_filter() on SimplifyEmptyArrayCheckRector (#7154)
1 parent fddd8c4 commit d0b22d4

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

rules-tests/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector/Fixture/fixture.php.inc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ final class Fixture
1313
{
1414
return empty($values) && is_array($values);
1515
}
16-
17-
public function functionCallInsideEmpty($values): bool
18-
{
19-
return is_array($values) && empty(array_filter($values));
20-
}
2116
}
2217

2318
?>
@@ -37,11 +32,6 @@ final class Fixture
3732
{
3833
return $values === [];
3934
}
40-
41-
public function functionCallInsideEmpty($values): bool
42-
{
43-
return array_filter($values) === [];
44-
}
4535
}
4636

4737
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector\Fixture;
4+
5+
final class SkipRightInsideArrayFilter
6+
{
7+
public function example(mixed $value): bool {
8+
if (is_array($value) && empty(array_filter($value))) {
9+
return true;
10+
}
11+
12+
return false;
13+
}
14+
}

rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\BinaryOp\Identical;
1111
use PhpParser\Node\Expr\Empty_;
1212
use PhpParser\Node\Expr\FuncCall;
13+
use PhpParser\Node\Expr\Variable;
1314
use Rector\NodeManipulator\BinaryOpManipulator;
1415
use Rector\Php71\ValueObject\TwoNodeMatch;
1516
use Rector\Rector\AbstractRector;
@@ -95,27 +96,19 @@ function (Node $node): bool {
9596
return false;
9697
}
9798

98-
return isset($node->getArgs()[0]);
99+
if (isset($node->getArgs()[0])) {
100+
return $node->getArgs()[0]->value instanceof Variable;
101+
}
102+
103+
return false;
99104
},
100105
// empty(...)
101106
function (Node $node): bool {
102107
if (! $node instanceof Empty_) {
103108
return false;
104109
}
105110

106-
if ($node->expr instanceof FuncCall) {
107-
if ($node->expr->isFirstClassCallable()) {
108-
return false;
109-
}
110-
111-
if (! $this->isName($node->expr, 'array_filter')) {
112-
return false;
113-
}
114-
115-
return isset($node->expr->getArgs()[0]);
116-
}
117-
118-
return true;
111+
return $node->expr instanceof Variable;
119112
}
120113
);
121114
}

0 commit comments

Comments
 (0)