Skip to content

Commit 6594d7d

Browse files
committed
feat(AppFramework): extend range check to optional parameters
Now it also applies when a paramater is documtend with a pending |null, but no further unionation is considered. Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 001b12c commit 6594d7d

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/private/AppFramework/Utility/ControllerMethodReflector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function reflect($object, string $method) {
5050
// extract type parameter information
5151
preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
5252
$this->types = array_combine($matches['var'], $matches['type']);
53-
preg_match_all('/@psalm-param\h+(?P<type>\w+)<(?P<rangeMin>(-?\d+|min)),\h*(?P<rangeMax>(-?\d+|max))>\h+\$(?P<var>\w+)/', $docs, $matches);
53+
preg_match_all('/@psalm-param\h+(\?)?(?P<type>\w+)<(?P<rangeMin>(-?\d+|min)),\h*(?P<rangeMax>(-?\d+|max))>(\|null)?\h+\$(?P<var>\w+)/', $docs, $matches);
5454
foreach ($matches['var'] as $index => $varName) {
5555
if ($matches['type'][$index] !== 'int') {
5656
// only int ranges are possible at the moment

tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public function test3() {
4343
/**
4444
* @psalm-param int<-4, 42> $rangedOne
4545
* @psalm-param int<min, max> $rangedTwo
46+
* @psalm-param int<1, 6>|null $rangedThree
47+
* @psalm-param ?int<-70, -30> $rangedFour
4648
* @return void
4749
*/
48-
public function test4(int $rangedOne, int $rangedTwo) {
50+
public function test4(int $rangedOne, int $rangedTwo, ?int $rangedThree, ?int $rangedFour) {
4951
}
5052
}
5153

@@ -239,5 +241,13 @@ public function testRangeDetection(): void {
239241
$rangeInfo2 = $reader->getRange('rangedTwo');
240242
$this->assertSame(PHP_INT_MIN, $rangeInfo2['min']);
241243
$this->assertSame(PHP_INT_MAX, $rangeInfo2['max']);
244+
245+
$rangeInfo3 = $reader->getRange('rangedThree');
246+
$this->assertSame(1, $rangeInfo3['min']);
247+
$this->assertSame(6, $rangeInfo3['max']);
248+
249+
$rangeInfo3 = $reader->getRange('rangedFour');
250+
$this->assertSame(-70, $rangeInfo3['min']);
251+
$this->assertSame(-30, $rangeInfo3['max']);
242252
}
243253
}

0 commit comments

Comments
 (0)