| Total Complexity | 40 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TextUtils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TextUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class TextUtils |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Get text length. |
||
| 22 | * |
||
| 23 | * @param string $text |
||
| 24 | * @param bool $strict Text length in bytes |
||
| 25 | * |
||
| 26 | * @return int |
||
| 27 | */ |
||
| 28 | public static function getTextLength($text, bool $strict = false) |
||
| 29 | { |
||
| 30 | $lenght = 0; |
||
| 31 | if ($strict) { |
||
| 32 | $lenght = null !== $text ? \strlen($text) : 0; |
||
|
|
|||
| 33 | } else { |
||
| 34 | $lenght = null !== $text ? mb_strlen($text) : 0; |
||
| 35 | } |
||
| 36 | |||
| 37 | return $lenght; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Truncating text. |
||
| 42 | * |
||
| 43 | * @param string $text |
||
| 44 | * @param bool|int $length |
||
| 45 | * @param bool $addDots |
||
| 46 | * @param bool $strict Used when a string length in bytes is required |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public static function textTruncate($text, $length = false, $addDots = true, bool $strict = false) |
||
| 51 | { |
||
| 52 | if (!$length) { |
||
| 53 | $length = Config::main('listview_max_textlength'); |
||
| 54 | } |
||
| 55 | $textLength = self::getTextLength($text, $strict); |
||
| 56 | if ($textLength > $length) { |
||
| 57 | if ($addDots) { |
||
| 58 | $length = $length > 3 ? $length - 3 : 0; |
||
| 59 | $text = $strict ? mb_strcut($text, 0, $length, Config::main('default_charset')) : mb_substr($text, 0, $length, Config::main('default_charset')); |
||
| 60 | $text .= '...'; |
||
| 61 | } else { |
||
| 62 | $text = $strict ? mb_strcut($text, 0, $length, Config::main('default_charset')) : mb_substr($text, 0, $length, Config::main('default_charset')); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return $text; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Truncating HTML by words. |
||
| 71 | * |
||
| 72 | * @param string $html |
||
| 73 | * @param int $length |
||
| 74 | * @param string $ending |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public static function htmlTruncateByWords(string $html, int $length = 0, string $ending = '...'): string |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Truncating HTML. |
||
| 127 | * |
||
| 128 | * @param string $html |
||
| 129 | * @param int $length |
||
| 130 | * @param string $ending |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public static function htmlTruncate(string $html, int $length = 255, string $ending = '...'): string |
||
| 135 | { |
||
| 136 | if (\strlen($html) <= $length) { |
||
| 137 | return $html; |
||
| 138 | } |
||
| 139 | $totalLength = \mb_strlen($ending); |
||
| 140 | $openTagsLength = 0; |
||
| 141 | $openTags = []; |
||
| 142 | preg_match_all('/(<.+?>)?([^<>]*)/s', $html, $tags, PREG_SET_ORDER); |
||
| 143 | $html = ''; |
||
| 144 | foreach ($tags as $tag) { |
||
| 145 | $tagLength = \mb_strlen($tag[0]); |
||
| 146 | if (($totalLength + $tagLength + $openTagsLength) >= $length) { |
||
| 147 | if (empty($html)) { |
||
| 148 | preg_match('/^<\s*([^\s>!]+).*?>$/s', $tag[1], $tagName); |
||
| 149 | $openTags[] = $tagName[1]; |
||
| 150 | $html = $tag[1] . self::textTruncate($tag[2], $length - 3, false); |
||
| 151 | } |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | if (!empty($tag[1])) { |
||
| 155 | if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $tag[1])) { |
||
| 156 | // if tag is a closing tag |
||
| 157 | } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $tag[1], $tagName)) { |
||
| 158 | $pos = array_search(strtolower($tagName[1]), $openTags); |
||
| 159 | if (false !== $pos) { |
||
| 160 | unset($openTags[$pos]); |
||
| 161 | $openTagsLength -= \mb_strlen("</{$tagName[1]}>"); |
||
| 162 | } |
||
| 163 | } elseif (preg_match('/^<\s*([^\s>!]+).*?>$/s', $tag[1], $tagName)) { |
||
| 164 | array_unshift($openTags, strtolower($tagName[1])); |
||
| 165 | $openTagsLength += \mb_strlen("</{$tagName[1]}>"); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | $html .= $tag[0]; |
||
| 169 | $totalLength += $tagLength; |
||
| 170 | } |
||
| 171 | $html .= $ending; |
||
| 172 | if ($openTags) { |
||
| 173 | $html .= '</' . implode('></', $openTags) . '>'; |
||
| 174 | } |
||
| 175 | return $html; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get all attributes of a tag. |
||
| 180 | * |
||
| 181 | * @param string $tag |
||
| 182 | * |
||
| 183 | * @return string[] |
||
| 184 | */ |
||
| 185 | public static function getTagAttributes(string $tag): array |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Truncating text. |
||
| 204 | * |
||
| 205 | * @param string $text |
||
| 206 | * @param bool|int $length |
||
| 207 | * @param bool $addDots |
||
| 208 | * @param bool $strict Used when a string length in bytes is required |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public static function textTruncateWithTooltip($text, $length = false, $addDots = true, bool $strict = false) |
||
| 226 |