Skip to content

Commit

Permalink
[Transform]Move ReservedFnFunctionRector to Php74 namespace (#1471)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 27, 2021
1 parent 35ef77b commit 602aa13
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 134 deletions.
3 changes: 3 additions & 0 deletions config/set/php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
use Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector;
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;
Expand Down Expand Up @@ -59,4 +60,6 @@
$services->set(RestoreDefaultNullToNullableTypePropertyRector::class);

$services->set(CurlyToSquareBracketArrayStringRector::class);

$services->set(ReservedFnFunctionRector::class);
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

namespace Rector\Tests\Transform\Rector\Function_\ReservedFnFunctionRector\Fixture;
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\Fixture;

class Fixture
{
public function run()
{
function reservedFn($value)
function fn($value)
{
return $value;
}

reservedFn(5);
fn(5);
}
}

?>
-----
<?php

namespace Rector\Tests\Transform\Rector\Function_\ReservedFnFunctionRector\Fixture;
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector\Fixture;

class Fixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\Function_\ReservedFnFunctionRector;
namespace Rector\Tests\Php74\Rector\Function_\ReservedFnFunctionRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
Expand All @@ -15,6 +15,7 @@ final class ReservedFnFunctionRectorTest extends AbstractRectorTestCase
*/
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

declare(strict_types=1);

use Rector\Transform\Rector\Function_\ReservedFnFunctionRector;
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReservedFnFunctionRector::class)
->configure([
// for testing purposes of "fn" even on PHP 7.3-
'reservedFn' => 'f',
]);
$services->set(ReservedFnFunctionRector::class);
};
105 changes: 105 additions & 0 deletions rules/Php74/Rector/Function_/ReservedFnFunctionRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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 (! $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;
}
}
123 changes: 0 additions & 123 deletions rules/Transform/Rector/Function_/ReservedFnFunctionRector.php

This file was deleted.

0 comments on commit 602aa13

Please sign in to comment.