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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SetList::PHP_85, LevelSetList::UP_TO_PHP_84]);
};
14 changes: 14 additions & 0 deletions config/set/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\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules(
[
ArrayFirstLastRector::class,
]
);
};
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ parameters:
- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'

# known value
- '#Method (.*?) should return 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|100000 but returns int#'
- '#Method (.*?) should return 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|100000 but returns int#'

-
message: '#Function "class_exists\(\)" cannot be used/left in the code#'
Expand Down Expand Up @@ -306,7 +306,7 @@ parameters:
message: '#PHPDoc tag @var with type int is not subtype of native type array\|PHPStan\\PhpDocParser\\Ast\\ConstExpr\\ConstExprNode\|Rector\\BetterPhpDocParser\\PhpDoc\\DoctrineAnnotationTagValueNode\|Rector\\BetterPhpDocParser\\PhpDoc\\StringNode\|Rector\\BetterPhpDocParser\\ValueObject\\PhpDoc\\DoctrineAnnotation\\CurlyListNode\|string#'
path: src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php

- '#Parameter \#1 \$phpVersion of method Rector\\Config\\RectorConfig\:\:phpVersion\(\) expects 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|100000, 79999 given#'
- '#Parameter \#1 \$phpVersion of method Rector\\Config\\RectorConfig\:\:phpVersion\(\) expects 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|80300\|80400\|80500\|100000, 79999 given#'

# node vs stmts mix
- '#expects array<PhpParser\\Node\\Stmt>, array<PhpParser\\Node> given#'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

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

final class ArrayFirstLastRectorTest 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\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

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

final class Fixture
{
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,14 @@
<?php

declare(strict_types=1);

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

final class SkipDifferentVariable
{
public function run(array $array, array $differentVariable)
{
echo $array[array_key_first($differentVariable)];
echo $array[array_key_last($differentVariable)];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ArrayFirstLastRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_85);
};
96 changes: 96 additions & 0 deletions rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\ArrayDimFetch;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
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\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\ArrayFirstLastRectorTest
*/
final class ArrayFirstLastRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var string
*/
private const ARRAY_KEY_FIRST = 'array_key_first';

/**
* @var string
*/
private const ARRAY_KEY_LAST = 'array_key_last';

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

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

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

if (! $this->isNames($node->dim, [self::ARRAY_KEY_FIRST, self::ARRAY_KEY_LAST])) {
return null;
}

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

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

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

$functionName = $this->isName($node->dim, self::ARRAY_KEY_FIRST)
? 'array_first'
: 'array_last';

return $this->nodeFactory->createFuncCall($functionName, [$node->var]);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ARRAY_FIRST_LAST;
}
}
1 change: 1 addition & 0 deletions src/Set/SetProvider/PHPSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function provide(): array
new Set(SetGroup::PHP, 'PHP 8.2', __DIR__ . '/../../../config/set/php82.php'),
new Set(SetGroup::PHP, 'PHP 8.3', __DIR__ . '/../../../config/set/php83.php'),
new Set(SetGroup::PHP, 'PHP 8.4', __DIR__ . '/../../../config/set/php84.php'),
new Set(SetGroup::PHP, 'PHP 8.5', __DIR__ . '/../../../config/set/php85.php'),
new Set(SetGroup::PHP, 'Polyfills', __DIR__ . '/../../../config/set/php-polyfills.php'),
];
}
Expand Down
5 changes: 5 additions & 0 deletions src/Set/ValueObject/LevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
final class LevelSetList
{
/**
* @var string
*/
public const UP_TO_PHP_85 = __DIR__ . '/../../../config/set/level/up-to-php85.php';

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

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

/**
* @var string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ final class PhpVersion
*/
public const PHP_84 = 80400;

/**
* @var int
*/
public const PHP_85 = 80500;

/**
* @var int
*/
Expand Down
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ final class PhpVersionFeature
*/
public const ARRAY_KEY_FIRST_LAST = PhpVersion::PHP_73;

/**
* @var int
* @see https://php.watch/versions/8.5/array_first-array_last
*/
public const ARRAY_FIRST_LAST = PhpVersion::PHP_85;

/**
* @var int
*/
Expand Down