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 COBAgentBlock 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 COBAgentBlock, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class COBAgentBlock extends COBBlock { |
||
27 | /// @cond INTERNAL_DOCS |
||
28 | |||
29 | private $agentName; |
||
30 | private $agentDescription; |
||
31 | |||
32 | private $lastUsageDate; //unix timestamp |
||
33 | private $reuseInterval; // seconds |
||
34 | private $quantityAvailable; |
||
35 | private $expiryDate; //unix timestamp |
||
36 | |||
37 | //'reserved' - never officially used by CL |
||
38 | private $reserved1; |
||
39 | private $reserved2; |
||
40 | private $reserved3; |
||
41 | |||
42 | private $dependencies = array(); |
||
43 | private $thumbnail; // SpriteFrame |
||
44 | |||
45 | private $installScript; |
||
46 | private $removeScript; |
||
47 | private $eventScripts; |
||
48 | /// @endcond |
||
49 | |||
50 | /// @brief Initialises a new COBAgentBlock with the given name and description. |
||
51 | /** As defaults can be made for everything else these are the only non-optional |
||
52 | * parts of a COB file in my opinion. Even then they could just be '' if you |
||
53 | * really felt like it. |
||
54 | * @param $agentName The name of the agent (as displayed in the C2 injector) |
||
55 | * @param $agentDescription The description of the agent (as displayed in the C2 injector) |
||
56 | */ |
||
57 | public function COBAgentBlock($agentName, $agentDescription) { |
||
62 | |||
63 | /// @brief Supposedly compiles the block into binary. Throws an error to say it's not implemented. |
||
64 | /** @return string |
||
65 | */ |
||
66 | public function Compile() { |
||
70 | |||
71 | /// @brief Gets the agent's name |
||
72 | /** @return string |
||
73 | */ |
||
74 | public function GetAgentName() { |
||
77 | /// @brief Gets the agent's description |
||
78 | /** @return string |
||
79 | */ |
||
80 | public function GetAgentDescription() { |
||
83 | /// @brief Gets the agent's install script |
||
84 | /** @return string |
||
85 | */ |
||
86 | public function GetInstallScript() { |
||
89 | /// @brief Gets the agent's remove script |
||
90 | /** @return string |
||
91 | */ |
||
92 | public function GetRemoveScript() { |
||
95 | /// @brief Gets the number of event scripts |
||
96 | /** @return int |
||
97 | */ |
||
98 | public function GetEventScriptCount() { |
||
101 | /// @brief Gets the agent's event scripts |
||
102 | /** @return array of strings, each string is an event script |
||
103 | */ |
||
104 | public function GetEventScripts() { |
||
107 | /// @brief Gets an event script |
||
108 | /** Event scripts are not necessarily in any order, so you have to work out what each script is for yourself. |
||
109 | * @param $whichScript Which script to get. |
||
110 | * @return A string containing the event script. Each line is seperated by a comma I think. |
||
111 | */ |
||
112 | public function GetEventScript($whichScript) { |
||
115 | /// @brief Gets the thumbnail of this agent as would be shown in the Injector |
||
116 | /** @return SpriteFrame SpriteFrame of the thumbnail |
||
117 | */ |
||
118 | public function GetThumbnail() { |
||
121 | /// @brief Gets dependencies of the given type |
||
122 | /** If type is null, will get all dependencies. |
||
123 | * @param $type One of the COB_DEPENDENCY_* constants, defined |
||
124 | * above. |
||
125 | * @return An array of COBDependency objects |
||
126 | */ |
||
127 | public function GetDependencies($type = null) { |
||
136 | /// @brief Gets the value of reserved1 |
||
137 | /** Reserved values weren't ever officially used by CL, |
||
138 | * but someone might find them useful for something else. |
||
139 | * @return int |
||
140 | */ |
||
141 | public function GetReserved1() { |
||
144 | /// @brief Gets the value of reserved2 |
||
145 | /** Reserved values weren't ever officially used by CL, |
||
146 | * but someone might find them useful for something else. |
||
147 | * @return int |
||
148 | */ |
||
149 | public function GetReserved2() { |
||
152 | /// @brief Gets the value of reserved3 |
||
153 | /** Reserved values weren't ever officially used by CL, |
||
154 | * but someone might find them useful for something else. |
||
155 | * @return int |
||
156 | */ |
||
157 | public function GetReserved3() { |
||
160 | /// @brief Adds a dependency to this agent |
||
161 | /** |
||
162 | * @param $dependency The COBDependency to add. |
||
163 | */ |
||
164 | public function AddDependency(COBDependency $dependency) { |
||
169 | /// @brief Sets the install script |
||
170 | /** |
||
171 | * @param $installScript the text of the script to add |
||
172 | */ |
||
173 | public function SetInstallScript($installScript) { |
||
176 | /// @brief Sets the remover script |
||
177 | /** |
||
178 | * @param $removeScript The text of the script to add |
||
179 | */ |
||
180 | public function SetRemoveScript($removeScript) { |
||
183 | /// @brief Adds an event script |
||
184 | /** |
||
185 | * @param $eventScript The text of the script to add |
||
186 | */ |
||
187 | public function AddEventScript($eventScript) { |
||
190 | /// @brief Sets the date this agent was last injected |
||
191 | /** |
||
192 | * @param $time The date this agent was last injected as a UNIX timestamp |
||
193 | */ |
||
194 | public function SetLastUsageDate($time) { |
||
201 | /// @brief Sets the date this agent will expire |
||
202 | /** |
||
203 | * @param $time The date this agent will expire as a UNIX timestamp |
||
204 | */ |
||
205 | public function SetExpiryDate($time) { |
||
208 | /// @brief Sets the quantity of the agent available |
||
209 | /** |
||
210 | * @param $quantity The quantity available, an integer. 0xFF means infinite. |
||
211 | */ |
||
212 | public function SetQuantityAvailable($quantity) { |
||
215 | /// @brief Sets the interval required between re-use. |
||
216 | /** @param $interval The interval in seconds, between re-use of this agent. |
||
217 | */ |
||
218 | public function SetReuseInterval($interval) { |
||
221 | /// @brief Adds the reserved variables to this agent |
||
222 | /** |
||
223 | * These variables have no meaning to Creatures 2 and don't appear in Creatures 1. |
||
224 | * They're all integers. |
||
225 | * @param $reserved1 The first reserved variable |
||
226 | * @param $reserved2 The second reserved variable |
||
227 | * @param $reserved3 The third reserved variable |
||
228 | */ |
||
229 | public function SetReserved($reserved1, $reserved2, $reserved3) { |
||
234 | /// @brief Add the thumbnail to this agent. |
||
235 | /** @param $frame The thumbnail as a SpriteFrame |
||
236 | */ |
||
237 | public function SetThumbnail(SpriteFrame $frame) { |
||
243 | /// @cond INTERNAL_DOCS |
||
244 | /** |
||
245 | * @brief Adds a remover script by reading from an RCB file. |
||
246 | * @param IReader $reader A StringReader or FileReader for the RCB |
||
247 | */ |
||
248 | public function AddC1RemoveScriptFromRCB(IReader $reader) { |
||
256 | |||
257 | /// @brief Creates a new COBAgentBlock from an IReader. |
||
258 | /** Reads from the current position of the IReader to fill out the data required by |
||
259 | * the COBAgentBlock, then creates one and adds all the fields to it. |
||
260 | * @param IReader $reader The IReader, seeked to the beginning of the contents of the agent block |
||
261 | * @return COBAgentBlock |
||
262 | */ |
||
263 | public static function CreateFromReaderC2(IReader $reader) { |
||
324 | /// @brief Creates a COBAgentBlock from an IReader |
||
325 | /** Reads from the current position of the IReader to fill out the data required by |
||
326 | * the COBAgentBlock, then creates one and adds all the fields to it. |
||
327 | * @param $reader The IReader, seeked to the beginning of the contents of the agent block |
||
328 | * |
||
329 | */ |
||
330 | public static function CreateFromReaderC1(IReader $reader) { |
||
379 | /// @endcond |
||
380 | } |
||
381 | |||
414 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.