Skip to content

Commit 5305a54

Browse files
committed
key
1 parent 73a21bc commit 5305a54

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Fixture
2222
{
2323
$found = true;
2424
foreach ($animals as $animal) {
25-
if (str_starts_with($animal, 'c')) {
25+
if (! str_starts_with($animal, 'c')) {
2626
$found = false;
2727
break;
2828
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector\Fixture;
4+
5+
class WithKey
6+
{
7+
public function run(array $animals)
8+
{
9+
$found = array_all($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector\Fixture;
18+
19+
class WithKey
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = true;
24+
foreach ($animals as $key => $animal) {
25+
if (!(str_starts_with($animal, 'c') && $key > 0)) {
26+
$found = false;
27+
break;
28+
}
29+
}
30+
}
31+
}
32+
33+
?>

rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\ArrowFunction;
99
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\BooleanNot;
1011
use PhpParser\Node\Expr\ConstFetch;
1112
use PhpParser\Node\Expr\FuncCall;
1213
use PhpParser\Node\Name;
1314
use PhpParser\Node\Stmt\Break_;
1415
use PhpParser\Node\Stmt\Expression;
1516
use PhpParser\Node\Stmt\Foreach_;
1617
use PhpParser\Node\Stmt\If_;
18+
use Rector\NodeTypeResolver\Node\AttributeKey;
1719
use Rector\Rector\AbstractRector;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1921
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -84,7 +86,9 @@ public function refactor(Node $node): ?array
8486
return null;
8587
}
8688

87-
$if = new If_($args[1]->value->expr, [
89+
$valueCond = $args[1]->value->expr;
90+
$valueCond->setAttribute(AttributeKey::ORIGINAL_NODE, null);
91+
$if = new If_(new BooleanNot($valueCond), [
8892
'stmts' => [
8993
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
9094
new Break_(),

0 commit comments

Comments
 (0)