| Conditions | 19 |
| Paths | 194 |
| Total Lines | 91 |
| 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 namespace Xoopsmodules\randomquote; |
||
| 191 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 192 | { |
||
| 193 | if ($considerHtml) { |
||
| 194 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 195 | if (strlen(preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 196 | return $text; |
||
| 197 | } |
||
| 198 | // splits all html-tags to scanable lines |
||
| 199 | preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); |
||
| 200 | $total_length = strlen($ending); |
||
| 201 | $open_tags = []; |
||
| 202 | $truncate = ''; |
||
| 203 | foreach ($lines as $line_matchings) { |
||
| 204 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 205 | if (!empty($line_matchings[1])) { |
||
| 206 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 207 | if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 208 | // do nothing |
||
| 209 | // if tag is a closing tag |
||
| 210 | } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 211 | // delete tag from $open_tags list |
||
| 212 | $pos = array_search($tag_matchings[1], $open_tags); |
||
| 213 | if (false !== $pos) { |
||
| 214 | unset($open_tags[$pos]); |
||
| 215 | } |
||
| 216 | // if tag is an opening tag |
||
| 217 | } elseif (preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 218 | // add tag to the beginning of $open_tags list |
||
| 219 | array_unshift($open_tags, strtolower($tag_matchings[1])); |
||
| 220 | } |
||
| 221 | // add html-tag to $truncate'd text |
||
| 222 | $truncate .= $line_matchings[1]; |
||
| 223 | } |
||
| 224 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 225 | $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 226 | if ($total_length + $content_length > $length) { |
||
| 227 | // the number of characters which are left |
||
| 228 | $left = $length - $total_length; |
||
| 229 | $entities_length = 0; |
||
| 230 | // search for html entities |
||
| 231 | if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { |
||
| 232 | // calculate the real length of all entities in the legal range |
||
| 233 | foreach ($entities[0] as $entity) { |
||
| 234 | if ($entity[1] + 1 - $entities_length <= $left) { |
||
| 235 | $left--; |
||
| 236 | $entities_length += strlen($entity[0]); |
||
| 237 | } else { |
||
| 238 | // no more characters left |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | $truncate .= substr($line_matchings[2], 0, $left + $entities_length); |
||
| 244 | // maximum lenght is reached, so get off the loop |
||
| 245 | break; |
||
| 246 | } else { |
||
| 247 | $truncate .= $line_matchings[2]; |
||
| 248 | $total_length += $content_length; |
||
| 249 | } |
||
| 250 | // if the maximum length is reached, get off the loop |
||
| 251 | if ($total_length >= $length) { |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | if (strlen($text) <= $length) { |
||
| 257 | return $text; |
||
| 258 | } else { |
||
| 259 | $truncate = substr($text, 0, $length - strlen($ending)); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | // if the words shouldn't be cut in the middle... |
||
| 263 | if (!$exact) { |
||
| 264 | // ...search the last occurance of a space... |
||
| 265 | $spacepos = strrpos($truncate, ' '); |
||
| 266 | if (isset($spacepos)) { |
||
| 267 | // ...and cut the text in this position |
||
| 268 | $truncate = substr($truncate, 0, $spacepos); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | // add the defined ending to the text |
||
| 272 | $truncate .= $ending; |
||
| 273 | if ($considerHtml) { |
||
| 274 | // close all unclosed html-tags |
||
| 275 | foreach ($open_tags as $tag) { |
||
| 276 | $truncate .= '</' . $tag . '>'; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | return $truncate; |
||
| 281 | } |
||
| 282 | } |
||
| 283 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.