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 | const STATE_NEW = 'New'; |
||
20 | const STATE_SUBMITTED = 'Submitted'; |
||
21 | const STATE_INVALID = 'Invalid'; |
||
22 | const STATE_QUEUED = 'Queued'; |
||
23 | const STATE_DEPLOYING = 'Deploying'; |
||
24 | const STATE_ABORTING = 'Aborting'; |
||
25 | const STATE_COMPLETED = 'Completed'; |
||
26 | const STATE_FAILED = 'Failed'; |
||
27 | |||
28 | const TR_SUBMIT = 'submit'; |
||
29 | const TR_INVALIDATE = 'invalidate'; |
||
30 | const TR_QUEUE = 'queue'; |
||
31 | const TR_DEPLOY = 'deploy'; |
||
32 | const TR_ABORT = 'abort'; |
||
33 | const TR_COMPLETE = 'complete'; |
||
34 | const TR_FAIL = 'fail'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $db = array( |
||
40 | "SHA" => "GitSHA", |
||
41 | "ResqueToken" => "Varchar(255)", |
||
42 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
43 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
44 | // to deployment history, and the branch may have been deleted. |
||
45 | "Branch" => "Varchar(255)", |
||
46 | "State" => "Enum('New, Submitted, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
47 | // JSON serialised DeploymentStrategy. |
||
48 | "Strategy" => "Text" |
||
49 | ); |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private static $has_one = array( |
||
55 | "Environment" => "DNEnvironment", |
||
56 | "Deployer" => "Member", |
||
57 | ); |
||
58 | |||
59 | private static $default_sort = '"LastEdited" DESC'; |
||
60 | |||
61 | public function getTitle() { |
||
64 | |||
65 | private static $summary_fields = array( |
||
66 | 'LastEdited' => 'Last Edited', |
||
67 | 'SHA' => 'SHA', |
||
68 | 'State' => 'State', |
||
69 | 'Deployer.Name' => 'Deployer' |
||
70 | ); |
||
71 | |||
72 | public function getFiniteState() { |
||
75 | |||
76 | public function setFiniteState($state) { |
||
80 | |||
81 | public function getStatus() { |
||
84 | |||
85 | public function getMachine() { |
||
139 | |||
140 | |||
141 | public function onQueue() { |
||
150 | |||
151 | public function onAbort() { |
||
155 | |||
156 | public function Link() { |
||
159 | |||
160 | public function LogLink() { |
||
163 | |||
164 | public function canView($member = null) { |
||
167 | |||
168 | /** |
||
169 | * Return a path to the log file. |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function logfile() { |
||
179 | |||
180 | /** |
||
181 | * @return DeploynautLogFile |
||
182 | */ |
||
183 | public function log() { |
||
186 | |||
187 | public function LogContent() { |
||
190 | |||
191 | /** |
||
192 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
193 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function ResqueStatus() { |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Fetch the git repository |
||
204 | * |
||
205 | * @return \Gitonomy\Git\Repository|null |
||
206 | */ |
||
207 | public function getRepository() { |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Gets the commit from source. The result is cached upstream in Repository. |
||
217 | * |
||
218 | * @return \Gitonomy\Git\Commit|null |
||
219 | */ |
||
220 | public function getCommit() { |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Gets the commit message. |
||
236 | * |
||
237 | * @return string|null |
||
238 | */ |
||
239 | public function getCommitMessage() { |
||
250 | |||
251 | /** |
||
252 | * Return all tags for the deployed commit. |
||
253 | * |
||
254 | * @return ArrayList |
||
255 | */ |
||
256 | public function getTags() { |
||
271 | |||
272 | /** |
||
273 | * Collate the list of additional flags to affix to this deployment. |
||
274 | * Elements of the array will be rendered literally. |
||
275 | * |
||
276 | * @return ArrayList |
||
277 | */ |
||
278 | public function getFullDeployMessages() { |
||
305 | |||
306 | /** |
||
307 | * Fetches the latest tag for the deployed commit |
||
308 | * |
||
309 | * @return \Varchar|null |
||
310 | */ |
||
311 | public function getTag() { |
||
318 | |||
319 | /** |
||
320 | * @return DeploymentStrategy |
||
321 | */ |
||
322 | public function getDeploymentStrategy() { |
||
328 | |||
329 | /** |
||
330 | * Return a list of things that are going to be deployed, such |
||
331 | * as the code version, and any infrastrucutral changes. |
||
332 | * |
||
333 | * @return ArrayList |
||
334 | */ |
||
335 | public function getChanges() { |
||
361 | |||
362 | /** |
||
363 | * Start a resque job for this deployment |
||
364 | * |
||
365 | * @return string Resque token |
||
366 | */ |
||
367 | protected function enqueueDeployment() { |
||
406 | } |
||
407 |
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.