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 |
||
| 10 | class Docblock { |
||
| 11 | |||
| 12 | protected $shortDescription; |
||
| 13 | protected $longDescription; |
||
| 14 | protected $tags; |
||
| 15 | protected $comparator = null; |
||
| 16 | |||
| 17 | const REGEX_TAGNAME = '[\w\-\_\\\\]+'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Static docblock factory |
||
| 21 | * |
||
| 22 | * @param string \Reflector|$docblock |
||
| 23 | * @return $this |
||
| 24 | */ |
||
| 25 | 1 | public static function create($docblock = null) { |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Creates a new docblock instance and parses the initial string or reflector object if given |
||
| 31 | * |
||
| 32 | * @param \ReflectionFunctionAbstract|\ReflectionClass|\ReflectionProperty|string a docblock to parse |
||
| 33 | */ |
||
| 34 | 12 | public function __construct($docblock = null) { |
|
| 35 | 12 | $this->tags = new ArrayList(); |
|
| 36 | 12 | $this->parse($docblock); |
|
| 37 | 10 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 41 | * @param \ReflectionFunctionAbstract|\ReflectionClass|\ReflectionProperty|string $docblock |
||
| 42 | * @throws \InvalidArgumentException if there is no getDocCommect() method available |
||
| 43 | */ |
||
| 44 | 12 | protected function parse($docblock) { |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Strips the asterisks from the DocBlock comment. |
||
| 64 | * |
||
| 65 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 66 | * @param string $comment String containing the comment text. |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 11 | protected function cleanInput($comment) { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Splits the Docblock into a short description, long description and |
||
| 85 | * block of tags. |
||
| 86 | * |
||
| 87 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 88 | * |
||
| 89 | * @param string $comment Comment to split into the sub-parts. |
||
| 90 | * |
||
| 91 | * @author RichardJ Special thanks to RichardJ for the regex responsible |
||
| 92 | * for the split. |
||
| 93 | * |
||
| 94 | * @return string[] containing the short-, long description and an element |
||
| 95 | * containing the tags. |
||
| 96 | */ |
||
| 97 | 10 | protected function splitDocBlock($comment) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Parses the tags |
||
| 160 | * |
||
| 161 | * @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock.php Original Method |
||
| 162 | * @param string $tags |
||
| 163 | * @throws \LogicException |
||
| 164 | * @throws \InvalidArgumentException |
||
| 165 | */ |
||
| 166 | 11 | protected function parseTags($tags) { |
|
| 167 | 11 | $tags = trim($tags); |
|
| 168 | 11 | if (!empty($tags)) { |
|
| 169 | |||
| 170 | // sanitize lines |
||
| 171 | 4 | $result = []; |
|
| 172 | 4 | foreach (explode("\n", $tags) as $line) { |
|
| 173 | 4 | if ($this->isTagLine($line) || count($result) == 0) { |
|
| 174 | 4 | $result[] = $line; |
|
| 175 | 4 | } else { |
|
| 176 | 1 | $result[count($result) - 1] .= PHP_EOL . $line; |
|
| 177 | } |
||
| 178 | 4 | } |
|
| 179 | |||
| 180 | // create proper Tag objects |
||
| 181 | 4 | if (count($result)) { |
|
| 182 | 4 | $this->tags->clear(); |
|
| 183 | 4 | foreach ($result as $line) { |
|
| 184 | 4 | $this->tags->add($this->parseTag($line)); |
|
| 185 | 3 | } |
|
| 186 | 3 | } |
|
| 187 | 3 | } |
|
| 188 | |||
| 189 | |||
| 190 | 10 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Checks whether the given line is a tag line (= starts with @) or not |
||
| 194 | * |
||
| 195 | * @param string $line |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | 4 | protected function isTagLine($line) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Parses an individual tag line |
||
| 204 | * |
||
| 205 | * @param string $line |
||
| 206 | * @throws \InvalidArgumentException |
||
| 207 | * @return \gossi\docblock\tags\AbstractTag |
||
| 208 | */ |
||
| 209 | 4 | protected function parseTag($line) { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the short description |
||
| 223 | * |
||
| 224 | * @return string the short description |
||
| 225 | */ |
||
| 226 | 3 | public function getShortDescription() { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Sets the short description |
||
| 232 | * |
||
| 233 | * @param string $description the new description |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | 2 | public function setShortDescription($description) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the long description |
||
| 243 | * |
||
| 244 | * @return string the long description |
||
| 245 | */ |
||
| 246 | 2 | public function getLongDescription() { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the long description |
||
| 252 | * |
||
| 253 | * @param string $description the new description |
||
| 254 | */ |
||
| 255 | 2 | public function setLongDescription($description) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Adds a tag to this docblock |
||
| 262 | * |
||
| 263 | * @param AbstractTag $tag |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | 4 | public function appendTag(AbstractTag $tag) { |
|
| 267 | 4 | $this->tags->add($tag); |
|
| 268 | 4 | return $this; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * removes tags (by tag name) |
||
| 273 | * |
||
| 274 | * @param string $tagName |
||
| 275 | */ |
||
| 276 | public function removeTags($tagName = null) { |
||
| 277 | $this->tags = $this->tags->filter(function ($tag) use ($tagName) { |
||
| 278 | return $tagName !== $tag->getTagName(); |
||
| 279 | }); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Checks whether a tag is present |
||
| 284 | * |
||
| 285 | * @param string $tagName |
||
| 286 | * @return boolean |
||
| 287 | */ |
||
| 288 | 1 | public function hasTag($tagName) { |
|
| 289 | return $this->tags->search($tagName, function (AbstractTag $tag, $query) { |
||
| 290 | 1 | return $tag->getTagName() == $query; |
|
| 291 | 1 | }); |
|
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets tags (by tag name) |
||
| 296 | * |
||
| 297 | * @param string $tagName |
||
| 298 | * @return ArrayList the tags |
||
| 299 | */ |
||
| 300 | 1 | public function getTags($tagName = null) { |
|
| 301 | return $this->tags->filter(function ($tag) use ($tagName) { |
||
| 302 | 1 | return $tagName === null || $tag->getTagName() == $tagName; |
|
| 303 | 1 | }); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * A list of tags sorted by tag-name |
||
| 308 | * |
||
| 309 | * @return ArrayList |
||
| 310 | */ |
||
| 311 | 6 | public function getSortedTags() { |
|
| 312 | 6 | if ($this->comparator === null) { |
|
| 313 | 6 | $this->comparator = new TagNameComparator(); |
|
| 314 | 6 | } |
|
| 315 | |||
| 316 | // 1) group by tag name |
||
| 317 | 6 | $group = new Map(); |
|
| 318 | 6 | foreach ($this->tags as $tag) { |
|
| 319 | 5 | if (!$group->has($tag->getTagName())) { |
|
| 320 | 5 | $group->set($tag->getTagName(), new ArrayList()); |
|
| 321 | 5 | } |
|
| 322 | |||
| 323 | 5 | $group->get($tag->getTagName())->add($tag); |
|
| 324 | 6 | } |
|
| 325 | |||
| 326 | // 2) Sort the group by tag name |
||
| 327 | 6 | $group->sortKeys(new TagNameComparator()); |
|
| 328 | |||
| 329 | // 3) flatten the group |
||
| 330 | 6 | $sorted = new ArrayList(); |
|
| 331 | 6 | foreach ($group->values() as $tags) { |
|
| 332 | 5 | $sorted->addAll($tags); |
|
| 333 | 6 | } |
|
| 334 | |||
| 335 | 6 | return $sorted; |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns true when there is no content in the docblock |
||
| 340 | * |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | 1 | public function isEmpty() { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns the string version of the docblock |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 5 | public function toString() { |
|
| 355 | 5 | $docblock = "/**\n"; |
|
| 356 | |||
| 357 | // short description |
||
| 358 | 5 | $short = trim($this->shortDescription); |
|
| 359 | 5 | if (!empty($short)) { |
|
| 360 | 3 | $docblock .= $this->writeLines(explode("\n", $short)); |
|
| 361 | 3 | } |
|
| 362 | |||
| 363 | // short description |
||
| 364 | 5 | $long = trim($this->longDescription); |
|
| 365 | 5 | if (!empty($long)) { |
|
| 366 | 2 | $docblock .= $this->writeLines(explode("\n", $long), !empty($short)); |
|
| 367 | 2 | } |
|
| 368 | |||
| 369 | // tags |
||
| 370 | 5 | $tags = $this->getSortedTags()->map(function($tag) { |
|
| 371 | 4 | return $tag->toString(); |
|
| 372 | 5 | }); |
|
| 373 | |||
| 374 | 5 | if (!$tags->isEmpty()) { |
|
| 375 | 4 | $docblock .= $this->writeLines($tags->toArray(), !empty($short) || !empty($long)); |
|
| 376 | 4 | } |
|
| 377 | |||
| 378 | 5 | $docblock .= ' */'; |
|
| 379 | |||
| 380 | 5 | return $docblock; |
|
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Writes multiple lines with ' * ' prefixed for docblock |
||
| 385 | * |
||
| 386 | * @param string[] $lines the lines to be written |
||
| 387 | * @param boolean $newline if a new line should be added before |
||
| 388 | * @return string the lines as string |
||
| 389 | */ |
||
| 390 | 5 | protected function writeLines($lines, $newline = false) { |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Magic toString() method |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 1 | public function __toString() { |
|
| 418 | |||
| 419 | } |
||
| 420 |