Conditions | 13 |
Paths | 76 |
Total Lines | 85 |
Code Lines | 54 |
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'); |
||
1 ignored issue
–
show
|
|||
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; |
||
126 | } // discard junk |
||
127 | if ($log['date'] < $trim_time) { |
||
128 | $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) |
||
129 | } else { |
||
130 | $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines |
||
131 | } |
||
132 | } |
||
133 | |||
134 | if (count($lines) == count($out_lines)) { |
||
135 | // nothing to trim |
||
136 | @unlink($fn . '_tmp'); |
||
1 ignored issue
–
show
|
|||
137 | io_unlock($fn); |
||
138 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | // sort the final result, it shouldn't be necessary, |
||
143 | // however the extra robustness in making the changelog cache self-correcting is worth it |
||
144 | ksort($out_lines); |
||
145 | $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum |
||
146 | if ($extra > 0) { |
||
147 | ksort($old_lines); |
||
148 | $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); |
||
149 | } |
||
150 | |||
151 | $eventData = [ |
||
152 | 'isMedia' => $media_changes, |
||
153 | 'trimmedChangelogLines' => $out_lines, |
||
154 | 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, |
||
155 | ]; |
||
156 | trigger_event('TASK_RECENTCHANGES_TRIM', $eventData); |
||
157 | $out_lines = $eventData['trimmedChangelogLines']; |
||
158 | |||
159 | // save trimmed changelog |
||
160 | io_saveFile($fn . '_tmp', implode('', $out_lines)); |
||
161 | @unlink($fn); |
||
1 ignored issue
–
show
|
|||
162 | if (!rename($fn . '_tmp', $fn)) { |
||
163 | // rename failed so try another way... |
||
164 | io_unlock($fn); |
||
165 | io_saveFile($fn, implode('', $out_lines)); |
||
166 | @unlink($fn . '_tmp'); |
||
1 ignored issue
–
show
|
|||
167 | } else { |
||
168 | io_unlock($fn); |
||
169 | } |
||
170 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
171 | return true; |
||
172 | } |
||
173 | |||
174 | // nothing done |
||
175 | echo "runTrimRecentChanges($media_changes): finished" . NL; |
||
176 | return false; |
||
177 | } |
||
178 | |||
240 |
If you suppress an error, we recommend checking for the error condition explicitly: