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
2 changes: 2 additions & 0 deletions src/Internal/Ast/Elimination/EliminationSafetyPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ final class EliminationSafetyPolicy
private const PERMISSIBLE_NON_OBJECT_GENERICS = [
'int' => true,
'integer' => true,
'float' => true,
'double' => true,
'int-mask' => true,
'int-mask-of' => true,
'value-of' => true,
Expand Down
73 changes: 72 additions & 1 deletion src/Internal/Ast/Elimination/Evaluator/ScalarEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ private static function satisfiesInteger(int $value, TypeNode $typeNode): bool
return self::satisfiesIntRange($value, $typeNode);
}

if ($base === 'float' || $base === 'double') {
return self::satisfiesFloatRange($value, $typeNode);
}

if ($base === 'int-mask' || $base === 'int-mask-of') {
return self::satisfiesIntMask($value, $typeNode);
}
Expand All @@ -185,7 +189,7 @@ private static function satisfiesInteger(int $value, TypeNode $typeNode): bool
if ($typeNode instanceof IdentifierTypeNode) {
return match (strtolower($typeNode->name)) {
'int', 'integer', 'scalar', 'numeric', 'number', 'mixed' => true,
'float', 'double' => true,
'float', 'double', 'finite-float' => true,
'positive-int', 'positive-float' => $value > 0,
'negative-int', 'negative-float' => $value < 0,
'non-positive-int', 'non-positive-float' => $value <= 0,
Expand Down Expand Up @@ -279,9 +283,17 @@ private static function satisfiesFloat(float $value, TypeNode $typeNode): bool
&& abs($value - (float) $typeNode->constExpr->value) < ArithmeticEvaluator::FLOAT_EPSILON;
}

if ($typeNode instanceof GenericTypeNode) {
$base = strtolower((string) $typeNode->type);
if ($base === 'float' || $base === 'double') {
return self::satisfiesFloatRange($value, $typeNode);
}
}

if ($typeNode instanceof IdentifierTypeNode) {
return match (strtolower($typeNode->name)) {
'float', 'double', 'scalar', 'numeric', 'number', 'mixed' => true,
'finite-float' => is_finite($value),
'positive-float' => $value > 0.0,
'negative-float' => $value < 0.0,
'non-positive-float' => $value <= 0.0,
Expand All @@ -296,6 +308,65 @@ private static function satisfiesFloat(float $value, TypeNode $typeNode): bool
return false;
}

/**
* Verifies if an int or float falls within static `float<min, max>` bounds.
*/
private static function satisfiesFloatRange(int|float $value, GenericTypeNode $node): bool
{
$floatValue = (float) $value;
if (is_nan($floatValue)) {
return false;
}

$min = self::resolveFloatRangeBound($node->genericTypes[0] ?? null);
$max = self::resolveFloatRangeBound($node->genericTypes[1] ?? null);

if ($min !== null && $floatValue < (float) $min) {
return false;
}

if ($max !== null && $floatValue > (float) $max) {
return false;
}

return true;
}

/**
* Resolves numeric bounds from float range AST nodes, ignoring 'min', 'max', and '*'.
*/
private static function resolveFloatRangeBound(?TypeNode $boundNode): int|float|null
{
if ($boundNode === null) {
return null;
}

if ($boundNode instanceof ConstTypeNode) {
if ($boundNode->constExpr instanceof ConstExprIntegerNode) {
return (int) $boundNode->constExpr->value;
}

if ($boundNode->constExpr instanceof ConstExprFloatNode) {
return (float) $boundNode->constExpr->value;
}
}

if ($boundNode instanceof IdentifierTypeNode) {
$name = strtolower($boundNode->name);
if ($name === 'min' || $name === 'max' || $name === '*') {
return null;
}

if (is_numeric($boundNode->name)) {
return str_contains($boundNode->name, '.') || stripos($boundNode->name, 'e') !== false
? (float) $boundNode->name
: (int) $boundNode->name;
}
}

return null;
}

/**
* Evaluates a string against string literals and string refinements (e.g. non-empty, casing).
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Ast/Elimination/StaticTypeEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ final class StaticTypeEvaluator
private const PERMISSIBLE_GENERIC_CONTAINERS = [
'int' => true,
'integer' => true,
'float' => true,
'double' => true,
'int-mask' => true,
'int-mask-of' => true,
'value-of' => true,
Expand Down
3 changes: 3 additions & 0 deletions src/Internal/Checker/InlineChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public static function reset(): void
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'non-empty-string' => true,
'numeric-string' => true,
'lowercase-string' => true,
Expand All @@ -185,6 +186,8 @@ public static function reset(): void
private const SCALAR_GENERIC_TYPES = [
'int' => true,
'integer' => true,
'float' => true,
'double' => true,
'int-mask' => true,
'int-mask-of' => true,
'class-string' => true,
Expand Down
4 changes: 4 additions & 0 deletions src/Internal/Diagnostic/TypeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static function formatGivenValue(mixed $value, bool $isSensitive = false)
}

if (\is_float($value)) {
if (is_nan($value)) {
return 'float (NAN)';
}

return "float ($value)";
}

Expand Down
8 changes: 7 additions & 1 deletion src/Internal/Docblock/DocblockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ final class DocblockParser
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'double' => true,
],
'double' => [
Expand All @@ -150,6 +151,7 @@ final class DocblockParser
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'double' => true,
],
'bool' => [
Expand Down Expand Up @@ -366,7 +368,7 @@ private static function computeContractFlags(
if (! ($inner instanceof IdentifierTypeNode)) {
if ($inner instanceof GenericTypeNode) {
$base = strtolower((string) $inner->type);
if (! \in_array($base, ['class-string', 'int', 'integer', 'int-mask', 'int-mask-of', 'key-of', 'value-of'], true)) {
if (! \in_array($base, ['class-string', 'int', 'integer', 'float', 'double', 'int-mask', 'int-mask-of', 'key-of', 'value-of'], true)) {
$paramsNeedPreInference = true;
}
} else {
Expand Down Expand Up @@ -1854,6 +1856,10 @@ private static function isRefinementOfBuiltin(string $refinement, string $builti
return true;
}

if (($builtin === 'float' || $builtin === 'double') && (str_starts_with($refinement, 'float<') || str_starts_with($refinement, 'double<'))) {
return true;
}

if ($builtin === 'string' && str_starts_with($refinement, 'class-string<')) {
return true;
}
Expand Down
26 changes: 26 additions & 0 deletions src/Internal/Generics/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ final class TemplateManager
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
],
'double' => [
'float' => true,
Expand All @@ -258,6 +259,7 @@ final class TemplateManager
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
],
'bool' => [
'bool' => true,
Expand Down Expand Up @@ -315,6 +317,7 @@ final class TemplateManager
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'numeric-string' => true,
],
'number' => [
Expand All @@ -335,6 +338,7 @@ final class TemplateManager
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'numeric-string' => true,
],
'scalar' => [
Expand All @@ -357,6 +361,7 @@ final class TemplateManager
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'non-empty-string' => true,
'numeric-string' => true,
'lowercase-string' => true,
Expand Down Expand Up @@ -419,6 +424,19 @@ final class TemplateManager
'scalar' => true,
];

/**
* O(1) lookup set for valid supertypes of float ranges.
*
* @var array<string, bool>
*/
private const FLOAT_SUPERTYPES = [
'float' => true,
'double' => true,
'numeric' => true,
'number' => true,
'scalar' => true,
];

/**
* O(1) lookup set for valid supertypes of string literals and class-string<T>.
*
Expand Down Expand Up @@ -3690,6 +3708,10 @@ public static function checkVariance(TypeNode $existing, TypeNode $expected, str
return true;
}

if (($baseExisting === 'float' || $baseExisting === 'double') && isset(self::FLOAT_SUPERTYPES[$lowerExpected])) {
return true;
}

if ($baseExisting === 'class-string' && isset(self::STRING_SUPERTYPES[$lowerExpected])) {
return true;
}
Expand Down Expand Up @@ -3876,6 +3898,10 @@ private static function isScalarSubtype(string $sub, string $super): bool
return true;
}

if ((str_starts_with($sub, 'float<') || str_starts_with($sub, 'double<')) && isset(self::FLOAT_SUPERTYPES[$super])) {
return true;
}

if ((str_starts_with($sub, 'class-string<') || ($sub !== '' && ($sub[0] === "'" || $sub[0] === '"'))) && isset(self::STRING_SUPERTYPES[$super])) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Resolver/SpecialTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final class SpecialTypeResolver
'non-positive-float' => true,
'non-negative-float' => true,
'non-zero-float' => true,
'finite-float' => true,
'class-string' => true,
'interface-string' => true,
'trait-string' => true,
Expand Down
Loading
Loading