Total Complexity | 76 |
Total Lines | 338 |
Duplicated Lines | 0 % |
Changes | 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 $text |
||
45 | * @param $action |
||
46 | * @param null $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() |
||
76 | { |
||
77 | return \APIHelpers::getkey($this->_cfg, 'cut', '<cut/>'); |
||
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 | |||
109 | $process = explode(":", $doing); |
||
110 | switch ($process[0]) { |
||
111 | case 'notags': |
||
112 | $this->_cfg['content'] = strip_tags($this->_cfg['content']); |
||
113 | break; |
||
114 | case 'noparser': |
||
115 | $this->_cfg['content'] = APIhelpers::sanitarTag($this->_cfg['content']); |
||
116 | break; |
||
117 | case 'chars': |
||
118 | if (!(isset($process[1]) && $process[1] > 0)) { |
||
119 | $process[1] = 200; |
||
120 | } |
||
121 | $this->_cfg['content'] = APIhelpers::mb_trim_word($this->_cfg['content'], $process[1]); |
||
122 | break; |
||
123 | case 'len': |
||
124 | if (!(isset($process[1]) && $process[1] > 0)) { |
||
125 | $process[1] = 200; |
||
126 | } |
||
127 | $this->_cfg['content'] = $this->summary($this->_cfg['content'], $process[1], 50, true, |
||
128 | $this->getCut()); |
||
129 | break; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return $this->dotted($dotted); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param $resource |
||
139 | * @param string $splitter |
||
140 | * @return array|mixed |
||
141 | */ |
||
142 | protected function beforeCut($resource, $splitter = '') |
||
143 | { |
||
144 | if ($splitter !== '') { |
||
145 | $summary = str_replace('<p>' . $splitter . '</p>', $splitter, |
||
146 | $resource); // For TinyMCE or if it isn't wrapped inside paragraph tags |
||
147 | $summary = explode($splitter, $summary, 2); |
||
148 | $this->_useCut = isset($summary[1]); |
||
149 | $summary = $summary['0']; |
||
150 | } else { |
||
151 | $summary = $resource; |
||
152 | } |
||
153 | |||
154 | return $summary; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param $resource |
||
159 | * @param $truncLen |
||
160 | * @param $truncOffset |
||
161 | * @param $truncChars |
||
162 | * @param string $splitter |
||
163 | * @return array|mixed|string |
||
164 | */ |
||
165 | protected function summary($resource, $truncLen, $truncOffset, $truncChars, $splitter = '') |
||
166 | { |
||
167 | if (isset($this->_useCut) && $splitter != '' && mb_strstr($resource, $splitter, 'UTF-8')) { |
||
168 | $summary = $this->beforeCut($resource, $splitter); |
||
169 | } else { |
||
170 | if ($this->_useCut !== true && (mb_strlen($resource, 'UTF-8') > $truncLen)) { |
||
171 | |||
172 | $summary = $this->html_substr($resource, $truncLen, $truncOffset, $truncChars); |
||
173 | if ($resource != $summary) { |
||
174 | $this->_useSubstr = true; |
||
175 | } |
||
176 | } else { |
||
177 | $summary = $resource; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | $summary = $this->closeTags($summary); |
||
182 | $summary = $this->rTriming($summary); |
||
183 | |||
184 | return $summary; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @see summary extender for Ditto (truncate::html_substr) |
||
189 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L142 |
||
190 | * |
||
191 | * @param $posttext |
||
192 | * @param int $minimum_length |
||
193 | * @param int $length_offset |
||
194 | * @param bool $truncChars |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function html_substr($posttext, $minimum_length = 200, $length_offset = 100, $truncChars = false) |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @see summary extender for Ditto (truncate::textTrunc) |
||
263 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L213 |
||
264 | * |
||
265 | * @param $string |
||
266 | * @param $limit |
||
267 | * @param string $break |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function textTrunc($string, $limit, $break = ". ") |
||
271 | { |
||
272 | // Original PHP code from The Art of Web: www.the-art-of-web.com |
||
273 | |||
274 | // return with no change if string is shorter than $limit |
||
275 | if (mb_strlen($string, 'UTF-8') < $limit) { |
||
276 | return $string; |
||
277 | } |
||
278 | |||
279 | $string = mb_substr($string, 0, $limit, 'UTF-8'); |
||
280 | if (false !== ($breakpoint = mb_strrpos($string, $break, 'UTF-8'))) { |
||
281 | $string = mb_substr($string, 0, $breakpoint + 1, 'UTF-8'); |
||
282 | } else { |
||
283 | if ($break != ' ') { |
||
284 | $string = $this->textTrunc($string, $limit, " "); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | return $string; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param $str |
||
293 | * @return mixed |
||
294 | */ |
||
295 | protected function rTriming($str) |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @see summary extender for Ditto (truncate::closeTags) |
||
307 | * @link https://github.com/modxcms/evolution/blob/develop/assets/snippets/ditto/extenders/summary.extender.inc.php#L227 |
||
308 | * @param $text |
||
309 | * @return string |
||
310 | */ |
||
311 | private function closeTags($text) |
||
360 |