Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

function dontremovecomment()
{
reset($callbacks);

// a comment before
while (list($key, $callback) = each($callbacks)) {
// comment inside
Expand All @@ -18,6 +20,8 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

function dontremovecomment()
{
reset($callbacks);

// a comment before
foreach ($callbacks as $key => $callback) {
// comment inside
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

function each1()
function each1(array $callbacks)
{
reset($callbacks);
while (list($key, $callback) = each($callbacks)) {
// comment
}
Expand All @@ -21,8 +22,9 @@ function each1()

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

function each1()
function each1(array $callbacks)
{
reset($callbacks);
foreach ($callbacks as $key => $callback) {
// comment
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class IncludeSuppress
{
public function run(array $items)
{
reset($items);
while (list(, $item) = @each($items)) {
// some code
}
Expand All @@ -22,6 +23,7 @@ final class IncludeSuppress
{
public function run(array $items)
{
reset($items);
foreach ($items as $item) {
// some code
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class ResetInsideNestedBlock
{
public function run(array $items, bool $flag)
{
if ($flag) {
reset($items);
while (list($key, $item) = each($items)) {
echo $key . $item;
}
}
}
}

?>
-----
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class ResetInsideNestedBlock
{
public function run(array $items, bool $flag)
{
if ($flag) {
reset($items);
foreach ($items as $key => $item) {
echo $key . $item;
}
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipAssignedFromVariable
{
public function run(array $source)
{
// the copy keeps the internal pointer of $source
$items = $source;
while (list(, $item) = each($items)) {
echo $item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipByReferenceParam
{
public function run(array &$items)
{
// the caller sees the pointer at the end after each()
reset($items);
while (list(, $item) = each($items)) {
echo $item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipNoResetBefore
{
public function run(array $items)
{
// the caller may have moved the internal pointer, each() continues from there
while (list(, $item) = each($items)) {
echo $item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipPointerMovedBefore
{
public function run(array $items)
{
reset($items);
next($items);
while (list(, $item) = each($items)) {
echo $item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipPointerMovedInLoop
{
public function run(array $items)
{
reset($items);
while (list($key, $item) = each($items)) {
if ($item === 'skip') {
next($items);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipPointerUsedAfter
{
public function run(array $items)
{
reset($items);
while (list(, $item) = each($items)) {
echo $item;
}

return key($items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;

final class SkipSecondLoop
{
public function run(array $items)
{
reset($items);
while (list(, $item) = each($items)) {
echo $item;
}

// the pointer is at the end, this loop never runs
while (list(, $item) = each($items)) {
echo $item;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;
// ref https://3v4l.org/mTeIL#v7.1.31 vs https://3v4l.org/UJH9S
final class TrailingCommaLast
{
public function run()
public function run(array $poster_xd)
{
reset($poster_xd);

while(list($code_name,) = each($poster_xd))
{
$poster_xd[$code_name] = preg_replace($orig_word, $replacement_word, $poster_xd[$code_name]);
Expand All @@ -23,8 +25,10 @@ namespace Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\Fixture;
// ref https://3v4l.org/mTeIL#v7.1.31 vs https://3v4l.org/UJH9S
final class TrailingCommaLast
{
public function run()
public function run(array $poster_xd)
{
reset($poster_xd);

foreach (array_keys($poster_xd) as $code_name) {
$poster_xd[$code_name] = preg_replace($orig_word, $replacement_word, $poster_xd[$code_name]);
}
Expand Down
Loading
Loading