Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Item |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The parent CHM instance. |
||
| 17 | * |
||
| 18 | * @var \CHMLib\CHM |
||
| 19 | */ |
||
| 20 | protected $chm; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The name of the tree item. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Is this item marked as new? |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $isNew; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The comment of the tree item. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $comment; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The keyword of the tree item. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $keyword; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The value of the "See Also" parameter. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $seeAlso; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The local path to the tree item. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $local; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The URL to the tree item. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $url; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The frame name for this item. |
||
| 73 | * |
||
| 74 | * @var string; |
||
| 75 | */ |
||
| 76 | protected $frameName; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The window name for this item. |
||
| 80 | * |
||
| 81 | * @var string; |
||
| 82 | */ |
||
| 83 | protected $windowName; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The path to an entry in another CHM file. |
||
| 87 | * |
||
| 88 | * @var string|array|null If it's an array, it has two keys: 'chm' and 'entry'. |
||
| 89 | */ |
||
| 90 | protected $merge; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The image number attribute. |
||
| 94 | * |
||
| 95 | * @var int|null |
||
| 96 | */ |
||
| 97 | protected $imageNumber; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The value of the X-Condition parameter. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $xCondition; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The sub-elements of this Item. |
||
| 108 | * |
||
| 109 | * @var \CHMLib\TOCIndex\Tree |
||
| 110 | */ |
||
| 111 | protected $children; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Initializes the instance. |
||
| 115 | * |
||
| 116 | 1 | * @param \CHMLib\CHM $chm The parent CHM instance. |
|
| 117 | * @param \DOMElement $object The OBJECT element. |
||
| 118 | 1 | * |
|
| 119 | 1 | * @throws \Exception Throw an Exception in case of errors. |
|
| 120 | 1 | * |
|
| 121 | 1 | * @return static |
|
|
|
|||
| 122 | 1 | */ |
|
| 123 | 1 | public function __construct(CHM $chm, DOMElement $object) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get the parent CHM instance. |
||
| 201 | * |
||
| 202 | 1 | * @return \CHMLib\CHM |
|
| 203 | */ |
||
| 204 | 1 | public function getCHM() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the name of the tree item. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getName() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Is this item marked as new? |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isNew() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the comment of the tree item. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getComment() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the keyword of the tree item. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getKeyword() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get the value of the "See Also" parameter. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getSeeAlso() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get the local path to the tree item. |
||
| 261 | * |
||
| 262 | 1 | * @return string |
|
| 263 | */ |
||
| 264 | 1 | public function getLocal() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get the URL to the tree item. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getURL() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the frame name for this item. |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getFrameName() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the window name for this item. |
||
| 291 | * |
||
| 292 | 1 | * @return string |
|
| 293 | */ |
||
| 294 | 1 | public function getWindowName() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get the path to an entry in another CHM file. |
||
| 301 | * |
||
| 302 | * @var array|null If not null, it's an array with two keys: 'chm' and 'entry'. |
||
| 303 | */ |
||
| 304 | public function getMerge() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the image number attribute. |
||
| 311 | * |
||
| 312 | 1 | * @return int|null |
|
| 313 | */ |
||
| 314 | 1 | public function getImageNumber() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the value of the X-Condition parameter. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getXCondition() |
||
| 328 | |||
| 329 | 1 | /** |
|
| 330 | 1 | * Get the sub-elements of this Item. |
|
| 331 | 1 | * |
|
| 332 | 1 | * @return \CHMLib\TOCIndex\Tree |
|
| 333 | */ |
||
| 334 | public function getChildren() |
||
| 338 | 1 | ||
| 339 | /** |
||
| 340 | * Resolve the items contained in other CHM files. |
||
| 341 | * |
||
| 342 | * @param \CHMLib\Map $map |
||
| 343 | 1 | * @param bool $ignoreErrors Set to true to ignore missing CHM and/or entries. |
|
| 344 | 1 | * |
|
| 345 | 1 | * @throws \Exception Throw an Exception in case of errors. |
|
| 346 | * |
||
| 347 | * @return static[] |
||
| 348 | */ |
||
| 349 | 1 | public function resolve(Map $map, $ignoreErrors = false) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Search the associated entry in the CHM file. |
||
| 380 | * |
||
| 381 | * @return \CHMLib\Entry|null |
||
| 382 | */ |
||
| 383 | public function findEntry() |
||
| 403 | } |
||
| 404 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.