Complex classes like StringType 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 StringType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class StringType extends Stringy implements TransmutableTypeInterface, ValueTypeInterface |
||
24 | { |
||
25 | use Transmutable; |
||
26 | use Boxable; |
||
27 | |||
28 | /** |
||
29 | * StringType constructor. |
||
30 | * Explicitly removing parent constructor. Type conversion should be done through StringType::from($mixed). |
||
31 | * |
||
32 | * @param string $str |
||
33 | * @param string|null $encoding |
||
34 | */ |
||
35 | 91 | public function __construct(string $str, string $encoding = null) |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | * |
||
45 | * @return string|int|float|bool|array |
||
46 | */ |
||
47 | 20 | public function __invoke(int $toType = Type::STRING) |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | 35 | public function get() |
|
84 | |||
85 | /** |
||
86 | * Creates a new static instance from string. |
||
87 | * |
||
88 | * Mainly here for code completion purposes... |
||
89 | * |
||
90 | * @param string $str |
||
91 | * @param string $encoding |
||
92 | * |
||
93 | * @return StringType |
||
94 | */ |
||
95 | 51 | public static function create($str = '', $encoding = 'UTF-8'): StringType |
|
99 | |||
100 | /** |
||
101 | * Explodes current instance into a collection object. |
||
102 | * |
||
103 | * @param string $delimiter |
||
104 | * @param int $limit default PHP_INT_MAX |
||
105 | * @param bool $trim default false, greedely trim the string before exploding |
||
106 | * |
||
107 | * @return Collection|StringType[] |
||
108 | */ |
||
109 | 8 | public function explode(string $delimiter, int $limit = PHP_INT_MAX, bool $trim = true): Collection |
|
116 | |||
117 | /** |
||
118 | * Pluralizes the string. |
||
119 | * |
||
120 | * @return StringType |
||
121 | */ |
||
122 | 1 | public function pluralize(): StringType |
|
126 | |||
127 | /** |
||
128 | * Singularizes the string. |
||
129 | * |
||
130 | * @return StringType |
||
131 | */ |
||
132 | 1 | public function singularize(): StringType |
|
136 | |||
137 | /** |
||
138 | * Returns position of the first occurrence of subStr null if not present. |
||
139 | * |
||
140 | * @param string $subStr Substring |
||
141 | * @param int $offset Chars to offset from start |
||
142 | * @param bool $caseSensitive Enable case sensitivity |
||
143 | * |
||
144 | * @return IntType |
||
145 | */ |
||
146 | 3 | public function strpos(string $subStr, int $offset = 0, bool $caseSensitive = false): IntType |
|
154 | |||
155 | /** |
||
156 | * Returns position of the last occurrence of subStr null if not present. |
||
157 | * |
||
158 | * @param string $subStr Substring |
||
159 | * @param int $offset Chars to offset from start |
||
160 | * @param bool $caseSensitive Enable case sensitivity |
||
161 | * |
||
162 | * @return IntType |
||
163 | */ |
||
164 | 1 | public function strrpos(string $subStr, int $offset = 0, bool $caseSensitive = false): IntType |
|
172 | |||
173 | /** |
||
174 | * @return BooleanType |
||
175 | */ |
||
176 | 6 | public function isSemVer(): BooleanType |
|
187 | |||
188 | /** |
||
189 | * @return string The current value of the $str property |
||
190 | */ |
||
191 | 43 | public function __toString(): string |
|
195 | |||
196 | /** |
||
197 | * Override trait to remove spaces. |
||
198 | * |
||
199 | * @return BooleanType |
||
200 | */ |
||
201 | 1 | public function toBoolType(): BooleanType |
|
205 | |||
206 | /** |
||
207 | * @return BooleanType |
||
208 | */ |
||
209 | 1 | public function toBoolean(): BooleanType |
|
213 | |||
214 | /** |
||
215 | * @return DateTimeType |
||
216 | */ |
||
217 | 1 | public function toDateTime(): DateTimeType |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | * |
||
225 | * @return StringType |
||
226 | */ |
||
227 | 54 | public static function valueOf($mixed): StringType |
|
231 | |||
232 | /** |
||
233 | * Returns substring from beginning until first instance of subsStr. |
||
234 | * |
||
235 | * @param string $subStr |
||
236 | * @param bool $includingSubStr |
||
237 | * @param bool $caseSensitive |
||
238 | * |
||
239 | * @return static |
||
240 | */ |
||
241 | 1 | public function subStrUntil($subStr, $includingSubStr = false, $caseSensitive = false) |
|
247 | |||
248 | /** |
||
249 | * Returns substring from first instance of subStr to end of string. |
||
250 | * @param $subStr |
||
251 | * @param bool $includingSubStr |
||
252 | * @param bool $caseSensitive |
||
253 | * |
||
254 | * @return static |
||
255 | */ |
||
256 | 1 | public function subStrAfter($subStr, $includingSubStr = false, $caseSensitive = false) |
|
260 | |||
261 | /** |
||
262 | * Returns substring between fromSubStr to toSubStr. End of string if toSubStr is not set. |
||
263 | * |
||
264 | * @param string $fromSubStr |
||
265 | * @param string $toSubStr |
||
266 | * @param bool $excludeFromSubStr |
||
267 | * @param bool $excludeToSubStr |
||
268 | * @param bool $caseSensitive |
||
269 | * @return self |
||
270 | */ |
||
271 | 2 | private function subStrBetween( |
|
296 | |||
297 | /** |
||
298 | * @return Inflector |
||
299 | */ |
||
300 | 2 | private function getInflector(): Inflector |
|
304 | |||
305 | /** |
||
306 | * Returns a mixed variable as a string. |
||
307 | * |
||
308 | * @param mixed $mixed |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | 54 | private static function asString($mixed): string |
|
347 | } |
||
348 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.