Conditions | 18 |
Paths | 4 |
Total Lines | 107 |
Code Lines | 70 |
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 |
||
108 | protected static function tasks(array $tasks) |
||
109 | { |
||
110 | $buildTask = function ($name, $steps) { |
||
111 | $body = function () {}; |
||
112 | $task = task($name, $body); |
||
113 | |||
114 | foreach ($steps as $step) { |
||
115 | $buildStep = function ($step) use (&$body, $task) { |
||
116 | extract($step); |
||
117 | |||
118 | if (isset($cd)) { |
||
119 | $prev = $body; |
||
120 | $body = function () use ($cd, $prev) { |
||
121 | $prev(); |
||
122 | cd($cd); |
||
123 | }; |
||
124 | } |
||
125 | |||
126 | if (isset($run)) { |
||
127 | $has = 'run'; |
||
128 | $prev = $body; |
||
129 | $body = function () use ($run, $prev) { |
||
130 | $prev(); |
||
131 | try { |
||
132 | run($run); |
||
133 | } catch (Exception $e) { |
||
134 | $e->setTaskFilename(self::$recipeFilename); |
||
135 | $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run)); |
||
136 | throw $e; |
||
137 | } |
||
138 | }; |
||
139 | } |
||
140 | |||
141 | if (isset($run_locally)) { |
||
142 | if (isset($has)) { |
||
143 | throw new ConfigurationException("Task step can not have both $has and run_locally."); |
||
144 | } |
||
145 | $has = 'run_locally'; |
||
146 | $prev = $body; |
||
147 | $body = function () use ($run_locally, $prev) { |
||
148 | $prev(); |
||
149 | try { |
||
150 | runLocally($run_locally); |
||
151 | } catch (Exception $e) { |
||
152 | $e->setTaskFilename(self::$recipeFilename); |
||
153 | $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run_locally)); |
||
154 | throw $e; |
||
155 | } |
||
156 | }; |
||
157 | } |
||
158 | |||
159 | if (isset($upload)) { |
||
160 | if (isset($has)) { |
||
161 | throw new ConfigurationException("Task step can not have both $has and upload."); |
||
162 | } |
||
163 | $has = 'upload'; |
||
164 | $prev = $body; |
||
165 | $body = function () use ($upload, $prev) { |
||
166 | $prev(); |
||
167 | upload($upload['src'], $upload['dest']); |
||
168 | }; |
||
169 | } |
||
170 | |||
171 | if (isset($download)) { |
||
172 | if (isset($has)) { |
||
173 | throw new ConfigurationException("Task step can not have both $has and download."); |
||
174 | } |
||
175 | $has = 'download'; |
||
176 | $prev = $body; |
||
177 | $body = function () use ($download, $prev) { |
||
178 | $prev(); |
||
179 | download($download['src'], $download['dest']); |
||
180 | }; |
||
181 | } |
||
182 | |||
183 | $methods = [ |
||
184 | 'desc', |
||
185 | 'once', |
||
186 | 'hidden', |
||
187 | 'limit', |
||
188 | 'select', |
||
189 | ]; |
||
190 | foreach ($methods as $method) { |
||
191 | if (isset($$method)) { |
||
192 | $task->$method($$method); |
||
193 | } |
||
194 | } |
||
195 | }; |
||
196 | |||
197 | $buildStep($step); |
||
198 | $task->setCallback($body); |
||
199 | } |
||
200 | }; |
||
201 | |||
202 | foreach ($tasks as $name => $config) { |
||
203 | foreach ($config as $key => $value) { |
||
204 | if (!is_int($key) || !is_string($value)) { |
||
205 | goto not_a_group_task; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | // Create a group task. |
||
210 | task($name, $config); |
||
211 | continue; |
||
212 | |||
213 | not_a_group_task: |
||
214 | $buildTask($name, $config); |
||
215 | } |
||
244 |