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 |
||
| 12 | class Text implements \JsonSerializable, Renderable |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $content = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Text constructor. |
||
| 21 | * @param mixed $content |
||
| 22 | */ |
||
| 23 | public function __construct($content = null) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param $result |
||
| 32 | * @return null|string |
||
| 33 | */ |
||
| 34 | protected function parse($result) |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * @return Text[]|array |
||
| 60 | */ |
||
| 61 | public function sentences() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return Text |
||
| 73 | * @TODO Improve regular expressions needed |
||
| 74 | */ |
||
| 75 | public function escape() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $type |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | public function typeof($type) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param $content |
||
| 101 | * @param bool $ignoreCase |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function is($content, $ignoreCase = false) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $pattern |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function match($pattern) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $pattern |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function matches($pattern) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param $content |
||
| 135 | * @param $ignoreCase |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function contains($content, $ignoreCase = false) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return Text |
||
| 149 | */ |
||
| 150 | public function onlyWords() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function toLowerCase() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function toUpperCase() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getContent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $content |
||
| 187 | * @return Text |
||
| 188 | */ |
||
| 189 | public function setContent($content) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Text |
||
| 200 | */ |
||
| 201 | public function italic() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return Text |
||
| 208 | */ |
||
| 209 | public function bold() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string|null $lang |
||
| 216 | * @return Text |
||
| 217 | */ |
||
| 218 | public function code(string $lang = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param $src |
||
| 228 | * @param bool $after |
||
| 229 | * @return Text |
||
| 230 | */ |
||
| 231 | public function image($src, $after = true) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param bool $after |
||
| 242 | * @return Text |
||
| 243 | */ |
||
| 244 | public function hr($after = true) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return Text |
||
| 254 | */ |
||
| 255 | public function quote() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $symbol |
||
| 262 | * @param string|null $symbolAfter |
||
| 263 | * @param bool $inline |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function wrap(string $symbol, string $symbolAfter = null, $inline = true) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param $text |
||
| 289 | * @param bool $newline |
||
| 290 | * @return Text |
||
| 291 | */ |
||
| 292 | public function before($text, $newline = false) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $text |
||
| 300 | * @return Text |
||
| 301 | */ |
||
| 302 | public function prepend($text) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param $text |
||
| 309 | * @param bool $newline |
||
| 310 | * @return Text |
||
| 311 | */ |
||
| 312 | public function after($text, $newline = false) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param $text |
||
| 320 | * @return Text |
||
| 321 | */ |
||
| 322 | public function append($text) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function __toString() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function toString() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function jsonSerialize() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function render() |
||
| 365 | } |
||
| 366 |