| Conditions | 18 |
| Paths | 2 |
| Total Lines | 83 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 70 | public static function methods($class) |
||
| 71 | { |
||
| 72 | $info = new ReflectionClass($class); |
||
| 73 | |||
| 74 | $methods = []; |
||
| 75 | foreach ($info->getMethods(ReflectionProperty::IS_PUBLIC) as $method) |
||
| 76 | { |
||
| 77 | $methods[] = $method->name; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Sort methods that: |
||
| 82 | * * Getters and setters are first |
||
| 83 | * * Getters and setters are side-by-side to each other counterpart |
||
| 84 | */ |
||
| 85 | $sort = function($a, $b) |
||
| 86 | { |
||
| 87 | // Whether is getter or setter |
||
| 88 | $isAGS = false; |
||
| 89 | $isBGS = false; |
||
| 90 | // Whether is getter |
||
| 91 | $isAG = false; |
||
| 92 | $isBG = false; |
||
| 93 | // Whether is setter |
||
| 94 | $isAS = false; |
||
| 95 | $isBS = false; |
||
| 96 | // Check if it's getter or setter |
||
| 97 | if (preg_match('~^[gs]et[A-Z0-9]~', $a)) |
||
| 98 | { |
||
| 99 | if(preg_match('~^get[A-Z0-9]~', $a)) |
||
| 100 | { |
||
| 101 | $isAG = true; |
||
| 102 | } |
||
| 103 | else |
||
| 104 | { |
||
| 105 | $isAS = true; |
||
| 106 | } |
||
| 107 | $a = substr($a, 3); |
||
| 108 | $isAGS = true; |
||
| 109 | } |
||
| 110 | // Check if it's getter or setter |
||
| 111 | if (preg_match('~^[gs]et[A-Z0-9]~', $b)) |
||
| 112 | { |
||
| 113 | if(preg_match('~^get[A-Z0-9]~', $b)) |
||
| 114 | { |
||
| 115 | $isBG = true; |
||
| 116 | } |
||
| 117 | else |
||
| 118 | { |
||
| 119 | $isBS = true; |
||
| 120 | } |
||
| 121 | $b = substr($b, 3); |
||
| 122 | $isBGS = true; |
||
| 123 | } |
||
| 124 | if($isAGS && !$isBGS) |
||
| 125 | { |
||
| 126 | return -1; |
||
| 127 | } |
||
| 128 | if(!$isAGS && $isBGS) |
||
| 129 | { |
||
| 130 | return 1; |
||
| 131 | } |
||
| 132 | if ($a == $b) |
||
| 133 | { |
||
| 134 | if($isAGS && $isBGS) |
||
| 135 | { |
||
| 136 | if($isAG && $isBS) |
||
| 137 | { |
||
| 138 | return -1; |
||
| 139 | } |
||
| 140 | if($isBG && $isAS) |
||
| 141 | { |
||
| 142 | return 1; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | return 0; |
||
| 146 | } |
||
| 147 | return ($a < $b) ? -1 : 1; |
||
| 148 | }; |
||
| 149 | usort($methods, $sort); |
||
| 150 | |||
| 151 | return $methods; |
||
| 152 | } |
||
| 153 | |||
| 173 |