| Total Complexity | 76 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SummaryText 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 SummaryText, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class SummaryText |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $_cfg = array('content' => '', 'summary' => '', 'original' => '', 'break' => ''); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool|null |
||
| 29 | */ |
||
| 30 | private $_useCut = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private $_useSubstr = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $_dotted = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * SummaryText constructor. |
||
| 44 | * @param string $text |
||
| 45 | * @param string $action |
||
| 46 | * @param null|string $break |
||
| 47 | */ |
||
| 48 | public function __construct($text, $action, $break = null) |
||
| 49 | { |
||
| 50 | $this->_cfg['content'] = is_scalar($text) ? $text : ''; |
||
|
|
|||
| 51 | $this->_cfg['original'] = $this->_cfg['content']; |
||
| 52 | $this->_cfg['summary'] = is_scalar($action) ? $action : ''; |
||
| 53 | $this->_cfg['break'] = is_scalar($break) ? $break : '. '; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param $cut |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function setCut($cut) |
||
| 61 | { |
||
| 62 | if (is_scalar($cut) && $cut != '') { |
||
| 63 | $this->_cfg['cut'] = $cut; |
||
| 64 | $flag = true; |
||
| 65 | } else { |
||
| 66 | $flag = false; |
||
| 67 | } |
||
| 68 | |||
| 69 | return $flag; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function getCut() |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $scheme |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | protected function dotted($scheme = 0) |
||
| 85 | { |
||
| 86 | if (($scheme == 1 && ($this->_useCut === true || $this->_useSubstr)) || ($scheme == 2 && $this->_useSubstr && $this->_useCut !== true)) { |
||
| 87 | $this->_cfg['content'] .= '…'; //... |
||
| 88 | } else { |
||
| 89 | if ($scheme && ($this->_useCut !== true|| $scheme != 2)) { |
||
| 90 | $this->_cfg['content'] .= '.'; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this->_cfg['content']; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param int $dotted |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | public function run($dotted = 0) |
||
| 102 | { |
||
| 103 | $this->_dotted = $dotted; |
||
| 104 | if (isset($this->_cfg['content'], $this->_cfg['summary']) && $this->_cfg['summary'] != '' && $this->_cfg['content'] != '') { |
||
| 105 | $param = explode(",", $this->_cfg['summary']); |
||
| 106 | $this->_cfg['content'] = $this->beforeCut($this->_cfg['content'], $this->getCut()); |
||
| 107 | foreach ($param as $doing) { |
||
| 108 | $process = explode(":", $doing); |
||
| 109 | switch ($process[0]) { |
||
| 110 | case 'notags': |
||
| 111 | $this->_cfg['content'] = strip_tags($this->_cfg['content']); |
||
| 112 | break; |
||
| 113 | case 'noparser': |
||
| 114 | $this->_cfg['content'] = APIhelpers::sanitarTag($this->_cfg['content']); |
||
| 115 | break; |
||
| 116 | case 'chars': |
||
| 117 | if (!(isset($process[1]) && $process[1] > 0)) { |
||
| 118 | $process[1] = 200; |
||
| 119 | } |
||
| 120 | $this->_cfg['content'] = APIhelpers::mb_trim_word($this->_cfg['content'], $process[1]); |
||
| 121 | break; |
||
| 122 | case 'len': |
||
| 123 | if (!(isset($process[1]) && $process[1] > 0)) { |
||
| 124 | $process[1] = 200; |
||
| 125 | } |
||
| 126 | $this->_cfg['content'] = $this->summary( |
||
| 127 | $this->_cfg['content'], |
||
| 128 | $process[1], |
||
| 129 | 50, |
||
| 130 | true, |
||
| 131 | $this->getCut() |
||
| 132 | ); |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this->dotted($dotted); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $resource |
||
| 143 | * @param string $splitter |
||
| 144 | * @return array|mixed |
||
| 145 | */ |
||
| 146 | protected function beforeCut($resource, $splitter = '') |
||
| 147 | { |
||
| 148 | if ($splitter !== '') { |
||
| 149 | $summary = str_replace( |
||
| 150 | '<p>' . $splitter . '</p>', |
||
| 151 | $splitter, |
||
| 152 | $resource |
||
| 153 | ); // For TinyMCE or if it isn't wrapped inside paragraph tags |
||
| 154 | $summary = explode($splitter, $summary, 2); |
||
| 155 | $this->_useCut = isset($summary[1]); |
||
| 156 | $summary = $summary['0']; |
||
| 157 | } else { |
||
| 158 | $summary = $resource; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $summary; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $resource |
||
| 166 | * @param $truncLen |
||
| 167 | * @param $truncOffset |
||
| 168 | * @param $truncChars |
||
| 169 | * @param string $splitter |
||
| 170 | * @return array|mixed|string |
||
| 171 | */ |
||
| 172 | protected function summary($resource, $truncLen, $truncOffset, $truncChars, $splitter = '') |
||
| 173 | { |
||
| 174 | if (isset($this->_useCut) && $splitter != '' && mb_strstr($resource, $splitter, 'UTF-8')) { |
||
| 175 | $summary = $this->beforeCut($resource, $splitter); |
||
| 176 | } else { |
||
| 177 | if ($this->_useCut !== true && (mb_strlen($resource, 'UTF-8') > $truncLen)) { |
||
| 178 | $summary = $this->html_substr($resource, $truncLen, $truncOffset, $truncChars); |
||
| 179 | if ($resource != $summary) { |
||
| 180 | $this->_useSubstr = true; |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | $summary = $resource; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $summary = $this->closeTags($summary); |
||
| 188 | $summary = $this->rTriming($summary); |
||
| 189 | |||
| 190 | return $summary; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @see summary extender for Ditto (truncate::html_substr) |
||
| 195 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L142 |
||
| 196 | * |
||
| 197 | * @param $posttext |
||
| 198 | * @param int $minimum_length |
||
| 199 | * @param int $length_offset |
||
| 200 | * @param bool $truncChars |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected function html_substr($posttext, $minimum_length = 200, $length_offset = 100, $truncChars = false) |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @see summary extender for Ditto (truncate::textTrunc) |
||
| 269 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L213 |
||
| 270 | * |
||
| 271 | * @param $string |
||
| 272 | * @param $limit |
||
| 273 | * @param string $break |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | protected function textTrunc($string, $limit, $break = ". ") |
||
| 277 | { |
||
| 278 | // Original PHP code from The Art of Web: www.the-art-of-web.com |
||
| 279 | |||
| 280 | // return with no change if string is shorter than $limit |
||
| 281 | if (mb_strlen($string, 'UTF-8') < $limit) { |
||
| 282 | return $string; |
||
| 283 | } |
||
| 284 | |||
| 285 | $string = mb_substr($string, 0, $limit, 'UTF-8'); |
||
| 286 | if (false !== ($breakpoint = mb_strrpos($string, $break, 0, 'UTF-8'))) { |
||
| 287 | $string = mb_substr($string, 0, $breakpoint + 1, 'UTF-8'); |
||
| 288 | } else { |
||
| 289 | if ($break != ' ') { |
||
| 290 | $string = $this->textTrunc($string, $limit, " "); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return $string; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param $str |
||
| 299 | * @return mixed |
||
| 300 | */ |
||
| 301 | protected function rTriming($str) |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @see summary extender for Ditto (truncate::closeTags) |
||
| 313 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L227 |
||
| 314 | * @param $text |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function closeTags($text) |
||
| 366 |