Total Complexity | 74 |
Total Lines | 406 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like StringFilter 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 StringFilter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class StringFilter extends AbstractFilter |
||
60 | { |
||
61 | |||
62 | /** |
||
63 | * Return the length of string or array |
||
64 | * @param mixed $variable |
||
65 | * @return int|mixed |
||
66 | */ |
||
67 | public static function length($variable) |
||
68 | { |
||
69 | if ($variable instanceof Traversable) { |
||
70 | return iterator_count($variable); |
||
71 | } |
||
72 | |||
73 | if (is_array($variable)) { |
||
74 | return count($variable); |
||
75 | } |
||
76 | |||
77 | if (is_object($variable)) { |
||
78 | if (method_exists($variable, 'size')) { |
||
79 | return $variable->size(); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | if (!is_string($variable)) { |
||
84 | return $variable; |
||
85 | } |
||
86 | |||
87 | if (function_exists('mb_strlen')) { |
||
88 | return mb_strlen($variable); |
||
89 | } |
||
90 | |||
91 | return strlen($variable); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Add one string to another |
||
96 | * @param mixed $variable |
||
97 | * @param mixed $value |
||
98 | * @return string|mixed |
||
99 | */ |
||
100 | public static function append($variable, $value) |
||
101 | { |
||
102 | if (!is_string($variable) || !is_string($value)) { |
||
103 | return $variable; |
||
104 | } |
||
105 | |||
106 | return $variable . $value; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Prefix a string to variable |
||
111 | * @param mixed $variable |
||
112 | * @param mixed $value |
||
113 | * @return string|mixed |
||
114 | */ |
||
115 | public static function prepend($variable, $value) |
||
116 | { |
||
117 | if (!is_string($variable) || !is_string($value)) { |
||
118 | return $variable; |
||
119 | } |
||
120 | |||
121 | return $value . $variable; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Remove a string to variable |
||
126 | * @param mixed $variable |
||
127 | * @param mixed $value |
||
128 | * @return string|mixed |
||
129 | */ |
||
130 | public static function remove($variable, $value) |
||
131 | { |
||
132 | if (!is_string($variable) || !is_string($value)) { |
||
133 | return $variable; |
||
134 | } |
||
135 | |||
136 | return str_replace($value, '', $variable); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Replace occurrences of a string with another |
||
141 | * @param mixed $variable |
||
142 | * @param mixed $value |
||
143 | * @param mixed $replacement |
||
144 | * @return string|mixed |
||
145 | */ |
||
146 | public static function replace($variable, $value, $replacement = '') |
||
147 | { |
||
148 | if (!is_string($variable) || !is_string($value) || !is_string($replacement)) { |
||
149 | return $variable; |
||
150 | } |
||
151 | |||
152 | return str_replace($value, $replacement, $variable); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Truncate a string down to x characters |
||
157 | * @param mixed $variable |
||
158 | * @param int|mixed $count |
||
159 | * @param string|mixed $ending |
||
160 | * @return string|mixed |
||
161 | */ |
||
162 | public static function truncate($variable, $count = 100, $ending = '...') |
||
163 | { |
||
164 | if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { |
||
165 | return $variable; |
||
166 | } |
||
167 | |||
168 | $numberChar = (int) $count; |
||
169 | if (strlen($variable) > $numberChar) { |
||
170 | return substr($variable, 0, $numberChar) . $ending; |
||
171 | } |
||
172 | |||
173 | return $variable; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Truncate string down to x words |
||
178 | * @param mixed $variable |
||
179 | * @param int|mixed $count |
||
180 | * @param string|mixed $ending |
||
181 | * @return string|mixed |
||
182 | */ |
||
183 | public static function truncateWord($variable, $count = 3, $ending = '...') |
||
184 | { |
||
185 | if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { |
||
186 | return $variable; |
||
187 | } |
||
188 | |||
189 | $numberWords = (int) $count; |
||
190 | $wordList = explode(' ', $variable); |
||
191 | if (count($wordList) > $numberWords) { |
||
192 | return implode(' ', array_slice($wordList, 0, $numberWords)) . $ending; |
||
193 | } |
||
194 | |||
195 | return $variable; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Put all letters to upper case |
||
200 | * @param mixed $variable |
||
201 | * @return string|mixed |
||
202 | */ |
||
203 | public static function upper($variable) |
||
204 | { |
||
205 | if (!is_string($variable)) { |
||
206 | return $variable; |
||
207 | } |
||
208 | |||
209 | if (function_exists('mb_strtoupper')) { |
||
210 | return mb_strtoupper($variable); |
||
211 | } |
||
212 | |||
213 | return strtoupper($variable); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * URL encodes a string |
||
218 | * @param mixed $variable |
||
219 | * @return string|mixed |
||
220 | */ |
||
221 | public static function urlEncode($variable) |
||
222 | { |
||
223 | if (!is_string($variable)) { |
||
224 | return $variable; |
||
225 | } |
||
226 | |||
227 | return urlencode($variable); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Decodes a URL-encoded string |
||
232 | * @param mixed $variable |
||
233 | * @return string|mixed |
||
234 | */ |
||
235 | public static function urlDecode($variable) |
||
236 | { |
||
237 | if (!is_string($variable)) { |
||
238 | return $variable; |
||
239 | } |
||
240 | |||
241 | return urldecode($variable); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Explicit string conversion. |
||
246 | * @param mixed $variable |
||
247 | * @return string|mixed |
||
248 | */ |
||
249 | public static function stringfy($variable) |
||
250 | { |
||
251 | if (is_array($variable)) { |
||
252 | return $variable; |
||
253 | } |
||
254 | |||
255 | return strval($variable); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Split input string into an array of sub |
||
260 | * strings separated by given pattern. |
||
261 | * @param string|mixed $variable |
||
262 | * @param mixed $pattern |
||
263 | * @return array<mixed>|mixed |
||
264 | */ |
||
265 | public static function split($variable, $pattern) |
||
266 | { |
||
267 | if (!is_string($variable) || !is_string($pattern)) { |
||
268 | return $variable; |
||
269 | } |
||
270 | |||
271 | return explode($pattern, $variable); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Pseudo-filter: negates auto-added escape filter |
||
276 | * @param mixed $variable |
||
277 | * @return string|mixed |
||
278 | */ |
||
279 | public static function raw($variable) |
||
280 | { |
||
281 | return $variable; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Escape a string |
||
286 | * @param mixed $variable |
||
287 | * @return string|mixed |
||
288 | */ |
||
289 | public static function escape($variable) |
||
290 | { |
||
291 | if (!is_string($variable)) { |
||
292 | return $variable; |
||
293 | } |
||
294 | |||
295 | return htmlentities($variable, ENT_QUOTES); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Escape a string once, keeping all previous HTML entities intact |
||
300 | * @param mixed $variable |
||
301 | * @return string|mixed |
||
302 | */ |
||
303 | public static function escapeOnce($variable) |
||
304 | { |
||
305 | if (!is_string($variable)) { |
||
306 | return $variable; |
||
307 | } |
||
308 | |||
309 | return htmlentities($variable, ENT_QUOTES, null, false); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Set default value if is blank |
||
314 | * @param mixed $variable |
||
315 | * @param mixed $value |
||
316 | * @return mixed |
||
317 | */ |
||
318 | public static function defaultValue($variable, $value) |
||
319 | { |
||
320 | $isBlank = (is_string($variable) && $variable === '') |
||
321 | || is_bool($variable) && $variable === false |
||
322 | || $variable === null; |
||
323 | |||
324 | |||
325 | return $isBlank ? $value : $variable; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Join elements of an array with a given |
||
330 | * character between them |
||
331 | * @param array<mixed>|Traversable|mixed $variable |
||
332 | * @param mixed $glue |
||
333 | * @return string|mixed |
||
334 | */ |
||
335 | public static function join($variable, $glue = ' ') |
||
336 | { |
||
337 | if (!is_string($glue)) { |
||
338 | return $variable; |
||
339 | } |
||
340 | |||
341 | if ($variable instanceof Traversable) { |
||
342 | $str = ''; |
||
343 | foreach ($variable as $element) { |
||
344 | if ($str) { |
||
345 | $str .= $glue; |
||
346 | } |
||
347 | |||
348 | $str .= $element; |
||
349 | } |
||
350 | |||
351 | return $str; |
||
352 | } |
||
353 | |||
354 | return is_array($variable) |
||
355 | ? implode($glue, $variable) |
||
356 | : $variable; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Put all letter to lower case |
||
361 | * @param mixed $variable |
||
362 | * @return string|mixed |
||
363 | */ |
||
364 | public static function lower($variable) |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Capitalize words in the input sentence |
||
379 | * @param mixed $variable |
||
380 | * @return string|mixed |
||
381 | */ |
||
382 | public static function capitalize($variable) |
||
383 | { |
||
384 | if (!is_string($variable)) { |
||
385 | return $variable; |
||
386 | } |
||
387 | |||
388 | return (string) preg_replace_callback( |
||
389 | '/(^|[^\p{L}\'])([\p{Ll}])/u', |
||
390 | function ($matches) { |
||
391 | return $matches[1] . ucfirst($matches[2]); |
||
392 | }, |
||
393 | ucwords($variable) |
||
394 | ); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Remove the left blank characters |
||
399 | * @param mixed $variable |
||
400 | * @return string|mixed |
||
401 | */ |
||
402 | public static function lstrip($variable) |
||
403 | { |
||
404 | if (!is_string($variable)) { |
||
405 | return $variable; |
||
406 | } |
||
407 | |||
408 | return ltrim($variable); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Remove the right blank characters |
||
413 | * @param mixed $variable |
||
414 | * @return string|mixed |
||
415 | */ |
||
416 | public static function rstrip($variable) |
||
417 | { |
||
418 | if (!is_string($variable)) { |
||
419 | return $variable; |
||
420 | } |
||
421 | |||
422 | return rtrim($variable); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Remove the left and right blank characters |
||
427 | * @param mixed $variable |
||
428 | * @return string|mixed |
||
429 | */ |
||
430 | public static function strip($variable) |
||
431 | { |
||
432 | if (!is_string($variable)) { |
||
433 | return $variable; |
||
434 | } |
||
435 | |||
436 | return trim($variable); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Removes HTML tags from text |
||
441 | * @param mixed $variable |
||
442 | * @return string|mixed |
||
443 | */ |
||
444 | public static function stripHtml($variable) |
||
445 | { |
||
446 | if (!is_string($variable)) { |
||
447 | return $variable; |
||
448 | } |
||
449 | |||
450 | return strip_tags($variable); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Strip all newlines (\n, \r) from string |
||
455 | * @param mixed $variable |
||
456 | * @return string|mixed |
||
457 | */ |
||
458 | public static function stripNewLine($variable) |
||
465 | } |
||
466 | } |
||
467 |