Complex classes like Text 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 Text, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Text implements \JsonSerializable, Renderable |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $content; |
||
16 | |||
17 | /** |
||
18 | * Text constructor. |
||
19 | * @param mixed $content |
||
20 | */ |
||
21 | public function __construct($content) |
||
25 | |||
26 | /** |
||
27 | * @param $result |
||
28 | * @return null|string |
||
29 | */ |
||
30 | protected function parse($result) |
||
52 | |||
53 | /** |
||
54 | * @return Text[]|array |
||
55 | */ |
||
56 | public function sentences() |
||
65 | |||
66 | /** |
||
67 | * @return Text |
||
68 | * @TODO Improve regular expressions needed |
||
69 | */ |
||
70 | public function escape() |
||
82 | |||
83 | /** |
||
84 | * @param $type |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function typeof($type) |
||
93 | |||
94 | /** |
||
95 | * @param $content |
||
96 | * @param bool $ignoreCase |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function is($content, $ignoreCase = false) |
||
107 | |||
108 | /** |
||
109 | * @param $pattern |
||
110 | * @return int |
||
111 | */ |
||
112 | public function match($pattern) |
||
116 | |||
117 | /** |
||
118 | * @param $pattern |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function matches($pattern) |
||
127 | |||
128 | /** |
||
129 | * @param $content |
||
130 | * @param $ignoreCase |
||
131 | * @return string |
||
132 | */ |
||
133 | public function contains($content, $ignoreCase = false) |
||
141 | |||
142 | /** |
||
143 | * @return Text |
||
144 | */ |
||
145 | public function onlyWords() |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function toLowerCase() |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function toUpperCase() |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getContent() |
||
179 | |||
180 | /** |
||
181 | * @param $content |
||
182 | * @return Text |
||
183 | */ |
||
184 | public function setContent($content) |
||
192 | |||
193 | /** |
||
194 | * @return Text |
||
195 | */ |
||
196 | public function italic() |
||
200 | |||
201 | /** |
||
202 | * @return Text |
||
203 | */ |
||
204 | public function bold() |
||
208 | |||
209 | /** |
||
210 | * @param string|null $lang |
||
211 | * @return Text |
||
212 | */ |
||
213 | public function code(string $lang = null) |
||
220 | |||
221 | /** |
||
222 | * @param $src |
||
223 | * @param bool $after |
||
224 | * @return Text |
||
225 | */ |
||
226 | public function image($src, $after = true) |
||
234 | |||
235 | /** |
||
236 | * @param bool $after |
||
237 | * @return Text |
||
238 | */ |
||
239 | public function hr($after = true) |
||
246 | |||
247 | /** |
||
248 | * @return Text |
||
249 | */ |
||
250 | public function quote() |
||
254 | |||
255 | /** |
||
256 | * @param string $symbol |
||
257 | * @param string|null $symbolAfter |
||
258 | * @return Text |
||
259 | */ |
||
260 | public function wrap(string $symbol, string $symbolAfter = null) |
||
269 | |||
270 | /** |
||
271 | * @param $text |
||
272 | * @param bool $newline |
||
273 | * @return Text |
||
274 | */ |
||
275 | public function before($text, $newline = false) |
||
279 | |||
280 | /** |
||
281 | * @param $text |
||
282 | * @param bool $newline |
||
283 | * @return Text |
||
284 | */ |
||
285 | public function after($text, $newline = false) |
||
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | public function __toString() |
||
297 | |||
298 | /** |
||
299 | * @return string |
||
300 | */ |
||
301 | public function toString() |
||
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | public function jsonSerialize() |
||
313 | |||
314 | /** |
||
315 | * @return string |
||
316 | */ |
||
317 | public function render() |
||
321 | } |
||
322 |