Skip to content

Commit 6b4befd

Browse files
committed
fix(controller): Support native int ranges
Signed-off-by: Joas Schilling <[email protected]>
1 parent 2896caf commit 6b4befd

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

lib/private/AppFramework/Utility/ControllerMethodReflector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class ControllerMethodReflector implements IControllerMethodReflector {
2525
*/
2626
public function reflect($object, string $method) {
2727
$reflection = new \ReflectionMethod($object, $method);
28+
$this->startLine = $reflection->getStartLine();
29+
$this->file = $reflection->getFileName();
30+
2831
$docs = $reflection->getDocComment();
2932

3033
if ($docs !== false) {
@@ -50,7 +53,7 @@ public function reflect($object, string $method) {
5053
// extract type parameter information
5154
preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
5255
$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);
56+
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);
5457
foreach ($matches['var'] as $index => $varName) {
5558
if ($matches['type'][$index] !== 'int') {
5659
// only int ranges are possible at the moment
@@ -134,4 +137,12 @@ public function getAnnotationParameter(string $name, string $key): string {
134137

135138
return '';
136139
}
140+
141+
public function getStartLine(): int {
142+
return $this->startLine;
143+
}
144+
145+
public function getFile(): string {
146+
return $this->file;
147+
}
137148
}

tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ 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) {
51+
}
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) {
4961
}
5062
}
5163

@@ -132,9 +144,6 @@ public function testReadTypeIntAnnotations(): void {
132144
public function arguments3($a, float $b, int $c, $d) {
133145
}
134146

135-
/**
136-
* @requires PHP 7
137-
*/
138147
public function testReadTypeIntAnnotationsScalarTypes(): void {
139148
$reader = new ControllerMethodReflector();
140149
$reader->reflect(
@@ -228,7 +237,7 @@ public function testInheritanceOverrideNoDocblock(): void {
228237
$this->assertFalse($reader->hasAnnotation('Annotation'));
229238
}
230239

231-
public function testRangeDetection(): void {
240+
public function testRangeDetectionPsalm(): void {
232241
$reader = new ControllerMethodReflector();
233242
$reader->reflect('Test\AppFramework\Utility\EndController', 'test4');
234243

@@ -239,5 +248,34 @@ public function testRangeDetection(): void {
239248
$rangeInfo2 = $reader->getRange('rangedTwo');
240249
$this->assertSame(PHP_INT_MIN, $rangeInfo2['min']);
241250
$this->assertSame(PHP_INT_MAX, $rangeInfo2['max']);
251+
252+
$rangeInfo3 = $reader->getRange('rangedThree');
253+
$this->assertSame(1, $rangeInfo3['min']);
254+
$this->assertSame(6, $rangeInfo3['max']);
255+
256+
$rangeInfo3 = $reader->getRange('rangedFour');
257+
$this->assertSame(-70, $rangeInfo3['min']);
258+
$this->assertSame(-30, $rangeInfo3['max']);
259+
}
260+
261+
public function testRangeDetectionNative(): void {
262+
$reader = new ControllerMethodReflector();
263+
$reader->reflect('Test\AppFramework\Utility\EndController', 'test5');
264+
265+
$rangeInfo1 = $reader->getRange('rangedOne');
266+
$this->assertSame(-4, $rangeInfo1['min']);
267+
$this->assertSame(42, $rangeInfo1['max']);
268+
269+
$rangeInfo2 = $reader->getRange('rangedTwo');
270+
$this->assertSame(PHP_INT_MIN, $rangeInfo2['min']);
271+
$this->assertSame(PHP_INT_MAX, $rangeInfo2['max']);
272+
273+
$rangeInfo3 = $reader->getRange('rangedThree');
274+
$this->assertSame(1, $rangeInfo3['min']);
275+
$this->assertSame(6, $rangeInfo3['max']);
276+
277+
$rangeInfo3 = $reader->getRange('rangedFour');
278+
$this->assertSame(-70, $rangeInfo3['min']);
279+
$this->assertSame(-30, $rangeInfo3['max']);
242280
}
243281
}

0 commit comments

Comments
 (0)