Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 198 additions & 1 deletion src/Internal/Checker/ConditionalChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@

namespace TypePHP\Internal\Checker;

use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeUnsealedTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use ReflectionClass;
use Throwable;
use TypePHP\Internal\Generics\TemplateManager;
Expand All @@ -21,7 +33,84 @@
final class ConditionalChecker
{
/**
* Recursively resolves multi-branch nested conditional types ($param is Target ? A : B or T is Target ? A : B).
* Checks whether a TypeNode AST contains any conditional types.
*/
public static function containsConditional(TypeNode $node): bool
{
if ($node instanceof ConditionalTypeNode || $node instanceof ConditionalTypeForParameterNode) {
return true;
}

if ($node instanceof NullableTypeNode || $node instanceof ArrayTypeNode) {
return self::containsConditional($node->type);
}

if ($node instanceof GenericTypeNode) {
if (self::containsConditional($node->type)) {
return true;
}
foreach ($node->genericTypes as $gt) {
if (self::containsConditional($gt)) {
return true;
}
}

return false;
}

if ($node instanceof UnionTypeNode || $node instanceof IntersectionTypeNode) {
foreach ($node->types as $t) {
if (self::containsConditional($t)) {
return true;
}
}

return false;
}

if ($node instanceof ArrayShapeNode) {
foreach ($node->items as $item) {
if (self::containsConditional($item->valueType)) {
return true;
}
}
if ($node->unsealedType !== null) {
return self::containsConditional($node->unsealedType->valueType)
|| ($node->unsealedType->keyType !== null && self::containsConditional($node->unsealedType->keyType));
}

return false;
}

if ($node instanceof ObjectShapeNode) {
foreach ($node->items as $item) {
if (self::containsConditional($item->valueType)) {
return true;
}
}

return false;
}

if ($node instanceof CallableTypeNode) {
foreach ($node->parameters as $p) {
if (self::containsConditional($p->type)) {
return true;
}
}

return self::containsConditional($node->returnType);
}

if ($node instanceof OffsetAccessTypeNode) {
return self::containsConditional($node->type) || self::containsConditional($node->offset);
}

return false;
}

/**
* Recursively resolves multi-branch and nested conditional types anywhere in a TypeNode AST.
*
* @param array<int|string, mixed> $vars
* @param array<string, TypeNode> $boundTemplates
Expand All @@ -41,6 +130,114 @@ public static function resolve(
return self::resolveTemplateConditional($typeNode, $vars, $boundTemplates, $registry, $function);
}

if (! self::containsConditional($typeNode)) {
return $typeNode;
}

if ($typeNode instanceof NullableTypeNode) {
return new NullableTypeNode(self::resolve($typeNode->type, $vars, $boundTemplates, $registry, $function));
}

if ($typeNode instanceof ArrayTypeNode) {
return new ArrayTypeNode(self::resolve($typeNode->type, $vars, $boundTemplates, $registry, $function));
}

if ($typeNode instanceof GenericTypeNode) {
$genericType = self::resolve($typeNode->type, $vars, $boundTemplates, $registry, $function);
$genericTypes = array_map(
fn ($t) => self::resolve($t, $vars, $boundTemplates, $registry, $function),
$typeNode->genericTypes
);

return new GenericTypeNode(
$genericType instanceof IdentifierTypeNode ? $genericType : $typeNode->type,
$genericTypes,
$typeNode->variances
);
}

if ($typeNode instanceof UnionTypeNode) {
return new UnionTypeNode(array_map(
fn ($t) => self::resolve($t, $vars, $boundTemplates, $registry, $function),
$typeNode->types
));
}

if ($typeNode instanceof IntersectionTypeNode) {
return new IntersectionTypeNode(array_map(
fn ($t) => self::resolve($t, $vars, $boundTemplates, $registry, $function),
$typeNode->types
));
}

if ($typeNode instanceof ArrayShapeNode) {
$newItems = [];
foreach ($typeNode->items as $item) {
$newItems[] = new ArrayShapeItemNode(
$item->keyName,
$item->optional,
self::resolve($item->valueType, $vars, $boundTemplates, $registry, $function)
);
}

$newUnsealed = null;
if ($typeNode->unsealedType !== null) {
$unsealedKey = $typeNode->unsealedType->keyType !== null
? self::resolve($typeNode->unsealedType->keyType, $vars, $boundTemplates, $registry, $function)
: null;
$unsealedVal = self::resolve($typeNode->unsealedType->valueType, $vars, $boundTemplates, $registry, $function);
$newUnsealed = new ArrayShapeUnsealedTypeNode($unsealedVal, $unsealedKey);
}

if ($typeNode->sealed) {
return ArrayShapeNode::createSealed($newItems, $typeNode->kind);
}

return ArrayShapeNode::createUnsealed($newItems, $newUnsealed, $typeNode->kind);
}

if ($typeNode instanceof ObjectShapeNode) {
$newItems = [];
foreach ($typeNode->items as $item) {
$newItems[] = new ObjectShapeItemNode(
$item->keyName,
$item->optional,
self::resolve($item->valueType, $vars, $boundTemplates, $registry, $function)
);
}

return new ObjectShapeNode($newItems);
}

if ($typeNode instanceof CallableTypeNode) {
$parameters = array_map(
fn (CallableTypeParameterNode $param) => new CallableTypeParameterNode(
self::resolve($param->type, $vars, $boundTemplates, $registry, $function),
$param->isReference,
$param->isVariadic,
$param->parameterName,
$param->isOptional
),
$typeNode->parameters
);

$returnType = self::resolve($typeNode->returnType, $vars, $boundTemplates, $registry, $function);

return new CallableTypeNode(
$typeNode->identifier,
$parameters,
$returnType,
$typeNode->templateTypes
);
}

if ($typeNode instanceof OffsetAccessTypeNode) {
return new OffsetAccessTypeNode(
self::resolve($typeNode->type, $vars, $boundTemplates, $registry, $function),
self::resolve($typeNode->offset, $vars, $boundTemplates, $registry, $function)
);
}

return $typeNode;
}

Expand Down
17 changes: 3 additions & 14 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
Expand Down Expand Up @@ -238,10 +236,7 @@ private static function validateSimpleParams(
): ?ErrorMessage {
foreach ($types as $paramName => $typeNode) {
if (isset($vars[$paramName]) || \array_key_exists($paramName, $vars)) {
if (
$typeNode instanceof ConditionalTypeForParameterNode ||
$typeNode instanceof ConditionalTypeNode
) {
if (ConditionalChecker::containsConditional($typeNode)) {
$typeNode = ConditionalChecker::resolve($typeNode, $vars, [], $registry, $effectiveFunction);
}

Expand Down Expand Up @@ -1095,10 +1090,7 @@ private static function validateSingleParam(
object|string|null $thisOrClass = null,
bool $isSensitive = false
): ?ErrorMessage {
if (
$typeNode instanceof ConditionalTypeForParameterNode ||
$typeNode instanceof ConditionalTypeNode
) {
if (ConditionalChecker::containsConditional($typeNode)) {
$typeNode = ConditionalChecker::resolve($typeNode, $vars, $boundTemplates, $registry, $effectiveFunction);
}

Expand All @@ -1115,10 +1107,7 @@ private static function validateSingleParam(
$typeNode = SpecialTypeResolver::resolve($typeNode, $effectiveFunction, $thisObj);
}

if (
$typeNode instanceof ConditionalTypeForParameterNode ||
$typeNode instanceof ConditionalTypeNode
) {
if (ConditionalChecker::containsConditional($typeNode)) {
$typeNode = ConditionalChecker::resolve($typeNode, $vars, $boundTemplates, $registry, $effectiveFunction);
}

Expand Down
9 changes: 4 additions & 5 deletions src/Internal/Checker/ReturnChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand Down Expand Up @@ -343,9 +342,9 @@ private static function evaluateReturn(

$hasGenerics = (\count($templates) > 0);
$hasAliases = (\count($aliases) > 0);
$isParamConditional = ($returnTypeNode instanceof ConditionalTypeForParameterNode);
$hasConditionals = ConditionalChecker::containsConditional($returnTypeNode);

if (! $hasGenerics && ! $hasAliases && ! $isParamConditional && ! ($returnTypeNode instanceof CallableTypeNode)) {
if (! $hasGenerics && ! $hasAliases && ! $hasConditionals && ! ($returnTypeNode instanceof CallableTypeNode)) {
$isDynamic = $contract['returnIsDynamic'] ?? (str_contains((string) $returnTypeNode, 'static') || str_contains((string) $returnTypeNode, '$this'));

if (! $isDynamic) {
Expand Down Expand Up @@ -386,7 +385,7 @@ private static function evaluateReturn(

$sig = null;
$boundCount = \count($boundTemplates);
if ($boundCount > 0 && $boundCount <= 2 && ! $isParamConditional && \count($aliases) === 0 && $thisObj === null) {
if ($boundCount > 0 && $boundCount <= 2 && ! $hasConditionals && \count($aliases) === 0 && $thisObj === null) {
if ($boundCount === 1) {
$first = reset($boundTemplates);
$sig = $first instanceof IdentifierTypeNode ? $first->name : (string) $first;
Expand All @@ -409,7 +408,7 @@ private static function evaluateReturn(
$resolvedType = $aliases[$resolvedType->name];
}

if (\count($boundTemplates) === 0 && \count($templates) > 0 && ! $isParamConditional && $thisObj === null) {
if (\count($boundTemplates) === 0 && \count($templates) > 0 && ! $hasConditionals && $thisObj === null) {
$resolvedType = self::$unboundReturnCache[$function] ??= SpecialTypeResolver::resolve(
TemplateSubstitutor::substitute($returnTypeNode, [], $templates),
$function,
Expand Down
7 changes: 1 addition & 6 deletions src/Internal/Checker/SelfOutChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace TypePHP\Internal\Checker;

use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use TypePHP\Internal\Docblock\DocblockParser;
use TypePHP\Internal\Generics\TemplateManager;
Expand Down Expand Up @@ -79,10 +77,7 @@ public static function checkSelfOut(
$selfOutNode = SpecialTypeResolver::resolve($selfOutNode, $effectiveFunction, $thisObj);
}

if (
$selfOutNode instanceof ConditionalTypeForParameterNode ||
$selfOutNode instanceof ConditionalTypeNode
) {
if (ConditionalChecker::containsConditional($selfOutNode)) {
$selfOutNode = ConditionalChecker::resolve($selfOutNode, $vars, $boundTemplates, $registry, $effectiveFunction);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Docblock/DocblockNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function normalize(string $doc): string
}

if (stripos($doc, 'callable') !== false || stripos($doc, 'closure') !== false) {
$doc = preg_replace('/(callable|Closure|static-closure)\s*\(([^)]*)\)(?!\s*:)/i', '$1($2): mixed', $doc) ?? $doc;
$doc = preg_replace('/(callable|Closure|static-closure)\s*(\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\))(?!\s*:)/i', '$1$2: mixed', $doc) ?? $doc;
}

if (str_contains($doc, '::') && str_contains($doc, ':')) {
Expand Down
Loading
Loading