| Conditions | 31 |
| Paths | 140 |
| Total Lines | 84 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 142 | public static function throwOnRequiredClass(string $class, ?\Exception $previous = null): void |
||
| 143 | { |
||
| 144 | // If the passed class is the resource being checked, we shouldn't throw. |
||
| 145 | if (null === $previous && self::$autoloadedClass === $class) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) { |
||
| 150 | if (null !== $previous) { |
||
| 151 | throw $previous; |
||
| 152 | } |
||
| 153 | |||
| 154 | return; |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($previous instanceof \ReflectionException) { |
||
| 158 | throw $previous; |
||
| 159 | } |
||
| 160 | |||
| 161 | $message = \sprintf('Class "%s" not found.', $class); |
||
| 162 | |||
| 163 | if ($class !== (self::$autoloadedClass ?? $class)) { |
||
| 164 | $message = substr_replace($message, \sprintf(' while loading "%s"', self::$autoloadedClass), -1, 0); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (null !== $previous) { |
||
| 168 | $message = $previous->getMessage(); |
||
| 169 | } |
||
| 170 | |||
| 171 | $e = new \ReflectionException($message, 0, $previous); |
||
| 172 | |||
| 173 | if (null !== $previous) { |
||
| 174 | throw $e; |
||
| 175 | } |
||
| 176 | |||
| 177 | $trace = debug_backtrace(); |
||
| 178 | $autoloadFrame = [ |
||
| 179 | 'function' => 'spl_autoload_call', |
||
| 180 | 'args' => [$class], |
||
| 181 | ]; |
||
| 182 | |||
| 183 | if (isset($trace[1])) { |
||
| 184 | $callerFrame = $trace[1]; |
||
| 185 | $i = 2; |
||
| 186 | } elseif (false !== $i = array_search($autoloadFrame, $trace, true)) { |
||
| 187 | $callerFrame = $trace[++$i]; |
||
| 188 | } else { |
||
| 189 | throw $e; |
||
| 190 | } |
||
| 191 | |||
| 192 | if (isset($callerFrame['function']) && !isset($callerFrame['class'])) { |
||
| 193 | switch ($callerFrame['function']) { |
||
| 194 | case 'get_class_methods': |
||
| 195 | case 'get_class_vars': |
||
| 196 | case 'get_parent_class': |
||
| 197 | case 'is_a': |
||
| 198 | case 'is_subclass_of': |
||
| 199 | case 'class_exists': |
||
| 200 | case 'class_implements': |
||
| 201 | case 'class_parents': |
||
| 202 | case 'trait_exists': |
||
| 203 | case 'defined': |
||
| 204 | case 'interface_exists': |
||
| 205 | case 'method_exists': |
||
| 206 | case 'property_exists': |
||
| 207 | case 'is_callable': |
||
| 208 | return; |
||
| 209 | } |
||
| 210 | |||
| 211 | $props = [ |
||
| 212 | 'file' => $callerFrame['file'] ?? null, |
||
| 213 | 'line' => $callerFrame['line'] ?? null, |
||
| 214 | 'trace' => \array_slice($trace, 1 + $i), |
||
| 215 | ]; |
||
| 216 | |||
| 217 | foreach ($props as $p => $v) { |
||
| 218 | if (null !== $v) { |
||
| 219 | $r = new \ReflectionProperty(\Exception::class, $p); |
||
| 220 | $r->setValue($e, $v); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | throw $e; |
||
| 226 | } |
||
| 228 |