Skip to content

Commit

Permalink
[NodeTypeResolver] Remove deprecated NodeTypeResolver getStaticType m…
Browse files Browse the repository at this point in the history
…ethod (#1230)

* [NodeTypeResolver] Remove NodeTypeResolver getStaticType method

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 14, 2021
1 parent 4b3a51c commit 8046b81
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 115 deletions.
106 changes: 0 additions & 106 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand All @@ -48,10 +43,8 @@
use Rector\NodeTypeResolver\NodeTypeCorrector\GenericClassStringTypeCorrector;
use Rector\NodeTypeResolver\NodeTypeCorrector\HasOffsetTypeCorrector;
use Rector\NodeTypeResolver\NodeTypeResolver\IdentifierTypeResolver;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier;
use Symfony\Contracts\Service\Attribute\Required;

final class NodeTypeResolver
{
Expand All @@ -60,8 +53,6 @@ final class NodeTypeResolver
*/
private array $nodeTypeResolvers = [];

private ArrayTypeAnalyzer $arrayTypeAnalyzer;

/**
* @param NodeTypeResolverInterface[] $nodeTypeResolvers
*/
Expand All @@ -82,14 +73,6 @@ public function __construct(
}
}

// Prevents circular dependency

#[Required]
public function autowireNodeTypeResolver(ArrayTypeAnalyzer $arrayTypeAnalyzer): void
{
$this->arrayTypeAnalyzer = $arrayTypeAnalyzer;
}

/**
* @param ObjectType[] $requiredTypes
*/
Expand Down Expand Up @@ -244,50 +227,6 @@ public function getNativeType(Expr $expr): Type
return $scope->getNativeType($expr);
}

/**
* @deprecated
* @see Use NodeTypeResolver::getType() instead
*/
public function getStaticType(Node $node): Type
{
$errorMessage = sprintf('Method "%s" is deprecated. Use "getType()" instead', __METHOD__);
trigger_error($errorMessage, E_USER_WARNING);
sleep(3);

if ($node instanceof Param || $node instanceof New_ || $node instanceof Return_) {
return $this->getType($node);
}

if (! $node instanceof Expr) {
return new MixedType();
}

if ($this->arrayTypeAnalyzer->isArrayType($node)) {
return $this->resolveArrayType($node);
}

if ($node instanceof Scalar) {
return $this->getType($node);
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return new MixedType();
}

$staticType = $scope->getType($node);
if ($staticType instanceof GenericObjectType) {
return $staticType;
}

if ($staticType instanceof ObjectType) {
$scope = $node->getAttribute(AttributeKey::SCOPE);
return $this->objectTypeSpecifier->narrowToFullyQualifiedOrAliasedObjectType($node, $staticType, $scope);
}

return $this->accessoryNonEmptyStringTypeCorrector->correct($staticType);
}

public function isNumberType(Node $node): bool
{
$nodeType = $this->getType($node);
Expand Down Expand Up @@ -373,20 +312,6 @@ private function isMatchingUnionType(Type $resolvedType, ObjectType $requiredObj
->yes();
}

private function resolveArrayType(Expr $expr): Type
{
/** @var Scope|null $scope */
$scope = $expr->getAttribute(AttributeKey::SCOPE);

if ($scope instanceof Scope) {
$arrayType = $scope->getType($expr);
$arrayType = $this->genericClassStringTypeCorrector->correct($arrayType);
return $this->removeNonEmptyArrayFromIntersectionWithArrayType($arrayType);
}

return new ArrayType(new MixedType(), new MixedType());
}

private function resolveByNodeTypeResolvers(Node $node): ?Type
{
foreach ($this->nodeTypeResolvers as $nodeClass => $nodeTypeResolver) {
Expand All @@ -400,37 +325,6 @@ private function resolveByNodeTypeResolvers(Node $node): ?Type
return null;
}

private function removeNonEmptyArrayFromIntersectionWithArrayType(Type $type): Type
{
if (! $type instanceof IntersectionType) {
return $type;
}

if (count($type->getTypes()) !== 2) {
return $type;
}

if (! $type->isSubTypeOf(new NonEmptyArrayType())->yes()) {
return $type;
}

$otherType = null;
foreach ($type->getTypes() as $intersectionedType) {
if ($intersectionedType instanceof NonEmptyArrayType) {
continue;
}

$otherType = $intersectionedType;
break;
}

if ($otherType === null) {
return $type;
}

return $otherType;
}

private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, ObjectType $requiredObjectType): bool
{
if ($resolvedObjectType->isInstanceOf($requiredObjectType->getClassName())->yes()) {
Expand Down
9 changes: 0 additions & 9 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,6 @@ protected function getObjectType(Node $node): Type
return $this->nodeTypeResolver->getType($node);
}

/**
* @deprecated
* Use @see AbstractRector::getType() instead, as single method to get types
*/
protected function getStaticType(Node $node): Type
{
return $this->nodeTypeResolver->getType($node);
}

/**
* @param Node|Node[] $nodes
*/
Expand Down

0 comments on commit 8046b81

Please sign in to comment.