Skip to content

Commit

Permalink
Extract common is_subclass_of and is_a logic
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Feb 28, 2022
1 parent 7ff5dfd commit 4fbf5a8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 82 deletions.
3 changes: 3 additions & 0 deletions conf/config.neon
Expand Up @@ -1438,6 +1438,9 @@ services:
tags:
- phpstan.typeSpecifier.functionTypeSpecifyingExtension

-
class: PHPStan\Type\Php\IsAFunctionTypeSpecifyingHelper

-
class: PHPStan\Type\Php\ArrayIsListFunctionTypeSpecifyingExtension
tags:
Expand Down
48 changes: 7 additions & 41 deletions src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
Expand Up @@ -9,18 +9,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use function count;
use function strtolower;

Expand All @@ -29,6 +20,12 @@ class IsAFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtens

private TypeSpecifier $typeSpecifier;

public function __construct(
private IsAFunctionTypeSpecifyingHelper $isAFunctionTypeSpecifyingHelper,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return strtolower($functionReflection->getName()) === 'is_a'
Expand All @@ -48,40 +45,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
return new SpecifiedTypes([], []);
}

$type = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use ($allowString): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if ($allowString) {
return TypeCombinator::union(
new ObjectType($type->getValue()),
new GenericClassStringType(new ObjectType($type->getValue())),
);
}
return new ObjectType($type->getValue());
}
if ($type instanceof GenericClassStringType) {
if ($allowString) {
return TypeCombinator::union(
$type->getGenericType(),
$type,
);
}
return $type->getGenericType();
}
if ($allowString) {
return TypeCombinator::union(
new ObjectWithoutClassType(),
new ClassStringType(),
);
}
return new ObjectWithoutClassType();
});

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$type,
$this->isAFunctionTypeSpecifyingHelper->determineType($classType, $allowString),
$context,
false,
$scope,
Expand Down
62 changes: 62 additions & 0 deletions src/Type/Php/IsAFunctionTypeSpecifyingHelper.php
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;

final class IsAFunctionTypeSpecifyingHelper
{

public function determineType(
Type $classType,
bool $allowString,
): Type
{
return TypeTraverser::map(
$classType,
static function (Type $type, callable $traverse) use ($allowString): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if ($allowString) {
return TypeCombinator::union(
new ObjectType($type->getValue()),
new GenericClassStringType(new ObjectType($type->getValue())),
);
}

return new ObjectType($type->getValue());
}
if ($type instanceof GenericClassStringType) {
if ($allowString) {
return TypeCombinator::union(
$type->getGenericType(),
$type,
);
}

return $type->getGenericType();
}
if ($allowString) {
return TypeCombinator::union(
new ObjectWithoutClassType(),
new ClassStringType(),
);
}

return new ObjectWithoutClassType();
},
);
}

}
48 changes: 7 additions & 41 deletions src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php
Expand Up @@ -9,18 +9,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use function count;
use function strtolower;

Expand All @@ -29,6 +20,12 @@ class IsSubclassOfFunctionTypeSpecifyingExtension implements FunctionTypeSpecify

private TypeSpecifier $typeSpecifier;

public function __construct(
private IsAFunctionTypeSpecifyingHelper $isAFunctionTypeSpecifyingHelper,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return strtolower($functionReflection->getName()) === 'is_subclass_of'
Expand All @@ -48,40 +45,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
return new SpecifiedTypes([], []);
}

$type = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use ($allowString): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if ($allowString) {
return TypeCombinator::union(
new ObjectType($type->getValue()),
new GenericClassStringType(new ObjectType($type->getValue())),
);
}
return new ObjectType($type->getValue());
}
if ($type instanceof GenericClassStringType) {
if ($allowString) {
return TypeCombinator::union(
$type->getGenericType(),
$type,
);
}
return $type->getGenericType();
}
if ($allowString) {
return TypeCombinator::union(
new ObjectWithoutClassType(),
new ClassStringType(),
);
}
return new ObjectWithoutClassType();
});

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$type,
$this->isAFunctionTypeSpecifyingHelper->determineType($classType, $allowString),
$context,
false,
$scope,
Expand Down

0 comments on commit 4fbf5a8

Please sign in to comment.