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 | const PAD_BOTH = STR_PAD_BOTH; |
||
| 14 | const PREG_NO_FLAGS = 0; |
||
| 15 | const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
||
| 16 | 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 | |||
| 20 | private $value; |
||
| 21 | |||
| 22 | 53 | public function __construct($value) |
|
| 23 | { |
||
| 24 | 53 | if (!is_string($value)) { |
|
| 25 | 1 | throw new TypeException('Value must be a string'); |
|
| 26 | } |
||
| 27 | |||
| 28 | 52 | $this->value = (string) $value; |
|
| 29 | 52 | } |
|
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 2 | public function toPrimitive() |
|
| 35 | { |
||
| 36 | 2 | return $this->value; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 28 | public function __toString() |
|
| 43 | { |
||
| 44 | 28 | return $this->value; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Split the string into a collection of ones |
||
| 49 | * |
||
| 50 | * @param string $delimiter |
||
| 51 | * |
||
| 52 | * @return TypedCollection |
||
| 53 | */ |
||
| 54 | 3 | public function split($delimiter = null) |
|
| 55 | { |
||
| 56 | 3 | $delimiter = (string) $delimiter; |
|
| 57 | 3 | $parts = empty($delimiter) ? |
|
| 58 | 3 | str_split($this->value) : explode($delimiter, $this->value); |
|
| 59 | 3 | $strings = []; |
|
| 60 | |||
| 61 | 3 | foreach ($parts as $part) { |
|
| 62 | 3 | $strings[] = new self($part); |
|
| 63 | 3 | } |
|
| 64 | |||
| 65 | 3 | return new TypedCollection( |
|
| 66 | 3 | self::class, |
|
| 67 | 1 | $strings |
|
| 68 | 3 | ); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns a collection of the string splitted by the given chunk size |
||
| 73 | * |
||
| 74 | * @param int $size |
||
| 75 | * |
||
| 76 | * @return TypedCollection |
||
| 77 | */ |
||
| 78 | 1 | public function chunk($size = 1) |
|
| 79 | { |
||
| 80 | 1 | return $this->split()->chunk((int) $size); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the position of the first occurence of the string |
||
| 85 | * |
||
| 86 | * @param string $needle |
||
| 87 | * @param int $offset |
||
| 88 | * |
||
| 89 | * @throws SubstringException If the string is not found |
||
| 90 | * |
||
| 91 | * @return int |
||
| 92 | */ |
||
| 93 | 3 | public function pos($needle, $offset = 0) |
|
| 94 | { |
||
| 95 | 3 | $position = mb_strpos($this->value, (string) $needle, (int) $offset); |
|
| 96 | |||
| 97 | 3 | if ($position === false) { |
|
| 98 | 1 | throw new SubstringException(sprintf( |
|
| 99 | 1 | 'Substring "%s" not found', |
|
| 100 | $needle |
||
| 101 | 1 | )); |
|
| 102 | } |
||
| 103 | |||
| 104 | 2 | return (int) $position; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Replace all occurences of the search string with the replacement one |
||
| 109 | * |
||
| 110 | * @param string $search |
||
| 111 | * @param string $replacement |
||
| 112 | * |
||
| 113 | * @return StringPrimitive |
||
| 114 | */ |
||
| 115 | 1 | public function replace($search, $replacement) |
|
| 116 | { |
||
| 117 | 1 | return new self(str_replace( |
|
| 118 | 1 | (string) $search, |
|
| 119 | 1 | (string) $replacement, |
|
| 120 | 1 | $this->value |
|
| 121 | 1 | )); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the string following the given delimiter |
||
| 126 | * |
||
| 127 | * @param string $delimiter |
||
| 128 | * |
||
| 129 | * @throws SubstringException If the string is not found |
||
| 130 | * |
||
| 131 | * @return StringInterface |
||
| 132 | */ |
||
| 133 | 2 | public function str($delimiter) |
|
| 134 | { |
||
| 135 | 2 | $sub = mb_strstr($this->value, (string) $delimiter); |
|
| 136 | |||
| 137 | 2 | if ($sub === false) { |
|
| 138 | 1 | throw new SubstringException(sprintf( |
|
| 139 | 1 | 'Substring "%s" not found', |
|
| 140 | $delimiter |
||
| 141 | 1 | )); |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | return new self($sub); |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the string in upper case |
||
| 149 | * |
||
| 150 | * @return StringPrimitive |
||
| 151 | */ |
||
| 152 | 1 | public function toUpper() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Return the string in lower case |
||
| 159 | * |
||
| 160 | * @return StringPrimitive |
||
| 161 | */ |
||
| 162 | 1 | public function toLower() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Return the string length |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | 2 | public function length() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Reverse the string |
||
| 179 | * |
||
| 180 | * @return StringPrimitive |
||
| 181 | */ |
||
| 182 | 1 | public function reverse() |
|
| 183 | { |
||
| 184 | 1 | return new self(strrev($this->value)); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Pad the string |
||
| 189 | * |
||
| 190 | * @param int $length |
||
| 191 | * @param string $character |
||
| 192 | * @param int $direction |
||
| 193 | * |
||
| 194 | * @return StringPrimitive |
||
| 195 | */ |
||
| 196 | 1 | public function pad($length, $character = ' ', $direction = self::PAD_RIGHT) |
|
| 197 | { |
||
| 198 | 1 | return new self(str_pad( |
|
| 199 | 1 | $this->value, |
|
| 200 | 1 | (int) $length, |
|
| 201 | 1 | (string) $character, |
|
| 202 | $direction |
||
| 203 | 1 | )); |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Pad to the right |
||
| 208 | * |
||
| 209 | * @param int $length |
||
| 210 | * @param string $character |
||
| 211 | * |
||
| 212 | * @return StringPrimitive |
||
| 213 | */ |
||
| 214 | 1 | public function rightPad($length, $character = ' ') |
|
| 215 | { |
||
| 216 | 1 | return $this->pad($length, $character, self::PAD_RIGHT); |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Pad to the left |
||
| 221 | * |
||
| 222 | * @param int $length |
||
| 223 | * @param string $character |
||
| 224 | * |
||
| 225 | * @return StringPrimitive |
||
| 226 | */ |
||
| 227 | 1 | public function leftPad($length, $character = ' ') |
|
| 228 | { |
||
| 229 | 1 | return $this->pad($length, $character, self::PAD_LEFT); |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Pad both sides |
||
| 234 | * |
||
| 235 | * @param int $length |
||
| 236 | * @param string $character |
||
| 237 | * |
||
| 238 | * @return StringPrimitive |
||
| 239 | */ |
||
| 240 | 1 | public function uniPad($length, $character = ' ') |
|
| 241 | { |
||
| 242 | 1 | return $this->pad($length, $character, self::PAD_BOTH); |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Find length of initial segment not matching mask |
||
| 247 | * |
||
| 248 | * @param string $mask |
||
| 249 | * @param int $start |
||
| 250 | * @param int $length |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | 1 | public function cspn($mask, $start = 0, $length = null) |
|
| 255 | { |
||
| 256 | 1 | $start = (int) $start; |
|
| 257 | |||
| 258 | 1 | if ($length === null) { |
|
| 259 | 1 | $value = strcspn($this->value, (string) $mask, $start); |
|
| 260 | 1 | } else { |
|
| 261 | 1 | $value = strcspn( |
|
| 262 | 1 | $this->value, |
|
| 263 | 1 | (string) $mask, |
|
| 264 | 1 | $start, |
|
| 265 | (int) $length |
||
| 266 | 1 | ); |
|
| 267 | } |
||
| 268 | |||
| 269 | 1 | return (int) $value; |
|
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Repeat the string n times |
||
| 274 | * |
||
| 275 | * @param int $repeat |
||
| 276 | * |
||
| 277 | * @return StringPrimitive |
||
| 278 | */ |
||
| 279 | 1 | public function repeat($repeat) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Shuffle the string |
||
| 286 | * |
||
| 287 | * @return StringPrimitive |
||
| 288 | */ |
||
| 289 | 1 | public function shuffle() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Strip slashes |
||
| 296 | * |
||
| 297 | * @return StringPrimitive |
||
| 298 | */ |
||
| 299 | 1 | public function stripSlashes() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Strip C-like slashes |
||
| 306 | * |
||
| 307 | * @return StringPrimitive |
||
| 308 | */ |
||
| 309 | 1 | public function stripCSlashes() |
|
| 310 | { |
||
| 311 | 1 | return new self(stripcslashes($this->value)); |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return the word count |
||
| 316 | * |
||
| 317 | * @param string $charlist |
||
| 318 | * |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | 1 | public function wordCount($charlist = '') |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Return the collection of words |
||
| 332 | * |
||
| 333 | * @param string $charlist |
||
| 334 | * |
||
| 335 | * @return TypedCollection |
||
| 336 | */ |
||
| 337 | 1 | public function words($charlist = '') |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Split the string using a regular expression |
||
| 353 | * |
||
| 354 | * @param string $regex |
||
| 355 | * @param int $limit |
||
| 356 | * @param int $flags |
||
| 357 | * |
||
| 358 | * @return TypedCollection |
||
| 359 | */ |
||
| 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 | * |
||
| 377 | * @param string $regex |
||
| 378 | * @param int $offset |
||
| 379 | * |
||
| 380 | * @throws Exception If the regex failed |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | 2 | public function match($regex, $offset = 0) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Return a collection of the elements matching the regex |
||
| 398 | * |
||
| 399 | * @param string $regex |
||
| 400 | * @param int $offset |
||
| 401 | * @param int $flags |
||
| 402 | * |
||
| 403 | * @throws Exception If the regex failed |
||
| 404 | * |
||
| 405 | * @return TypedCollection |
||
| 406 | */ |
||
| 407 | 2 | 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 | * @param string $regex |
||
| 433 | * @param string $replacement |
||
| 434 | * @param int $limit |
||
| 435 | * |
||
| 436 | * @throws Exception If the regex failed |
||
| 437 | * |
||
| 438 | * @return StringPrimitive |
||
| 439 | */ |
||
| 440 | 1 | public function pregReplace($regex, $replacement, $limit = -1) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Return part of the string |
||
| 458 | * |
||
| 459 | * @param int $start |
||
| 460 | * @param int $length |
||
| 461 | * |
||
| 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 | 1 | public function sprintf() |
|
| 488 | } |
||
| 489 |