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