Skip to content

Commit d218134

Browse files
clxmstaabondrejmirtes
authored andcommitted
Added integer range phpdoc support
1 parent 28ac816 commit d218134

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,27 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
474474
}
475475

476476
return new ErrorType();
477+
} elseif ($mainTypeName === 'int') {
478+
if (count($genericTypes) === 2) { // int<min, max>, int<1, 3>
479+
480+
if ($genericTypes[0] instanceof ConstantIntegerType) {
481+
$min = $genericTypes[0]->getValue();
482+
} elseif ($typeNode->genericTypes[0] instanceof IdentifierTypeNode && $typeNode->genericTypes[0]->name === 'min') {
483+
$min = null;
484+
} else {
485+
return new ErrorType();
486+
}
487+
488+
if ($genericTypes[1] instanceof ConstantIntegerType) {
489+
$max = $genericTypes[1]->getValue();
490+
} elseif ($typeNode->genericTypes[1] instanceof IdentifierTypeNode && $typeNode->genericTypes[1]->name === 'max') {
491+
$max = null;
492+
} else {
493+
return new ErrorType();
494+
}
495+
496+
return IntegerRangeType::fromInterval($min, $max);
497+
}
477498
}
478499

479500
$mainType = $this->resolveIdentifierTypeNode($typeNode->type, $nameScope);

tests/PHPStan/Analyser/data/integer-range-types.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,36 @@ function (int $a, int $b, int $c): void {
156156
assertType('int', $b * $c);
157157
assertType('int', $a * $b * $c);
158158
};
159+
160+
class X {
161+
/**
162+
* @var int<0, 100>
163+
*/
164+
public $percentage;
165+
/**
166+
* @var int<min, 100>
167+
*/
168+
public $min;
169+
/**
170+
* @var int<0, max>
171+
*/
172+
public $max;
173+
174+
/**
175+
* @var int<0, something>
176+
*/
177+
public $error1;
178+
/**
179+
* @var int<something, 100>
180+
*/
181+
public $error2;
182+
183+
public function supportsPhpdocIntegerRange() {
184+
assertType('int<0, 100>', $this->percentage);
185+
assertType('int<min, 100>', $this->min);
186+
assertType('int<0, max>', $this->max);
187+
188+
assertType('*ERROR*', $this->error1);
189+
assertType('*ERROR*', $this->error2);
190+
}
191+
}

0 commit comments

Comments
 (0)