Updated Rector to commit 7cf24218f0

7cf24218f0 [DowngradePhp72] Add DowngradeJsonDecodeNullAssociativeArgRector (#1723)
This commit is contained in:
Tomas Votruba 2022-01-25 10:05:47 +00:00
parent 73a413e194
commit 4fd7947603
10 changed files with 126 additions and 21 deletions

View File

@ -14,7 +14,7 @@ Use Rector to handle **instant upgrades** for you.
### 2. Automated Refactoring
Do you have code quality you need, but struggle to keep it with new developers in your team? Do you want see smart code-reviews even when every senior developers sleeps?
Do you have code quality you need, but struggle to keep it with new developers in your team? Do you want to see smart code-reviews even when every senior developers sleeps?
Add Rector to your CI and let it **continuously refactor your code** and keep the code quality high.

View File

@ -6,6 +6,7 @@ namespace RectorPrefix20220125;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
@ -18,4 +19,5 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector::class);
$services->set(\Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector::class);
$services->set(\Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector::class);
$services->set(\Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector::class);
};

View File

@ -0,0 +1,101 @@
<?php
declare (strict_types=1);
namespace Rector\DowngradePhp72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\BooleanType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\DowngradeJsonDecodeNullAssociativeArgRectorTest
*/
final class DowngradeJsonDecodeNullAssociativeArgRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer;
public function __construct(\Rector\Core\NodeAnalyzer\ArgsAnalyzer $argsAnalyzer)
{
$this->argsAnalyzer = $argsAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Downgrade json_decode() with null associative argument function', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
declare(strict_types=1);
function exactlyNull(string $json)
{
$value = json_decode($json, null);
}
function possiblyNull(string $json, ?bool $associative)
{
$value = json_decode($json, $associative);
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
declare(strict_types=1);
function exactlyNull(string $json)
{
$value = json_decode($json, false);
}
function possiblyNull(string $json, ?bool $associative)
{
$value = json_decode($json, (bool) $associative);
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if (!$this->isName($node, 'json_decode')) {
return null;
}
$createdByRule = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CREATED_BY_RULE) ?? [];
if (\in_array(self::class, $createdByRule, \true)) {
return null;
}
$args = $node->getArgs();
if ($this->argsAnalyzer->hasNamedArg($args)) {
return null;
}
if (!isset($args[1])) {
return null;
}
$associativeValue = $args[1]->value;
if ($associativeValue instanceof \PhpParser\Node\Expr\Cast\Bool_) {
return null;
}
$type = $this->nodeTypeResolver->getType($associativeValue);
if ($type instanceof \PHPStan\Type\BooleanType) {
return null;
}
if ($associativeValue instanceof \PhpParser\Node\Expr\ConstFetch && $this->valueResolver->isNull($associativeValue)) {
$node->args[1]->value = $this->nodeFactory->createFalse();
return $node;
}
$node->args[1]->value = new \PhpParser\Node\Expr\Cast\Bool_($associativeValue);
return $node;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '5ba03adbab3b2a45fe92c4d01093e7e8c147efd7';
public const PACKAGE_VERSION = '7cf24218f0eafd908470b52f9dc6c48d225fd273';
/**
* @var string
*/
public const RELEASE_DATE = '2022-01-25 09:30:24';
public const RELEASE_DATE = '2022-01-25 09:58:12';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20220125\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f::getLoader();
return ComposerAutoloaderInit071d6d9198851483713c938d039196ac::getLoader();

View File

@ -2009,6 +2009,7 @@ return array(
'Rector\\DowngradePhp72\\NodeAnalyzer\\SealedClassAnalyzer' => $baseDir . '/rules/DowngradePhp72/NodeAnalyzer/SealedClassAnalyzer.php',
'Rector\\DowngradePhp72\\PhpDoc\\NativeParamToPhpDocDecorator' => $baseDir . '/rules/DowngradePhp72/PhpDoc/NativeParamToPhpDocDecorator.php',
'Rector\\DowngradePhp72\\Rector\\ClassMethod\\DowngradeParameterTypeWideningRector' => $baseDir . '/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradeJsonDecodeNullAssociativeArgRector' => $baseDir . '/rules/DowngradePhp72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradePregUnmatchedAsNullConstantRector' => $baseDir . '/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradeStreamIsattyRector' => $baseDir . '/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php',
'Rector\\DowngradePhp72\\Rector\\FunctionLike\\DowngradeObjectTypeDeclarationRector' => $baseDir . '/rules/DowngradePhp72/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f
class ComposerAutoloaderInit071d6d9198851483713c938d039196ac
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit071d6d9198851483713c938d039196ac', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit071d6d9198851483713c938d039196ac', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit071d6d9198851483713c938d039196ac::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit071d6d9198851483713c938d039196ac::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire6f3b157e003d01292f3fc956cce4bd4f($fileIdentifier, $file);
composerRequire071d6d9198851483713c938d039196ac($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f
* @param string $file
* @return void
*/
function composerRequire6f3b157e003d01292f3fc956cce4bd4f($fileIdentifier, $file)
function composerRequire071d6d9198851483713c938d039196ac($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 ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f
class ComposerStaticInit071d6d9198851483713c938d039196ac
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2404,6 +2404,7 @@ class ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f
'Rector\\DowngradePhp72\\NodeAnalyzer\\SealedClassAnalyzer' => __DIR__ . '/../..' . '/rules/DowngradePhp72/NodeAnalyzer/SealedClassAnalyzer.php',
'Rector\\DowngradePhp72\\PhpDoc\\NativeParamToPhpDocDecorator' => __DIR__ . '/../..' . '/rules/DowngradePhp72/PhpDoc/NativeParamToPhpDocDecorator.php',
'Rector\\DowngradePhp72\\Rector\\ClassMethod\\DowngradeParameterTypeWideningRector' => __DIR__ . '/../..' . '/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradeJsonDecodeNullAssociativeArgRector' => __DIR__ . '/../..' . '/rules/DowngradePhp72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradePregUnmatchedAsNullConstantRector' => __DIR__ . '/../..' . '/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php',
'Rector\\DowngradePhp72\\Rector\\FuncCall\\DowngradeStreamIsattyRector' => __DIR__ . '/../..' . '/rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php',
'Rector\\DowngradePhp72\\Rector\\FunctionLike\\DowngradeObjectTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/DowngradePhp72/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php',
@ -3874,9 +3875,9 @@ class ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit6f3b157e003d01292f3fc956cce4bd4f::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit071d6d9198851483713c938d039196ac::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit071d6d9198851483713c938d039196ac::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit071d6d9198851483713c938d039196ac::$classMap;
}, null, ClassLoader::class);
}

View File

@ -40,7 +40,7 @@ final class Json
*/
public static function decode(string $json, int $flags = 0)
{
$value = \json_decode($json, null, 512, $flags | \JSON_BIGINT_AS_STRING);
$value = \json_decode($json, \false, 512, $flags | \JSON_BIGINT_AS_STRING);
if ($error = \json_last_error()) {
throw new \RectorPrefix20220125\Nette\Utils\JsonException(\json_last_error_msg(), $error);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20220125\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f', false) && !interface_exists('ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f', false) && !trait_exists('ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f', false)) {
spl_autoload_call('RectorPrefix20220125\ComposerAutoloaderInit6f3b157e003d01292f3fc956cce4bd4f');
if (!class_exists('ComposerAutoloaderInit071d6d9198851483713c938d039196ac', false) && !interface_exists('ComposerAutoloaderInit071d6d9198851483713c938d039196ac', false) && !trait_exists('ComposerAutoloaderInit071d6d9198851483713c938d039196ac', false)) {
spl_autoload_call('RectorPrefix20220125\ComposerAutoloaderInit071d6d9198851483713c938d039196ac');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20220125\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -71,9 +71,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220125\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire6f3b157e003d01292f3fc956cce4bd4f')) {
function composerRequire6f3b157e003d01292f3fc956cce4bd4f() {
return \RectorPrefix20220125\composerRequire6f3b157e003d01292f3fc956cce4bd4f(...func_get_args());
if (!function_exists('composerRequire071d6d9198851483713c938d039196ac')) {
function composerRequire071d6d9198851483713c938d039196ac() {
return \RectorPrefix20220125\composerRequire071d6d9198851483713c938d039196ac(...func_get_args());
}
}
if (!function_exists('scanPath')) {