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 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 sub-elements of this Item. |
||
| 101 | * |
||
| 102 | * @var Tree |
||
| 103 | */ |
||
| 104 | protected $children; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Initializes the instance. |
||
| 108 | * |
||
| 109 | * @param CHM $chm The parent CHM instance. |
||
| 110 | * @param DOMElement $object The OBJECT element. |
||
| 111 | * |
||
| 112 | * @throws Exception Throw an Exception in case of errors. |
||
| 113 | * |
||
| 114 | * @return static |
||
|
|
|||
| 115 | */ |
||
| 116 | 1 | public function __construct(CHM $chm, DOMElement $object) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get the parent CHM instance. |
||
| 189 | * |
||
| 190 | * @return CHM |
||
| 191 | */ |
||
| 192 | 1 | public function getCHM() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get the name of the tree item. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getName() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Is this item marked as new? |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function isNew() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get the comment of the tree item. |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getComment() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the keyword of the tree item. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getKeyword() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the value of the "See Also" parameter. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getSeeAlso() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get the local path to the tree item. |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 1 | public function getLocal() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get the URL to the tree item. |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getURL() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the frame name for this item. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getFrameName() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the window name for this item. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 1 | public function getWindowName() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get the path to an entry in another CHM file. |
||
| 289 | * |
||
| 290 | * @var array|null If not null, it's an array with two keys: 'chm' and 'entry'. |
||
| 291 | */ |
||
| 292 | public function getMerge() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the image number attribute. |
||
| 299 | * |
||
| 300 | * @return int|null |
||
| 301 | */ |
||
| 302 | 1 | public function getImageNumber() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get the sub-elements of this Item. |
||
| 309 | * |
||
| 310 | * @return Tree |
||
| 311 | */ |
||
| 312 | public function getChildren() |
||
| 316 | |||
| 317 | 1 | /** |
|
| 318 | * Resolve the items contained in other CHM files. |
||
| 319 | 1 | * |
|
| 320 | 1 | * @param Map $map |
|
| 321 | 1 | * @param bool $ignoreErrors Set to true to ignore missing CHM and/or entries. |
|
| 322 | 1 | * |
|
| 323 | * @throws Exception Throw an Exception in case of errors. |
||
| 324 | * |
||
| 325 | * @return static[] |
||
| 326 | */ |
||
| 327 | 1 | public function resolve(Map $map, $ignoreErrors = false) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Search the associated entry in the CHM file. |
||
| 358 | * |
||
| 359 | * @return \CHMLib\Entry|null |
||
| 360 | */ |
||
| 361 | public function findEntry() |
||
| 381 | } |
||
| 382 |
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.