Conditions | 22 |
Paths | 2214 |
Total Lines | 81 |
Code Lines | 41 |
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 |
||
252 | public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { |
||
253 | $text = ''; |
||
254 | $trimmed = false; |
||
255 | |||
256 | if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
||
257 | |||
258 | if ( $force ) { |
||
259 | $text = ClassyHelper::trim_words($this->post_excerpt, $len, false); |
||
260 | $trimmed = true; |
||
261 | } else { |
||
262 | $text = $this->post_excerpt; |
||
263 | } |
||
264 | |||
265 | } |
||
266 | |||
267 | if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) { |
||
268 | |||
269 | $pieces = explode($readmore_matches[0], $this->post_content); |
||
270 | $text = $pieces[0]; |
||
271 | |||
272 | if ( $force ) { |
||
273 | $text = ClassyHelper::trim_words($text, $len, false); |
||
274 | $trimmed = true; |
||
275 | } |
||
276 | |||
277 | $text = do_shortcode( $text ); |
||
278 | |||
279 | } |
||
280 | |||
281 | if ( !strlen($text) ) { |
||
282 | |||
283 | $text = ClassyHelper::trim_words($this->get_content(), $len, false); |
||
284 | $trimmed = true; |
||
285 | |||
286 | } |
||
287 | |||
288 | if ( !strlen(trim($text)) ) { |
||
289 | |||
290 | return trim($text); |
||
291 | |||
292 | } |
||
293 | |||
294 | if ( $strip ) { |
||
295 | |||
296 | $text = trim(strip_tags($text)); |
||
297 | |||
298 | } |
||
299 | |||
300 | if ( strlen($text) ) { |
||
301 | |||
302 | $text = trim($text); |
||
303 | $last = $text[strlen($text) - 1]; |
||
304 | |||
305 | if ( $last != '.' && $trimmed ) { |
||
306 | $text .= ' … '; |
||
307 | } |
||
308 | |||
309 | if ( !$strip ) { |
||
310 | $last_p_tag = strrpos($text, '</p>'); |
||
311 | if ( $last_p_tag !== false ) { |
||
312 | $text = substr($text, 0, $last_p_tag); |
||
313 | } |
||
314 | if ( $last != '.' && $trimmed ) { |
||
315 | $text .= ' … '; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
||
320 | $text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore_matches[1]) . '</a>'; |
||
321 | } elseif ( $readmore ) { |
||
322 | $text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore) . '</a>'; |
||
323 | } |
||
324 | |||
325 | if ( !$strip ) { |
||
326 | $text .= '</p>'; |
||
327 | } |
||
328 | |||
329 | } |
||
330 | |||
331 | return trim($text); |
||
332 | } |
||
333 | |||
334 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.