Skip to content

Commit d298e69

Browse files
authored
SplObjectStorage iterates over objects (#4789)
1 parent cea927d commit d298e69

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

src/Type/ObjectType.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,21 +1735,20 @@ public function getNakedClassReflection(): ?ClassReflection
17351735

17361736
public function getClassReflection(): ?ClassReflection
17371737
{
1738-
if ($this->classReflection !== null) {
1739-
return $this->classReflection;
1740-
}
1738+
if ($this->classReflection === null) {
1739+
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
1740+
if (!$reflectionProvider->hasClass($this->className)) {
1741+
return null;
1742+
}
17411743

1742-
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
1743-
if (!$reflectionProvider->hasClass($this->className)) {
1744-
return null;
1744+
$this->classReflection = $reflectionProvider->getClass($this->className);
17451745
}
17461746

1747-
$classReflection = $reflectionProvider->getClass($this->className);
1748-
if ($classReflection->isGeneric()) {
1749-
return $this->classReflection = $classReflection->withTypes(array_values($classReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()));
1747+
if ($this->classReflection->isGeneric()) {
1748+
return $this->classReflection->withTypes(array_values($this->classReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()));
17501749
}
17511750

1752-
return $this->classReflection = $classReflection;
1751+
return $this->classReflection;
17531752
}
17541753

17551754
public function getAncestorWithClassName(string $className): ?self
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Bug13985;
4+
5+
use SplObjectStorage;
6+
use function PHPStan\Testing\assertType;
7+
8+
function example(mixed $param): void
9+
{
10+
if ($param instanceof SplObjectStorage) {
11+
foreach ($param as $key => $value) {
12+
assertType('int', $key);
13+
assertType('object', $value);
14+
}
15+
}
16+
}
17+
18+
class X {}
19+
20+
/**
21+
* @param SplObjectStorage<X, int> $splObjectStorage
22+
* @return void
23+
*/
24+
function genericExample(SplObjectStorage $splObjectStorage): void
25+
{
26+
foreach ($splObjectStorage as $key => $value) {
27+
assertType('int', $key);
28+
assertType('Bug13985\X', $value);
29+
}
30+
assertType('int', $splObjectStorage->getInfo());
31+
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug4789;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function doFoo(\DatePeriod $p) {
8+
foreach ($p as $dt) {
9+
assertType('DateTimeInterface', $dt);
10+
}
11+
}

0 commit comments

Comments
 (0)