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
14 changes: 14 additions & 0 deletions config/set/downgrade-php85.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_84);
$rectorConfig->rules([
DowngradeArrayFirstLastRector::class,
]);
};
3 changes: 2 additions & 1 deletion config/set/level/down-to-php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Rector\Set\ValueObject\DowngradeSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeSetList::PHP_84]);
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_84, DowngradeSetList::PHP_84]);
};
10 changes: 10 additions & 0 deletions config/set/level/down-to-php84.php
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\Set\ValueObject\DowngradeSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeSetList::PHP_85]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;

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

final class DowngradeArrayFirstLastRectorTest 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,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;

final class Fixture
{
public function run(array $array)
{
echo array_first($array);
echo array_last($array);
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;

final class Fixture
{
public function run(array $array)
{
echo $array[array_key_first($array)];
echo $array[array_key_last($array)];
}
}

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\Fixture;

final class SkipDifferentFuncCall
{
public function run()
{
return strlen('test');
}
}

?>
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\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeArrayFirstLastRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,11 @@ private function refactorArrayConstValue(Array_ $array): ?Array_
continue;
}

/**
* @var ArrayItem $item
* @var ClassConstFetch $value
* @var Identifier $name
*/
$value = $item->value;
/** @var Identifier $name */
/** @var ClassConstFetch $value */
$name = $value->name;

/** @var Identifier $name */
$classLike = $this->astResolver->resolveClassFromName($type->getClassName());
if (! $classLike instanceof ClassLike) {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://php.watch/versions/8.5/array_first-array_last
* @see \Rector\Tests\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector\DowngradeArrayFirstLastRectorTest
*/
final class DowngradeArrayFirstLastRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [FuncCall::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace array_first() and array_last() with $array[array_key_first($array)] and $array[array_key_last($array)]',
[
new CodeSample(
<<<'CODE_SAMPLE'
echo array_first($array);
echo array_last($array);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo $array[array_key_first($array)];
echo $array[array_key_last($array)];
CODE_SAMPLE
),
]
);
}

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

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

$args = $node->getArgs();

if (count($args) !== 1) {
return null;
}

$functionName = $this->isName($node, 'array_first')
? 'array_key_first'
: 'array_key_last';

return new ArrayDimFetch(
$args[0]->value,
$this->nodeFactory->createFuncCall($functionName, [$args[0]->value])
);
}
}
5 changes: 5 additions & 0 deletions src/Set/ValueObject/DowngradeLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
final class DowngradeLevelSetList
{
/**
* @var string
*/
public const DOWN_TO_PHP_84 = __DIR__ . '/../../../config/set/level/down-to-php84.php';

/**
* @var string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Set/ValueObject/DowngradeSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ final class DowngradeSetList
* @var string
*/
public const PHP_84 = __DIR__ . '/../../../config/set/downgrade-php84.php';

/**
* @var string
*/
public const PHP_85 = __DIR__ . '/../../../config/set/downgrade-php85.php';
}
Loading