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\MetricsPower\Helper; |
||
17 | |||
18 | use FRZB\Component\MetricsPower\Traits\WithPrivateEmptyConstructor; |
||
19 | use JetBrains\PhpStorm\Immutable; |
||
20 | |||
21 | /** @internal */ |
||
22 | #[Immutable] |
||
23 | final class ClassHelper |
||
24 | { |
||
25 | use WithPrivateEmptyConstructor; |
||
26 | |||
27 | final public const DEFAULT_SHORT_NAME = 'InvalidClassName'; |
||
28 | |||
29 | public static function getShortName(object|string $target): string |
||
30 | { |
||
31 | return self::getReflectionClass($target)?->getShortName() ?? self::DEFAULT_SHORT_NAME; |
||
32 | } |
||
33 | |||
34 | public static function getProperties(object $target): array |
||
35 | { |
||
36 | return get_object_vars($target); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @template T |
||
41 | * |
||
42 | * @param class-string<T>|T $target |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
43 | * |
||
44 | * @return null|\ReflectionClass<T> |
||
45 | */ |
||
46 | public static function getReflectionClass(object|string $target): ?\ReflectionClass |
||
47 | { |
||
48 | try { |
||
49 | return $target instanceof \ReflectionClass ? $target : new \ReflectionClass($target); |
||
50 | } catch (\ReflectionException) { |
||
51 | return null; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @template T |
||
57 | * |
||
58 | * @param class-string<T>|T $target |
||
0 ignored issues
–
show
|
|||
59 | * |
||
60 | * @return null|\ReflectionClass<T> |
||
61 | */ |
||
62 | public static function getParentReflectionClass(object|string $target): ?\ReflectionClass |
||
63 | { |
||
64 | return self::getReflectionClass($target)?->getParentClass() ?: null; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @template T |
||
69 | * |
||
70 | * @param class-string<T> $attributeClass |
||
0 ignored issues
–
show
|
|||
71 | * |
||
72 | * @return \Iterator<\ReflectionAttribute<T>> |
||
73 | */ |
||
74 | public static function getReflectionAttributes(object|string $target, string $attributeClass): iterable |
||
75 | { |
||
76 | return self::getReflectionClass($target)?->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF) ?? []; |
||
0 ignored issues
–
show
|
|||
77 | } |
||
78 | } |
||
79 |