Complex classes like Docblock 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 Docblock, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 16 | class Docblock { |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | protected $shortDescription; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | protected $longDescription; |
||
| 23 | |||
| 24 | /** @var ArrayList */ |
||
| 25 | protected $tags; |
||
| 26 | |||
| 27 | /** @var null|Comparator */ |
||
| 28 | protected $comparator = null; |
||
| 29 | |||
| 30 | const REGEX_TAGNAME = '[\w\-\_\\\\]+'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Static docblock factory |
||
| 34 | * |
||
| 35 | * @param ReflectionFunctionAbstract|ReflectionClass|ReflectionProperty|string $docblock a docblock to parse |
||
| 36 | * |
||
| 37 | * @return $this |
||
| 38 | */ |
||
| 39 | 1 | public static function create($docblock = ''): self { |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Creates a new docblock instance and parses the initial string or reflector object if given |
||
| 45 | * |
||
| 46 | * @param ReflectionFunctionAbstract|ReflectionClass|ReflectionProperty|string $docblock a docblock to parse |
||
| 47 | */ |
||
| 48 | 12 | public function __construct($docblock = '') { |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 55 | * |
||
| 56 | * @param ReflectionFunctionAbstract|ReflectionClass|ReflectionProperty|string $docblock |
||
| 57 | * |
||
| 58 | * @throws InvalidArgumentException if there is no getDocCommect() method available |
||
| 59 | */ |
||
| 60 | 12 | protected function parse($docblock): void { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Strips the asterisks from the DocBlock comment. |
||
| 80 | * |
||
| 81 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 82 | * |
||
| 83 | * @param string $comment String containing the comment text. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 11 | protected function cleanInput(string $comment): string { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Splits the Docblock into a short description, long description and |
||
| 103 | * block of tags. |
||
| 104 | * |
||
| 105 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 106 | * |
||
| 107 | * @param string $comment Comment to split into the sub-parts. |
||
| 108 | * |
||
| 109 | * @author RichardJ Special thanks to RichardJ for the regex responsible |
||
| 110 | * for the split. |
||
| 111 | * |
||
| 112 | * @return string[] containing the short-, long description and an element |
||
| 113 | * containing the tags. |
||
| 114 | */ |
||
| 115 | 10 | protected function splitDocBlock(string $comment): array { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Parses the tags |
||
| 179 | * |
||
| 180 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 181 | * |
||
| 182 | * @param string $tags |
||
| 183 | * |
||
| 184 | * @throws LogicException |
||
| 185 | * @throws InvalidArgumentException |
||
| 186 | */ |
||
| 187 | 11 | protected function parseTags(string $tags): void { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Checks whether the given line is a tag line (= starts with @) or not |
||
| 213 | * |
||
| 214 | * @param string $line |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 4 | protected function isTagLine(string $line): bool { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Parses an individual tag line |
||
| 224 | * |
||
| 225 | * @param string $line |
||
| 226 | * |
||
| 227 | * @throws InvalidArgumentException |
||
| 228 | * |
||
| 229 | * @return AbstractTag |
||
| 230 | */ |
||
| 231 | 4 | protected function parseTag(string $line): AbstractTag { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Returns the short description |
||
| 245 | * |
||
| 246 | * @return string the short description |
||
| 247 | */ |
||
| 248 | 3 | public function getShortDescription(): string { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Sets the short description |
||
| 254 | * |
||
| 255 | * @param string $description the new description |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | 2 | public function setShortDescription(string $description = ''): self { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns the long description |
||
| 267 | * |
||
| 268 | * @return string the long description |
||
| 269 | */ |
||
| 270 | 2 | public function getLongDescription(): string { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Sets the long description |
||
| 276 | * |
||
| 277 | * @param string $description the new description |
||
| 278 | * |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | 2 | public function setLongDescription(string $description = ''): self { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Adds a tag to this docblock |
||
| 289 | * |
||
| 290 | * @param AbstractTag $tag |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | 4 | public function appendTag(AbstractTag $tag): self { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * removes tags (by tag name) |
||
| 302 | * |
||
| 303 | * @param string $tagName |
||
| 304 | */ |
||
| 305 | public function removeTags(string $tagName = ''): void { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks whether a tag is present |
||
| 313 | * |
||
| 314 | * @param string $tagName |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | 1 | public function hasTag(string $tagName): bool { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Gets tags (by tag name) |
||
| 326 | * |
||
| 327 | * @param string $tagName |
||
| 328 | * |
||
| 329 | * @return ArrayList the tags |
||
| 330 | */ |
||
| 331 | 1 | public function getTags(string $tagName = null): ArrayList { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * A list of tags sorted by tag-name |
||
| 339 | * |
||
| 340 | * @return ArrayList |
||
| 341 | */ |
||
| 342 | 6 | public function getSortedTags(): ArrayList { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Returns true when there is no content in the docblock |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | 1 | public function isEmpty(): bool { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Returns the string version of the docblock |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 5 | public function toString(): string { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Writes multiple lines with ' * ' prefixed for docblock |
||
| 416 | * |
||
| 417 | * @param string[] $lines the lines to be written |
||
| 418 | * @param bool $newline if a new line should be added before |
||
| 419 | * |
||
| 420 | * @return string the lines as string |
||
| 421 | */ |
||
| 422 | 5 | protected function writeLines(array $lines, bool $newline = false): string { |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Magic toString() method |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 1 | public function __toString() { |
|
| 450 | } |
||
| 451 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.