Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CHM 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 CHM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CHM |
||
15 | { |
||
16 | /** |
||
17 | * The reader that provides the data. |
||
18 | * |
||
19 | * @var \CHMLib\Reader\Reader |
||
20 | */ |
||
21 | protected $reader; |
||
22 | |||
23 | /** |
||
24 | * The CHM initial header. |
||
25 | * |
||
26 | * @var \CHMLib\Header\ITSF |
||
27 | */ |
||
28 | protected $itsf; |
||
29 | |||
30 | /** |
||
31 | * The directory listing header. |
||
32 | * |
||
33 | * @var \CHMLib\Header\ITSP |
||
34 | */ |
||
35 | protected $itsp; |
||
36 | |||
37 | /** |
||
38 | * The entries found in this CHM. |
||
39 | * |
||
40 | * @var \CHMLib\Entry[] |
||
41 | */ |
||
42 | protected $entries; |
||
43 | |||
44 | /** |
||
45 | * The data sections. |
||
46 | * |
||
47 | * @var \CHMLib\Section\Section[] |
||
48 | */ |
||
49 | protected $sections; |
||
50 | |||
51 | /** |
||
52 | * The TOC. |
||
53 | * |
||
54 | * @var \CHMLib\TOCIndex\Tree|null|false |
||
55 | */ |
||
56 | protected $toc; |
||
57 | |||
58 | /** |
||
59 | * The index. |
||
60 | * |
||
61 | * @var \CHMLib\TOCIndex\Tree|null|false |
||
62 | */ |
||
63 | protected $index; |
||
64 | |||
65 | /** |
||
66 | * Initializes the instance. |
||
67 | * |
||
68 | * @param \CHMLib\Reader\Reader $reader The reader that provides the data. |
||
69 | * |
||
70 | * @throws \Exception Throws an Exception in case of errors. |
||
71 | */ |
||
72 | 4 | public function __construct(Reader $reader) |
|
105 | |||
106 | /** |
||
107 | * Destruct the instance. |
||
108 | */ |
||
109 | public function __destruct() |
||
113 | |||
114 | /** |
||
115 | * Create a new CHM instance reading a file. |
||
116 | * |
||
117 | * @param string $filename |
||
118 | * |
||
119 | * @return static |
||
120 | */ |
||
121 | 4 | public static function fromFile($filename) |
|
127 | |||
128 | /** |
||
129 | * Get the reader that provides the data. |
||
130 | * |
||
131 | * @return \CHMLib\Reader\Reader |
||
132 | */ |
||
133 | 486 | public function getReader() |
|
137 | |||
138 | /** |
||
139 | * Get the CHM initial header. |
||
140 | * |
||
141 | * @return \CHMLib\Header\ITSF |
||
142 | */ |
||
143 | 4 | public function getITSF() |
|
147 | |||
148 | /** |
||
149 | * Get the directory listing header. |
||
150 | * |
||
151 | * @return \CHMLib\Header\ITSP |
||
152 | */ |
||
153 | public function getITSP() |
||
157 | |||
158 | /** |
||
159 | * Get an entry given its full path. |
||
160 | * |
||
161 | * @param string $path The full path of the entry to look for. |
||
162 | * @param bool $caseSensitive Perform a case-sensitive search? |
||
163 | * |
||
164 | * @return \CHMLib\Entry|null |
||
165 | */ |
||
166 | 491 | public function getEntryByPath($path, $caseSensitive = false) |
|
178 | |||
179 | /** |
||
180 | * Get the entries contained in this CHM. |
||
181 | * |
||
182 | * @param int|null $type One or more Entry::TYPE_... values (defaults to Entry::TYPE_FILE | Entry::TYPE_DIRECTORY if null). |
||
183 | */ |
||
184 | 3 | public function getEntries($type = null) |
|
198 | |||
199 | /** |
||
200 | * Return a section given its index. |
||
201 | * |
||
202 | * @param int $i |
||
203 | * |
||
204 | * @return \CHMLib\Section\Section|null |
||
205 | */ |
||
206 | 491 | public function getSectionByIndex($i) |
|
210 | |||
211 | /** |
||
212 | * Retrieve the list of the entries contained in this CHM. |
||
213 | * |
||
214 | * @throws \Exception Throws an Exception in case of errors. |
||
215 | * |
||
216 | * @return \CHMLib\Entry[] |
||
217 | */ |
||
218 | 4 | protected function retrieveEntryList() |
|
250 | |||
251 | /** |
||
252 | * Retrieve the list of the data sections contained in this CHM. |
||
253 | * |
||
254 | * @throws \Exception Throws an Exception in case of errors. |
||
255 | */ |
||
256 | 4 | protected function retrieveSectionList() |
|
293 | |||
294 | /** |
||
295 | * Get the TOC of this CHM file (if available). |
||
296 | * |
||
297 | * @return \CHMLib\TOCIndex\Tree|null |
||
298 | */ |
||
299 | 1 | View Code Duplication | public function getTOC() |
314 | |||
315 | /** |
||
316 | * Get the index of this CHM file (if available). |
||
317 | * |
||
318 | * @return \CHMLib\TOCIndex\Tree|null |
||
319 | */ |
||
320 | View Code Duplication | public function getIndex() |
|
335 | } |
||
336 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.