Skip to content

Commit 19151d3

Browse files
authored
Make ReflectionObject, RecursiveFilterIterator, ParentIterator, RecursiveCachingIterator and RecursiveRegexIterator generic in stubs (#6165)
1 parent 3dbc905 commit 19151d3

8 files changed

Lines changed: 219 additions & 1 deletion

File tree

‎conf/config.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ parameters:
3030
checkParameterCastableToNumberFunctions: false
3131
skipCheckGenericClasses:
3232
- DOMNamedNodeMap
33+
- ParentIterator
34+
- RecursiveCachingIterator
35+
- RecursiveFilterIterator
36+
- RecursiveRegexIterator
37+
- ReflectionObject
3338
stricterFunctionMap: false
3439
reportPreciseLineForUnusedFunctionParameter: false
3540
checkPrintfParameterTypes: false

‎src/PhpDoc/ReflectionClassStubFilesExtension.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public function getFiles(): array
1818
if (!$this->phpVersion->supportsLazyObjects()) {
1919
return [
2020
__DIR__ . '/../../stubs/ReflectionClass.stub',
21+
__DIR__ . '/../../stubs/ReflectionObject.stub',
2122
];
2223
}
2324

2425
return [
2526
__DIR__ . '/../../stubs/ReflectionClassWithLazyObjects.stub',
27+
__DIR__ . '/../../stubs/ReflectionObjectWithLazyObjects.stub',
2628
];
2729
}
2830

