Skip to content

Commit 841c855

Browse files
Rework
1 parent 7e2ab56 commit 841c855

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\Php;
4+
5+
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\Reflection\AllowedSubTypesClassReflectionExtension;
7+
use PHPStan\Reflection\ClassReflection;
8+
use PHPStan\Type\UnionType;
9+
use function count;
10+
11+
#[AutowiredService]
12+
final class SealedAllowedSubTypesClassReflectionExtension implements AllowedSubTypesClassReflectionExtension
13+
{
14+
15+
public function supports(ClassReflection $classReflection): bool
16+
{
17+
return count($classReflection->getSealedTags()) > 0;
18+
}
19+
20+
public function getAllowedSubTypes(ClassReflection $classReflection): array
21+
{
22+
$types = [];
23+
24+
foreach ($classReflection->getSealedTags() as $sealedTag) {
25+
$type = $sealedTag->getType();
26+
if ($type instanceof UnionType) {
27+
$types = $type->getTypes();
28+
} else {
29+
$types = [$type];
30+
}
31+
}
32+
33+
return $types;
34+
}
35+
36+
}

tests/PHPStan/Rules/Classes/AllowedSubTypesRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ public function testRule(): void
2626
]);
2727
}
2828

29+
public function testSealed(): void
30+
{
31+
$this->analyse([__DIR__ . '/data/sealed.php'], [
32+
[
33+
'Type Sealed\BazClass is not allowed to be a subtype of Sealed\BaseClass.',
34+
11,
35+
],
36+
[
37+
'Type Sealed\BazClass2 is not allowed to be a subtype of Sealed\BaseInterface.',
38+
19,
39+
],
40+
[
41+
'Type Sealed\BazInterface is not allowed to be a subtype of Sealed\BaseInterface2.',
42+
27,
43+
],
44+
]);
45+
}
46+
2947
public static function getAdditionalConfigFiles(): array
3048
{
3149
return [

tests/PHPStan/Rules/Classes/SealedRuleTest.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)