| Conditions | 13 |
| Paths | 76 |
| Total Lines | 84 |
| Code Lines | 53 |
| 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 |
||
| 36 | protected function runTrimRecentChanges($media_changes = false) |
||
| 37 | { |
||
| 38 | global $conf; |
||
| 39 | |||
| 40 | echo "runTrimRecentChanges($media_changes): started" . NL; |
||
| 41 | |||
| 42 | $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); |
||
| 43 | |||
| 44 | // Trim the Recent Changes |
||
| 45 | // Trims the recent changes cache to the last $conf['changes_days'] recent |
||
| 46 | // changes or $conf['recent'] items, which ever is larger. |
||
| 47 | // The trimming is only done once a day. |
||
| 48 | if (file_exists($fn) && |
||
| 49 | (@filemtime($fn . '.trimmed') + 86400) < time() && |
||
| 50 | !file_exists($fn . '_tmp')) { |
||
| 51 | @touch($fn . '.trimmed'); |
||
|
1 ignored issue
–
show
|
|||
| 52 | io_lock($fn); |
||
| 53 | $lines = file($fn); |
||
| 54 | if (count($lines) <= $conf['recent']) { |
||
| 55 | // nothing to trim |
||
| 56 | io_unlock($fn); |
||
| 57 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock |
||
| 62 | $trim_time = time() - $conf['recent_days'] * 86400; |
||
| 63 | $out_lines = []; |
||
| 64 | $old_lines = []; |
||
| 65 | for ($i = 0; $i < count($lines); $i++) { |
||
| 66 | $log = parseChangelogLine($lines[$i]); |
||
| 67 | if ($log === false) { |
||
| 68 | continue; |
||
| 69 | } // discard junk |
||
| 70 | if ($log['date'] < $trim_time) { |
||
| 71 | $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) |
||
| 72 | } else { |
||
| 73 | $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if (count($lines) == count($out_lines)) { |
||
| 78 | // nothing to trim |
||
| 79 | @unlink($fn . '_tmp'); |
||
|
1 ignored issue
–
show
|
|||
| 80 | io_unlock($fn); |
||
| 81 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | // sort the final result, it shouldn't be necessary, |
||
| 86 | // however the extra robustness in making the changelog cache self-correcting is worth it |
||
| 87 | ksort($out_lines); |
||
| 88 | $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum |
||
| 89 | if ($extra > 0) { |
||
| 90 | ksort($old_lines); |
||
| 91 | $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); |
||
| 92 | } |
||
| 93 | |||
| 94 | $eventData = [ |
||
| 95 | 'trimmedChangelogLines' => $out_lines, |
||
| 96 | 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, |
||
| 97 | ]; |
||
| 98 | trigger_event('TRIM_RECENT_CHANGES', $eventData); |
||
| 99 | $out_lines = $eventData['trimmedChangelogLines']; |
||
| 100 | |||
| 101 | // save trimmed changelog |
||
| 102 | io_saveFile($fn . '_tmp', implode('', $out_lines)); |
||
| 103 | @unlink($fn); |
||
|
1 ignored issue
–
show
|
|||
| 104 | if (!rename($fn . '_tmp', $fn)) { |
||
| 105 | // rename failed so try another way... |
||
| 106 | io_unlock($fn); |
||
| 107 | io_saveFile($fn, implode('', $out_lines)); |
||
| 108 | @unlink($fn . '_tmp'); |
||
|
1 ignored issue
–
show
|
|||
| 109 | } else { |
||
| 110 | io_unlock($fn); |
||
| 111 | } |
||
| 112 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | // nothing done |
||
| 117 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 182 |
If you suppress an error, we recommend checking for the error condition explicitly: