Skip to content

Commit

Permalink
[TypeDeclaration] Handle infinite loop on array_reverse with index on…
Browse files Browse the repository at this point in the history
… AddMethodCallBasedStrictParamTypeRector on php8+ feature (#3678)

* [TypeDeclaration] Handle infinite loop on array_reverse with index

* [ci-review] Rector Rectify

* add fixture for php7.4 as well

* fix namespace

* [ci-review] Rector Rectify

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 24, 2023
1 parent fb89e31 commit 52e213f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class SkipNoReturnArrayReverse
{
public function run()
{
$parts = array_reverse(explode('/', '/some/test/url'));
foreach ($parts as $index => $uri) {
if ($index === 0) {
continue;
}

$this->someOtherMethod($index, $uri);
}
}

private function someOtherMethod(int $index, string $uri)
{
return sprintf('%d-%s', $index, $uri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\FixtureUnion;

final class SkipNoReturnArrayReverse
{
public function run()
{
$parts = array_reverse(explode('/', '/some/test/url'));
foreach ($parts as $index => $uri) {
if ($index === 0) {
continue;
}

$this->someOtherMethod($index, $uri);
}
}

private function someOtherMethod(int $index, string $uri)
{
return sprintf('%d-%s', $index, $uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ public function __construct(
public function complete(ClassMethod $classMethod, array $classParameterTypes, int $maxUnionTypes): ?ClassMethod
{
$hasChanged = false;
$totalTypes = count($classParameterTypes);

foreach ($classParameterTypes as $position => $argumentStaticType) {
if ($totalTypes > 1 && $argumentStaticType instanceof UnionType) {
return null;
}

/** @var Type $argumentStaticType */
if ($this->shouldSkipArgumentStaticType($classMethod, $argumentStaticType, $position, $maxUnionTypes)) {
continue;
Expand Down

0 comments on commit 52e213f

Please sign in to comment.