mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit b91f35ff6e833e2abb6591044309984549f0b539
b91f35ff6e
Fix FinalizeClassesWithoutChildrenRector for changed class, use more reliable reflection to detect Doctrine attributes (#3276)
This commit is contained in:
parent
4372e75c82
commit
f0db3872cf
@ -4,10 +4,10 @@ declare (strict_types=1);
|
||||
namespace Rector\Privatization\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Core\NodeAnalyzer\DoctrineEntityAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
|
||||
@ -19,14 +19,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
*/
|
||||
final class FinalizeClassesWithoutChildrenRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const DOCTRINE_ORM_MAPPING_ANNOTATION = ['Doctrine\\ORM\\Mapping\\Entity', 'Doctrine\\ORM\\Mapping\\Embeddable'];
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const DOCTRINE_ODM_MAPPING_ANNOTATION = ['Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\Document', 'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\EmbeddedDocument'];
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
|
||||
@ -47,12 +39,18 @@ final class FinalizeClassesWithoutChildrenRector extends AbstractRector
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(ClassAnalyzer $classAnalyzer, FamilyRelationsAnalyzer $familyRelationsAnalyzer, VisibilityManipulator $visibilityManipulator, ReflectionResolver $reflectionResolver)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\DoctrineEntityAnalyzer
|
||||
*/
|
||||
private $doctrineEntityAnalyzer;
|
||||
public function __construct(ClassAnalyzer $classAnalyzer, FamilyRelationsAnalyzer $familyRelationsAnalyzer, VisibilityManipulator $visibilityManipulator, ReflectionResolver $reflectionResolver, DoctrineEntityAnalyzer $doctrineEntityAnalyzer)
|
||||
{
|
||||
$this->classAnalyzer = $classAnalyzer;
|
||||
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
|
||||
$this->visibilityManipulator = $visibilityManipulator;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->doctrineEntityAnalyzer = $doctrineEntityAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -99,45 +97,23 @@ CODE_SAMPLE
|
||||
if ($this->shouldSkipClass($node)) {
|
||||
return null;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
if ($phpDocInfo->hasByAnnotationClasses(self::DOCTRINE_ORM_MAPPING_ANNOTATION)) {
|
||||
return null;
|
||||
}
|
||||
if ($phpDocInfo->hasByAnnotationClasses(self::DOCTRINE_ODM_MAPPING_ANNOTATION)) {
|
||||
if ($this->doctrineEntityAnalyzer->hasClassAnnotation($node)) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
if ($this->doctrineEntityAnalyzer->hasClassReflectionAttribute($classReflection)) {
|
||||
return null;
|
||||
}
|
||||
$childrenClassReflections = $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection);
|
||||
if ($childrenClassReflections !== []) {
|
||||
return null;
|
||||
}
|
||||
if ($this->hasDoctrineAttr($node)) {
|
||||
return null;
|
||||
}
|
||||
$this->visibilityManipulator->makeFinal($node);
|
||||
return $node;
|
||||
}
|
||||
private function hasDoctrineAttr(Class_ $class) : bool
|
||||
{
|
||||
foreach ($class->attrGroups as $attrGroup) {
|
||||
foreach ($attrGroup->attrs as $attribute) {
|
||||
if (!$attribute->name instanceof FullyQualified) {
|
||||
continue;
|
||||
}
|
||||
$className = $this->nodeNameResolver->getName($attribute->name);
|
||||
if (\in_array($className, self::DOCTRINE_ORM_MAPPING_ANNOTATION, \true)) {
|
||||
return \true;
|
||||
}
|
||||
if (\in_array($className, self::DOCTRINE_ODM_MAPPING_ANNOTATION, \true)) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function shouldSkipClass(Class_ $class) : bool
|
||||
{
|
||||
if ($class->isFinal()) {
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'fbf9506c5de7389260e8a842c911b3315b053df3';
|
||||
public const PACKAGE_VERSION = 'b91f35ff6e833e2abb6591044309984549f0b539';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-01-13 15:10:38';
|
||||
public const RELEASE_DATE = '2023-01-13 15:32:52';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
50
src/NodeAnalyzer/DoctrineEntityAnalyzer.php
Normal file
50
src/NodeAnalyzer/DoctrineEntityAnalyzer.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
final class DoctrineEntityAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const DOCTRINE_MAPPING_CLASSES = ['Doctrine\\ORM\\Mapping\\Entity', 'Doctrine\\ORM\\Mapping\\Embeddable', 'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\Document', 'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\EmbeddedDocument'];
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
public function __construct(PhpDocInfoFactory $phpDocInfoFactory)
|
||||
{
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
}
|
||||
public function hasClassAnnotation(Class_ $class) : bool
|
||||
{
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
|
||||
if (!$phpDocInfo instanceof PhpDocInfo) {
|
||||
return \false;
|
||||
}
|
||||
return $phpDocInfo->hasByAnnotationClasses(self::DOCTRINE_MAPPING_CLASSES);
|
||||
}
|
||||
public function hasClassReflectionAttribute(ClassReflection $classReflection) : bool
|
||||
{
|
||||
/** @var ReflectionClass $nativeReflectionClass */
|
||||
$nativeReflectionClass = $classReflection->getNativeReflection();
|
||||
// skip early in case of no attributes at all
|
||||
if ((\method_exists($nativeReflectionClass, 'getAttributes') ? $nativeReflectionClass->getAttributes() : []) === []) {
|
||||
return \false;
|
||||
}
|
||||
foreach (self::DOCTRINE_MAPPING_CLASSES as $doctrineMappingClass) {
|
||||
// skip entities
|
||||
if ((\method_exists($nativeReflectionClass, 'getAttributes') ? $nativeReflectionClass->getAttributes($doctrineMappingClass) : []) !== []) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitd2d59cdc3af9e2a300f86669d6294375::getLoader();
|
||||
return ComposerAutoloaderInitb28dbaf8f7232df85b184294fd29ad82::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1418,6 +1418,7 @@ return array(
|
||||
'Rector\\Core\\NodeAnalyzer\\CoalesceAnalyzer' => $baseDir . '/src/NodeAnalyzer/CoalesceAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => $baseDir . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => $baseDir . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\DoctrineEntityAnalyzer' => $baseDir . '/src/NodeAnalyzer/DoctrineEntityAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => $baseDir . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ExprAnalyzer' => $baseDir . '/src/NodeAnalyzer/ExprAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\InlineHTMLAnalyzer' => $baseDir . '/src/NodeAnalyzer/InlineHTMLAnalyzer.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitd2d59cdc3af9e2a300f86669d6294375
|
||||
class ComposerAutoloaderInitb28dbaf8f7232df85b184294fd29ad82
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitd2d59cdc3af9e2a300f86669d6294375
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitd2d59cdc3af9e2a300f86669d6294375', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb28dbaf8f7232df85b184294fd29ad82', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitd2d59cdc3af9e2a300f86669d6294375', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb28dbaf8f7232df85b184294fd29ad82', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82::$files;
|
||||
$requireFile = static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375
|
||||
class ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -1663,6 +1663,7 @@ class ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375
|
||||
'Rector\\Core\\NodeAnalyzer\\CoalesceAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/CoalesceAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\CompactFuncCallAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/CompactFuncCallAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ConstFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ConstFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\DoctrineEntityAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/DoctrineEntityAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\EnumAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/EnumAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ExprAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ExprAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\InlineHTMLAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/InlineHTMLAnalyzer.php',
|
||||
@ -3064,9 +3065,9 @@ class ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitd2d59cdc3af9e2a300f86669d6294375::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb28dbaf8f7232df85b184294fd29ad82::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user