Updated Rector to commit 90bbd4e1a0

90bbd4e1a0 [Php74] Add GetCalledClassToSelfClassRector (#1971)
This commit is contained in:
Tomas Votruba 2022-03-27 18:45:00 +00:00
parent c6551c2127
commit a641d86cfa
11 changed files with 157 additions and 31 deletions

View File

@ -10,6 +10,7 @@ use Rector\Php74\Rector\Double\RealToFloatTypeCastRector;
use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
use Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector;
use Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector;
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
@ -34,6 +35,7 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
$services->set(\Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector::class);
$services->set(\Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector::class);
$services->set(\Rector\Php74\Rector\Double\RealToFloatTypeCastRector::class);

View File

@ -1,4 +1,4 @@
# 506 Rules Overview
# 507 Rules Overview
<br>
@ -66,7 +66,7 @@
- [Php73](#php73) (9)
- [Php74](#php74) (15)
- [Php74](#php74) (16)
- [Php80](#php80) (17)
@ -7837,9 +7837,28 @@ Change `filter_var()` with slash escaping to `addslashes()`
<br>
### GetCalledClassToSelfClassRector
Change `get_called_class()` to self::class on final class
- class: [`Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector`](../rules/Php74/Rector/FuncCall/GetCalledClassToSelfClassRector.php)
```diff
final class SomeClass
{
public function callOnMe()
{
- var_dump(get_called_class());
+ var_dump(self::class);
}
}
```
<br>
### GetCalledClassToStaticClassRector
Change `get_called_class()` to static::class
Change `get_called_class()` to static::class on non-final class
- class: [`Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector`](../rules/Php74/Rector/FuncCall/GetCalledClassToStaticClassRector.php)

View File

@ -0,0 +1,85 @@
<?php
declare (strict_types=1);
namespace Rector\Php74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
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://wiki.php.net/rfc/deprecations_php_7_4 (not confirmed yet)
* @see https://3v4l.org/GU9dP
* @see \Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\GetCalledClassToSelfClassRectorTest
*/
final class GetCalledClassToSelfClassRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
public function __construct(\Rector\Core\NodeAnalyzer\ClassAnalyzer $classAnalyzer)
{
$this->classAnalyzer = $classAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change get_called_class() to self::class on final class', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
public function callOnMe()
{
var_dump(get_called_class());
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
public function callOnMe()
{
var_dump(self::class);
}
}
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, 'get_called_class')) {
return null;
}
$class = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Class_::class);
if (!$class instanceof \PhpParser\Node\Stmt\Class_) {
return null;
}
if ($class->isFinal()) {
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::SELF(), 'class');
}
if ($this->classAnalyzer->isAnonymousClass($class)) {
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::SELF(), 'class');
}
return null;
}
public function provideMinPhpVersion() : int
{
return \Rector\Core\ValueObject\PhpVersionFeature::CLASSNAME_CONSTANT;
}
}

View File

