hgraca /
php-helper
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Hgraca\Helper; |
||
| 4 | |||
| 5 | use Hgraca\Helper\Concept\ReflectionHelperAbstract; |
||
| 6 | use ReflectionClass; |
||
| 7 | use ReflectionException; |
||
| 8 | use ReflectionMethod; |
||
| 9 | use ReflectionProperty; |
||
| 10 | |||
| 11 | final class ClassHelper extends ReflectionHelperAbstract |
||
| 12 | { |
||
| 13 | 1 | public static function extractCanonicalClassName(string $classFqcn): string |
|
| 14 | { |
||
| 15 | 1 | return substr($classFqcn, strrpos($classFqcn, '\\') + 1); |
|
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Returns a key-value array with all constants in the class |
||
| 20 | * It uses Reflection to find them |
||
| 21 | * |
||
| 22 | * @param string $classFqcn |
||
| 23 | * |
||
| 24 | * @return array |
||
| 25 | */ |
||
| 26 | 1 | public static function findConstants(string $classFqcn): array |
|
| 27 | { |
||
| 28 | 1 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
| 29 | |||
| 30 | 1 | return $reflectionClass->getConstants(); |
|
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Returns an array with all properties names of the mixed |
||
| 35 | * |
||
| 36 | * @param string $classFqcn |
||
| 37 | * @param string[] $excludedNames |
||
| 38 | * @param string[] $excludedVisibility = ['public', 'protected', 'private', 'static'] |
||
| 39 | * |
||
| 40 | * @return string[] |
||
| 41 | */ |
||
| 42 | 4 | public static function findPropertiesNames( |
|
| 43 | string $classFqcn, |
||
| 44 | array $excludedNames = [], |
||
| 45 | array $excludedVisibility = [] |
||
| 46 | ) { |
||
| 47 | 4 | $defaultVisibility = ['public' => true, 'protected' => true, 'private' => true, 'static' => true]; |
|
| 48 | 4 | $excludedVisibility = array_merge( |
|
| 49 | $defaultVisibility, |
||
| 50 | 4 | self::setGivenExcludedVisivilitiesToKeysWithValueFalse($excludedVisibility) |
|
| 51 | ); |
||
| 52 | |||
| 53 | 4 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
| 54 | 4 | $reflectionPropertiesArray = $reflectionClass->getProperties(); |
|
| 55 | |||
| 56 | 4 | return self::filterProperties($excludedNames, $excludedVisibility, $reflectionPropertiesArray); |
|
| 57 | } |
||
| 58 | |||
| 59 | 5 | public static function hasMethod(string $classFqcn, string $method): bool |
|
| 60 | { |
||
| 61 | 5 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
| 62 | |||
| 63 | 5 | return $reflectionClass->hasMethod($method); |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $classFqcn |
||
| 68 | * @param string $method |
||
| 69 | * |
||
| 70 | * @return array [index => [name, class]] |
||
| 71 | */ |
||
| 72 | 2 | View Code Duplication | public static function getParameters(string $classFqcn, string $method): array |
| 73 | { |
||
| 74 | try { |
||
| 75 | 2 | $reflectionMethod = new ReflectionMethod($classFqcn, $method); |
|
| 76 | } catch (ReflectionException $e) { |
||
| 77 | return []; |
||
| 78 | } |
||
| 79 | |||
| 80 | 2 | foreach ($reflectionMethod->getParameters() as $index => $param) { |
|
| 81 | 1 | $reflectionParameters[$index]['name'] = $param->getName(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 82 | 1 | if (null !== $param->getClass()) { |
|
| 83 | 1 | $reflectionParameters[$index]['class'] = $param->getClass()->name; |
|
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | 2 | return $reflectionParameters ?? []; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return ReflectionProperty[] |
||
| 92 | */ |
||
| 93 | 10 | public static function getReflectionProperties(string $classFqcn): array |
|
| 94 | { |
||
| 95 | 10 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
| 96 | |||
| 97 | 10 | return $reflectionClass->getProperties(); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param ReflectionProperty[] $propertyList |
||
| 102 | */ |
||
| 103 | 4 | public static function setReflectionPropertiesAccessible(array &$propertyList) |
|
| 104 | { |
||
| 105 | 4 | foreach ($propertyList as $reflectionProperty) { |
|
| 106 | 4 | $reflectionProperty->setAccessible(true); |
|
| 107 | } |
||
| 108 | 4 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Gets the property from the current class, when not available will look on parent classes before failing |
||
| 112 | * |
||
| 113 | * @throws ReflectionException |
||
| 114 | * |
||
| 115 | * @return ReflectionProperty |
||
| 116 | */ |
||
| 117 | 6 | public static function getReflectionProperty(ReflectionClass $class, string $propertyName): ReflectionProperty |
|
| 118 | { |
||
| 119 | try { |
||
| 120 | 6 | return $class->getProperty($propertyName); |
|
| 121 | 4 | } catch (ReflectionException $e) { |
|
| 122 | 4 | $parentClass = $class->getParentClass(); |
|
| 123 | 4 | if ($parentClass === false) { |
|
| 124 | 2 | throw $e; |
|
| 125 | } |
||
| 126 | |||
| 127 | 4 | return self::getReflectionProperty($parentClass, $propertyName); |
|
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 4 | private static function excludeProperty( |
|
| 132 | array $excludedNames, |
||
| 133 | array $excludedVisibility, |
||
| 134 | ReflectionProperty $reflectionProperty |
||
| 135 | ): bool { |
||
| 136 | 4 | return in_array($reflectionProperty->getName(), $excludedNames) || |
|
| 137 | 4 | (!$excludedVisibility['public'] && $reflectionProperty->isPublic()) || |
|
| 138 | 4 | (!$excludedVisibility['protected'] && $reflectionProperty->isProtected()) || |
|
| 139 | 4 | (!$excludedVisibility['private'] && $reflectionProperty->isPrivate()) || |
|
| 140 | 4 | (!$excludedVisibility['static'] && $reflectionProperty->isStatic()); |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string[] $excludedNames |
||
| 145 | * @param bool[] $excludedVisibility |
||
| 146 | * @param ReflectionProperty[] $reflectionPropertiesArray |
||
| 147 | * |
||
| 148 | * @return string[] |
||
| 149 | */ |
||
| 150 | 4 | private static function filterProperties( |
|
| 151 | array $excludedNames, |
||
| 152 | array $excludedVisibility, |
||
| 153 | array $reflectionPropertiesArray |
||
| 154 | ) { |
||
| 155 | 4 | $propertyArray = []; |
|
| 156 | |||
| 157 | /** @var ReflectionProperty $reflectionProperty */ |
||
| 158 | 4 | foreach ($reflectionPropertiesArray as $reflectionProperty) { |
|
| 159 | 4 | if (self::excludeProperty($excludedNames, $excludedVisibility, $reflectionProperty)) { |
|
| 160 | 3 | continue; |
|
| 161 | } |
||
| 162 | |||
| 163 | 4 | $reflectionProperty->setAccessible(true); |
|
| 164 | 4 | $propertyArray[] = $reflectionProperty->getName(); |
|
| 165 | } |
||
| 166 | |||
| 167 | 4 | return $propertyArray; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $excludedVisibility |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | 4 | private static function setGivenExcludedVisivilitiesToKeysWithValueFalse(array $excludedVisibility) |
|
| 176 | { |
||
| 177 | 4 | return array_fill_keys(array_keys(array_flip($excludedVisibility)), false); |
|
| 178 | } |
||
| 179 | } |
||
| 180 |