Skip to content

Commit

Permalink
[StaticTypeMapper] Reduce instanceof TypeWithClassName usage via Clas…
Browse files Browse the repository at this point in the history
…sNameFromObjectTypeResolver (rectorphp#6421)

* remove instanceof TypeWithClassName on InlineIsAInstanceOfRector

* remove instanceof TypeWithClassName on LocalMethodCallFinder

* remove instanceof TypeWithClassName on IssetOnPropertyObjectToPropertyExistsRector

* remove instanceof TypeWithClassName on AstResolver

* remove instanceof TypeWithClassName on PropertyAnalyzer

* remove instanceof TypeWithClassName on UnitializedPropertyAnalyzer

* remove instanceof TypeWithClassName on CallCollectionAnalyzer

* remove instanceof TypeWithClassName on BreakingVariableRenameGuard

* remove instanceof TypeWithClassName on DateTimeAtNamingConventionGuard

* remove instanceof TypeWithClassName on ArrayCallableToMethodCallFactory

* remove instanceof TypeWithClassName on TypeUnwrapper

* remove instanceof TypeWithClassName on ReflectionResolver

* remove instanceof TypeWithClassName on ThrowWithPreviousExceptionRector

* remove instanceof TypeWithClassName on PropertyNaming

* remove instanceof TypeWithClassName on TypeProvidingExprFromClassResolver

* remove instanceof TypeWithClassName on ValueResolver

* remove instanceof TypeWithClassName on PropertyFetchFinder

* remove instanceof TypeWithClassName on ExactCompareFactory

* remove instanceof TypeWithClassName on CallTypesResolver

* remove instanceof TypeWithClassName on PropertyPresenceChecker

* [ci-review] Rector Rectify

* remove instanceof TypeWithClassName on SplArrayFixedTypeNarrower

* Fix phpstan

* Fix phpstan

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Nov 12, 2024
1 parent bcfb598 commit 84bb596
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -206,7 +206,9 @@ private function resolveExceptionArgumentPosition(Name $exceptionName): ?int

foreach ($parametersAcceptorWithPhpDocs->getParameters() as $position => $parameterReflectionWithPhpDoc) {
$parameterType = $parameterReflectionWithPhpDoc->getType();
if (! $parameterType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($parameterReflectionWithPhpDoc->getType());

if ($className === null) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\TypeWithClassName;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -106,11 +106,7 @@ private function resolveClassName(Expr $expr): ?string
$type = $type->getGenericType();
}

if (! $type instanceof TypeWithClassName) {
return null;
}

return $type->getClassName();
return ClassNameFromObjectTypeResolver::resolve($type);
}

private function isFirstObjectType(Expr $expr): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -196,18 +196,20 @@ private function createIdenticalToNull(PropertyFetch $propertyFetch): Identical
private function matchPropertyTypeClassReflection(PropertyFetch $propertyFetch): ?ClassReflection
{
$propertyFetchVarType = $this->getType($propertyFetch->var);
if (! $propertyFetchVarType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($propertyFetchVarType);

if ($className === null) {
return null;
}

if ($propertyFetchVarType->getClassName() === 'stdClass') {
if ($className === 'stdClass') {
return null;
}

if (! $this->reflectionProvider->hasClass($propertyFetchVarType->getClassName())) {
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}

return $this->reflectionProvider->getClass($propertyFetchVarType->getClassName());
return $this->reflectionProvider->getClass($className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

final readonly class ArrayCallableToMethodCallFactory
{
Expand Down Expand Up @@ -46,7 +46,9 @@ public function create(Array_ $array): ?MethodCall
}

$firstItemType = $this->nodeTypeResolver->getType($firstItem->value);
if (! $firstItemType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($firstItemType);

if ($className === null) {
return null;
}

Expand Down
7 changes: 4 additions & 3 deletions rules/DeadCode/NodeAnalyzer/CallCollectionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeWithClassName;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

final readonly class CallCollectionAnalyzer
{
Expand All @@ -33,7 +33,8 @@ public function isExists(array $calls, string $classMethodName, string $classNam
$callerRoot = $call instanceof StaticCall ? $call->class : $call->var;
$callerType = $this->nodeTypeResolver->getType($callerRoot);

if (! $callerType instanceof TypeWithClassName) {
$callerTypeClasName = ClassNameFromObjectTypeResolver::resolve($callerType);
if ($callerTypeClasName === null) {
// handle fluent by $this->bar()->baz()->qux()
// that methods don't have return type
if ($callerType instanceof MixedType && ! $callerType->isExplicitMixed()) {
Expand Down Expand Up @@ -66,7 +67,7 @@ public function isExists(array $calls, string $classMethodName, string $classNam
return true;
}

if ($callerType->getClassName() !== $className) {
if ($callerTypeClasName !== $className) {
continue;
}

Expand Down
8 changes: 5 additions & 3 deletions rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\Naming\Naming\ConflictingNameResolver;
use Rector\Naming\Naming\OverridenExistingNamesResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\Util\StringUtils;

/**
Expand Down Expand Up @@ -190,11 +190,13 @@ private function isDateTimeAtNamingConvention(Param $param): bool
{
$type = $this->nodeTypeResolver->getType($param);
$type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type);
if (! $type instanceof TypeWithClassName) {

$className = ClassNameFromObjectTypeResolver::resolve($type);
if ($className === null) {
return false;
}

if (! is_a($type->getClassName(), DateTimeInterface::class, true)) {
if (! is_a($className, DateTimeInterface::class, true)) {
return false;
}

Expand Down
7 changes: 4 additions & 3 deletions rules/Naming/Guard/DateTimeAtNamingConventionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Rector\Naming\Guard;

use DateTimeInterface;
use PHPStan\Type\TypeWithClassName;
use Rector\Naming\ValueObject\PropertyRename;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\Util\StringUtils;

final readonly class DateTimeAtNamingConventionGuard
Expand All @@ -24,11 +24,12 @@ public function isConflicting(PropertyRename $propertyRename): bool
$type = $this->nodeTypeResolver->getType($propertyRename->getProperty());
$type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type);

if (! $type instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($type);
if ($className === null) {
return false;
}

if (! is_a($type->getClassName(), DateTimeInterface::class, true)) {
if (! is_a($className, DateTimeInterface::class, true)) {
return false;
}

Expand Down
14 changes: 10 additions & 4 deletions rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Naming\RectorNamingInflector;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
use Rector\Util\StringUtils;
Expand Down Expand Up @@ -278,7 +279,9 @@ private function normalizeShortClassName(string $shortClassName): string
private function resolveClassNameFromType(Type $type): ?string
{
$type = TypeCombinator::removeNull($type);
if (! $type instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($type);

if ($className === null) {
return null;
}

Expand All @@ -295,8 +298,11 @@ private function resolveClassNameFromType(Type $type): ?string
return null;
}

return $type instanceof AliasedObjectType
? $type->getClassName()
: $this->nodeTypeResolver->getFullyQualifiedClassName($type);
if ($type instanceof AliasedObjectType) {
return $className;
}

/** @var TypeWithClassName $type */
return $this->nodeTypeResolver->getFullyQualifiedClassName($type);
}
}
6 changes: 3 additions & 3 deletions rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\AstResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;

final readonly class UnitializedPropertyAnalyzer
Expand All @@ -40,11 +40,11 @@ public function isUnitialized(Expr $expr): bool
$varType = $varType->getStaticObjectType();
}

if (! $varType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($varType);
if ($className === null) {
return false;
}

$className = $varType->getClassName();
$classLike = $this->astResolver->resolveClassFromName($className);

if (! $classLike instanceof ClassLike) {
Expand Down
12 changes: 7 additions & 5 deletions rules/Strict/NodeFactory/ExactCompareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\PhpParser\Node\NodeFactory;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

final readonly class ExactCompareFactory
{
Expand Down Expand Up @@ -123,8 +123,9 @@ private function createFromUnionType(
return new Identical($expr, $this->nodeFactory->createTrue());
}

if ($unionType instanceof TypeWithClassName) {
return new Instanceof_($expr, new FullyQualified($unionType->getClassName()));
$className = ClassNameFromObjectTypeResolver::resolve($unionType);
if ($className !== null) {
return new Instanceof_($expr, new FullyQualified($className));
}

$nullConstFetch = $this->nodeFactory->createNull();
Expand Down Expand Up @@ -250,8 +251,9 @@ private function createTruthyFromUnionType(
return new NotIdentical($expr, $this->nodeFactory->createTrue());
}

if ($unionType instanceof TypeWithClassName) {
return new BooleanNot(new Instanceof_($expr, new FullyQualified($unionType->getClassName())));
$className = ClassNameFromObjectTypeResolver::resolve($unionType);
if ($className !== null) {
return new BooleanNot(new Instanceof_($expr, new FullyQualified($className)));
}

$toNullIdentical = new Identical($expr, $this->nodeFactory->createNull());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
use Rector\ValueObject\MethodName;

final readonly class TypeProvidingExprFromClassResolver
Expand Down Expand Up @@ -121,8 +121,9 @@ private function isMatchingType(Type $readableType, ObjectType $objectType): boo
}

$readableType = TypeCombinator::removeNull($readableType);
$className = ClassNameFromObjectTypeResolver::resolve($readableType);

if (! $readableType instanceof TypeWithClassName) {
if ($className === null) {
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

final readonly class CallTypesResolver
{
Expand Down Expand Up @@ -124,11 +124,10 @@ private function narrowParentObjectTreeToSingleObjectChildType(Type $type): Type
return $type;
}

/** @var TypeWithClassName $firstUnionedType */
$firstUnionedType = $type->getTypes()[0];

foreach ($type->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($unionedType);
if ($className === null) {
return $type;
}

Expand All @@ -143,7 +142,8 @@ private function narrowParentObjectTreeToSingleObjectChildType(Type $type): Type
private function isTypeWithClassNameOnly(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($unionedType);
if ($className === null) {
return false;
}
}
Expand Down
11 changes: 6 additions & 5 deletions rules/TypeDeclaration/TypeInferer/SplArrayFixedTypeNarrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;

final class SplArrayFixedTypeNarrower
{
Expand All @@ -17,7 +17,8 @@ public function narrow(Type $paramType): Type
return $paramType;
}

if (! $paramType instanceof TypeWithClassName) {
$className = ClassNameFromObjectTypeResolver::resolve($paramType);
if ($className === null) {
return $paramType;
}

Expand All @@ -27,18 +28,18 @@ public function narrow(Type $paramType): Type

$types = [];

if ($paramType->getClassName() === 'PhpCsFixer\Tokenizer\Tokens') {
if ($className === 'PhpCsFixer\Tokenizer\Tokens') {
$types[] = new ObjectType('PhpCsFixer\Tokenizer\Token');
}

if ($paramType->getClassName() === 'PhpCsFixer\Doctrine\Annotation\Tokens') {
if ($className === 'PhpCsFixer\Doctrine\Annotation\Tokens') {
$types[] = new ObjectType('PhpCsFixer\Doctrine\Annotation\Token');
}

if ($types === []) {
return $paramType;
}

return new GenericObjectType($paramType->getClassName(), $types);
return new GenericObjectType($className, $types);
}
}
Loading

0 comments on commit 84bb596

Please sign in to comment.