Updated Rector to commit df459e711ae5bd8f359df40fbef863cc386bf107

df459e711a Handle AlwaysRememberedExpr in deep Expr (#4310)
This commit is contained in:
Tomas Votruba 2023-06-21 12:07:07 +00:00
parent f41e5dc13b
commit 3236151dbe
9 changed files with 133 additions and 21 deletions

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitb182314be48c7453fab0665e5cc0c4bc
class ComposerAutoloaderInit9ea78b2fb335d8d9676a644a47187f7a
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitb182314be48c7453fab0665e5cc0c4bc
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitb182314be48c7453fab0665e5cc0c4bc', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit9ea78b2fb335d8d9676a644a47187f7a', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitb182314be48c7453fab0665e5cc0c4bc', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit9ea78b2fb335d8d9676a644a47187f7a', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a::$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 ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc
class ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3097,9 +3097,9 @@ class ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb182314be48c7453fab0665e5cc0c4bc::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit9ea78b2fb335d8d9676a644a47187f7a::$classMap;
}, null, ClassLoader::class);
}

View File

@ -1982,12 +1982,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-downgrade-php.git",
"reference": "6935466206df434288f406717cb077cf7a0d41ed"
"reference": "2500281e7826a14f732915d9edf78d5ea4470b06"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/6935466206df434288f406717cb077cf7a0d41ed",
"reference": "6935466206df434288f406717cb077cf7a0d41ed",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/2500281e7826a14f732915d9edf78d5ea4470b06",
"reference": "2500281e7826a14f732915d9edf78d5ea4470b06",
"shasum": ""
},
"require": {
@ -2011,7 +2011,7 @@
"tomasvotruba\/type-coverage": "^0.2",
"tomasvotruba\/unused-public": "^0.1"
},
"time": "2023-06-18T02:27:23+00:00",
"time": "2023-06-21T12:01:34+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main f0c1a5f'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 6935466'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 8288f71'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 100ca8c'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main f0c1a5f'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 2500281'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 8288f71'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 100ca8c'));
private function __construct()
{
}

View File

@ -3,12 +3,21 @@
declare (strict_types=1);
namespace Rector\DowngradePhp73\Rector\ConstFetch;
use RectorPrefix202306\Nette\Utils\Json;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified as NameFullyQualified;
use PhpParser\Node\Stmt\Expression as StmtExpression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
use Rector\Enum\JsonConstant;
@ -36,6 +45,10 @@ final class DowngradePhp73JsonConstRector extends AbstractRector
* @var string
*/
private const PHP73_JSON_CONSTANT_IS_KNOWN = 'php73_json_constant_is_known';
/**
* @var array<string>
*/
private const REFACTOR_FUNCS = ['json_decode', 'json_encode'];
public function __construct(JsonConstCleaner $jsonConstCleaner, DefineFuncCallAnalyzer $defineFuncCallAnalyzer)
{
$this->jsonConstCleaner = $jsonConstCleaner;
@ -48,6 +61,18 @@ json_encode($content, JSON_THROW_ON_ERROR);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
json_encode($content, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
CODE_SAMPLE
), new CodeSample(<<<'CODE_SAMPLE'
$content = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$content = json_decode($json, null, 512, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
CODE_SAMPLE
)]);
}
@ -56,11 +81,11 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [ConstFetch::class, BitwiseOr::class, If_::class];
return [ConstFetch::class, BitwiseOr::class, If_::class, StmtExpression::class];
}
/**
* @param ConstFetch|BitwiseOr|If_ $node
* @return int|null|\PhpParser\Node\Expr|\PhpParser\Node\Stmt\If_
* @param ConstFetch|BitwiseOr|If_|StmtExpression $node
* @return int|null|Expr|If_|array<StmtExpression|If_>
*/
public function refactor(Node $node)
{
@ -71,6 +96,9 @@ CODE_SAMPLE
if ((bool) $node->getAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN)) {
return null;
}
if ($node instanceof StmtExpression) {
return $this->refactorStmt($node);
}
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
}
private function refactorIf(If_ $if) : ?If_
@ -84,4 +112,86 @@ CODE_SAMPLE
});
return $if;
}
private function resolveFuncCall(StmtExpression $stmtExpression) : ?FuncCall
{
$expr = $stmtExpression->expr;
if ($expr instanceof Assign) {
if ($expr->expr instanceof FuncCall) {
return $expr->expr;
}
return null;
}
if ($expr instanceof FuncCall) {
return $expr;
}
return null;
}
/**
* Add an alternative throwing error behavior after any `json_encode`
* or `json_decode` function called with the `JSON_THROW_ON_ERROR` flag set.
* This is a partial improvement of removing the `JSON_THROW_ON_ERROR` flag,
* only when the flags are directly set in the function call.
* If the flags are set from a variable, that would require a much more
* complex analysis to be 100% accurate, beyond Rector actual capabilities.
* @return null|array<StmtExpression|If_>
*/
private function refactorStmt(StmtExpression $stmtExpression) : ?array
{
// retrieve a `FuncCall`, if any, from the statement
$funcCall = $this->resolveFuncCall($stmtExpression);
// Nothing to do if no `FuncCall` found
if (!$funcCall instanceof FuncCall) {
return null;
}
// Nothing to do if not a refactored function
if (!\in_array($this->getName($funcCall), self::REFACTOR_FUNCS, \true)) {
return null;
}
// Nothing to do if the flag `JSON_THROW_ON_ERROR` is not set in args
if (!$this->hasConstFetchInArgs($funcCall->args, 'JSON_THROW_ON_ERROR')) {
return null;
}
$nodes = [$stmtExpression];
$nodes[] = new If_(new NotIdentical(new FuncCall(new Name('json_last_error')), new ConstFetch(new Name('JSON_ERROR_NONE'))), ['stmts' => [new Throw_(new New_(new NameFullyQualified('Exception'), [new Arg(new FuncCall(new Name('json_last_error_msg')))]))]]);
return $nodes;
}
/**
* Search if a given constant is set within a list of `Arg`
* @param array<Arg|VariadicPlaceholder> $args
*/
private function hasConstFetchInArgs(array $args, string $constName) : bool
{
foreach ($args as $arg) {
// Only `Arg` instances are handled.
if (!$arg instanceof Arg) {
return \false;
}
$value = $arg->value;
if ($value instanceof ConstFetch && $this->getName($value) === $constName) {
return \true;
}
if ($value instanceof BitwiseOr) {
return $this->hasConstFetchInBitwiseOr($value, $constName);
}
}
return \false;
}
/**
* Search if a given constant is set within a `BitwiseOr`
*/
private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constName) : bool
{
$found = \false;
foreach ([$bitwiseOr->left, $bitwiseOr->right] as $subNode) {
// Only `Node` instances can hold the constant.
if (!$subNode instanceof Node) {
continue;
}
$found = \true === $subNode instanceof BitwiseOr ? $this->hasConstFetchInBitwiseOr($subNode, $constName) : (\true === $subNode instanceof ConstFetch ? $this->getName($subNode) === $constName : \false);
if ($found) {
break;
}
}
return $found;
}
}

View File

@ -12,7 +12,6 @@ use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IterableType;
use PHPStan\Type\ObjectType;

View File

@ -29,6 +29,9 @@ class LoaderLoadException extends \Exception
if (!\is_string($resource)) {
try {
$resource = \json_encode($resource, 0);
if (\json_last_error() !== \JSON_ERROR_NONE) {
throw new \Exception(\json_last_error_msg());
}
} catch (\JsonException $exception) {
$resource = \sprintf('resource of type "%s"', \get_debug_type($resource));
}