Updated Rector to commit d706d342846639545e2a19eadd618fac31751417

d706d34284 Make JsonThrowOnErrorRector work with local scope to detect error_json methods (#4451)
This commit is contained in:
Tomas Votruba 2023-07-09 11:07:02 +00:00
parent bc1a862545
commit a319accada
5 changed files with 46 additions and 33 deletions

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@ -22,6 +23,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var bool
*/
private $hasChanged = \false;
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Adds JSON_THROW_ON_ERROR to json_encode() and json_decode() to throw JsonException on error', [new CodeSample(<<<'CODE_SAMPLE'
@ -39,21 +44,38 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
return [StmtsAwareInterface::class];
}
/**
* @param FuncCall $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->shouldSkip($node)) {
// if found, skip it :)
$hasJsonErrorFuncCall = (bool) $this->betterNodeFinder->findFirst($node, function (Node $node) : bool {
return $this->isNames($node, ['json_last_error', 'json_last_error_msg']);
});
if ($hasJsonErrorFuncCall) {
return null;
}
if ($this->isName($node, 'json_encode')) {
return $this->processJsonEncode($node);
}
if ($this->isName($node, 'json_decode')) {
return $this->processJsonDecode($node);
$this->hasChanged = \false;
$this->traverseNodesWithCallable($node, function (Node $currentNode) : ?FuncCall {
if (!$currentNode instanceof FuncCall) {
return null;
}
if ($this->shouldSkipFuncCall($currentNode)) {
return null;
}
if ($this->isName($currentNode, 'json_encode')) {
return $this->processJsonEncode($currentNode);
}
if ($this->isName($currentNode, 'json_decode')) {
return $this->processJsonDecode($currentNode);
}
return null;
});
if ($this->hasChanged) {
return $node;
}
return null;
}
@ -61,11 +83,8 @@ CODE_SAMPLE
{
return PhpVersionFeature::JSON_EXCEPTION;
}
private function shouldSkip(FuncCall $funcCall) : bool
private function shouldSkipFuncCall(FuncCall $funcCall) : bool
{
if (!$this->isNames($funcCall, ['json_encode', 'json_decode'])) {
return \true;
}
if ($funcCall->isFirstClassCallable()) {
return \true;
}
@ -80,21 +99,14 @@ CODE_SAMPLE
return \true;
}
}
if ($this->isFirstValueStringOrArray($funcCall)) {
return \true;
}
return (bool) $this->betterNodeFinder->findFirstNext($funcCall, function (Node $node) : bool {
if (!$node instanceof FuncCall) {
return \false;
}
return $this->isNames($node, ['json_last_error', 'json_last_error_msg']);
});
return $this->isFirstValueStringOrArray($funcCall);
}
private function processJsonEncode(FuncCall $funcCall) : ?FuncCall
{
if (isset($funcCall->args[1])) {
return null;
}
$this->hasChanged = \true;
$funcCall->args[1] = new Arg($this->createConstFetch('JSON_THROW_ON_ERROR'));
return $funcCall;
}
@ -110,6 +122,7 @@ CODE_SAMPLE
if (!isset($funcCall->args[2])) {
$funcCall->args[2] = new Arg(new LNumber(512));
}
$this->hasChanged = \true;
$funcCall->args[3] = new Arg($this->createConstFetch('JSON_THROW_ON_ERROR'));
return $funcCall;
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'cfc88dc019bc47deb44280e14940af355dfd1e81';
public const PACKAGE_VERSION = 'd706d342846639545e2a19eadd618fac31751417';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-09 10:37:21';
public const RELEASE_DATE = '2023-07-09 11:03:00';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6::getLoader();
return ComposerAutoloaderInit8fd9100642750c440892cc90b725b580::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6
class ComposerAutoloaderInit8fd9100642750c440892cc90b725b580
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit8fd9100642750c440892cc90b725b580', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit978c3c65a70434a42ecc983d56681bb6', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit8fd9100642750c440892cc90b725b580', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit8fd9100642750c440892cc90b725b580::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit8fd9100642750c440892cc90b725b580::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit978c3c65a70434a42ecc983d56681bb6
class ComposerStaticInit8fd9100642750c440892cc90b725b580
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3070,9 +3070,9 @@ class ComposerStaticInit978c3c65a70434a42ecc983d56681bb6
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit978c3c65a70434a42ecc983d56681bb6::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit8fd9100642750c440892cc90b725b580::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8fd9100642750c440892cc90b725b580::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8fd9100642750c440892cc90b725b580::$classMap;
}, null, ClassLoader::class);
}