| Conditions | 10 |
| Paths | 9 |
| Total Lines | 65 |
| 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 |
||
| 42 | public static function getHooks($classpath, $namespace, $classname = null, $extends = null, $method = null) { |
||
| 43 | |||
| 44 | // |
||
| 45 | $hooks = []; |
||
| 46 | |||
| 47 | // Get the filenames. |
||
| 48 | $filenames = FileHelper::getFileNames($classpath, ".php"); |
||
| 49 | |||
| 50 | // Handle each filenames. |
||
| 51 | foreach ($filenames as $filename) { |
||
| 52 | |||
| 53 | // Check the class name. |
||
| 54 | if (null !== $classname && 0 === preg_match($classname, $filename)) { |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | // Import the class. |
||
| 59 | try { |
||
| 60 | require_once $classpath . "/" . $filename; |
||
| 61 | } catch (Exception $ex) { |
||
| 62 | throw new SyntaxErrorException($classpath . "/" . $filename); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Init. the complete class name. |
||
| 66 | $completeClassname = $namespace . basename($filename, ".php"); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $rc = new ReflectionClass($completeClassname); |
||
| 70 | } catch (ReflectionException $ex) { |
||
| 71 | throw new ClassNotFoundException($completeClassname, $ex); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Check the extends. |
||
| 75 | if (false === (null === $extends || true === $rc->isSubclassOf($extends))) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Initialize the hook. |
||
| 80 | $hook = []; |
||
| 81 | |||
| 82 | $hook["classpath"] = $classpath; |
||
| 83 | $hook["namespace"] = $namespace; |
||
| 84 | $hook["filename"] = $filename; |
||
| 85 | $hook["class"] = $rc; |
||
| 86 | $hook["method"] = null; |
||
| 87 | |||
| 88 | $hooks[] = $hook; |
||
| 89 | |||
| 90 | // Check the method. |
||
| 91 | if (null === $method) { |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $rm = $rc->getMethod($method); |
||
| 97 | |||
| 98 | $hooks[count($hooks) - 1]["method"] = $rm; |
||
| 99 | } catch (ReflectionException $ex) { |
||
| 100 | throw new MethodNotFoundException($method, $ex); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | // Returns the hooks. |
||
| 105 | return $hooks; |
||
| 106 | } |
||
| 107 | |||
| 143 |