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 HackedProject 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 HackedProject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class HackedProject { |
||
19 | const STATUS_UNCHECKED = 1; |
||
20 | |||
21 | const STATUS_PERMISSION_DENIED = 2; |
||
22 | |||
23 | const STATUS_HACKED = 3; |
||
24 | |||
25 | const STATUS_DELETED = 4; |
||
26 | |||
27 | const STATUS_UNHACKED = 5; |
||
28 | |||
29 | /** @var Project */ |
||
30 | var $project; |
||
|
|||
31 | |||
32 | var $name = ''; |
||
33 | |||
34 | var $project_info = array(); |
||
35 | |||
36 | /** @var HackedProjectWebDownloader */ |
||
37 | var $remote_files_downloader; |
||
38 | |||
39 | /* @var hackedFileGroup $remote_files */ |
||
40 | var $remote_files; |
||
41 | |||
42 | /* @var hackedFileGroup $local_files */ |
||
43 | var $local_files; |
||
44 | |||
45 | var $local_folder; |
||
46 | |||
47 | var $project_type = ''; |
||
48 | var $existing_version = ''; |
||
49 | |||
50 | var $result = array(); |
||
51 | |||
52 | var $project_identified = FALSE; |
||
53 | var $remote_downloaded = FALSE; |
||
54 | var $remote_hashed = FALSE; |
||
55 | var $local_hashed = FALSE; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * @param Project $project |
||
60 | * @param string $local_folder |
||
61 | */ |
||
62 | public function __construct(Project $project, $local_folder = null) { |
||
71 | |||
72 | /** |
||
73 | * @return Project |
||
74 | */ |
||
75 | public function getProject() |
||
79 | |||
80 | /** |
||
81 | * Get the Human readable title of this project. |
||
82 | */ |
||
83 | public function getTitle() { |
||
88 | |||
89 | /** |
||
90 | * Identify the project from the name we've been created with. |
||
91 | * |
||
92 | * We leverage the update (status) module to get the data we require about |
||
93 | * projects. We just pull the information in, and make descisions about this |
||
94 | * project being from CVS or not. |
||
95 | */ |
||
96 | public function identifyProject() { |
||
116 | |||
117 | /** |
||
118 | * Downloads the remote project to be hashed later. |
||
119 | */ |
||
120 | public function downloadRemoteProject() { |
||
129 | |||
130 | /** |
||
131 | * Hashes the remote project downloaded earlier. |
||
132 | */ |
||
133 | View Code Duplication | public function hashRemoteProject() { |
|
155 | |||
156 | /** |
||
157 | * Locate the base directory of the local project. |
||
158 | */ |
||
159 | public function locateLocalProject() { |
||
173 | |||
174 | /** |
||
175 | * Hash the local version of the project. |
||
176 | */ |
||
177 | View Code Duplication | public function hashLocalProject() { |
|
194 | |||
195 | /** |
||
196 | * Compute the differences between our version and the canonical version of the project. |
||
197 | */ |
||
198 | public function computeDifferences() { |
||
228 | |||
229 | /** |
||
230 | * Return a nice report, a simple overview of the status of this project. |
||
231 | */ |
||
232 | public function computeReport() { |
||
281 | |||
282 | /** |
||
283 | * Return a nice detailed report. |
||
284 | * @return array |
||
285 | */ |
||
286 | public function computeDetails() { |
||
309 | |||
310 | /** |
||
311 | * @param string $file |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function fileIsDiffable($file) { |
||
321 | |||
322 | /** |
||
323 | * @param string $storage |
||
324 | * @param string $file |
||
325 | * |
||
326 | * @return bool|string |
||
327 | */ |
||
328 | public function getFileLocation($storage = 'local', $file) { |
||
340 | |||
341 | /** |
||
342 | * @param string $status |
||
343 | * @return string |
||
344 | */ |
||
345 | public static function getStatusLabel($status) { |
||
360 | } |
||
361 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.