Passed
Push — main ( 647b36...a2d64f )
by Fractal
01:40
created

ClassHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 10
c 1
b 0
f 0
dl 0
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getShortName() 0 3 1
A getParentReflectionClass() 0 3 2
A getReflectionAttributes() 0 3 1
A getReflectionClass() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\TransactionalMessenger\Helper;
6
7
use JetBrains\PhpStorm\Immutable;
8
9
/** @internal */
10
#[Immutable]
11
final class ClassHelper
12
{
13
    final public const DEFAULT_SHORT_NAME = 'InvalidClassName';
14
15
    private function __construct()
16
    {
17
    }
18
19
    public static function getShortName(string|object $target): string
20
    {
21
        return self::getReflectionClass($target)?->getShortName() ?? self::DEFAULT_SHORT_NAME;
22
    }
23
24
    /**
25
     * @template T
26
     *
27
     * @param class-string<T>|T $target
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T>|T at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>|T.
Loading history...
28
     *
29
     * @return null|\ReflectionClass<T>
30
     */
31
    public static function getReflectionClass(string|object $target): ?\ReflectionClass
32
    {
33
        try {
34
            return $target instanceof \ReflectionClass ? $target : new \ReflectionClass($target);
35
        } catch (\ReflectionException) {
36
            return null;
37
        }
38
    }
39
40
    /**
41
     * @template T
42
     *
43
     * @param class-string<T>|T $target
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T>|T at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>|T.
Loading history...
44
     *
45
     * @return null|\ReflectionClass<T>
46
     */
47
    public static function getParentReflectionClass(string|object $target): ?\ReflectionClass
48
    {
49
        return self::getReflectionClass($target)?->getParentClass() ?: null;
50
    }
51
52
    /**
53
     * @template T
54
     *
55
     * @param class-string<T> $attributeClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
56
     *
57
     * @return \Iterator<\ReflectionAttribute<T>>
58
     */
59
    public static function getReflectionAttributes(string|object $target, string $attributeClass): iterable
60
    {
61
        return self::getReflectionClass($target)?->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF) ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getReflecti..._INSTANCEOF) ?? array() returns the type ReflectionAttribute[]|array which is incompatible with the documented return type Iterator.
Loading history...
62
    }
63
}
64