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 DNDeployment 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 DNDeployment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private static $db = array( |
||
23 | "SHA" => "GitSHA", |
||
24 | "ResqueToken" => "Varchar(255)", |
||
25 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
26 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
27 | // to deployment history, and the branch may have been deleted. |
||
28 | "Branch" => "Varchar(255)", |
||
29 | "State" => "Enum('New, Approved, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
30 | // JSON serialised DeploymentStrategy. |
||
31 | "Strategy" => "Text" |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private static $has_one = array( |
||
38 | "Environment" => "DNEnvironment", |
||
39 | "Deployer" => "Member", |
||
40 | ); |
||
41 | |||
42 | private static $default_sort = '"LastEdited" DESC'; |
||
43 | |||
44 | public function getTitle() { |
||
47 | |||
48 | private static $summary_fields = array( |
||
49 | 'LastEdited' => 'Last Edited', |
||
50 | 'SHA' => 'SHA', |
||
51 | 'Status' => 'Status', |
||
52 | 'Deployer.Name' => 'Deployer' |
||
53 | ); |
||
54 | |||
55 | public function getFiniteState() { |
||
58 | |||
59 | public function setFiniteState($state) { |
||
63 | |||
64 | public function getStatus() { |
||
67 | |||
68 | public function getMachine() { |
||
106 | |||
107 | |||
108 | public function onQueue() { |
||
117 | |||
118 | public function onAbort() { |
||
122 | |||
123 | public function Link() { |
||
126 | |||
127 | public function LogLink() { |
||
130 | |||
131 | public function canView($member = null) { |
||
134 | |||
135 | /** |
||
136 | * Return a path to the log file. |
||
137 | * @return string |
||
138 | */ |
||
139 | protected function logfile() { |
||
146 | |||
147 | /** |
||
148 | * @return DeploynautLogFile |
||
149 | */ |
||
150 | public function log() { |
||
153 | |||
154 | public function LogContent() { |
||
157 | |||
158 | /** |
||
159 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
160 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function ResqueStatus() { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Fetch the git repository |
||
171 | * |
||
172 | * @return \Gitonomy\Git\Repository|null |
||
173 | */ |
||
174 | public function getRepository() { |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Gets the commit from source. The result is cached upstream in Repository. |
||
184 | * |
||
185 | * @return \Gitonomy\Git\Commit|null |
||
186 | */ |
||
187 | public function getCommit() { |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Gets the commit message. |
||
203 | * |
||
204 | * @return string|null |
||
205 | */ |
||
206 | public function getCommitMessage() { |
||
217 | |||
218 | /** |
||
219 | * Return all tags for the deployed commit. |
||
220 | * |
||
221 | * @return ArrayList |
||
222 | */ |
||
223 | public function getTags() { |
||
238 | |||
239 | /** |
||
240 | * Collate the list of additional flags to affix to this deployment. |
||
241 | * Elements of the array will be rendered literally. |
||
242 | * |
||
243 | * @return ArrayList |
||
244 | */ |
||
245 | public function getFullDeployMessages() { |
||
272 | |||
273 | /** |
||
274 | * Fetches the latest tag for the deployed commit |
||
275 | * |
||
276 | * @return \Varchar|null |
||
277 | */ |
||
278 | public function getTag() { |
||
285 | |||
286 | /** |
||
287 | * @return DeploymentStrategy |
||
288 | */ |
||
289 | public function getDeploymentStrategy() { |
||
295 | |||
296 | /** |
||
297 | * Return a list of things that are going to be deployed, such |
||
298 | * as the code version, and any infrastrucutral changes. |
||
299 | * |
||
300 | * @return ArrayList |
||
301 | */ |
||
302 | public function getChanges() { |
||
328 | |||
329 | /** |
||
330 | * Start a resque job for this deployment |
||
331 | * |
||
332 | * @return string Resque token |
||
333 | */ |
||
334 | protected function enqueueDeployment() { |
||
373 | } |
||
374 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.