Complex classes like StringPrimitive 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 StringPrimitive, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class StringPrimitive implements PrimitiveInterface, StringableInterface |
||
| 10 | { |
||
| 11 | const PAD_RIGHT = STR_PAD_RIGHT; |
||
| 12 | const PAD_LEFT = STR_PAD_LEFT; |
||
| 13 | 52 | const PAD_BOTH = STR_PAD_BOTH; |
|
| 14 | const PREG_NO_FLAGS = 0; |
||
| 15 | 52 | const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
|
| 16 | 1 | const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
|
| 17 | const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
||
| 18 | const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
||
| 19 | 51 | ||
| 20 | 51 | private $value; |
|
| 21 | |||
| 22 | public function __construct($value) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | 27 | */ |
|
| 34 | public function toPrimitive() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function __toString() |
||
| 46 | |||
| 47 | 3 | /** |
|
| 48 | 3 | * Split the string into a collection of ones |
|
| 49 | 3 | * |
|
| 50 | 3 | * @param string $delimiter |
|
| 51 | * |
||
| 52 | 3 | * @return TypedCollection |
|
| 53 | 3 | */ |
|
| 54 | 3 | public function split($delimiter = null) |
|
| 70 | |||
| 71 | 1 | /** |
|
| 72 | * Returns a collection of the string splitted by the given chunk size |
||
| 73 | * |
||
| 74 | * @param int $size |
||
| 75 | * |
||
| 76 | * @return TypedCollection |
||
| 77 | */ |
||
| 78 | public function chunk($size = 1) |
||
| 82 | |||
| 83 | /** |
||
| 84 | 3 | * Returns the position of the first occurence of the string |
|
| 85 | * |
||
| 86 | 3 | * @param string $needle |
|
| 87 | * @param int $offset |
||
| 88 | 3 | * |
|
| 89 | 1 | * @throws SubstringException If the string is not found |
|
| 90 | 1 | * |
|
| 91 | * @return int |
||
| 92 | 1 | */ |
|
| 93 | public function pos($needle, $offset = 0) |
||
| 106 | 1 | ||
| 107 | /** |
||
| 108 | 1 | * Replace all occurences of the search string with the replacement one |
|
| 109 | 1 | * |
|
| 110 | 1 | * @param string $search |
|
| 111 | 1 | * @param string $replacement |
|
| 112 | 1 | * |
|
| 113 | * @return StringPrimitive |
||
| 114 | */ |
||
| 115 | public function replace($search, $replacement) |
||
| 123 | |||
| 124 | 2 | /** |
|
| 125 | * Returns the string following the given delimiter |
||
| 126 | 2 | * |
|
| 127 | * @param string $delimiter |
||
| 128 | 2 | * |
|
| 129 | 1 | * @throws SubstringException If the string is not found |
|
| 130 | 1 | * |
|
| 131 | * @return StringInterface |
||
| 132 | 1 | */ |
|
| 133 | public function str($delimiter) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the string in upper case |
||
| 149 | * |
||
| 150 | * @return StringPrimitive |
||
| 151 | */ |
||
| 152 | public function toUpper() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return the string in lower case |
||
| 159 | * |
||
| 160 | * @return StringPrimitive |
||
| 161 | */ |
||
| 162 | public function toLower() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the string length |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | public function length() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Reverse the string |
||
| 179 | * |
||
| 180 | * @return StringPrimitive |
||
| 181 | */ |
||
| 182 | public function reverse() |
||
| 186 | |||
| 187 | 1 | /** |
|
| 188 | * Pad the string |
||
| 189 | 1 | * |
|
| 190 | 1 | * @param int $length |
|
| 191 | 1 | * @param string $character |
|
| 192 | 1 | * @param int $direction |
|
| 193 | * |
||
| 194 | 1 | * @return StringPrimitive |
|
| 195 | */ |
||
| 196 | public function pad($length, $character = ' ', $direction = self::PAD_RIGHT) |
||
| 205 | 1 | ||
| 206 | /** |
||
| 207 | 1 | * Pad to the right |
|
| 208 | * |
||
| 209 | * @param int $length |
||
| 210 | * @param string $character |
||
| 211 | * |
||
| 212 | * @return StringPrimitive |
||
| 213 | */ |
||
| 214 | public function rightPad($length, $character = ' ') |
||
| 218 | 1 | ||
| 219 | /** |
||
| 220 | 1 | * Pad to the left |
|
| 221 | * |
||
| 222 | * @param int $length |
||
| 223 | * @param string $character |
||
| 224 | * |
||
| 225 | * @return StringPrimitive |
||
| 226 | */ |
||
| 227 | public function leftPad($length, $character = ' ') |
||
| 231 | 1 | ||
| 232 | /** |
||
| 233 | 1 | * Pad both sides |
|
| 234 | * |
||
| 235 | * @param int $length |
||
| 236 | * @param string $character |
||
| 237 | * |
||
| 238 | * @return StringPrimitive |
||
| 239 | */ |
||
| 240 | public function uniPad($length, $character = ' ') |
||
| 244 | |||
| 245 | 1 | /** |
|
| 246 | * Find length of initial segment not matching mask |
||
| 247 | 1 | * |
|
| 248 | * @param string $mask |
||
| 249 | 1 | * @param int $start |
|
| 250 | 1 | * @param int $length |
|
| 251 | 1 | * |
|
| 252 | 1 | * @return int |
|
| 253 | 1 | */ |
|
| 254 | 1 | public function cspn($mask, $start = 0, $length = null) |
|
| 271 | |||
| 272 | 1 | /** |
|
| 273 | * Repeat the string n times |
||
| 274 | * |
||
| 275 | * @param int $repeat |
||
| 276 | * |
||
| 277 | * @return StringPrimitive |
||
| 278 | */ |
||
| 279 | public function repeat($repeat) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Shuffle the string |
||
| 286 | * |
||
| 287 | * @return StringPrimitive |
||
| 288 | */ |
||
| 289 | public function shuffle() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Strip slashes |
||
| 296 | * |
||
| 297 | * @return StringPrimitive |
||
| 298 | */ |
||
| 299 | public function stripSlashes() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Strip C-like slashes |
||
| 306 | * |
||
| 307 | * @return StringPrimitive |
||
| 308 | */ |
||
| 309 | public function stripCSlashes() |
||
| 313 | |||
| 314 | 1 | /** |
|
| 315 | 1 | * Return the word count |
|
| 316 | 1 | * |
|
| 317 | * @param string $charlist |
||
| 318 | 1 | * |
|
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | public function wordCount($charlist = '') |
||
| 329 | |||
| 330 | 1 | /** |
|
| 331 | * Return the collection of words |
||
| 332 | 1 | * |
|
| 333 | 1 | * @param string $charlist |
|
| 334 | 1 | * |
|
| 335 | * @return TypedCollection |
||
| 336 | 1 | */ |
|
| 337 | 1 | public function words($charlist = '') |
|
| 350 | |||
| 351 | 1 | /** |
|
| 352 | * Split the string using a regular expression |
||
| 353 | 1 | * |
|
| 354 | * @param string $regex |
||
| 355 | 1 | * @param int $limit |
|
| 356 | 1 | * @param int $flags |
|
| 357 | 1 | * |
|
| 358 | * @return TypedCollection |
||
| 359 | 1 | */ |
|
| 360 | 1 | public function pregSplit($regex, $limit = -1, $flags = self::PREG_NO_FLAGS) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Check if the string match the given regular expression |
||
| 376 | 2 | * |
|
| 377 | * @param string $regex |
||
| 378 | 2 | * @param int $offset |
|
| 379 | 2 | * |
|
| 380 | * @throws Exception If the regex failed |
||
| 381 | 2 | * |
|
| 382 | 1 | * @return bool |
|
| 383 | */ |
||
| 384 | public function match($regex, $offset = 0) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Return a collection of the elements matching the regex |
||
| 398 | * |
||
| 399 | 2 | * @param string $regex |
|
| 400 | * @param int $offset |
||
| 401 | 2 | * @param int $flags |
|
| 402 | 2 | * |
|
| 403 | 2 | * @throws Exception If the regex failed |
|
| 404 | 2 | * |
|
| 405 | 2 | * @return TypedCollection |
|
| 406 | 2 | */ |
|
| 407 | public function getMatches($regex, $offset = 0, $flags = self::PREG_NO_FLAGS) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Replace part of the string by using a regular expression |
||
| 431 | * |
||
| 432 | 1 | * @param string $regex |
|
| 433 | * @param string $replacement |
||
| 434 | 1 | * @param int $limit |
|
| 435 | 1 | * |
|
| 436 | 1 | * @throws Exception If the regex failed |
|
| 437 | 1 | * |
|
| 438 | * @return StringPrimitive |
||
| 439 | 1 | */ |
|
| 440 | public function pregReplace($regex, $replacement, $limit = -1) |
||
| 455 | |||
| 456 | 1 | /** |
|
| 457 | * Return part of the string |
||
| 458 | 1 | * |
|
| 459 | 1 | * @param int $start |
|
| 460 | 1 | * @param int $length |
|
| 461 | 1 | * |
|
| 462 | * @return StringPrimitive |
||
| 463 | */ |
||
| 464 | 1 | public function substring($start, $length = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Return a formatted string |
||
| 477 | * |
||
| 478 | * @return StringPrimitive |
||
| 479 | */ |
||
| 480 | public function sprintf() |
||
| 488 | } |
||
| 489 |