Skip to content
Merged
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
4 changes: 3 additions & 1 deletion config/set/downgrade-php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\DowngradePhp82\Rector\Class_\DowngradeReadonlyClassRector;
use Rector\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;
use Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_81);
$rectorConfig->rules([
DowngradeReadonlyClassRector::class,
DowngradeStandaloneNullTrueFalseReturnTypeRector::class
DowngradeStandaloneNullTrueFalseReturnTypeRector::class,
DowngradeIteratorCountToArrayRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeIteratorCountToArrayRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;

final class PossibleNotTraversable
{
function test(array|\Traversable $data) {
$c = iterator_count($data);
$v = iterator_to_array($data);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;

final class PossibleNotTraversable
{
function test(array|\Traversable $data) {
$c = iterator_count(is_array($data) ? new \ArrayIterator($data) : $data);
$v = iterator_to_array(is_array($data) ? new \ArrayIterator($data) : $data);
}
}

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

namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;

final class SkipAlreadyHasObjectType
{
function test(array|\Traversable $data) {
$c = iterator_count(is_array($data) ? new \ArrayIterator($data) : $data);
$v = iterator_to_array(is_array($data) ? new \ArrayIterator($data) : $data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;

final class SkipAlreadyTraversable
{
function test(\Traversable $data)
{
$c = iterator_count($data);
$v = iterator_to_array($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeIteratorCountToArrayRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp82\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Type;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @changelog https://www.php.net/manual/en/migration82.other-changes.php#migration82.other-changes.functions.spl
*
* @see \Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\DowngradeIteratorCountToArrayRectorTest
* @see https://3v4l.org/TPW7a#v8.1.31
*/
final class DowngradeIteratorCountToArrayRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer
) {
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Ensure pass Traversable instance before use in iterator_count() and iterator_to_array()',
[
new CodeSample(
<<<'CODE_SAMPLE'
function test(array|Traversable $data) {
$c = iterator_count($data);
$v = iterator_to_array($data);
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function test(array|Traversable $data) {
$c = iterator_count(is_array($data) ? new ArrayIterator($data) : $data);
$v = iterator_to_array(is_array($data) ? new ArrayIterator($data) : $data);
}
CODE_SAMPLE
),
]
);
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isNames($node, ['iterator_count', 'iterator_to_array'])) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
if ($this->argsAnalyzer->hasNamedArg($args)) {
return null;
}

if (! isset($args[0])) {
return null;
}

$type = $this->nodeTypeResolver->getType($args[0]->value);
if ($this->shouldSkip($type)) {
return null;
}

Assert::isInstanceOf($node->args[0], Arg::class);

$firstValue = $node->args[0]->value;
$node->args[0]->value = new Ternary(
$this->nodeFactory->createFuncCall('is_array', [new Arg($firstValue)]),
new New_(new FullyQualified('ArrayIterator'), [new Arg($firstValue)]),
$firstValue
);

return $node;
}

private function shouldSkip(Type $type): bool
{
if ($type->isArray()->yes()) {
return false;
}

return $type->isObject()
->yes();
}
}
Loading