mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
[Php72] Handle usage super global on CreateFunctionToAnonymousFunctionRector (#5778)
This commit is contained in:
parent
d7592f59ec
commit
d3cbe44928
|
@ -11,6 +11,7 @@ use PhpParser\Node\Expr\Assign;
|
|||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
|
@ -18,6 +19,7 @@ use PhpParser\Node\Scalar\String_;
|
|||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\PhpParser\Parser\InlineCodeParser;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
|
||||
|
@ -42,12 +44,19 @@ final class CreateFunctionToAnonymousFunctionRector extends AbstractRector
|
|||
*/
|
||||
private $anonymousFunctionFactory;
|
||||
|
||||
/**
|
||||
* @var ReservedKeywordAnalyzer
|
||||
*/
|
||||
private $reservedKeywordAnalyzer;
|
||||
|
||||
public function __construct(
|
||||
InlineCodeParser $inlineCodeParser,
|
||||
AnonymousFunctionFactory $anonymousFunctionFactory
|
||||
AnonymousFunctionFactory $anonymousFunctionFactory,
|
||||
ReservedKeywordAnalyzer $reservedKeywordAnalyzer
|
||||
) {
|
||||
$this->inlineCodeParser = $inlineCodeParser;
|
||||
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
|
||||
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -101,9 +110,24 @@ CODE_SAMPLE
|
|||
|
||||
$params = $this->createParamsFromString($node->args[0]->value);
|
||||
$stmts = $this->parseStringToBody($node->args[1]->value);
|
||||
$returnType = null;
|
||||
|
||||
return $this->anonymousFunctionFactory->create($params, $stmts, $returnType);
|
||||
$refactored = $this->anonymousFunctionFactory->create($params, $stmts, null);
|
||||
foreach ($refactored->uses as $key => $use) {
|
||||
if (! $use->var instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$variableName = $this->getName($use->var);
|
||||
if ($variableName === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
|
||||
unset($refactored->uses[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $refactored;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;
|
||||
|
||||
final class SuperGlobal
|
||||
{
|
||||
function register_skin_deactivation_hook_function($code, $function)
|
||||
{
|
||||
$GLOBALS[ 'register_skin_deactivation_hook_function' . $code] = $function;
|
||||
$fn=create_function('$skin', ' call_user_func($GLOBALS["register_skin_deactivation_hook_function' . $code . '"]); delete_option("skin_is_activated_' . $code. '");');
|
||||
add_action( 'switch_s' , $fn );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;
|
||||
|
||||
final class SuperGlobal
|
||||
{
|
||||
function register_skin_deactivation_hook_function($code, $function)
|
||||
{
|
||||
$GLOBALS[ 'register_skin_deactivation_hook_function' . $code] = $function;
|
||||
$fn=function ($skin) use ($code) {
|
||||
call_user_func($GLOBALS["register_skin_deactivation_hook_function{$code}"]);
|
||||
delete_option("skin_is_activated_{$code}");
|
||||
};
|
||||
add_action( 'switch_s' , $fn );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;
|
||||
|
||||
final class SuperGlobalPost
|
||||
{
|
||||
function register_skin_deactivation_hook_function($code, $function)
|
||||
{
|
||||
$_POST[ 'register_skin_deactivation_hook_function' . $code] = $function;
|
||||
$fn=create_function('$skin', ' call_user_func($_POST["register_skin_deactivation_hook_function' . $code . '"]); delete_option("skin_is_activated_' . $code. '");');
|
||||
add_action( 'switch_s' , $fn );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\Fixture;
|
||||
|
||||
final class SuperGlobalPost
|
||||
{
|
||||
function register_skin_deactivation_hook_function($code, $function)
|
||||
{
|
||||
$_POST[ 'register_skin_deactivation_hook_function' . $code] = $function;
|
||||
$fn=function ($skin) use ($code) {
|
||||
call_user_func($_POST["register_skin_deactivation_hook_function{$code}"]);
|
||||
delete_option("skin_is_activated_{$code}");
|
||||
};
|
||||
add_action( 'switch_s' , $fn );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -93,6 +93,7 @@ final class ReservedKeywordAnalyzer
|
|||
'_FILES',
|
||||
'_REQUEST',
|
||||
'_SESSION',
|
||||
'GLOBALS',
|
||||
];
|
||||
|
||||
public function isNativeVariable(string $name): bool
|
||||
|
|
Loading…
Reference in New Issue
Block a user