‎stubs/ReflectionObject.stub‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @template-covariant T of object
5+
* @extends ReflectionClass<T>
6+
*/
7+
class ReflectionObject extends ReflectionClass
8+
{
9+
10+
/**
11+
* @param T $argument
12+
*/
13+
public function __construct($argument) {}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @template T of object
5+
* @extends ReflectionClass<T>
6+
*/
7+
class ReflectionObject extends ReflectionClass
8+
{
9+
10+
/**
11+
* @param T $argument
12+
*/
13+
public function __construct($argument) {}
14+
15+
}

‎stubs/iterable.stub‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,93 @@ class RegexIterator extends FilterIterator {
456456
public function key() {}
457457
}
458458

459+
/**
460+
* @template-covariant TKey
461+
* @template-covariant TValue
462+
* @template TIterator as RecursiveIterator<TKey, TValue>
463+
*
464+
* @template-extends FilterIterator<TKey, TValue, TIterator>
465+
* @template-implements RecursiveIterator<TKey, TValue>
466+
*/
467+
abstract class RecursiveFilterIterator extends FilterIterator implements RecursiveIterator {
468+
/**
469+
* @param TIterator $iterator
470+
*/
471+
public function __construct(RecursiveIterator $iterator) {}
472+
473+
/**
474+
* @return bool
475+
*/
476+
public function hasChildren() {}
477+
478+
/**
479+
* @return static
480+
*/
481+
public function getChildren() {}
482+
}
483+
484+
/**
485+
* @template-covariant TKey
486+
* @template-covariant TValue
487+
* @template TIterator as RecursiveIterator<TKey, TValue>
488+
*
489+
* @template-extends RecursiveFilterIterator<TKey, TValue, TIterator>
490+
*/
491+
class ParentIterator extends RecursiveFilterIterator {
492+
}
493+
494+
/**
495+
* @template TKey
496+
* @template TValue
497+
* @template TIterator as Iterator<TKey, TValue>
498+
*
499+
* @template-extends CachingIterator<TKey, TValue, TIterator>
500+
* @template-implements RecursiveIterator<TKey, TValue>
501+
*/
502+
class RecursiveCachingIterator extends CachingIterator implements RecursiveIterator {
503+
/**
504+
* @param TIterator $iterator
505+
* @param int-mask-of<CachingIterator::*> $flags
506+
*/
507+
public function __construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING) {}
508+
509+
/**
510+
* @return bool
511+
*/
512+
public function hasChildren() {}
513+
514+
/**
515+
* @return static
516+
*/
517+
public function getChildren() {}
518+
}
519+
520+
/**
521+
* @template TKey
522+
* @template TValue
523+
* @template TIterator of RecursiveIterator<TKey, TValue>
524+
*
525+
* @template-extends RegexIterator<TKey, TValue, TIterator>
526+
* @template-implements RecursiveIterator<TKey, TValue>
527+
*/
528+
class RecursiveRegexIterator extends RegexIterator implements RecursiveIterator {
529+
/**
530+
* @param TIterator $iterator
531+
* @param RegexIterator::MATCH|RegexIterator::GET_MATCH|RegexIterator::ALL_MATCHES|RegexIterator::SPLIT|RegexIterator::REPLACE $mode
532+
*/
533+
public function __construct(RecursiveIterator $iterator, string $pattern, int $mode = RegexIterator::MATCH, int $flags = 0, int $preg_flags = 0) {}
534+
535+
/**
536+
* @return bool
537+
*/
538+
public function hasChildren() {}
539+
540+
/**
541+
* @return static
542+
*/
543+
public function getChildren() {}
544+
}
545+
459546
/**
460547
* @template-implements Iterator<never, never>
461548
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15032;
4+
5+
use ReflectionObject;
6+
use function PHPStan\Testing\assertType;
7+
8+
/**
9+
* @template T of object
10+
* @param T $object
11+
* @return T
12+
*/
13+
function createInstance(object $object): object
14+
{
15+
$ref = new ReflectionObject($object);
16+
assertType('ReflectionObject<T of object (function Bug15032\createInstance(), argument)>', $ref);
17+
18+
$ret = $ref->newInstance();
19+
assertType('T of object (function Bug15032\createInstance(), argument)', $ret);
20+
21+
return $ret;
22+
}
23+
24+
function concreteObject(\Exception $e): void
25+
{
26+
$ref = new ReflectionObject($e);
27+
assertType('ReflectionObject<Exception>', $ref);
28+
assertType('class-string<Exception>', $ref->getName());
29+
assertType('class-string<Exception>', $ref->name);
30+
assertType('Exception', $ref->newInstance());
31+
assertType('Exception', $ref->newInstanceArgs([]));
32+
assertType('Exception', $ref->newInstanceWithoutConstructor());
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace RecursiveIteratorsStub;
4+
5+
use ParentIterator;
6+
use RecursiveArrayIterator;
7+
use RecursiveCachingIterator;
8+
use RecursiveRegexIterator;
9+
use SplFileInfo;
10+
use function PHPStan\Testing\assertType;
11+
12+
class Foo
13+
{
14+
15+
/**
16+
* @param ParentIterator<string, SplFileInfo, \RecursiveDirectoryIterator> $parent
17+
* @param RecursiveCachingIterator<string, SplFileInfo, \RecursiveDirectoryIterator> $caching
18+
* @param RecursiveRegexIterator<string, SplFileInfo, \RecursiveDirectoryIterator> $regex
19+
*/
20+
public function doFoo($parent, $caching, $regex): void
21+
{
22+
foreach ($parent as $key => $value) {
23+
assertType('string', $key);
24+
assertType('SplFileInfo', $value);
25+
}
26+
foreach ($caching as $key => $value) {
27+
assertType('string', $key);
28+
assertType('SplFileInfo', $value);
29+
}
30+
foreach ($regex as $key => $value) {
31+
assertType('string', $key);
32+
assertType('SplFileInfo', $value);
33+
}
34+
35+
assertType('ParentIterator<string, SplFileInfo, RecursiveDirectoryIterator>', $parent->getChildren());
36+
assertType('RecursiveCachingIterator<string, SplFileInfo, RecursiveDirectoryIterator>', $caching->getChildren());
37+
assertType('RecursiveRegexIterator<string, SplFileInfo, RecursiveDirectoryIterator>', $regex->getChildren());
38+
39+
assertType('Iterator<string, SplFileInfo>', $parent->getInnerIterator());
40+
}
41+
42+
/**
43+
* @param RecursiveArrayIterator<int, string> $it
44+
*/
45+
public function doBar($it): void
46+
{
47+
$caching = new RecursiveCachingIterator($it);
48+
assertType('RecursiveCachingIterator<int, string, RecursiveArrayIterator<int, string>>', $caching);
49+
assertType('string', $caching->current());
50+
assertType('int', $caching->key());
51+
52+
$parent = new ParentIterator($it);
53+
assertType('ParentIterator<int, string, RecursiveArrayIterator<int, string>>', $parent);
54+
55+
$regex = new RecursiveRegexIterator($it, '~foo~');
56+
assertType('RecursiveRegexIterator<int, string, RecursiveArrayIterator<int, string>>', $regex);
57+
assertType('string', $regex->current());
58+
assertType('int', $regex->key());
59+
}
60+
61+
}

‎tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public function testBug9615(): void
673673
$this->phpVersionId = PHP_VERSION_ID;
674674
$this->analyse([__DIR__ . '/data/bug-9615.php'], [
675675
[
676-
'Return type mixed of method Bug9615\ExpectComplaintsHere::accept() is not covariant with tentative return type bool of method FilterIterator<mixed,mixed,Traversable<mixed, mixed>>::accept().',
676+
'Return type mixed of method Bug9615\ExpectComplaintsHere::accept() is not covariant with tentative return type bool of method FilterIterator<mixed,mixed,RecursiveIterator<mixed, mixed>>::accept().',
677677
19,
678678
$tipText,
679679
],

0 commit comments

Comments
 (0)