Conditions | 26 |
Paths | 29 |
Total Lines | 83 |
Code Lines | 44 |
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 |
||
68 | public static function getAvailableClassMethods (string $code, string $class): array |
||
69 | { |
||
70 | $code = self::stripComments ($code); |
||
71 | |||
72 | $split1 = $split2 = false; |
||
73 | |||
74 | $len = strlen ($code); |
||
75 | $classLen = strlen ($class); |
||
76 | |||
77 | $class_predefined = false; |
||
78 | $class_close = null; |
||
79 | |||
80 | $methods = []; |
||
81 | |||
82 | for ($i = 0; $i < $len; ++$i) |
||
83 | { |
||
84 | if ($code[$i] == '\'' && !$split2) |
||
85 | $split1 = !$split1; |
||
86 | |||
87 | elseif ($code[$i] == '"' && !$split1) |
||
88 | $split2 = !$split2; |
||
89 | |||
90 | elseif (!$split1 && !$split2) |
||
91 | { |
||
92 | if ($code[$i] == 'c' && substr ($code, $i, 5) == 'class') |
||
93 | { |
||
94 | for ($j = $i + 5; $j < $len; ++$j) |
||
95 | if (in_array ($code[$j], ["\n", "\r", "\t", ' '])) |
||
96 | continue; |
||
97 | |||
98 | else |
||
99 | { |
||
100 | if (substr ($code, $j, $classLen) == $class) |
||
101 | $class_predefined = true; |
||
102 | |||
103 | $i = $j; |
||
104 | |||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | elseif ($class_predefined == true) |
||
110 | { |
||
111 | if ($code[$i] == 's' && substr ($code, $i, 6) == 'static') |
||
112 | { |
||
113 | for ($j = $i + 6; $j < $len; ++$j) |
||
114 | if (!in_array ($code[$j], ["\n", "\r", "\t", ' '])) |
||
115 | break; |
||
116 | |||
117 | if ($code[$j] == 'f' && substr ($code, $j, 8) == 'function') |
||
118 | { |
||
119 | for ($j = $j + 8; $j < $len; ++$j) |
||
120 | if (in_array ($code[$j], ["\n", "\r", "\t", ' '])) |
||
121 | continue; |
||
122 | |||
123 | else |
||
124 | { |
||
125 | $i = $j; |
||
126 | $methods[] = trim (substr ($code, $j, strpos ($code, '(', $j) - $j)); |
||
127 | |||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | elseif ($code[$i] == '{') |
||
134 | { |
||
135 | if ($class_close === null) |
||
136 | $class_close = 1; |
||
137 | |||
138 | else ++$class_close; |
||
139 | } |
||
140 | |||
141 | elseif ($code[$i] == '}') |
||
142 | --$class_close; |
||
143 | |||
144 | if ($class_close === 0) |
||
145 | return $methods; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return $methods; |
||
151 | } |
||
182 |