| Conditions | 5 |
| Paths | 16 |
| Total Lines | 59 |
| Code Lines | 35 |
| 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 |
||
| 98 | function drush_cerbere_update() |
||
| 99 | { |
||
| 100 | // Get paramaters. |
||
| 101 | $patterns = func_get_args(); |
||
| 102 | if (empty($patterns)) { |
||
| 103 | $patterns = array( |
||
| 104 | '*.info', |
||
| 105 | '*.info.yml', |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Get options. |
||
| 110 | $format = drush_get_option('format', 'table'); |
||
| 111 | $flat = in_array($format, array('table', 'csv')); |
||
| 112 | $level = drush_get_option('level', 'all'); |
||
| 113 | $use_cache = !drush_get_option('no-cache', false); |
||
| 114 | $progress = !drush_get_option('no-progress', false); |
||
| 115 | $hacked = drush_get_option('hacked', false); |
||
| 116 | |||
| 117 | $cerbere = new Cerbere(); |
||
| 118 | |||
| 119 | // Parsers. |
||
| 120 | $cerbere->addParser(new Composer()); |
||
| 121 | $cerbere->addParser(new Info()); |
||
| 122 | $cerbere->addParser(new Make()); |
||
| 123 | $cerbere->addParser(new Yaml()); |
||
| 124 | |||
| 125 | // Action. |
||
| 126 | $action = new Update(); |
||
| 127 | if ($use_cache) { |
||
| 128 | $cache = new FilesystemCache(sys_get_temp_dir() . '/cerbere'); |
||
| 129 | $action->setCache($cache); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Progress bar. |
||
| 133 | if ($progress) { |
||
| 134 | $progress_bar = new CerbereProgressBarListener(); |
||
| 135 | $cerbere->addLoggerListener($progress_bar); |
||
| 136 | $action->addLoggerListener($progress_bar); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Hacked reporting. |
||
| 140 | if ($hacked) { |
||
| 141 | $hacked_listener = new CerbereHackedListener(); |
||
| 142 | $action->addLoggerListener($hacked_listener); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Job. |
||
| 146 | $job = new Job(); |
||
| 147 | $job->setVersioning(new Local()); |
||
| 148 | $job->setAction($action); |
||
| 149 | $job->setSource(getcwd(), array()); |
||
| 150 | $job->setPatterns($patterns, true); |
||
| 151 | |||
| 152 | // Run it ! |
||
| 153 | $report = $cerbere->run($job, array('flat' => $flat, 'level' => $level)); |
||
| 154 | |||
| 155 | return $report; |
||
| 156 | } |
||
| 157 | |||
| 212 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.