@ -5,7 +5,9 @@ namespace Rector\Php74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@ -18,9 +20,18 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class GetCalledClassToStaticClassRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
public function __construct(\Rector\Core\NodeAnalyzer\ClassAnalyzer $classAnalyzer)
{
$this->classAnalyzer = $classAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change get_called_class() to static::class', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change get_called_class() to static::class on non-final class', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function callOnMe()
@ -55,7 +66,17 @@ CODE_SAMPLE
if (!$this->isName($node, 'get_called_class')) {
return null;
}
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::STATIC(), 'class');
$class = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Class_::class);
if (!$class instanceof \PhpParser\Node\Stmt\Class_) {
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::STATIC(), 'class');
}
if ($this->classAnalyzer->isAnonymousClass($class)) {
return null;
}
if (!$class->isFinal()) {
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::STATIC(), 'class');
}
return null;
}
public function provideMinPhpVersion() : int
{

View File

@ -84,15 +84,15 @@ CODE_SAMPLE
if ($class->isFinal()) {
return null;
}
if ($node->isPrivate()) {
return null;
}
if ($node->isProtected()) {
if (!$node->isPublic()) {
return null;
}
if ($node->isFinal()) {
return null;
}
if ($this->classAnalyzer->isAnonymousClass($class)) {
return null;
}
if ($this->isClassHasChildren($class)) {
return null;
}
@ -105,9 +105,6 @@ CODE_SAMPLE
}
private function isClassHasChildren(\PhpParser\Node\Stmt\Class_ $class) : bool
{
if ($this->classAnalyzer->isAnonymousClass($class)) {
return \false;
}
$className = (string) $this->nodeNameResolver->getName($class);
if (!$this->reflectionProvider->hasClass($className)) {
return \false;

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '82656ade38409a188114aebd347ff9a2b5af97ba';
public const PACKAGE_VERSION = '90bbd4e1a03b4404b3988c863fb3e46d0fa1411b';
/**
* @var string
*/
public const RELEASE_DATE = '2022-03-27 20:36:49';
public const RELEASE_DATE = '2022-03-27 20:38:09';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20220327\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 ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524::getLoader();
return ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89::getLoader();

View File

@ -2625,6 +2625,7 @@ return array(
'Rector\\Php74\\Rector\\FuncCall\\ArrayKeyExistsOnPropertyRector' => $baseDir . '/rules/Php74/Rector/FuncCall/ArrayKeyExistsOnPropertyRector.php',
'Rector\\Php74\\Rector\\FuncCall\\ArraySpreadInsteadOfArrayMergeRector' => $baseDir . '/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php',
'Rector\\Php74\\Rector\\FuncCall\\FilterVarToAddSlashesRector' => $baseDir . '/rules/Php74/Rector/FuncCall/FilterVarToAddSlashesRector.php',
'Rector\\Php74\\Rector\\FuncCall\\GetCalledClassToSelfClassRector' => $baseDir . '/rules/Php74/Rector/FuncCall/GetCalledClassToSelfClassRector.php',
'Rector\\Php74\\Rector\\FuncCall\\GetCalledClassToStaticClassRector' => $baseDir . '/rules/Php74/Rector/FuncCall/GetCalledClassToStaticClassRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => $baseDir . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
'Rector\\Php74\\Rector\\Function_\\ReservedFnFunctionRector' => $baseDir . '/rules/Php74/Rector/Function_/ReservedFnFunctionRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524
class ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89', '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\ComposerStaticInit8dff0a86436b1a661f6807b929cb0524::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit92f7be94f77045afc7362469e574fa89::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit8dff0a86436b1a661f6807b929cb0524::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit92f7be94f77045afc7362469e574fa89::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire8dff0a86436b1a661f6807b929cb0524($fileIdentifier, $file);
composerRequire92f7be94f77045afc7362469e574fa89($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524
* @param string $file
* @return void
*/
function composerRequire8dff0a86436b1a661f6807b929cb0524($fileIdentifier, $file)
function composerRequire92f7be94f77045afc7362469e574fa89($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 ComposerStaticInit8dff0a86436b1a661f6807b929cb0524
class ComposerStaticInit92f7be94f77045afc7362469e574fa89
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -2994,6 +2994,7 @@ class ComposerStaticInit8dff0a86436b1a661f6807b929cb0524
'Rector\\Php74\\Rector\\FuncCall\\ArrayKeyExistsOnPropertyRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/ArrayKeyExistsOnPropertyRector.php',
'Rector\\Php74\\Rector\\FuncCall\\ArraySpreadInsteadOfArrayMergeRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php',
'Rector\\Php74\\Rector\\FuncCall\\FilterVarToAddSlashesRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/FilterVarToAddSlashesRector.php',
'Rector\\Php74\\Rector\\FuncCall\\GetCalledClassToSelfClassRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/GetCalledClassToSelfClassRector.php',
'Rector\\Php74\\Rector\\FuncCall\\GetCalledClassToStaticClassRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/GetCalledClassToStaticClassRector.php',
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
'Rector\\Php74\\Rector\\Function_\\ReservedFnFunctionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Function_/ReservedFnFunctionRector.php',
@ -3837,9 +3838,9 @@ class ComposerStaticInit8dff0a86436b1a661f6807b929cb0524
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit8dff0a86436b1a661f6807b929cb0524::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8dff0a86436b1a661f6807b929cb0524::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8dff0a86436b1a661f6807b929cb0524::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit92f7be94f77045afc7362469e574fa89::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit92f7be94f77045afc7362469e574fa89::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit92f7be94f77045afc7362469e574fa89::$classMap;
}, null, ClassLoader::class);
}

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('RectorPrefix20220327\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524', false) && !interface_exists('ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524', false) && !trait_exists('ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524', false)) {
spl_autoload_call('RectorPrefix20220327\ComposerAutoloaderInit8dff0a86436b1a661f6807b929cb0524');
if (!class_exists('ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89', false) && !interface_exists('ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89', false) && !trait_exists('ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89', false)) {
spl_autoload_call('RectorPrefix20220327\ComposerAutoloaderInit92f7be94f77045afc7362469e574fa89');
}
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('RectorPrefix20220327\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220327\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire8dff0a86436b1a661f6807b929cb0524')) {
function composerRequire8dff0a86436b1a661f6807b929cb0524() {
return \RectorPrefix20220327\composerRequire8dff0a86436b1a661f6807b929cb0524(...func_get_args());
if (!function_exists('composerRequire92f7be94f77045afc7362469e574fa89')) {
function composerRequire92f7be94f77045afc7362469e574fa89() {
return \RectorPrefix20220327\composerRequire92f7be94f77045afc7362469e574fa89(...func_get_args());
}
}
if (!function_exists('scanPath')) {