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 ExecTrait 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 ExecTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait ExecTrait |
||
13 | { |
||
14 | /** |
||
15 | * @var bool |
||
16 | */ |
||
17 | protected $background = false; |
||
18 | |||
19 | /** |
||
20 | * @var null|int |
||
21 | */ |
||
22 | protected $timeout = null; |
||
23 | |||
24 | /** |
||
25 | * @var null|int |
||
26 | */ |
||
27 | protected $idleTimeout = null; |
||
28 | |||
29 | /** |
||
30 | * @var null|array |
||
31 | */ |
||
32 | protected $env = null; |
||
33 | |||
34 | /** |
||
35 | * @var Process |
||
36 | */ |
||
37 | protected $process; |
||
38 | |||
39 | /** |
||
40 | * @var resource|string |
||
41 | */ |
||
42 | protected $input; |
||
43 | |||
44 | /** |
||
45 | * @var boolean |
||
46 | */ |
||
47 | protected $interactive = null; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $isPrinted = true; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $isMetadataPrinted = true; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $workingDirectory; |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | abstract public function getCommandDescription(); |
||
68 | |||
69 | /** |
||
70 | * @see \Robo\Common\ProgressIndicatorAwareTrait |
||
71 | * @see \Robo\Common\Timer |
||
72 | */ |
||
73 | abstract protected function startTimer(); |
||
74 | |||
75 | /** |
||
76 | * @see \Robo\Common\ProgressIndicatorAwareTrait |
||
77 | * @see \Robo\Common\Timer |
||
78 | */ |
||
79 | abstract protected function stopTimer(); |
||
80 | |||
81 | /** |
||
82 | * @return null|float |
||
83 | * |
||
84 | * @see \Robo\Common\ProgressIndicatorAwareTrait |
||
85 | * @see \Robo\Common\Timer |
||
86 | */ |
||
87 | abstract protected function getExecutionTime(); |
||
88 | |||
89 | /** |
||
90 | * @return bool |
||
91 | * |
||
92 | * @see \Robo\Common\TaskIO |
||
93 | */ |
||
94 | abstract protected function hideTaskProgress(); |
||
95 | |||
96 | /** |
||
97 | * @param bool $inProgress |
||
98 | * |
||
99 | * @see \Robo\Common\TaskIO |
||
100 | */ |
||
101 | abstract protected function showTaskProgress($inProgress); |
||
102 | |||
103 | /** |
||
104 | * @param string $text |
||
105 | * @param null|array $context |
||
106 | * |
||
107 | * @see \Robo\Common\TaskIO |
||
108 | */ |
||
109 | abstract protected function printTaskInfo($text, $context = null); |
||
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | * |
||
114 | * @see \Robo\Common\VerbosityThresholdTrait |
||
115 | */ |
||
116 | abstract public function verbosityMeetsThreshold(); |
||
117 | |||
118 | /** |
||
119 | * @param string $message |
||
120 | * |
||
121 | * @see \Robo\Common\VerbosityThresholdTrait |
||
122 | */ |
||
123 | abstract public function writeMessage($message); |
||
124 | |||
125 | /** |
||
126 | * Sets $this->interactive() based on posix_isatty(). |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function detectInteractive() |
||
142 | |||
143 | /** |
||
144 | * Executes command in background mode (asynchronously) |
||
145 | * |
||
146 | * @param bool $arg |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function background($arg = true) |
||
155 | |||
156 | /** |
||
157 | * Stop command if it runs longer then $timeout in seconds |
||
158 | * |
||
159 | * @param int $timeout |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function timeout($timeout) |
||
168 | |||
169 | /** |
||
170 | * Stops command if it does not output something for a while |
||
171 | * |
||
172 | * @param int $timeout |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function idleTimeout($timeout) |
||
181 | |||
182 | /** |
||
183 | * Set a single environment variable, or multiple. |
||
184 | * |
||
185 | * @param string|array $env |
||
186 | * @param bool|string $value |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function env($env, $value = null) |
||
197 | |||
198 | /** |
||
199 | * Sets the environment variables for the command |
||
200 | * |
||
201 | * @param array $env |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function envVars(array $env) |
||
210 | |||
211 | /** |
||
212 | * Pass an input to the process. Can be resource created with fopen() or string |
||
213 | * |
||
214 | * @param resource|string $input |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setInput($input) |
||
223 | |||
224 | /** |
||
225 | * Attach tty to process for interactive input |
||
226 | * |
||
227 | * @param bool $interactive |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function interactive($interactive = true) |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Is command printing its output to screen |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function getPrinted() |
||
247 | |||
248 | /** |
||
249 | * Changes working directory of command |
||
250 | * |
||
251 | * @param string $dir |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function dir($dir) |
||
260 | |||
261 | /** |
||
262 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
263 | * |
||
264 | * @param bool $arg |
||
265 | * |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function silent($arg) |
||
276 | |||
277 | /** |
||
278 | * Should command output be printed |
||
279 | * |
||
280 | * @param bool $arg |
||
281 | * |
||
282 | * @return $this |
||
283 | * |
||
284 | * @deprecated |
||
285 | */ |
||
286 | public function printed($arg) |
||
291 | |||
292 | /** |
||
293 | * Should command output be printed |
||
294 | * |
||
295 | * @param bool $arg |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function printOutput($arg) |
||
306 | |||
307 | /** |
||
308 | * Should command metadata be printed. I,e., command and timer. |
||
309 | * |
||
310 | * @param bool $arg |
||
311 | * |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function printMetadata($arg) |
||
321 | |||
322 | /** |
||
323 | * @param \Symfony\Component\Process\Process $process |
||
324 | * @param callable $output_callback |
||
325 | * |
||
326 | * @return \Robo\ResultData |
||
327 | */ |
||
328 | protected function execute($process, $output_callback = null) |
||
401 | |||
402 | protected function stop() |
||
412 | |||
413 | /** |
||
414 | * @param array $context |
||
415 | */ |
||
416 | protected function printAction($context = []) |
||
427 | |||
428 | /** |
||
429 | * @param string $command |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | protected function formatCommandDisplay($command) |
||
440 | |||
441 | /** |
||
442 | * Gets the data array to be passed to Result(). |
||
443 | * |
||
444 | * @return array |
||
445 | * The data array passed to Result(). |
||
446 | */ |
||
447 | protected function getResultData() |
||
455 | } |
||
456 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: