| Conditions | 13 |
| Paths | 76 |
| Total Lines | 88 |
| 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 |
||
| 93 | protected function runTrimRecentChanges($media_changes = false) |
||
| 94 | { |
||
| 95 | global $conf; |
||
| 96 | |||
| 97 | echo "runTrimRecentChanges($media_changes): started" . NL; |
||
| 98 | |||
| 99 | $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); |
||
| 100 | |||
| 101 | // Trim the Recent Changes |
||
| 102 | // Trims the recent changes cache to the last $conf['changes_days'] recent |
||
| 103 | // changes or $conf['recent'] items, which ever is larger. |
||
| 104 | // The trimming is only done once a day. |
||
| 105 | if (file_exists($fn) && |
||
| 106 | (@filemtime($fn . '.trimmed') + 86400) < time() && |
||
| 107 | !file_exists($fn . '_tmp')) { |
||
| 108 | @touch($fn . '.trimmed'); |
||
| 109 | io_lock($fn); |
||
| 110 | $lines = file($fn); |
||
| 111 | if (count($lines) <= $conf['recent']) { |
||
| 112 | // nothing to trim |
||
| 113 | io_unlock($fn); |
||
| 114 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock |
||
| 119 | $trim_time = time() - $conf['recent_days'] * 86400; |
||
| 120 | $out_lines = []; |
||
| 121 | $old_lines = []; |
||
| 122 | for ($i = 0; $i < count($lines); $i++) { |
||
|
|
|||
| 123 | $log = parseChangelogLine($lines[$i]); |
||
| 124 | if ($log === false) { |
||
| 125 | continue; // discard junk |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($log['date'] < $trim_time) { |
||
| 129 | // keep old lines for now (append .$i to prevent key collisions) |
||
| 130 | $old_lines[$log['date'] . ".$i"] = $lines[$i]; |
||
| 131 | } else { |
||
| 132 | // definitely keep these lines |
||
| 133 | $out_lines[$log['date'] . ".$i"] = $lines[$i]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if (count($lines) == count($out_lines)) { |
||
| 138 | // nothing to trim |
||
| 139 | @unlink($fn . '_tmp'); |
||
| 140 | io_unlock($fn); |
||
| 141 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | // sort the final result, it shouldn't be necessary, |
||
| 146 | // however the extra robustness in making the changelog cache self-correcting is worth it |
||
| 147 | ksort($out_lines); |
||
| 148 | $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum |
||
| 149 | if ($extra > 0) { |
||
| 150 | ksort($old_lines); |
||
| 151 | $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); |
||
| 152 | } |
||
| 153 | |||
| 154 | $eventData = [ |
||
| 155 | 'isMedia' => $media_changes, |
||
| 156 | 'trimmedChangelogLines' => $out_lines, |
||
| 157 | 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, |
||
| 158 | ]; |
||
| 159 | Event::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData); |
||
| 160 | $out_lines = $eventData['trimmedChangelogLines']; |
||
| 161 | |||
| 162 | // save trimmed changelog |
||
| 163 | io_saveFile($fn . '_tmp', implode('', $out_lines)); |
||
| 164 | @unlink($fn); |
||
| 165 | if (!rename($fn . '_tmp', $fn)) { |
||
| 166 | // rename failed so try another way... |
||
| 167 | io_unlock($fn); |
||
| 168 | io_saveFile($fn, implode('', $out_lines)); |
||
| 169 | @unlink($fn . '_tmp'); |
||
| 170 | } else { |
||
| 171 | io_unlock($fn); |
||
| 172 | } |
||
| 173 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 174 | return true; |
||
| 175 | } |
||
| 176 | |||
| 177 | // nothing done |
||
| 178 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 241 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: