Skip to content

Commit 33c983f

Browse files
authored
Walk ancestors for stub method PHPDoc on built-in classes without their own stub entries
1 parent 4f0ab8e commit 33c983f

4 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎src/Reflection/Php/PhpClassReflectionExtension.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,8 @@ private function findMethodPhpDocIncludingAncestors(
13171317
if ($resolved !== null) {
13181318
return [$resolved, $declaringClass];
13191319
}
1320-
if (!$this->stubPhpDocProvider->isKnownClass($declaringClassName)) {
1320+
$isKnownClass = $this->stubPhpDocProvider->isKnownClass($declaringClassName);
1321+
if (!$isKnownClass && !$declaringClass->isBuiltin()) {
13211322
return null;
13221323
}
13231324

@@ -1335,6 +1336,10 @@ private function findMethodPhpDocIncludingAncestors(
13351336
continue;
13361337
}
13371338

1339+
if (!$isKnownClass && $ancestor->isGeneric()) {
1340+
continue;
1341+
}
1342+
13381343
return [$resolved, $ancestor];
13391344
}
13401345

‎stubs/date.stub‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ interface DateTimeInterface {
4141
{}
4242
}
4343

44-
class DateTimeImmutable implements DateTimeInterface {
45-
}
46-
47-
class DateTime implements DateTimeInterface {
48-
}
49-
5044
/** @return \DateInterval&object{days:int} */
5145
function date_diff(\DateTimeInterface $baseObject, \DateTimeInterface $targetObject, bool $absolute = false): \DateInterval
5246
{}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug14632;
4+
5+
use DateTime;
6+
use DateTimeImmutable;
7+
use DateTimeInterface;
8+
use function PHPStan\Testing\assertType;
9+
10+
function testDateTimeInterface(DateTimeInterface $a, DateTimeInterface $b): void {
11+
$interval = $a->diff($b);
12+
assertType('DateInterval&object{days: int}', $interval);
13+
assertType('int', $interval->days);
14+
}
15+
16+
function testDateTimeImmutable(DateTimeImmutable $a, DateTimeImmutable $b): void {
17+
$interval = $a->diff($b);
18+
assertType('DateInterval&object{days: int}', $interval);
19+
assertType('int', $interval->days);
20+
}
21+
22+
function testDateTime(DateTime $a, DateTime $b): void {
23+
$interval = $a->diff($b);
24+
assertType('DateInterval&object{days: int}', $interval);
25+
assertType('int', $interval->days);
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php // lint >= 8.4
2+
3+
namespace BuiltinGenericAncestorStubs;
4+
5+
use Dom\NodeList;
6+
use MultipleIterator;
7+
use SplFileObject;
8+
use WeakMap;
9+
use function PHPStan\Testing\assertType;
10+
11+
function testSplFileObject(SplFileObject $file): void {
12+
assertType('int', $file->key());
13+
assertType('array|string|false', $file->current());
14+
}
15+
16+
function testMultipleIterator(MultipleIterator $it): void {
17+
assertType('array', $it->key());
18+
assertType('array', $it->current());
19+
}
20+
21+
/** @param WeakMap<object, string> $map */
22+
function testWeakMap(WeakMap $map): void {
23+
assertType('int<0, max>', $map->count());
24+
}
25+
26+
function testDomNodeList(NodeList $list): void {
27+
assertType('int<0, max>', $list->count());
28+
}

0 commit comments

Comments
 (0)