| Conditions | 19 |
| Paths | 194 |
| Total Lines | 99 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 declare(strict_types=1); |
||
| 55 | public static function truncateHtml( |
||
| 56 | string $text, |
||
| 57 | ?int $length = null, |
||
| 58 | ?string $ending = null, |
||
| 59 | ?bool $exact = null, |
||
| 60 | ?bool $considerHtml = null |
||
| 61 | ): string { |
||
| 62 | $length ??= 100; |
||
| 63 | $ending ??= '...'; |
||
| 64 | $exact ??= false; |
||
| 65 | $considerHtml ??= true; |
||
| 66 | $openTags = []; |
||
| 67 | if ($considerHtml) { |
||
| 68 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 69 | if (\mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 70 | return $text; |
||
| 71 | } |
||
| 72 | // splits all html-tags to scanable lines |
||
| 73 | \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
||
| 74 | $totalLength = (int)\mb_strlen($ending); |
||
| 75 | //$openTags = []; |
||
| 76 | $truncate = ''; |
||
| 77 | foreach ($lines as $lineMatchings) { |
||
| 78 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 79 | if (!empty($lineMatchings[1])) { |
||
| 80 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 81 | if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $lineMatchings[1])) { |
||
| 82 | // do nothing |
||
| 83 | // if tag is a closing tag |
||
| 84 | } elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/', $lineMatchings[1], $tagMatchings)) { |
||
| 85 | // delete tag from $openTags list |
||
| 86 | $pos = \array_search($tagMatchings[1], $openTags, true); |
||
| 87 | if (false !== $pos) { |
||
| 88 | unset($openTags[$pos]); |
||
| 89 | } |
||
| 90 | // if tag is an opening tag |
||
| 91 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $lineMatchings[1], $tagMatchings)) { |
||
| 92 | // add tag to the beginning of $openTags list |
||
| 93 | \array_unshift($openTags, \mb_strtolower($tagMatchings[1])); |
||
| 94 | } |
||
| 95 | // add html-tag to $truncate'd text |
||
| 96 | $truncate .= $lineMatchings[1]; |
||
| 97 | } |
||
| 98 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 99 | $contentLength = (int)\mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $lineMatchings[2])); |
||
| 100 | if (($totalLength + $contentLength) > $length) { |
||
| 101 | // the number of characters which are left |
||
| 102 | $left = $length - $totalLength; |
||
| 103 | $entitiesLength = 0; |
||
| 104 | // search for html entities |
||
| 105 | if (\preg_match_all('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', $lineMatchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
||
| 106 | // calculate the real length of all entities in the legal range |
||
| 107 | foreach ($entities[0] as $entity) { |
||
| 108 | if ($left >= $entity[1] + 1 - $entitiesLength) { |
||
| 109 | $left--; |
||
| 110 | $entitiesLength += \mb_strlen($entity[0]); |
||
| 111 | } else { |
||
| 112 | // no more characters left |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | $truncate .= \mb_substr($lineMatchings[2], 0, $left + $entitiesLength); |
||
| 118 | // maximum length is reached, so get off the loop |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | $truncate .= $lineMatchings[2]; |
||
| 122 | $totalLength += $contentLength; |
||
| 123 | |||
| 124 | // if the maximum length is reached, get off the loop |
||
| 125 | if ($totalLength >= $length) { |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | if (\mb_strlen($text) <= $length) { |
||
| 131 | return $text; |
||
| 132 | } |
||
| 133 | $truncate = \mb_substr($text, 0, $length - \mb_strlen($ending)); |
||
| 134 | } |
||
| 135 | // if the words shouldn't be cut in the middle... |
||
| 136 | if (!$exact) { |
||
| 137 | // ...search the last occurance of a space... |
||
| 138 | $spacepos = \mb_strrpos($truncate, ' '); |
||
| 139 | if (isset($spacepos)) { |
||
| 140 | // ...and cut the text in this position |
||
| 141 | $truncate = \mb_substr($truncate, 0, $spacepos); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | // add the defined ending to the text |
||
| 145 | $truncate .= $ending; |
||
| 146 | if ($considerHtml) { |
||
| 147 | // close all unclosed html-tags |
||
| 148 | foreach ($openTags as $tag) { |
||
| 149 | $truncate .= '</' . $tag . '>'; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $truncate; |
||
| 154 | } |
||
| 270 |