Skip to content
Draft
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
2 changes: 2 additions & 0 deletions config/set/php-polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\Config\RectorConfig;
use Rector\Php73\Rector\BooleanOr\IsCountableRector;
use Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector;
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
use Rector\Php80\Rector\Identical\StrEndsWithRector;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\Php80\Rector\NotIdentical\MbStrContainsRector;
Expand All @@ -25,6 +26,7 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
ArrayKeyFirstLastRector::class,
ArrayKeysToArrayKeyFirstLastRector::class,
IsCountableRector::class,
GetDebugTypeRector::class,
StrStartsWithRector::class,
Expand Down
2 changes: 2 additions & 0 deletions config/set/php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Php73\Rector\BooleanOr\IsCountableRector;
use Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector;
use Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector;
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;
use Rector\Php73\Rector\FuncCall\RegexDashEscapeRector;
use Rector\Php73\Rector\FuncCall\SensitiveDefineRector;
use Rector\Php73\Rector\FuncCall\SetCookieRector;
Expand Down Expand Up @@ -40,6 +41,7 @@
IsCountableRector::class,

ArrayKeyFirstLastRector::class,
ArrayKeysToArrayKeyFirstLastRector::class,

SensitiveDefineRector::class,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;

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

final class ArrayKeysToArrayKeyFirstLastRectorTest 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,35 @@
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class SomeClass
{
public function run(array $items)
{
$firstKey = current(array_keys($items));
$alsoFirstKey = reset(array_keys($items));
$lastKey = end(array_keys($items));

return [$firstKey, $alsoFirstKey, $lastKey];
}
}

?>
-----
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class SomeClass
{
public function run(array $items)
{
$firstKey = array_key_first($items);
$alsoFirstKey = array_key_first($items);
$lastKey = array_key_last($items);

return [$firstKey, $alsoFirstKey, $lastKey];
}
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class NestedExpression
{
public function run(array $items)
{
return trim((string) current(array_keys($this->resolveItems($items))));
}

private function resolveItems(array $items): array
{
return $items;
}
}

?>
-----
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class NestedExpression
{
public function run(array $items)
{
return trim((string) array_key_first($this->resolveItems($items)));
}

private function resolveItems(array $items): array
{
return $items;
}
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class SkipArrayKeysWithSearchValue
{
public function run(array $items)
{
// array_keys() with a search value returns only the matching keys
$firstMatchingKey = current(array_keys($items, 'value'));
$lastMatchingKey = end(array_keys($items, 'value', true));

return [$firstMatchingKey, $lastMatchingKey];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\Fixture;

final class SkipOtherCalls
{
public function run(array $items, array $keys)
{
// not array_keys()
$first = current(array_values($items));

// no inner call at all
$stillFirst = current($keys);

// key() of the keys array is always 0, not the first key
$zero = key(array_keys($items));

// first class callable
$callable = current(...);

// spread
$spread = current(...array_keys($items));

return [$first, $stillFirst, $zero, $callable, $spread];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector;

return RectorConfig::configure()
->withRules([ArrayKeysToArrayKeyFirstLastRector::class]);
127 changes: 127 additions & 0 deletions rules/Php73/Rector/FuncCall/ArrayKeysToArrayKeyFirstLastRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

namespace Rector\Php73\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php73\Rector\FuncCall\ArrayKeysToArrayKeyFirstLastRector\ArrayKeysToArrayKeyFirstLastRectorTest
*/
final class ArrayKeysToArrayKeyFirstLastRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
/**
* @var array<string, string>
*/
private const array PREVIOUS_TO_NEW_FUNCTIONS = [
'current' => 'array_key_first',
'reset' => 'array_key_first',
'end' => 'array_key_last',
];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make use of array_key_first() and array_key_last() instead of reading a single key off array_keys()',
[
new CodeSample(
<<<'CODE_SAMPLE'
$firstKey = current(array_keys($items));
$lastKey = end(array_keys($items));
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$firstKey = array_key_first($items);
$lastKey = array_key_last($items);
CODE_SAMPLE
),
]
);
}

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

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

$funcName = $this->getName($node);
if ($funcName === null) {
return null;
}

if (! isset(self::PREVIOUS_TO_NEW_FUNCTIONS[$funcName])) {
return null;
}

$args = $node->getArgs();
if (count($args) !== 1) {
return null;
}

if ($args[0]->name instanceof Identifier || $args[0]->unpack) {
return null;
}

$arrayKeysFuncCall = $args[0]->value;
if (! $arrayKeysFuncCall instanceof FuncCall) {
return null;
}

if (! $this->isName($arrayKeysFuncCall, 'array_keys')) {
return null;
}

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

$arrayKeysArgs = $arrayKeysFuncCall->getArgs();

// array_keys() with a search value returns only the matching keys, so the first one is not the array's first key
if (count($arrayKeysArgs) !== 1) {
return null;
}

if ($arrayKeysArgs[0]->name instanceof Identifier || $arrayKeysArgs[0]->unpack) {
return null;
}

$node->name = new Name(self::PREVIOUS_TO_NEW_FUNCTIONS[$funcName]);
$node->args = $arrayKeysArgs;

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ARRAY_KEY_FIRST_LAST;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_73;
}
}
Loading