Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Access Union::getObjectClassReflections
  • Loading branch information
VincentLanglet committed Nov 9, 2025
commit 9ff2ba18232da649a5a3d2bd8d526eb5e4b3345e
13 changes: 12 additions & 1 deletion src/Rules/PhpDoc/RequireExtendsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Rules\ClassNameUsageLocation;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_column;
use function array_map;
Expand Down Expand Up @@ -59,8 +60,18 @@ public function checkExtendsTags(Scope $scope, Node $node, array $extendsTags):
continue;
}

if ($type instanceof UnionType) {
$classReflections = [];
foreach ($type->getTypes() as $subType) {
$classReflections[] = $subType->getObjectClassReflections();
}
$classReflections = array_merge(...$classReflections);
} else {
$classReflections = $type->getObjectClassReflections();
}

sort($classNames);
$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $type->getObjectClassReflections());
$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $classReflections);
$referencedClassReflectionsMap = array_column($referencedClassReflections, 0, 1);
foreach ($classNames as $class) {
$referencedClassReflection = $referencedClassReflectionsMap[$class] ?? null;
Expand Down
13 changes: 12 additions & 1 deletion src/Rules/PhpDoc/RequireImplementsDefinitionTraitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Rules\ClassNameUsageLocation;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_column;
use function array_map;
Expand Down Expand Up @@ -66,7 +67,17 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $type->getObjectClassReflections());
if ($type instanceof UnionType) {
$classReflections = [];
foreach ($type->getTypes() as $subType) {
$classReflections[] = $subType->getObjectClassReflections();
}
$classReflections = array_merge(...$classReflections);
} else {
$classReflections = $type->getObjectClassReflections();
}

$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $classReflections);
$referencedClassReflectionsMap = array_column($referencedClassReflections, 0, 1);
foreach ($classNames as $class) {
$referencedClassReflection = $referencedClassReflectionsMap[$class] ?? null;
Expand Down
13 changes: 12 additions & 1 deletion src/Rules/PhpDoc/SealedDefinitionClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Rules\ClassNameUsageLocation;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_column;
use function array_map;
Expand Down Expand Up @@ -69,7 +70,17 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $type->getObjectClassReflections());
if ($type instanceof UnionType) {
$classReflections = [];
foreach ($type->getTypes() as $subType) {
$classReflections[] = $subType->getObjectClassReflections();
}
$classReflections = array_merge(...$classReflections);
} else {
$classReflections = $type->getObjectClassReflections();
}

$referencedClassReflections = array_map(static fn ($reflection) => [$reflection, $reflection->getName()], $classReflections);
$referencedClassReflectionsMap = array_column($referencedClassReflections, 0, 1);
foreach ($classNames as $class) {
$referencedClassReflection = $referencedClassReflectionsMap[$class] ?? null;
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/SealedDefinitionClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function testRule(): void
26,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'PHPDoc tag @phpstan-sealed contains unknown class IncompatibleSealed\UnknownClass.',
46,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/incompatible-sealed.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ interface ValidInterface {}
* @phpstan-sealed SomeInterface
*/
interface ValidInterface2 {}

/**
* @phpstan-sealed UnknownClass|SomeClass
*/
class InvalidClassWithUnion {}