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 (case sensitive) of the entry to look for. |
||
162 | * |
||
163 | * @return \CHMLib\Entry|null |
||
164 | */ |
||
165 | 491 | public function getEntryByPath($path) |
|
169 | |||
170 | /** |
||
171 | * Get the entries contained in this CHM. |
||
172 | * |
||
173 | * @param int|null $type One or more Entry::TYPE_... values (defaults to Entry::TYPE_FILE | Entry::TYPE_DIRECTORY if null). |
||
174 | */ |
||
175 | 3 | public function getEntries($type = null) |
|
189 | |||
190 | /** |
||
191 | * Return a section given its index. |
||
192 | * |
||
193 | * @param int $i |
||
194 | * |
||
195 | * @return \CHMLib\Section\Section|null |
||
196 | */ |
||
197 | 491 | public function getSectionByIndex($i) |
|
201 | |||
202 | /** |
||
203 | * Retrieve the list of the entries contained in this CHM. |
||
204 | * |
||
205 | * @throws \Exception Throws an Exception in case of errors. |
||
206 | * |
||
207 | * @return \CHMLib\Entry[] |
||
208 | */ |
||
209 | 4 | protected function retrieveEntryList() |
|
241 | |||
242 | /** |
||
243 | * Retrieve the list of the data sections contained in this CHM. |
||
244 | * |
||
245 | * @throws \Exception Throws an Exception in case of errors. |
||
246 | */ |
||
247 | 4 | protected function retrieveSectionList() |
|
248 | { |
||
249 | 4 | $nameList = $this->getEntryByPath('::DataSpace/NameList'); |
|
250 | |||
251 | 4 | if ($nameList === null) { |
|
252 | throw new Exception("Missing required entry: '::DataSpace/NameList'"); |
||
253 | } |
||
254 | 4 | if ($nameList->getContentSectionIndex() !== 0) { |
|
255 | throw new Exception("The content of the entry '{$nameList->getPath()}' should be in section 0, but it's in section {$nameList->getContentSection()}"); |
||
|
|||
256 | } |
||
257 | |||
258 | 4 | $nameListReader = new StringReader($nameList->getContents()); |
|
259 | 4 | /* Length */ $nameListReader->readUInt16(); |
|
260 | 4 | $numSections = $nameListReader->readUInt16(); |
|
261 | 4 | if ($numSections === 0) { |
|
262 | throw new Exception('No content section defined.'); |
||
263 | } |
||
264 | 4 | for ($i = 0; $i < $numSections; ++$i) { |
|
265 | 4 | $nameLength = $nameListReader->readUInt16(); |
|
266 | 4 | $utf16name = $nameListReader->readString($nameLength * 2); |
|
267 | 4 | $nameListReader->readUInt16(); |
|
268 | 4 | $name = iconv('UTF-16LE', 'UTF-8', $utf16name); |
|
269 | 4 | switch ($name) { |
|
270 | 4 | case 'Uncompressed': |
|
271 | 4 | break; |
|
272 | 4 | case 'MSCompressed': |
|
273 | 4 | if ($i === 0) { |
|
274 | throw new Exception('First data section should be Uncompressed'); |
||
275 | } else { |
||
276 | 4 | $this->sections[$i] = new Section\MSCompressedSection($this); |
|
277 | } |
||
278 | 4 | break; |
|
279 | default: |
||
280 | throw new Exception("Unknown data section: $name"); |
||
281 | } |
||
282 | } |
||
283 | 4 | } |
|
284 | |||
285 | /** |
||
286 | * Get the TOC of this CHM file (if available). |
||
287 | * |
||
288 | * @return \CHMLib\TOCIndex\Tree|null |
||
289 | */ |
||
290 | 1 | View Code Duplication | public function getTOC() |
305 | |||
306 | /** |
||
307 | * Get the index of this CHM file (if available). |
||
308 | * |
||
309 | * @return \CHMLib\TOCIndex\Tree|null |
||
310 | */ |
||
311 | View Code Duplication | public function getIndex() |
|
326 | } |
||
327 |
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.