Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4f1b7c3
Enhance type inference and validation in ParamChecker, TemplateManage…
rcalicdan Nov 11, 2026
fbac1bb
Enhance ParamChecker and TemplateManager with improved type inference…
rcalicdan Nov 11, 2026
a93b2c8
Add ConditionalReturnGenericsTest for filterNotNull and flatten behav…
rcalicdan Nov 11, 2026
e8a9a14
Add GenericMapFixture and tests for factory reconstruction variance i…
rcalicdan Nov 11, 2026
1adbee5
Refactor TemplateManager to simplify type matching logic and add Unio…
rcalicdan Nov 11, 2026
23d8561
Improve type annotations and formatting in various tests and Template…
rcalicdan Nov 11, 2026
49952aa
Refactor ParamChecker to simplify traversable type resolution; add Sp…
rcalicdan Nov 11, 2026
dbfbc56
Enhance ParamChecker to utilize class templates in type substitution …
rcalicdan Nov 11, 2026
1a324db
Enhance ParamChecker to support class alias substitution in traversab…
rcalicdan Nov 12, 2026
6fe6b10
Enhance ParamChecker to support class templates in template inference…
rcalicdan Nov 12, 2026
9c136f6
Enhance FunctionContractInjector and RuntimeTypeChecker to support sc…
rcalicdan Nov 12, 2026
511eda9
Fix formatting in FunctionContractInjector by adding spaces in anonym…
rcalicdan Nov 12, 2026
4f83324
Enhance ParamChecker to support ObjectShapeNode in template inference…
rcalicdan Nov 12, 2026
ffbab62
Add tests for cloned store template widening in immutable collections…
rcalicdan Nov 12, 2026
a806e80
Refactor ParamChecker to improve return type handling and add Nullabl…
rcalicdan Nov 12, 2026
4acaa29
Enhance template inference by adding generator support in ParamChecke…
rcalicdan Nov 12, 2026
42286d4
Refactor TemplateManager to utilize WeakMap for instance template bin…
rcalicdan Nov 12, 2026
245c90e
Enhance ParamChecker and TemplateManager with improved call binding h…
rcalicdan Nov 13, 2026
a5fdcd8
Enhance propagateBindingsToInstanceProperties method to prevent prema…
rcalicdan Nov 13, 2026
f346a48
Implement generics for map and set structures, including immutable an…
rcalicdan Nov 13, 2026
1b877dd
Add caching for generic object contexts and template variances in Par…
rcalicdan Nov 13, 2026
fe86ceb
Fix formatting by adding missing newlines in ParamChecker and Templat…
rcalicdan Nov 13, 2026
fc1cd2a
Fix PHP version check placement in ReturnGenericReificationTest
rcalicdan Nov 13, 2026
81e624c
Add generic iterable and animal producer classes with tests for type …
rcalicdan Nov 14, 2026
8c613ab
Refactor contract cache handling in checkers and enhance method hiera…
rcalicdan Nov 15, 2026
4fc37a1
Enhance ConstantAndEnumEvaluator with enum support and add comprehens…
rcalicdan Nov 15, 2026
8f33cba
Refactor ArrayLiteralEvaluator to improve non-empty array and list ch…
rcalicdan Nov 15, 2026
fcd31f0
Refactor DocblockNormalizer to quote unquoted class constant keys in …
rcalicdan Nov 15, 2026
be2cf99
Enhance ContractVisitor with side-effect detection and hoisting for i…
rcalicdan Nov 16, 2026
8c5f832
Enhance ContractVisitor and TemplateManager with improved generic typ…
rcalicdan Nov 16, 2026
ad2363c
Enhance InlineChecker and SpecialTypeResolver with namespace extracti…
rcalicdan Nov 16, 2026
6a24751
Refactor FunctionContractInjector to simplify return statement checks…
rcalicdan Nov 16, 2026
4822890
Add active call order tracking to TemplateManager and implement tests…
rcalicdan Nov 16, 2026
78e0c2a
Enhance DocblockNormalizer to support nested angle brackets and calla…
rcalicdan Nov 16, 2026
f4cec52
fix code styling
rcalicdan Nov 16, 2026
87fd29f
Enhance ReportCommand output to include detailed report file informat…
rcalicdan Nov 16, 2026
6f18aa8
Fix missing newline at end of file in ReportCommand.php
rcalicdan Nov 16, 2026
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
318 changes: 283 additions & 35 deletions src/Internal/Ast/ContractVisitor.php

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions src/Internal/Ast/Elimination/Evaluator/ArrayLiteralEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ private static function satisfiesIdentifierArray(Array_ $arrayExpr, IdentifierTy
return match ($name) {
'array', 'iterable', 'mixed' => true,
'list' => self::isSequentialList($arrayExpr),
'non-empty-array' => \count($arrayExpr->items) > 0,
'non-empty-list' => \count($arrayExpr->items) > 0 && self::isSequentialList($arrayExpr),
'non-empty-array' => self::hasConcreteItem($arrayExpr),
'non-empty-list' => self::hasConcreteItem($arrayExpr) && self::isSequentialList($arrayExpr),
default => false,
};
}
Expand Down Expand Up @@ -184,7 +184,7 @@ private static function satisfiesList(Array_ $arrayExpr, TypeNode $itemType, boo

$expectedIdx = 0;
foreach ($arrayExpr->items as $item) {
if ($item === null) {
if ($item === null || $item->unpack) {
return false;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ private static function satisfiesKeyValueArray(Array_ $arrayExpr, TypeNode $keyT

$currentIdx = 0;
foreach ($arrayExpr->items as $item) {
if ($item === null) {
if ($item === null || $item->unpack) {
return false;
}

Expand Down Expand Up @@ -276,7 +276,7 @@ private static function satisfiesArrayShape(Array_ $arrayExpr, ArrayShapeNode $s
$actualItems = [];
$currentIdx = 0;
foreach ($arrayExpr->items as $item) {
if ($item === null) {
if ($item === null || $item->unpack) {
return false;
}

Expand Down Expand Up @@ -342,7 +342,7 @@ private static function isSequentialList(Array_ $arrayExpr): bool
{
$expectedIdx = 0;
foreach ($arrayExpr->items as $item) {
if ($item === null) {
if ($item === null || $item->unpack) {
return false;
}

Expand All @@ -356,4 +356,18 @@ private static function isSequentialList(Array_ $arrayExpr): bool

return true;
}

/**
* Checks whether an array expression contains at least one non-unpacked concrete item.
*/
private static function hasConcreteItem(Array_ $arrayExpr): bool
{
foreach ($arrayExpr->items as $item) {
if ($item !== null && ! $item->unpack) {
return true;
}
}

return false;
}
}
132 changes: 118 additions & 14 deletions src/Internal/Ast/Elimination/Evaluator/ConstantAndEnumEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TypePHP\Internal\Ast\Elimination\Evaluator;

use BackedEnum;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\ClassConstFetch;
Expand All @@ -16,6 +17,7 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use TypePHP\Internal\Resolver\SpecialTypeResolver;
use UnitEnum;

/**
* Statically evaluates whether constant fetches, enum cases, bitmasks,
Expand All @@ -25,13 +27,25 @@
*/
final class ConstantAndEnumEvaluator
{
/**
* O(1) hash map of relative class keywords.
*
* @var array<string, true>
*/
private const RELATIVE_CLASS_KEYWORDS = [
'self' => true,
'static' => true,
'parent' => true,
'$this' => true,
];

/**
* Determines whether an expression satisfies constant or enum contracts.
*
* Flow of logic:
* 1. Evaluates ClassConstFetch nodes against exact constants, wildcards, and base enum types.
* 1. Evaluates ClassConstFetch nodes against exact constants, wildcards, and loaded enum cases.
* 2. Evaluates bitwise OR expressions (A | B) against `int-mask-of` wildcards.
* 3. Evaluates enum backing property reads (Enum::Case->value) against `value-of<Enum>`.
* 3. Evaluates backed enum property reads (Enum::Case->value) against `value-of<Enum>`.
*/
public static function satisfies(Expr $expr, TypeNode $typeNode, string $currentFile = ''): bool
{
Expand Down Expand Up @@ -60,9 +74,17 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
}

$constName = $fetch->name->toString();
if (strtolower($constName) === 'class') {
return false;
}

$targetClassName = $fetch->class->toString();

if ($typeNode instanceof ConstTypeNode && $typeNode->constExpr instanceof ConstFetchNode) {
if (! self::classesMatch($targetClassName, $typeNode->constExpr->className, $currentFile)) {
return false;
}

$docConstName = $typeNode->constExpr->name;

if (str_contains($docConstName, '*')) {
Expand All @@ -73,20 +95,31 @@ private static function satisfiesClassConst(ClassConstFetch $fetch, TypeNode $ty
}

if ($typeNode instanceof IdentifierTypeNode) {
if ($currentFile !== '') {
$resolvedFetchClass = SpecialTypeResolver::resolveFqcnForFile($targetClassName, $currentFile, false);
$resolvedTypeClass = SpecialTypeResolver::resolveFqcnForFile($typeNode->name, $currentFile, false);
if (! self::classesMatch($targetClassName, $typeNode->name, $currentFile)) {
return false;
}

return $resolvedFetchClass !== '' && $resolvedFetchClass === $resolvedTypeClass;
$resolvedEnum = self::resolveClassForReflection($targetClassName, $currentFile);
if ($resolvedEnum === '' || ! enum_exists($resolvedEnum, false)) {
return false;
}

return $targetClassName === $typeNode->name;
return self::isEnumCase($resolvedEnum, $constName);
}

if ($typeNode instanceof GenericTypeNode && strtolower((string) $typeNode->type) === 'int-mask-of') {
$inner = $typeNode->genericTypes[0] ?? null;
if ($inner instanceof ConstTypeNode && $inner->constExpr instanceof ConstFetchNode) {
return self::matchesWildcard($constName, $inner->constExpr->name);
if (! self::classesMatch($targetClassName, $inner->constExpr->className, $currentFile)) {
return false;
}

$pattern = $inner->constExpr->name;
if (str_contains($pattern, '*')) {
return self::matchesWildcard($constName, $pattern);
}

return $constName === $pattern;
}
}

Expand All @@ -107,15 +140,15 @@ private static function satisfiesBitwiseOr(BitwiseOr $expr, TypeNode $typeNode,
}

/**
* Verifies whether an Enum::Case->value property access satisfies `value-of<Enum>`.
* Verifies whether a BackedEnum::Case->value property access satisfies `value-of<Enum>`.
*/
private static function satisfiesEnumBackingValue(PropertyFetch $fetch, TypeNode $typeNode, string $currentFile): bool
{
if (! ($fetch->name instanceof Identifier) || $fetch->name->name !== 'value') {
return false;
}

if (! ($fetch->var instanceof ClassConstFetch) || ! ($fetch->var->class instanceof Name)) {
if (! ($fetch->var instanceof ClassConstFetch) || ! ($fetch->var->class instanceof Name) || ! ($fetch->var->name instanceof Identifier)) {
return false;
}

Expand All @@ -129,15 +162,86 @@ private static function satisfiesEnumBackingValue(PropertyFetch $fetch, TypeNode
}

$enumClassName = $fetch->var->class->toString();
$caseName = $fetch->var->name->toString();

if (strtolower($caseName) === 'class') {
return false;
}

if (! self::classesMatch($enumClassName, $enumTarget->name, $currentFile)) {
return false;
}

$resolvedEnum = self::resolveClassForReflection($enumClassName, $currentFile);
if ($resolvedEnum === '' || ! enum_exists($resolvedEnum, false) || ! is_subclass_of($resolvedEnum, BackedEnum::class)) {
return false;
}

return self::isEnumCase($resolvedEnum, $caseName);
}

/**
* Verifies whether two class references deterministically denote the same class/enum.
*/
private static function classesMatch(string $fetchClass, string $docClass, string $currentFile): bool
{
if ($docClass === '') {
return false;
}

$lowerFetch = strtolower($fetchClass);
$lowerDoc = strtolower($docClass);

if ($lowerFetch === 'self' && $lowerDoc === 'self') {
return true;
}

if (isset(self::RELATIVE_CLASS_KEYWORDS[$lowerFetch]) || isset(self::RELATIVE_CLASS_KEYWORDS[$lowerDoc])) {
return false;
}

if ($fetchClass === $docClass) {
return true;
}

if ($currentFile !== '') {
$resolvedEnumClass = SpecialTypeResolver::resolveFqcnForFile($enumClassName, $currentFile, false);
$resolvedTargetClass = SpecialTypeResolver::resolveFqcnForFile($enumTarget->name, $currentFile, false);
$resolvedFetchClass = SpecialTypeResolver::resolveFqcnForFile($fetchClass, $currentFile, false);
$resolvedTypeClass = SpecialTypeResolver::resolveFqcnForFile($docClass, $currentFile, false);

return $resolvedEnumClass !== '' && $resolvedEnumClass === $resolvedTargetClass;
return $resolvedFetchClass !== '' && $resolvedFetchClass === $resolvedTypeClass;
}

return $enumClassName === $enumTarget->name;
return false;
}

/**
* Resolves a class name without invoking autoloading.
*/
private static function resolveClassForReflection(string $rawClass, string $currentFile): string
{
if ($currentFile !== '') {
return SpecialTypeResolver::resolveFqcnForFile($rawClass, $currentFile, false);
}

return ltrim($rawClass, '\\');
}

/**
* Verifies that a symbol is a declared enum case (excluding regular class constants on enums).
*/
private static function isEnumCase(string $enumClass, string $caseName): bool
{
if (! enum_exists($enumClass, false) || ! is_subclass_of($enumClass, UnitEnum::class)) {
return false;
}

foreach ($enumClass::cases() as $case) {
if ($case->name === $caseName) {
return true;
}
}

return false;
}

/**
Expand Down
Loading
Loading