Skip to content

Commit d0da446

Browse files
Merge pull request #57418 from nextcloud/backport/57413/stable32
[stable32] fix(controller): Support native int ranges
2 parents 66ac6e3 + ea0d1d3 commit d0da446

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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))>(\|null)?\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: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public function test3() {
4949
*/
5050
public function test4(int $rangedOne, int $rangedTwo, ?int $rangedThree, ?int $rangedFour) {
5151
}
52+
53+
/**
54+
* @param int<-4, 42> $rangedOne
55+
* @param int<min, max> $rangedTwo
56+
* @param int<1, 6>|null $rangedThree
57+
* @param ?int<-70, -30> $rangedFour
58+
* @return void
59+
*/
60+
public function test5(int $rangedOne, int $rangedTwo, ?int $rangedThree, ?int $rangedFour) {
61+
}
5262
}
5363

5464
class EndController extends MiddleController {
@@ -230,7 +240,7 @@ public function testInheritanceOverrideNoDocblock(): void {
230240
$this->assertFalse($reader->hasAnnotation('Annotation'));
231241
}
232242

233-
public function testRangeDetection(): void {
243+
public function testRangeDetectionPsalm(): void {
234244
$reader = new ControllerMethodReflector();
235245
$reader->reflect('Test\AppFramework\Utility\EndController', 'test4');
236246

@@ -250,4 +260,25 @@ public function testRangeDetection(): void {
250260
$this->assertSame(-70, $rangeInfo3['min']);
251261
$this->assertSame(-30, $rangeInfo3['max']);
252262
}
263+
264+
public function testRangeDetectionNative(): void {
265+
$reader = new ControllerMethodReflector();
266+
$reader->reflect('Test\AppFramework\Utility\EndController', 'test5');
267+
268+
$rangeInfo1 = $reader->getRange('rangedOne');
269+
$this->assertSame(-4, $rangeInfo1['min']);
270+
$this->assertSame(42, $rangeInfo1['max']);
271+
272+
$rangeInfo2 = $reader->getRange('rangedTwo');
273+
$this->assertSame(PHP_INT_MIN, $rangeInfo2['min']);
274+
$this->assertSame(PHP_INT_MAX, $rangeInfo2['max']);
275+
276+
$rangeInfo3 = $reader->getRange('rangedThree');
277+
$this->assertSame(1, $rangeInfo3['min']);
278+
$this->assertSame(6, $rangeInfo3['max']);
279+
280+
$rangeInfo3 = $reader->getRange('rangedFour');
281+
$this->assertSame(-70, $rangeInfo3['min']);
282+
$this->assertSame(-30, $rangeInfo3['max']);
283+
}
253284
}

0 commit comments

Comments
 (0)