andrey-helldar /
support
| 1 | <?php |
||||||||
| 2 | /* |
||||||||
| 3 | * This file is part of the "andrey-helldar/support" project. |
||||||||
| 4 | * |
||||||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||||||
| 6 | * file that was distributed with this source code. |
||||||||
| 7 | * |
||||||||
| 8 | * @author Andrey Helldar <[email protected]> |
||||||||
| 9 | * |
||||||||
| 10 | * @copyright 2021 Andrey Helldar |
||||||||
| 11 | * |
||||||||
| 12 | * @license MIT |
||||||||
| 13 | * |
||||||||
| 14 | * @see https://github.com/andrey-helldar/support |
||||||||
| 15 | */ |
||||||||
| 16 | |||||||||
| 17 | namespace Helldar\Support\Helpers; |
||||||||
| 18 | |||||||||
| 19 | use Helldar\Support\Facades\Helpers\Arr as ArrHelper; |
||||||||
| 20 | use Helldar\Support\Facades\Helpers\Is as IsHelper; |
||||||||
| 21 | use Helldar\Support\Facades\Helpers\Reflection as ReflectionHelper; |
||||||||
| 22 | use ReflectionClass; |
||||||||
| 23 | |||||||||
| 24 | class Instance |
||||||||
| 25 | { |
||||||||
| 26 | /** |
||||||||
| 27 | * Checks if the item being checked inherits from other objects and interfaces. |
||||||||
| 28 | * |
||||||||
| 29 | * @param object|string $haystack |
||||||||
| 30 | * @param string|string[] $needles |
||||||||
| 31 | * |
||||||||
| 32 | * @return bool |
||||||||
| 33 | */ |
||||||||
| 34 | 134 | public function of($haystack, $needles): bool |
|||||||
| 35 | { |
||||||||
| 36 | 134 | if (! $this->exists($haystack)) { |
|||||||
| 37 | 122 | return false; |
|||||||
| 38 | } |
||||||||
| 39 | |||||||||
| 40 | 36 | $reflection = $this->resolve($haystack); |
|||||||
| 41 | 36 | $classname = $this->classname($haystack); |
|||||||
| 42 | |||||||||
| 43 | 36 | foreach (ArrHelper::wrap($needles) as $needle) { |
|||||||
| 44 | 36 | if (! $this->exists($needle)) { |
|||||||
| 45 | 2 | continue; |
|||||||
| 46 | } |
||||||||
| 47 | |||||||||
| 48 | if ( |
||||||||
| 49 | 36 | $haystack instanceof $needle || |
|||||||
| 50 | 30 | $classname === $this->classname($needle) || |
|||||||
| 51 | 30 | $reflection->isSubclassOf($needle) || |
|||||||
| 52 | 30 | ($reflection->isInterface() && $reflection->implementsInterface($needle)) || |
|||||||
| 53 | 36 | in_array($needle, $reflection->getTraitNames(), true) |
|||||||
| 54 | ) { |
||||||||
| 55 | 14 | return true; |
|||||||
| 56 | } |
||||||||
| 57 | } |
||||||||
| 58 | |||||||||
| 59 | 30 | return false; |
|||||||
| 60 | } |
||||||||
| 61 | |||||||||
| 62 | /** |
||||||||
| 63 | * Extract the trailing name component from a file path. |
||||||||
| 64 | * |
||||||||
| 65 | * @param object|string $class |
||||||||
| 66 | * |
||||||||
| 67 | * @return string|null |
||||||||
| 68 | */ |
||||||||
| 69 | 2 | public function basename($class): ?string |
|||||||
| 70 | { |
||||||||
| 71 | 2 | $class = $this->classname($class); |
|||||||
| 72 | |||||||||
| 73 | 2 | return basename(str_replace('\\', '/', $class)) ?: null; |
|||||||
| 74 | } |
||||||||
| 75 | |||||||||
| 76 | /** |
||||||||
| 77 | * Gets the class name of the object. |
||||||||
| 78 | * |
||||||||
| 79 | * @param object|string|null $class |
||||||||
| 80 | * |
||||||||
| 81 | * @return string|null |
||||||||
| 82 | */ |
||||||||
| 83 | 42 | public function classname($class = null): ?string |
|||||||
| 84 | { |
||||||||
| 85 | 42 | if (IsHelper::object($class)) { |
|||||||
| 86 | 40 | return get_class($class); |
|||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
| 87 | } |
||||||||
| 88 | |||||||||
| 89 | 36 | return class_exists($class) || interface_exists($class) ? $class : null; |
|||||||
|
0 ignored issues
–
show
It seems like
$class can also be of type null and object; however, parameter $interface of interface_exists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$class can also be of type null and object; however, parameter $class of class_exists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 90 | } |
||||||||
| 91 | |||||||||
| 92 | /** |
||||||||
| 93 | * Checks if the object exists. |
||||||||
| 94 | * |
||||||||
| 95 | * @param object|string $haystack |
||||||||
| 96 | * |
||||||||
| 97 | * @return bool |
||||||||
| 98 | */ |
||||||||
| 99 | 136 | public function exists($haystack): bool |
|||||||
| 100 | { |
||||||||
| 101 | 136 | if (IsHelper::object($haystack)) { |
|||||||
| 102 | 38 | return true; |
|||||||
| 103 | } |
||||||||
| 104 | |||||||||
| 105 | 136 | return IsHelper::string($haystack) && (class_exists($haystack) || interface_exists($haystack) || trait_exists($haystack)); |
|||||||
|
0 ignored issues
–
show
It seems like
$haystack can also be of type object; however, parameter $class of class_exists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$haystack can also be of type object; however, parameter $interface of interface_exists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$haystack can also be of type object; however, parameter $trait of trait_exists() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 106 | } |
||||||||
| 107 | |||||||||
| 108 | /** |
||||||||
| 109 | * Creates a ReflectionClass object. |
||||||||
| 110 | * |
||||||||
| 111 | * @param object|ReflectionClass|string $class |
||||||||
| 112 | * |
||||||||
| 113 | * @return \ReflectionClass |
||||||||
| 114 | */ |
||||||||
| 115 | 36 | protected function resolve($class): ReflectionClass |
|||||||
| 116 | { |
||||||||
| 117 | 36 | return ReflectionHelper::resolve($class); |
|||||||
| 118 | } |
||||||||
| 119 | } |
||||||||
| 120 |