Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like StringObject 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 StringObject, and based on these observations, apply Extract Interface, too.
1 | <?php namespace BuildR\Utils; |
||
24 | class StringObject |
||
25 | extends AbstractExtensionReceiver |
||
26 | implements StringConvertibleInterface, Countable, IteratorAggregate { |
||
27 | |||
28 | /** |
||
29 | * @type string |
||
30 | */ |
||
31 | private $string; |
||
32 | |||
33 | /** |
||
34 | * StringObject constructor. |
||
35 | * |
||
36 | * @param $string |
||
37 | * |
||
38 | * @throws \BuildR\Foundation\Exception\InvalidArgumentException |
||
39 | */ |
||
40 | 92 | public function __construct($string) { |
|
47 | |||
48 | /** |
||
49 | * Remove all whitespace character from the given string |
||
50 | * |
||
51 | * @return \BuildR\Utils\StringObject |
||
52 | */ |
||
53 | 4 | public function removeWhitespace() { |
|
58 | |||
59 | /** |
||
60 | * Appends the given substring to the current string |
||
61 | * |
||
62 | * @param string $substring |
||
63 | * |
||
64 | * @return \BuildR\Utils\StringObject |
||
65 | */ |
||
66 | 5 | public function append($substring) { |
|
71 | |||
72 | /** |
||
73 | * Prepends the given substring to the current string |
||
74 | * |
||
75 | * @param string $substring |
||
76 | * |
||
77 | * @return \BuildR\Utils\StringObject |
||
78 | */ |
||
79 | 6 | public function prepend($substring) { |
|
84 | |||
85 | /** |
||
86 | * Return an array where each element represent a single character from the current string |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 9 | public function chars() { |
|
95 | |||
96 | /** |
||
97 | * Determines the length of the current string |
||
98 | * |
||
99 | * @return int |
||
100 | */ |
||
101 | 3 | public function length() { |
|
106 | |||
107 | /** |
||
108 | * Determines that character exist in the given position |
||
109 | * If the character is a whitespace this function will return |
||
110 | * TRUE to indicate that index is part of the string |
||
111 | * |
||
112 | * This function counts characters from 1 |
||
113 | * |
||
114 | * @param int $index |
||
115 | * |
||
116 | * @return \BuildR\Utils\StringObject |
||
117 | */ |
||
118 | 5 | public function charAt($index) { |
|
125 | |||
126 | /** |
||
127 | * Format the string to lowercase |
||
128 | * |
||
129 | * @return \BuildR\Utils\StringObject |
||
130 | */ |
||
131 | 6 | public function toLower() { |
|
136 | |||
137 | /** |
||
138 | * Format the string to uppercase |
||
139 | * |
||
140 | * @return \BuildR\Utils\StringObject |
||
141 | */ |
||
142 | 4 | public function toUpper() { |
|
147 | |||
148 | /** |
||
149 | * Format the current string first character to uppercase. |
||
150 | * Leave any other character untouched |
||
151 | * |
||
152 | * @return \BuildR\Utils\StringObject |
||
153 | * |
||
154 | * @codeCoverageIgnore |
||
155 | */ |
||
156 | public function ucfirst() { |
||
161 | |||
162 | /** |
||
163 | * Split the current string trough spaces and makes every words first |
||
164 | * character uppercase. Leave any other character untouched |
||
165 | * |
||
166 | * @return \BuildR\Utils\StringObject |
||
167 | * |
||
168 | * @codeCoverageIgnore |
||
169 | */ |
||
170 | public function ucwords() { |
||
175 | |||
176 | /** |
||
177 | * Determines that the current string starts with |
||
178 | * the given substring |
||
179 | * |
||
180 | * @param string $match |
||
181 | * |
||
182 | * @return bool |
||
183 | * |
||
184 | * @codeCoverageIgnore |
||
185 | */ |
||
186 | public function startsWith($match) { |
||
189 | |||
190 | /** |
||
191 | * Determines that the current string ends with |
||
192 | * the given substring |
||
193 | * |
||
194 | * @param string $match |
||
195 | * |
||
196 | * @return bool |
||
197 | * |
||
198 | * @codeCoverageIgnore |
||
199 | */ |
||
200 | public function endsWith($match) { |
||
203 | |||
204 | /** |
||
205 | * Determines that the given string contains the |
||
206 | * given substring |
||
207 | * |
||
208 | * @param string $match |
||
209 | * |
||
210 | * @return bool |
||
211 | * |
||
212 | * @codeCoverageIgnore |
||
213 | */ |
||
214 | public function contains($match) { |
||
217 | |||
218 | /** |
||
219 | * Determines that the current string contains ALL of the |
||
220 | * given substring(s) |
||
221 | * |
||
222 | * @param array $matches |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 4 | View Code Duplication | public function containsAll(array $matches = []) { |
235 | |||
236 | /** |
||
237 | * Determines that the current string contains ANY of the |
||
238 | * given substring(s) |
||
239 | * |
||
240 | * @param array $matches |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | 3 | View Code Duplication | public function containsAny(array $matches = []) { |
253 | |||
254 | /** |
||
255 | * Returns the defined part of the current string |
||
256 | * |
||
257 | * @param int $start Tha starting character (If -1 starts from the end) |
||
258 | * @param int|NULL $length |
||
259 | * |
||
260 | * @return \BuildR\Utils\StringObject |
||
261 | */ |
||
262 | 20 | public function substring($start, $length = NULL) { |
|
272 | |||
273 | /** |
||
274 | * Returns the firs X character from the string. If the provided number higher |
||
275 | * tha the string length the full string will be returned but not more. |
||
276 | * |
||
277 | * @param int $charCount |
||
278 | * |
||
279 | * @return \BuildR\Utils\StringObject |
||
280 | */ |
||
281 | 9 | View Code Duplication | public function first($charCount) { |
290 | |||
291 | /** |
||
292 | * Returns the last X character from the current string |
||
293 | * |
||
294 | * @param int $charCount |
||
295 | * |
||
296 | * @return \BuildR\Utils\StringObject |
||
297 | */ |
||
298 | 7 | View Code Duplication | public function last($charCount) { |
307 | |||
308 | /** |
||
309 | * Iterates over parts of the string. The current string split through |
||
310 | * the given delimiter and tha closure will be called on all elements. |
||
311 | * |
||
312 | * The closer takes two arguments: |
||
313 | * - (string) $part The current part of the string |
||
314 | * - (string) $delimiter the delimiter passed to this function |
||
315 | * |
||
316 | * @param $delimiter |
||
317 | * @param callable $callback |
||
318 | */ |
||
319 | 3 | public function map($delimiter, callable $callback) { |
|
326 | |||
327 | /** |
||
328 | * Split string along the delimiter |
||
329 | * |
||
330 | * @param string $delimiter |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | 15 | public function split($delimiter) { |
|
339 | |||
340 | /** |
||
341 | * Split the string along the delimiter and returns |
||
342 | * the given index from the segments |
||
343 | * |
||
344 | * @param string $delimiter |
||
345 | * @param int $index |
||
346 | * |
||
347 | * @return \BuildR\Utils\StringObject |
||
348 | */ |
||
349 | 5 | public function segment($delimiter, $index) { |
|
355 | |||
356 | /** |
||
357 | * Split string along the delimiter and |
||
358 | * return the last segment |
||
359 | * |
||
360 | * @param string $delimiter |
||
361 | * |
||
362 | * @return \BuildR\Utils\StringObject |
||
363 | * |
||
364 | * @codeCoverageIgnore |
||
365 | */ |
||
366 | public function lastSegment($delimiter) { |
||
372 | |||
373 | /** |
||
374 | * Split the string along the delimiter and |
||
375 | * returns the first segment |
||
376 | * |
||
377 | * @param string $delimiter |
||
378 | * |
||
379 | * @return \BuildR\Utils\StringObject |
||
380 | * |
||
381 | * @codeCoverageIgnore |
||
382 | */ |
||
383 | public function firstSegment($delimiter) { |
||
389 | |||
390 | /** |
||
391 | * Split down cameCase or PascalCase strings |
||
392 | * |
||
393 | * Example: |
||
394 | * helloWorld -> hello World |
||
395 | * ExampleHelloWorld -> ExampleHelloWorld |
||
396 | * |
||
397 | * @return \BuildR\Utils\StringObject |
||
398 | */ |
||
399 | 5 | public function splitCamelCase() { |
|
405 | |||
406 | /** |
||
407 | * Split snake_case string. |
||
408 | * |
||
409 | * Example |
||
410 | * hello_world -> hello world |
||
411 | * |
||
412 | * @return \BuildR\Utils\StringObject |
||
413 | */ |
||
414 | 5 | public function splitSnakeCase() { |
|
420 | |||
421 | /** |
||
422 | * Try to split any case method into words. |
||
423 | * This convert the following cases into words: |
||
424 | * |
||
425 | * - PascalCase |
||
426 | * - camelCase |
||
427 | * - snake_case |
||
428 | * |
||
429 | * Example: |
||
430 | * helloExampleWorld_olah -> hello Example World olah |
||
431 | * |
||
432 | * @return \BuildR\Utils\StringObject |
||
433 | */ |
||
434 | 2 | public function caseFree() { |
|
439 | |||
440 | /** |
||
441 | * Limit the length of the current string. If the current string is |
||
442 | * longer tha the given limit chopped down to limit and the end |
||
443 | * substring will be concatenated. |
||
444 | * |
||
445 | * @param int $limit |
||
446 | * @param string $end |
||
447 | * |
||
448 | * @return \BuildR\Utils\StringObject |
||
449 | */ |
||
450 | 3 | public function limit($limit = 120, $end = '...') { |
|
459 | |||
460 | /** |
||
461 | * Convert current string to 'Title Case' |
||
462 | * |
||
463 | * Example: |
||
464 | * 'hello world example' -> 'Hello World Example' |
||
465 | * |
||
466 | * @return \BuildR\Utils\StringObject |
||
467 | */ |
||
468 | 2 | public function toTitleCase() { |
|
473 | |||
474 | /** |
||
475 | * Convert current string to snake_case |
||
476 | * |
||
477 | * @return \BuildR\Utils\StringObject |
||
478 | */ |
||
479 | 2 | public function toSnakeCase() { |
|
484 | |||
485 | /** |
||
486 | * Convert current string to PascalCase |
||
487 | * |
||
488 | * @return \BuildR\Utils\StringObject |
||
489 | */ |
||
490 | 2 | public function toPascalCase() { |
|
496 | |||
497 | /** |
||
498 | * Convert current string to camelCase |
||
499 | * |
||
500 | * @return \BuildR\Utils\StringObject |
||
501 | */ |
||
502 | 2 | public function toCamelCase() { |
|
522 | |||
523 | /** |
||
524 | * Clone the current object a set a new value (base string) |
||
525 | * on the clone. |
||
526 | * |
||
527 | * This method allow instances immutability, but allow instances |
||
528 | * to retain loaded extensions. |
||
529 | * |
||
530 | * @param $newValue |
||
531 | * |
||
532 | * @return \BuildR\Utils\StringObject |
||
533 | */ |
||
534 | 69 | protected function createClone($newValue) { |
|
544 | |||
545 | //=========================================== |
||
546 | // StringConvertibleInterface Implementation |
||
547 | //=========================================== |
||
548 | |||
549 | /** |
||
550 | * {@inheritDoc} |
||
551 | */ |
||
552 | 16 | public function __toString() { |
|
555 | |||
556 | /** |
||
557 | * {@inheritDoc} |
||
558 | */ |
||
559 | 74 | public function toString() { |
|
562 | |||
563 | // ========================= |
||
564 | // Countable Implementation |
||
565 | // ========================= |
||
566 | |||
567 | /** |
||
568 | * {@inheritDoc} |
||
569 | * |
||
570 | * @codeCoverageIgnore |
||
571 | */ |
||
572 | public function count() { |
||
575 | |||
576 | // ============================= |
||
577 | // ArrayIterator Implementation |
||
578 | // ============================= |
||
579 | |||
580 | /** |
||
581 | * {@inheritDoc} |
||
582 | */ |
||
583 | 4 | public function getIterator() { |
|
586 | |||
587 | // ========================================== |
||
588 | // ExtensionReceiverInterface Implementation |
||
589 | // ========================================== |
||
590 | |||
591 | /** |
||
592 | * {@inheritDoc} |
||
593 | * |
||
594 | * @internal |
||
595 | */ |
||
596 | 5 | public function shouldReceiveType() { |
|
599 | |||
600 | /** |
||
601 | * {@inheritDoc} |
||
602 | * |
||
603 | * @internal |
||
604 | */ |
||
605 | 2 | public function getCurrentValue() { |
|
608 | |||
609 | /** |
||
610 | * {@inheritDoc} |
||
611 | * |
||
612 | * @internal |
||
613 | */ |
||
614 | 2 | public function processResult(array $result) { |
|
623 | |||
624 | } |
||
625 |