mirror of https://github.com/rectorphp/rector.git
[Php74] Remove ReservedFnFunctionRector (#2447)
* [Php74] Remove ReservedFnFunctionRector * unregister rule * phpstan
This commit is contained in:
parent
0c16eb2ad9
commit
4f07a4f30f
|
@ -1,4 +1,4 @@
|
|||
# 518 Rules Overview
|
||||
# 517 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -70,7 +70,7 @@
|
|||
|
||||
- [Php73](#php73) (9)
|
||||
|
||||
- [Php74](#php74) (14)
|
||||
- [Php74](#php74) (13)
|
||||
|
||||
- [Php80](#php80) (18)
|
||||
|
||||
|
@ -8058,31 +8058,6 @@ Change deprecated (real) to (float)
|
|||
|
||||
<br>
|
||||
|
||||
### ReservedFnFunctionRector
|
||||
|
||||
Change `fn()` function name to `f()`, since it will be reserved keyword
|
||||
|
||||
- class: [`Rector\Php74\Rector\Function_\ReservedFnFunctionRector`](../rules/Php74/Rector/Function_/ReservedFnFunctionRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- function fn($value)
|
||||
+ function f($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
- fn(5);
|
||||
+ f(5);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RestoreDefaultNullToNullableTypePropertyRector
|
||||
|
||||
Add null default to properties with PHP 7.4 property nullable type
|
||||
|
|
|
@ -11,7 +11,6 @@ use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
|
|||
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
|
||||
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
|
||||
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
|
||||
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
|
||||
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
|
||||
use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector;
|
||||
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
|
||||
|
@ -56,6 +55,4 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
$rectorConfig->rule(RestoreDefaultNullToNullableTypePropertyRector::class);
|
||||
|
||||
$rectorConfig->rule(CurlyToSquareBracketArrayStringRector::class);
|
||||
|
||||
$rectorConfig->rule(ReservedFnFunctionRector::class);
|
||||
};
|
||||
|
|
|
@ -725,3 +725,4 @@ parameters:
|
|||
|
||||
# resolve later with configurable types
|
||||
- '#On passing a constant, the method should have an enum type\. See https\://phpstan\.org/writing\-php\-code/phpdoc\-types\#literals\-and\-constants#'
|
||||
- '#Instead of calling all public methods of value object, pass it directly#'
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
function fn($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
fn(5);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
function f($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
f(5);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\FixtureRenameNameNode;
|
||||
|
||||
class SkipVariableFuncCall
|
||||
{
|
||||
/** @var list<Closure():void> */
|
||||
private array $callbacks = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($this->callbacks as $fn) {
|
||||
$fn();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RenameNameNodeTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureRenameNameNode');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ReservedFnFunctionRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->markTestSkipped('Requires tweaked PHP Parser to verify `fn` as valid function to simulate php <7.4');
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(ReservedFnFunctionRector::class);
|
||||
};
|
|
@ -1,109 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php74\Rector\Function_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @changelog https://github.com/php/php-src/pull/3941/files#diff-7e3a1a5df28a1cbd8c0fb6db68f243da
|
||||
* @see \Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\ReservedFnFunctionRectorTest
|
||||
*/
|
||||
final class ReservedFnFunctionRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const NEW_ORIGINAL_NAME = 'f';
|
||||
|
||||
public function __construct(
|
||||
private readonly ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
}
|
||||
|
||||
public function provideMinPhpVersion(): int
|
||||
{
|
||||
return PhpVersionFeature::RESERVED_FN_FUNCTION_NAME;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change fn() function name to f(), since it will be reserved keyword', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
function fn($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
fn(5);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
function f($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
f(5);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Function_::class, FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Function_|FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof FuncCall && ! $node->name instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->name, 'fn')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$newName = self::NEW_ORIGINAL_NAME;
|
||||
$count = 1;
|
||||
|
||||
while ($this->reflectionProvider->hasFunction(new Name($newName), null)) {
|
||||
$newName = self::NEW_ORIGINAL_NAME . $count;
|
||||
++$count;
|
||||
}
|
||||
|
||||
$node->name = $node instanceof FuncCall ? new Name($newName) : new Identifier($newName);
|
||||
return $node;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue