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 | * Determines that the current string starts with |
||
| 150 | * the given substring |
||
| 151 | * |
||
| 152 | * @param string $match |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | * |
||
| 156 | * @codeCoverageIgnore |
||
| 157 | */ |
||
| 158 | public function startsWith($match) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Determines that the current string ends with |
||
| 164 | * the given substring |
||
| 165 | * |
||
| 166 | * @param string $match |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | * |
||
| 170 | * @codeCoverageIgnore |
||
| 171 | */ |
||
| 172 | public function endsWith($match) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Determines that the given string contains the |
||
| 178 | * given substring |
||
| 179 | * |
||
| 180 | * @param string $match |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | * |
||
| 184 | * @codeCoverageIgnore |
||
| 185 | */ |
||
| 186 | public function contains($match) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Determines that the current string contains ALL of the |
||
| 192 | * given substring(s) |
||
| 193 | * |
||
| 194 | * @param array $matches |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 4 | View Code Duplication | public function containsAll(array $matches = []) { |
| 207 | |||
| 208 | /** |
||
| 209 | * Determines that the current string contains ANY of the |
||
| 210 | * given substring(s) |
||
| 211 | * |
||
| 212 | * @param array $matches |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 3 | View Code Duplication | public function containsAny(array $matches = []) { |
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the defined part of the current string |
||
| 228 | * |
||
| 229 | * @param int $start Tha starting character (If -1 starts from the end) |
||
| 230 | * @param int|NULL $length |
||
| 231 | * |
||
| 232 | * @return \BuildR\Utils\StringObject |
||
| 233 | */ |
||
| 234 | 20 | public function substring($start, $length = NULL) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the firs X character from the string. If the provided number higher |
||
| 247 | * tha the string length the full string will be returned but not more. |
||
| 248 | * |
||
| 249 | * @param int $charCount |
||
| 250 | * |
||
| 251 | * @return \BuildR\Utils\StringObject |
||
| 252 | */ |
||
| 253 | 9 | View Code Duplication | public function first($charCount) { |
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the last X character from the current string |
||
| 265 | * |
||
| 266 | * @param int $charCount |
||
| 267 | * |
||
| 268 | * @return \BuildR\Utils\StringObject |
||
| 269 | */ |
||
| 270 | 7 | View Code Duplication | public function last($charCount) { |
| 279 | |||
| 280 | /** |
||
| 281 | * Iterates over parts of the string. The current string split through |
||
| 282 | * the given delimiter and tha closure will be called on all elements. |
||
| 283 | * |
||
| 284 | * The closer takes two arguments: |
||
| 285 | * - (string) $part The current part of the string |
||
| 286 | * - (string) $delimiter the delimiter passed to this function |
||
| 287 | * |
||
| 288 | * @param $delimiter |
||
| 289 | * @param callable $callback |
||
| 290 | */ |
||
| 291 | 3 | public function map($delimiter, callable $callback) { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Split string along the delimiter |
||
| 301 | * |
||
| 302 | * @param string $delimiter |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | 15 | public function split($delimiter) { |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Split the string along the delimiter and returns |
||
| 314 | * the given index from the segments |
||
| 315 | * |
||
| 316 | * @param string $delimiter |
||
| 317 | * @param int $index |
||
| 318 | * |
||
| 319 | * @return \BuildR\Utils\StringObject |
||
| 320 | */ |
||
| 321 | 5 | public function segment($delimiter, $index) { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Split string along the delimiter and |
||
| 330 | * return the last segment |
||
| 331 | * |
||
| 332 | * @param string $delimiter |
||
| 333 | * |
||
| 334 | * @return \BuildR\Utils\StringObject |
||
| 335 | * |
||
| 336 | * @codeCoverageIgnore |
||
| 337 | */ |
||
| 338 | public function lastSegment($delimiter) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Split the string along the delimiter and |
||
| 347 | * returns the first segment |
||
| 348 | * |
||
| 349 | * @param string $delimiter |
||
| 350 | * |
||
| 351 | * @return \BuildR\Utils\StringObject |
||
| 352 | * |
||
| 353 | * @codeCoverageIgnore |
||
| 354 | */ |
||
| 355 | public function firstSegment($delimiter) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Split down cameCase or PascalCase strings |
||
| 364 | * |
||
| 365 | * Example: |
||
| 366 | * helloWorld -> hello World |
||
| 367 | * ExampleHelloWorld -> ExampleHelloWorld |
||
| 368 | * |
||
| 369 | * @return \BuildR\Utils\StringObject |
||
| 370 | */ |
||
| 371 | 5 | public function splitCamelCase() { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Split snake_case string. |
||
| 380 | * |
||
| 381 | * Example |
||
| 382 | * hello_world -> hello world |
||
| 383 | * |
||
| 384 | * @return \BuildR\Utils\StringObject |
||
| 385 | */ |
||
| 386 | 5 | public function splitSnakeCase() { |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Try to split any case method into words. |
||
| 395 | * This convert the following cases into words: |
||
| 396 | * |
||
| 397 | * - PascalCase |
||
| 398 | * - camelCase |
||
| 399 | * - snake_case |
||
| 400 | * |
||
| 401 | * Example: |
||
| 402 | * helloExampleWorld_olah -> hello Example World olah |
||
| 403 | * |
||
| 404 | * @return \BuildR\Utils\StringObject |
||
| 405 | */ |
||
| 406 | 2 | public function caseFree() { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Limit the length of the current string. If the current string is |
||
| 414 | * longer tha the given limit chopped down to limit and the end |
||
| 415 | * substring will be concatenated. |
||
| 416 | * |
||
| 417 | * @param int $limit |
||
| 418 | * @param string $end |
||
| 419 | * |
||
| 420 | * @return \BuildR\Utils\StringObject |
||
| 421 | */ |
||
| 422 | 3 | public function limit($limit = 120, $end = '...') { |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Convert current string to 'Title Case' |
||
| 434 | * |
||
| 435 | * Example: |
||
| 436 | * 'hello world example' -> 'Hello World Example' |
||
| 437 | * |
||
| 438 | * @return \BuildR\Utils\StringObject |
||
| 439 | */ |
||
| 440 | 2 | public function toTitleCase() { |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Convert current string to snake_case |
||
| 448 | * |
||
| 449 | * @return \BuildR\Utils\StringObject |
||
| 450 | */ |
||
| 451 | 2 | public function toSnakeCase() { |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Convert current string to PascalCase |
||
| 459 | * |
||
| 460 | * @return \BuildR\Utils\StringObject |
||
| 461 | */ |
||
| 462 | 2 | public function toPascalCase() { |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Convert current string to camelCase |
||
| 471 | * |
||
| 472 | * @return \BuildR\Utils\StringObject |
||
| 473 | */ |
||
| 474 | 2 | public function toCamelCase() { |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Clone the current object a set a new value (base string) |
||
| 498 | * on the clone. |
||
| 499 | * |
||
| 500 | * This method allow instances immutability, but allow instances |
||
| 501 | * to retain loaded extensions. |
||
| 502 | * |
||
| 503 | * @param $newValue |
||
| 504 | * |
||
| 505 | * @return \BuildR\Utils\StringObject |
||
| 506 | */ |
||
| 507 | 69 | protected function createClone($newValue) { |
|
| 517 | |||
| 518 | //=========================================== |
||
| 519 | // StringConvertibleInterface Implementation |
||
| 520 | //=========================================== |
||
| 521 | |||
| 522 | /** |
||
| 523 | * {@inheritDoc} |
||
| 524 | */ |
||
| 525 | 16 | public function __toString() { |
|
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritDoc} |
||
| 531 | */ |
||
| 532 | 74 | public function toString() { |
|
| 535 | |||
| 536 | // ========================= |
||
| 537 | // Countable Implementation |
||
| 538 | // ========================= |
||
| 539 | |||
| 540 | /** |
||
| 541 | * {@inheritDoc} |
||
| 542 | * |
||
| 543 | * @codeCoverageIgnore |
||
| 544 | */ |
||
| 545 | public function count() { |
||
| 548 | |||
| 549 | // ============================= |
||
| 550 | // ArrayIterator Implementation |
||
| 551 | // ============================= |
||
| 552 | |||
| 553 | /** |
||
| 554 | * {@inheritDoc} |
||
| 555 | */ |
||
| 556 | 4 | public function getIterator() { |
|
| 559 | |||
| 560 | // ========================================== |
||
| 561 | // ExtensionReceiverInterface Implementation |
||
| 562 | // ========================================== |
||
| 563 | |||
| 564 | /** |
||
| 565 | * {@inheritDoc} |
||
| 566 | * |
||
| 567 | * @internal |
||
| 568 | */ |
||
| 569 | 5 | public function shouldReceiveType() { |
|
| 572 | |||
| 573 | /** |
||
| 574 | * {@inheritDoc} |
||
| 575 | * |
||
| 576 | * @internal |
||
| 577 | */ |
||
| 578 | 2 | public function getCurrentValue() { |
|
| 581 | |||
| 582 | /** |
||
| 583 | * {@inheritDoc} |
||
| 584 | * |
||
| 585 | * @internal |
||
| 586 | */ |
||
| 587 | 2 | public function processResult(array $result) { |
|
| 596 | |||
| 597 | } |
||
| 598 |