Skip to content

Commit

Permalink
Updated Rector to commit a0f2f32c7970d925681fc4cec64efa1ee90b6ea8
Browse files Browse the repository at this point in the history
rectorphp/rector-src@a0f2f32 [VendorLocker] Remove AstResolver usage on ClassMethodReturnTypeOverrideGuard (#5116)
  • Loading branch information
TomasVotruba committed Oct 5, 2023
1 parent 1c164a6 commit a40e240
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,21 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\NodeAnalyzer\MagicClassMethodAnalyzer;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
final class ClassMethodReturnTypeOverrideGuard
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
/**
* @readonly
* @var \Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer
*/
private $familyRelationsAnalyzer;
/**
* @readonly
* @var \Rector\Core\PhpParser\AstResolver
*/
private $astResolver;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
Expand All @@ -67,16 +49,9 @@ final class ClassMethodReturnTypeOverrideGuard
* @var \Rector\Core\NodeAnalyzer\MagicClassMethodAnalyzer
*/
private $magicClassMethodAnalyzer;
/**
* @var array<class-string, array<string>>
*/
private const CHAOTIC_CLASS_METHOD_NAMES = ['PhpParser\\NodeVisitor' => ['enterNode', 'leaveNode', 'beforeTraverse', 'afterTraverse']];
public function __construct(NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider, FamilyRelationsAnalyzer $familyRelationsAnalyzer, AstResolver $astResolver, ReflectionResolver $reflectionResolver, ReturnTypeInferer $returnTypeInferer, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, FilePathHelper $filePathHelper, MagicClassMethodAnalyzer $magicClassMethodAnalyzer)
public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReflectionResolver $reflectionResolver, ReturnTypeInferer $returnTypeInferer, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, FilePathHelper $filePathHelper, MagicClassMethodAnalyzer $magicClassMethodAnalyzer)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->reflectionProvider = $reflectionProvider;
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
$this->astResolver = $astResolver;
$this->reflectionResolver = $reflectionResolver;
$this->returnTypeInferer = $returnTypeInferer;
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
Expand All @@ -85,14 +60,9 @@ public function __construct(NodeNameResolver $nodeNameResolver, ReflectionProvid
}
public function shouldSkipClassMethod(ClassMethod $classMethod, Scope $scope) : bool
{
// 1. skip magic methods
if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) {
return \true;
}
// 2. skip chaotic contract class methods
if ($this->shouldSkipChaoticClassMethods($classMethod)) {
return \true;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (!$classReflection instanceof ClassReflection) {
return \true;
Expand All @@ -116,7 +86,8 @@ public function shouldSkipClassMethod(ClassMethod $classMethod, Scope $scope) :
if ($classMethod->returnType instanceof Node) {
return \true;
}
return $this->shouldSkipHasChildHasReturnType($childrenClassReflections, $classMethod);
$returnType = $this->returnTypeInferer->inferFunctionLike($classMethod);
return !$returnType->isVoid()->yes();
}
private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scope) : bool
{
Expand Down Expand Up @@ -158,52 +129,4 @@ private function isReturnTypeChangeAllowed(ClassMethod $classMethod, Scope $scop
$isParentInVendor = \strpos($normalizedFileName, '/vendor/') !== \false;
return $isCurrentInVendor && $isParentInVendor || !$isCurrentInVendor && !$isParentInVendor;
}
/**
* @param ClassReflection[] $childrenClassReflections
*/
private function shouldSkipHasChildHasReturnType(array $childrenClassReflections, ClassMethod $classMethod) : bool
{
$returnType = $this->returnTypeInferer->inferFunctionLike($classMethod);
if (!$returnType->isVoid()->yes()) {
return \true;
}
$methodName = $this->nodeNameResolver->getName($classMethod);
foreach ($childrenClassReflections as $childClassReflection) {
if (!$childClassReflection->hasNativeMethod($methodName)) {
continue;
}
$methodReflection = $childClassReflection->getNativeMethod($methodName);
$method = $this->astResolver->resolveClassMethodFromMethodReflection($methodReflection);
if (!$method instanceof ClassMethod) {
continue;
}
if ($method->returnType instanceof Node) {
return \true;
}
$childReturnType = $this->returnTypeInferer->inferFunctionLike($method);
if ($childReturnType->isVoid()->yes()) {
continue;
}
return \true;
}
return \false;
}
private function shouldSkipChaoticClassMethods(ClassMethod $classMethod) : bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (!$classReflection instanceof ClassReflection) {
return \true;
}
foreach (self::CHAOTIC_CLASS_METHOD_NAMES as $chaoticClass => $chaoticMethodNames) {
if (!$this->reflectionProvider->hasClass($chaoticClass)) {
continue;
}
$chaoticClassReflection = $this->reflectionProvider->getClass($chaoticClass);
if (!$classReflection->isSubclassOf($chaoticClassReflection->getName())) {
continue;
}
return $this->nodeNameResolver->isNames($classMethod, $chaoticMethodNames);
}
return \false;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '8cab945ae45b651cdf66c8bfbb1603c514733896';
public const PACKAGE_VERSION = 'a0f2f32c7970d925681fc4cec64efa1ee90b6ea8';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-10-04 15:11:16';
public const RELEASE_DATE = '2023-10-05 12:35:58';
/**
* @var int
*/
Expand Down

0 comments on commit a40e240

Please sign in to comment.