Skip to content
Open
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
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

final class ArrayValuesPattern
{
public function run(array $array)
{
echo array_values($array)[0];
echo array_values($array)[count($array) - 1];
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

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

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

final class SkipArrayValuesDifferentVariable
{
public function run(array $array, array $differentVariable)
{
echo array_values($array)[count($differentVariable) - 1];
}
}
104 changes: 96 additions & 8 deletions rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -40,11 +42,15 @@ public function getRuleDefinition(): RuleDefinition
<<<'CODE_SAMPLE'
echo $array[array_key_first($array)];
echo $array[array_key_last($array)];
echo array_values($array)[0];
echo array_values($array)[count($array) - 1];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo array_first($array);
echo array_last($array);
echo array_first($array);
echo array_last($array);
CODE_SAMPLE
),
]
Expand All @@ -63,6 +69,24 @@ public function getNodeTypes(): array
* @param ArrayDimFetch $node
*/
public function refactor(Node $node): ?FuncCall
{
if ($node->dim instanceof FuncCall) {
return $this->refactorArrayKeyPattern($node);
}

if ($node->var instanceof FuncCall && ($node->dim instanceof Int_ || $node->dim instanceof Minus)) {
return $this->refactorArrayValuesPattern($node);
}

return null;
}

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

private function refactorArrayKeyPattern(ArrayDimFetch $node): ?FuncCall
{
if (! $node->dim instanceof FuncCall) {
return null;
Expand All @@ -84,12 +108,7 @@ public function refactor(Node $node): ?FuncCall
return null;
}

$scope = ScopeFetcher::fetch($node->var);
if ($scope->isInExpressionAssign($node)) {
return null;
}

if ($node->getAttribute(AttributeKey::IS_UNSET_VAR)) {
if ($this->shouldSkip($node, $node->var)) {
return null;
}

Expand All @@ -100,8 +119,77 @@ public function refactor(Node $node): ?FuncCall
return $this->nodeFactory->createFuncCall($functionName, [$node->var]);
}

public function provideMinPhpVersion(): int
private function refactorArrayValuesPattern(ArrayDimFetch $node): ?FuncCall
{
return PhpVersionFeature::ARRAY_FIRST_LAST;
if (! $node->var instanceof FuncCall) {
return null;
}

if (! $this->isName($node->var, 'array_values')) {
return null;
}

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

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

if ($this->shouldSkip($node, $node)) {
return null;
}

$arrayArg = $node->var->getArgs()[0]
->value;

if ($node->dim instanceof Int_ && $node->dim->value === 0) {
return $this->nodeFactory->createFuncCall('array_first', [$arrayArg]);
}

if ($node->dim instanceof Minus) {
if (! $node->dim->left instanceof FuncCall) {
return null;
}

if (! $this->isName($node->dim->left, 'count')) {
return null;
}

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

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

if (! $node->dim->right instanceof Int_ || $node->dim->right->value !== 1) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($arrayArg, $node->dim->left->getArgs()[0]->value)) {
return null;
}

return $this->nodeFactory->createFuncCall('array_last', [$arrayArg]);
}

return null;
}

private function shouldSkip(ArrayDimFetch $node, Node $scopeNode): bool
{
$scope = ScopeFetcher::fetch($scopeNode);
if ($scope->isInExpressionAssign($node)) {
return true;
}

if ($node->getAttribute(AttributeKey::IS_UNSET_VAR)) {
return true;
}

return false;
}
}