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 |
||
15 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
16 | |||
17 | const STATE_NEW = 'New'; |
||
18 | const STATE_SUBMITTED = 'Submitted'; |
||
19 | const STATE_INVALID = 'Invalid'; |
||
20 | const STATE_QUEUED = 'Queued'; |
||
21 | const STATE_DEPLOYING = 'Deploying'; |
||
22 | const STATE_ABORTING = 'Aborting'; |
||
23 | const STATE_COMPLETED = 'Completed'; |
||
24 | const STATE_FAILED = 'Failed'; |
||
25 | |||
26 | const TR_SUBMIT = 'submit'; |
||
27 | const TR_INVALIDATE = 'invalidate'; |
||
28 | const TR_QUEUE = 'queue'; |
||
29 | const TR_DEPLOY = 'deploy'; |
||
30 | const TR_ABORT = 'abort'; |
||
31 | const TR_COMPLETE = 'complete'; |
||
32 | const TR_FAIL = 'fail'; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private static $db = array( |
||
38 | "SHA" => "GitSHA", |
||
39 | "ResqueToken" => "Varchar(255)", |
||
40 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
41 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
42 | // to deployment history, and the branch may have been deleted. |
||
43 | "Branch" => "Varchar(255)", |
||
44 | "State" => "Enum('New, Submitted, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
45 | // JSON serialised DeploymentStrategy. |
||
46 | "Strategy" => "Text" |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private static $has_one = array( |
||
53 | "Environment" => "DNEnvironment", |
||
54 | "Deployer" => "Member", |
||
55 | ); |
||
56 | |||
57 | private static $default_sort = '"LastEdited" DESC'; |
||
58 | |||
59 | private static $dependencies = [ |
||
60 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
61 | ]; |
||
62 | |||
63 | public function getTitle() { |
||
66 | |||
67 | private static $summary_fields = array( |
||
68 | 'LastEdited' => 'Last Edited', |
||
69 | 'SHA' => 'SHA', |
||
70 | 'State' => 'State', |
||
71 | 'Deployer.Name' => 'Deployer' |
||
72 | ); |
||
73 | |||
74 | public function getFiniteState() { |
||
77 | |||
78 | public function setFiniteState($state) { |
||
82 | |||
83 | public function getStatus() { |
||
86 | |||
87 | public function getMachine() { |
||
90 | |||
91 | public function Link() { |
||
94 | |||
95 | public function LogLink() { |
||
98 | |||
99 | public function canView($member = null) { |
||
102 | |||
103 | /** |
||
104 | * Return a path to the log file. |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function logfile() { |
||
114 | |||
115 | /** |
||
116 | * @return DeploynautLogFile |
||
117 | */ |
||
118 | public function log() { |
||
121 | |||
122 | public function LogContent() { |
||
125 | |||
126 | /** |
||
127 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
128 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function ResqueStatus() { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * Fetch the git repository |
||
139 | * |
||
140 | * @return \Gitonomy\Git\Repository|null |
||
141 | */ |
||
142 | public function getRepository() { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Gets the commit from source. The result is cached upstream in Repository. |
||
152 | * |
||
153 | * @return \Gitonomy\Git\Commit|null |
||
154 | */ |
||
155 | public function getCommit() { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Gets the commit message. |
||
171 | * |
||
172 | * @return string|null |
||
173 | */ |
||
174 | public function getCommitMessage() { |
||
185 | |||
186 | /** |
||
187 | * Return all tags for the deployed commit. |
||
188 | * |
||
189 | * @return ArrayList |
||
190 | */ |
||
191 | public function getTags() { |
||
206 | |||
207 | /** |
||
208 | * Collate the list of additional flags to affix to this deployment. |
||
209 | * Elements of the array will be rendered literally. |
||
210 | * |
||
211 | * @return ArrayList |
||
212 | */ |
||
213 | public function getFullDeployMessages() { |
||
240 | |||
241 | /** |
||
242 | * Fetches the latest tag for the deployed commit |
||
243 | * |
||
244 | * @return \Varchar|null |
||
245 | */ |
||
246 | public function getTag() { |
||
253 | |||
254 | /** |
||
255 | * @return DeploymentStrategy |
||
256 | */ |
||
257 | public function getDeploymentStrategy() { |
||
263 | |||
264 | /** |
||
265 | * Return a list of things that are going to be deployed, such |
||
266 | * as the code version, and any infrastrucutral changes. |
||
267 | * |
||
268 | * @return ArrayList |
||
269 | */ |
||
270 | public function getChanges() { |
||
296 | |||
297 | /** |
||
298 | * Start a resque job for this deployment |
||
299 | * |
||
300 | * @return string Resque token |
||
301 | */ |
||
302 | public function enqueueDeployment() { |
||
341 | } |
||
342 |
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.