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 Job 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 Job, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Job extends Tasks |
||
32 | { |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $definitions = array(); |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $dryRun = false; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $started = false; |
||
|
|||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $initialized = false; |
||
52 | |||
53 | /** |
||
54 | * Job constructor. |
||
55 | * |
||
56 | * @param Console $console Console |
||
57 | */ |
||
58 | public function __construct(Console $console) |
||
82 | |||
83 | /** |
||
84 | * Variable configuration |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function configureVariables() |
||
114 | |||
115 | /** |
||
116 | * Configure options and arguments |
||
117 | * |
||
118 | * @param string $name The name |
||
119 | * @param mixed $value The value |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function offsetSet($name, $value) |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Run an array of tasks |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function run() |
||
151 | { |
||
152 | $this->started = true; |
||
153 | |||
154 | if (isset($this->console->getConfig()['workspace'])) { |
||
155 | $this->console->getFilesystem()->ensureDirectoryExists($this->expand('{config["workspace"]}')); |
||
156 | } |
||
157 | |||
158 | $input = $this->console->getInput(); |
||
159 | $this->dryRun = $input->getOption('dry-run'); |
||
160 | foreach ($this->definitions as $from => $info) { |
||
161 | $config = $info['config']; |
||
162 | if ($info['type'] === 'option') { |
||
163 | if ($input->hasOption($from)) { |
||
164 | $value = $input->getOption($from); |
||
165 | if (($value === null || is_array($value) && !isset($value[0]) && count($value) <= 1) |
||
166 | && preg_match('/(^|\|)bool(ean)?($|\|)/', $config['type']) && strpos($config['type'], '|') |
||
167 | ) { |
||
168 | if ($input->hasParameterOption($opt = '--' . $from) |
||
169 | || array_key_exists('shortcut', $config) |
||
170 | && $input->hasParameterOption($opt = '-' . $config['shortcut']) |
||
171 | ) { |
||
172 | // Set value to true, when option has no value |
||
173 | $value = true; |
||
174 | } |
||
175 | } |
||
176 | $info['context']->set($info['variable'], $value); |
||
177 | } |
||
178 | } elseif ($input->hasArgument($from)) { |
||
179 | $info['context']->set($info['variable'], $input->getArgument($from)); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return parent::run(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Override get the input options and arguments for the JobCommand |
||
188 | * |
||
189 | * @param \Netresearch\Kite\Task $task The task |
||
190 | * |
||
191 | * @return $this|mixed $this or the task return value when this is running |
||
192 | */ |
||
193 | public function addTask(Task $task) |
||
201 | |||
202 | /** |
||
203 | * Add variables from the task to the job |
||
204 | * |
||
205 | * @param Task $task The task |
||
206 | * @param Variables $context Context to set the variable to, when job is run. |
||
207 | * When null, variable is set on task |
||
208 | * @param bool|null $overrideExisting Whether to override existing args (true), |
||
209 | * don't override them (false) or throw an |
||
210 | * exception (null) |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | public function addVariablesFromTask(Task $task, $context = null, $overrideExisting = null) |
||
257 | |||
258 | /** |
||
259 | * camelCase to lower-case-dashed |
||
260 | * |
||
261 | * @param string $string String |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | protected function camelCaseToLowerCaseDashed($string) |
||
275 | |||
276 | /** |
||
277 | * Add the retrieved options and arguments to a definition |
||
278 | * |
||
279 | * @return InputDefinition |
||
280 | */ |
||
281 | public function getDefinition() |
||
332 | |||
333 | /** |
||
334 | * Determine wether this job is dry run |
||
335 | * |
||
336 | * @return boolean |
||
337 | */ |
||
338 | public function isDryRun() |
||
342 | } |
||
343 | ?> |
||
344 |