1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace FRZB\Component\TransactionalMessenger\Helper; |
17
|
|
|
|
18
|
|
|
use JetBrains\PhpStorm\Immutable; |
19
|
|
|
|
20
|
|
|
/** @internal */ |
21
|
|
|
#[Immutable] |
22
|
|
|
final class ClassHelper |
23
|
|
|
{ |
24
|
|
|
final public const DEFAULT_SHORT_NAME = 'InvalidClassName'; |
25
|
|
|
|
26
|
|
|
private function __construct() {} |
27
|
|
|
|
28
|
|
|
public static function getShortName(object|string $target): string |
29
|
|
|
{ |
30
|
|
|
return self::getReflectionClass($target)?->getShortName() ?? self::DEFAULT_SHORT_NAME; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function getInheritanceList(object|string $target): array |
34
|
|
|
{ |
35
|
|
|
$isTargetObject = \is_object($target); |
36
|
|
|
$targetClass = $isTargetObject ? $target::class : $target; |
37
|
|
|
|
38
|
|
|
if (!class_exists($targetClass)) { |
39
|
|
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return [$targetClass => $targetClass, ...(class_parents($target, false) ?: [])]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @psalm-template TTarget |
47
|
|
|
* |
48
|
|
|
* @psalm-param class-string<TTarget>|TTarget $target |
49
|
|
|
* |
50
|
|
|
* @psalm-return ?\ReflectionClass<TTarget> |
51
|
|
|
*/ |
52
|
|
|
public static function getReflectionClass(object|string $target): ?\ReflectionClass |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
return $target instanceof \ReflectionClass ? $target : new \ReflectionClass($target); |
56
|
|
|
} catch (\ReflectionException) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @psalm-template TTarget |
63
|
|
|
* |
64
|
|
|
* @psalm-param class-string<TTarget>|TTarget $target |
65
|
|
|
* |
66
|
|
|
* @psalm-return ?\ReflectionClass<TTarget> |
67
|
|
|
*/ |
68
|
|
|
public static function getParentReflectionClass(object|string $target): ?\ReflectionClass |
69
|
|
|
{ |
70
|
|
|
return self::getReflectionClass($target)?->getParentClass() ?: null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @psalm-template TTarget |
75
|
|
|
* |
76
|
|
|
* @psalm-param class-string<TTarget>|TTarget $target |
77
|
|
|
* |
78
|
|
|
* @psalm-return \Iterator<\ReflectionAttribute<TTarget>> |
79
|
|
|
*/ |
80
|
|
|
public static function getReflectionAttributes(object|string $target, string $attributeClass): iterable |
81
|
|
|
{ |
82
|
|
|
return self::getReflectionClass($target)?->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF) ?? []; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|