Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ public function getClassReflection(): ?ClassReflection

$classReflection = $reflectionProvider->getClass($this->className);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still cache the classReflection before the withType call ?

Maybe the code could be

if ($this->classReflection === null) {
      $reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
		if (!$reflectionProvider->hasClass($this->className)) {
			return null;
		}
		
		$this->classReflection = $reflectionProvider->getClass($this->className);
}

if ($this->classReflection->isGeneric()) {
     return $this->classReflection->withTypes(array_values($classReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()));
}

return $this->classReflection;

if ($classReflection->isGeneric()) {
return $this->classReflection = $classReflection->withTypes(array_values($classReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()));
return $classReflection->withTypes(array_values($classReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()));
}

return $this->classReflection = $classReflection;
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13985.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Bug13985;

use SplObjectStorage;
use function PHPStan\Testing\assertType;

function example(mixed $param): void
{
if ($param instanceof SplObjectStorage) {
foreach ($param as $key => $value) {
assertType('int', $key);
assertType('object', $value);
}
}
}

class X {}

/**
* @param SplObjectStorage<X, int> $splObjectStorage
* @return void
*/
function genericExample(SplObjectStorage $splObjectStorage): void
{
foreach ($splObjectStorage as $key => $value) {
assertType('int', $key);
assertType('Bug13985\X', $value);
}
assertType('int', $splObjectStorage->getInfo());

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4789.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // lint >= 8.0

namespace Bug4789;

use function PHPStan\Testing\assertType;

function doFoo(\DatePeriod $p) {
foreach ($p as $dt) {
assertType('DateTimeInterface', $dt);
}
}
Loading