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 | private $value; |
||
| 12 | |||
| 13 | 52 | public function __construct($value) |
|
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | 2 | public function toPrimitive() |
|
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | 27 | public function __toString() |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Split the string into a collection of ones |
||
| 40 | * |
||
| 41 | * @param string $delimiter |
||
| 42 | * |
||
| 43 | * @return TypedCollection |
||
| 44 | */ |
||
| 45 | 3 | public function split($delimiter = null) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Returns a collection of the string splitted by the given chunk size |
||
| 64 | * |
||
| 65 | * @param int $size |
||
| 66 | * |
||
| 67 | * @return TypedCollection |
||
| 68 | */ |
||
| 69 | 1 | public function chunk($size = 1) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the position of the first occurence of the string |
||
| 76 | * |
||
| 77 | * @param string $needle |
||
| 78 | * @param int $offset |
||
| 79 | * |
||
| 80 | * @throws SubstringException If the string is not found |
||
| 81 | * |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | 3 | public function pos($needle, $offset = 0) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Replace all occurences of the search string with the replacement one |
||
| 100 | * |
||
| 101 | * @param string $search |
||
| 102 | * @param string $replacement |
||
| 103 | * |
||
| 104 | * @return StringPrimitive |
||
| 105 | */ |
||
| 106 | 1 | public function replace($search, $replacement) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the string following the given delimiter |
||
| 117 | * |
||
| 118 | * @param string $delimiter |
||
| 119 | * |
||
| 120 | * @throws SubstringException If the string is not found |
||
| 121 | * |
||
| 122 | * @return StringInterface |
||
| 123 | */ |
||
| 124 | 2 | public function str($delimiter) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Return the string in upper case |
||
| 140 | * |
||
| 141 | * @return StringPrimitive |
||
| 142 | */ |
||
| 143 | 1 | public function toUpper() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Return the string in lower case |
||
| 150 | * |
||
| 151 | * @return StringPrimitive |
||
| 152 | */ |
||
| 153 | 1 | public function toLower() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Return the string length |
||
| 160 | * |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | 2 | public function length() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Reverse the string |
||
| 170 | * |
||
| 171 | * @return StringPrimitive |
||
| 172 | */ |
||
| 173 | 1 | public function reverse() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Pad the string |
||
| 180 | * |
||
| 181 | * @param int $length |
||
| 182 | * @param string $character |
||
| 183 | * @param int $direction |
||
| 184 | * |
||
| 185 | * @return StringPrimitive |
||
| 186 | */ |
||
| 187 | 1 | public function pad($length, $character = ' ', $direction = STR_PAD_RIGHT) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Pad to the right |
||
| 199 | * |
||
| 200 | * @param int $length |
||
| 201 | * @param string $character |
||
| 202 | * |
||
| 203 | * @return StringPrimitive |
||
| 204 | */ |
||
| 205 | 1 | public function rightPad($length, $character = ' ') |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Pad to the left |
||
| 212 | * |
||
| 213 | * @param int $length |
||
| 214 | * @param string $character |
||
| 215 | * |
||
| 216 | * @return StringPrimitive |
||
| 217 | */ |
||
| 218 | 1 | public function leftPad($length, $character = ' ') |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Pad both sides |
||
| 225 | * |
||
| 226 | * @param int $length |
||
| 227 | * @param string $character |
||
| 228 | * |
||
| 229 | * @return StringPrimitive |
||
| 230 | */ |
||
| 231 | 1 | public function uniPad($length, $character = ' ') |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Find length of initial segment not matching mask |
||
| 238 | * |
||
| 239 | * @param string $mask |
||
| 240 | * @param int $start |
||
| 241 | * @param int $length |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | 1 | public function cspn($mask, $start = 0, $length = null) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Repeat the string n times |
||
| 265 | * |
||
| 266 | * @param int $repeat |
||
| 267 | * |
||
| 268 | * @return StringPrimitive |
||
| 269 | */ |
||
| 270 | 1 | public function repeat($repeat) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Shuffle the string |
||
| 277 | * |
||
| 278 | * @return StringPrimitive |
||
| 279 | */ |
||
| 280 | 1 | public function shuffle() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Strip slashes |
||
| 287 | * |
||
| 288 | * @return StringPrimitive |
||
| 289 | */ |
||
| 290 | 1 | public function stripSlashes() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Strip C-like slashes |
||
| 297 | * |
||
| 298 | * @return StringPrimitive |
||
| 299 | */ |
||
| 300 | 1 | public function stripCSlashes() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Return the word count |
||
| 307 | * |
||
| 308 | * @param string $charlist |
||
| 309 | * |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | 1 | public function wordCount($charlist = '') |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Return the collection of words |
||
| 323 | * |
||
| 324 | * @param string $charlist |
||
| 325 | * |
||
| 326 | * @return TypedCollection |
||
| 327 | */ |
||
| 328 | 1 | public function words($charlist = '') |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Split the string using a regular expression |
||
| 344 | * |
||
| 345 | * @param string $regex |
||
| 346 | * @param int $limit |
||
| 347 | * @param int $flags |
||
| 348 | * |
||
| 349 | * @return TypedCollection |
||
| 350 | */ |
||
| 351 | 1 | public function pregSplit($regex, $limit = -1, $flags = 0) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Check if the string match the given regular expression |
||
| 367 | * |
||
| 368 | * @param string $regex |
||
| 369 | * @param int $flags |
||
| 370 | * @param int $offset |
||
| 371 | * |
||
| 372 | * @throws Exception If the regex failed |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 2 | public function match($regex, $flags = 0, $offset = 0) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Return a collection of the elements matching the regex |
||
| 390 | * |
||
| 391 | * @param string $regex |
||
| 392 | * @param int $flags |
||
| 393 | * @param int $offset |
||
| 394 | * |
||
| 395 | * @throws Exception If the regex failed |
||
| 396 | * |
||
| 397 | * @return TypedCollection |
||
| 398 | */ |
||
| 399 | 2 | public function getMatches($regex, $flags = 0, $offset = 0) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Replace part of the string by using a regular expression |
||
| 423 | * |
||
| 424 | * @param string $regex |
||
| 425 | * @param string $replacement |
||
| 426 | * @param int $limit |
||
| 427 | * |
||
| 428 | * @throws Exception If the regex failed |
||
| 429 | * |
||
| 430 | * @return StringPrimitive |
||
| 431 | */ |
||
| 432 | 1 | public function pregReplace($regex, $replacement, $limit = -1) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Return part of the string |
||
| 450 | * |
||
| 451 | * @param int $start |
||
| 452 | * @param int $length |
||
| 453 | * |
||
| 454 | * @return StringPrimitive |
||
| 455 | */ |
||
| 456 | 1 | public function substring($start, $length = null) |
|
| 466 | } |
||
| 467 |