Skip to content

Commit 0099469

Browse files
authored
Merge pull request #57417 from nextcloud/backport/57413/stable31
2 parents 2d14dce + 81fc235 commit 0099469

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

lib/private/AppFramework/Utility/ControllerMethodReflector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ class ControllerMethodReflector implements IControllerMethodReflector {
1818
private $types = [];
1919
private $parameters = [];
2020
private array $ranges = [];
21+
private int $startLine = 0;
22+
private string $file = '';
2123

2224
/**
2325
* @param object $object an object or classname
2426
* @param string $method the method which we want to inspect
2527
*/
2628
public function reflect($object, string $method) {
2729
$reflection = new \ReflectionMethod($object, $method);
30+
$this->startLine = $reflection->getStartLine();
31+
$this->file = $reflection->getFileName();
32+
2833
$docs = $reflection->getDocComment();
2934

3035
if ($docs !== false) {
@@ -50,7 +55,7 @@ public function reflect($object, string $method) {
5055
// extract type parameter information
5156
preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
5257
$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);
58+
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);
5459
foreach ($matches['var'] as $index => $varName) {
5560
if ($matches['type'][$index] !== 'int') {
5661
// only int ranges are possible at the moment
@@ -134,4 +139,12 @@ public function getAnnotationParameter(string $name, string $key): string {
134139

135140
return '';
136141
}
142+
143+
public function getStartLine(): int {
144+
return $this->startLine;
145+
}
146+
147+
public function getFile(): string {
148+
return $this->file;
149+
}
137150
}

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)