Complex classes like LockedCommandDecorator 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 LockedCommandDecorator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class LockedCommandDecorator extends Command |
||
13 | { |
||
14 | /** |
||
15 | * @var Command |
||
16 | */ |
||
17 | private $decoratedCommand; |
||
18 | |||
19 | /** |
||
20 | * @var string|LockHandler |
||
21 | */ |
||
22 | private $lockName; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $lockPath; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param Command $command |
||
33 | * @param string|LockHandler $lockName |
||
34 | * @param string $lockPath |
||
35 | */ |
||
36 | 30 | public function __construct(Command $command, $lockName = null, $lockPath = null) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 3 | public function ignoreValidationErrors() |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 3 | public function setApplication(Application $application = null) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 3 | public function setHelperSet(HelperSet $helperSet) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 3 | public function getHelperSet() |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 3 | public function getApplication() |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 3 | public function isEnabled() |
|
90 | |||
91 | /** |
||
92 | * @param string|LockHandler|null $lockName |
||
93 | */ |
||
94 | 30 | public function setLockName($lockName) |
|
98 | |||
99 | /** |
||
100 | * Runs the command. |
||
101 | * |
||
102 | * Before the decorated command is run, a lock is requested. |
||
103 | * When failed to acquire the lock, the command exits. |
||
104 | * |
||
105 | * @param InputInterface $input An InputInterface instance |
||
106 | * @param OutputInterface $output An OutputInterface instance |
||
107 | * |
||
108 | * @return int The command exit code |
||
109 | */ |
||
110 | 12 | public function run(InputInterface $input, OutputInterface $output) |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 3 | public function setCode(callable $code) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 15 | public function mergeApplicationDefinition($mergeArgs = true) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 3 | public function setDefinition($definition) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 15 | public function getDefinition() |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 3 | public function getNativeDefinition() |
|
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | 3 | public function addArgument($name, $mode = null, $description = '', $default = null) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 3 | public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 3 | public function setName($name) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 3 | public function setProcessTitle($title) |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | 3 | public function getName() |
|
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 3 | public function setDescription($description) |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 3 | public function getDescription() |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 3 | public function setHelp($help) |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | 3 | public function getHelp() |
|
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | 3 | public function getProcessedHelp() |
|
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | 3 | public function setAliases($aliases) |
|
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | 3 | public function getAliases() |
|
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | 3 | public function getSynopsis($short = false) |
|
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | 3 | public function getHelper($name) |
|
298 | |||
299 | /** |
||
300 | * Get the locking helper. |
||
301 | * |
||
302 | * @param InputInterface $input |
||
303 | * |
||
304 | * @return LockHandler |
||
305 | */ |
||
306 | 12 | private function getLockHandler(InputInterface $input) |
|
317 | |||
318 | /** |
||
319 | * Get the name for the lock. |
||
320 | * |
||
321 | * @param InputInterface $input |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 15 | public function getLockName(InputInterface $input) |
|
341 | |||
342 | /** |
||
343 | * Get the lock path. |
||
344 | * |
||
345 | * @param InputInterface $input |
||
346 | * |
||
347 | * @return null|string |
||
348 | */ |
||
349 | 12 | public function getLockPath(InputInterface $input) |
|
365 | |||
366 | /** |
||
367 | * Write the "is locked" message. |
||
368 | * |
||
369 | * @param InputInterface $input |
||
370 | * @param OutputInterface $output |
||
371 | */ |
||
372 | 6 | private function writeLockedMessage(InputInterface $input, OutputInterface $output) |
|
388 | } |
||
389 |