1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #7775 from fluffycondor/improve-int-range-parsing

Improve int range parsing
This commit is contained in:
orklah 2022-03-12 08:27:23 +01:00 committed by GitHub
commit c1ad5f6be0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 8 deletions

View File

@ -830,19 +830,24 @@ class TypeParser
throw new TypeParseTreeException('Union types are not allowed in int range type');
}
if ($param0_union_types[0] instanceof TNamedObject &&
$param0_union_types[0]->value === TIntRange::BOUND_MAX
) {
throw new TypeParseTreeException("min bound for int range param can't be 'max'");
if (!($param0_union_types[0] instanceof TNamedObject) && !($param0_union_types[0] instanceof TLiteralInt) ||
!($param1_union_types[0] instanceof TNamedObject) && !($param1_union_types[0] instanceof TLiteralInt)) {
throw new TypeParseTreeException('Unsupported parameter');
}
if ($param1_union_types[0] instanceof TNamedObject &&
$param1_union_types[0]->value === TIntRange::BOUND_MIN
) {
throw new TypeParseTreeException("max bound for int range param can't be 'min'");
if ($param0_union_types[0] instanceof TNamedObject
&& $param0_union_types[0]->value !== TIntRange::BOUND_MIN) {
throw new TypeParseTreeException('Incorrect named object as a min boundary');
}
if ($param1_union_types[0] instanceof TNamedObject
&& $param1_union_types[0]->value !== TIntRange::BOUND_MAX) {
throw new TypeParseTreeException('Incorrect named object as a max boundary');
}
$min_bound = null;
$max_bound = null;
if ($param0_union_types[0] instanceof TLiteralInt) {
$min_bound = $param0_union_types[0]->value;
}
@ -858,6 +863,12 @@ class TypeParser
return new TPositiveInt();
}
if (is_int($min_bound) && is_int($max_bound) && $min_bound > $max_bound) {
throw new TypeParseTreeException(
"Min bound can't be greater than max bound, int<" . $min_bound . "," . $max_bound . "> given"
);
}
return new TIntRange($min_bound, $max_bound);
}

View File

@ -748,6 +748,66 @@ class IntRangeTest extends TestCase
}',
'error_message' => 'DocblockTypeContradiction',
],
'maxSpecifiedAsFirst' => [
'<?php
/**
* @param int<max, 0> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
'minSpecifiedAsSecond' => [
'<?php
/**
* @param int<0, min> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
'unknownConstant' => [
'<?php
/**
* @param int<0, FOO> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
'floatAsABoundary' => [
'<?php
/**
* @param int<0, 5.5> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
'stringAsABoundary' => [
'<?php
/**
* @param int<0, "bar"> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
'minGreaterThanMax' => [
'<?php
/**
* @param int<4, 3> $a
*/
function scope(int $a){
return $a;
}',
'error_message' => 'InvalidDocblock',
],
];
}
}