Complex classes like AggregateCommand 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 AggregateCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class AggregateCommand extends AbstractCommand implements IConfigAwareCommand |
||
26 | { |
||
27 | |||
28 | const SETTING_AGGREGATE_IGNORE = 'aggregate.ignore'; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | protected function configure() |
||
34 | { |
||
35 | $this |
||
36 | ->setName('aggregate') |
||
37 | ->setDescription( |
||
38 | 'Runs other command sequentially on every working copy on a path' |
||
39 | ) |
||
40 | ->addArgument( |
||
41 | 'sub-command', |
||
42 | InputArgument::OPTIONAL, |
||
43 | 'Command to execute on each found working copy' |
||
44 | ) |
||
45 | ->addArgument( |
||
46 | 'path', |
||
47 | InputArgument::OPTIONAL, |
||
48 | 'Path to folder with working copies', |
||
49 | '.' |
||
50 | ) |
||
51 | ->addOption( |
||
52 | 'ignore-add', |
||
53 | null, |
||
54 | InputOption::VALUE_REQUIRED, |
||
55 | 'Adds path to ignored directory list' |
||
56 | ) |
||
57 | ->addOption( |
||
58 | 'ignore-remove', |
||
59 | null, |
||
60 | InputOption::VALUE_REQUIRED, |
||
61 | 'Removes path to ignored directory list' |
||
62 | ) |
||
63 | ->addOption( |
||
64 | 'ignore-show', |
||
65 | null, |
||
66 | InputOption::VALUE_NONE, |
||
67 | 'Show ignored directory list' |
||
68 | ); |
||
69 | |||
70 | parent::configure(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Copies relevant options from supported sub-commands. |
||
75 | * |
||
76 | * @param Application $application An Application instance. |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function setApplication(Application $application = null) |
||
81 | { |
||
82 | parent::setApplication($application); |
||
83 | |||
84 | // No application is provided, when this command is disabled. |
||
85 | if ( !$application ) { |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | $input_definition = $this->getDefinition(); |
||
90 | |||
91 | foreach ( $this->getSubCommands() as $sub_command ) { |
||
92 | assert($sub_command instanceof IAggregatorAwareCommand); |
||
93 | $copy_options = $sub_command->getAggregatedOptions(); |
||
94 | |||
95 | if ( !$copy_options ) { |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | $sub_command_input_definition = $sub_command->getDefinition(); |
||
100 | |||
101 | foreach ( $copy_options as $copy_option_name ) { |
||
102 | $copy_option = $sub_command_input_definition->getOption($copy_option_name); |
||
103 | $input_definition->addOption($copy_option); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Return possible values for the named argument |
||
110 | * |
||
111 | * @param string $argumentName Argument name. |
||
112 | * @param CompletionContext $context Completion context. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function completeArgumentValues($argumentName, CompletionContext $context) |
||
126 | |||
127 | /** |
||
128 | * Returns available sub command names. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | protected function getSubCommandNames() |
||
136 | |||
137 | /** |
||
138 | * Returns available sub commands. |
||
139 | * |
||
140 | * @return Command[] |
||
141 | */ |
||
142 | protected function getSubCommands() |
||
143 | { |
||
144 | $ret = array(); |
||
145 | |||
146 | foreach ( $this->getApplication()->all() as $alias => $command ) { |
||
147 | if ( $command instanceof IAggregatorAwareCommand ) { |
||
148 | $ret[$alias] = $command; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | return $ret; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | * |
||
158 | * @throws \RuntimeException When "sub-command" argument not specified. |
||
159 | * @throws \RuntimeException When specified sub-command doesn't support aggregation. |
||
160 | */ |
||
161 | protected function execute(InputInterface $input, OutputInterface $output) |
||
162 | { |
||
163 | if ( $this->processIgnoreAdd() || $this->processIgnoreRemove() || $this->processIgnoreShow() ) { |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | $sub_command = $this->io->getArgument('sub-command'); |
||
168 | |||
169 | if ( $sub_command === null ) { |
||
170 | throw new \RuntimeException('Not enough arguments (missing: "sub-command").'); |
||
171 | } |
||
172 | |||
173 | if ( !in_array($sub_command, $this->getSubCommandNames()) ) { |
||
174 | throw new \RuntimeException( |
||
175 | 'The "' . $sub_command . '" sub-command is unknown or doesn\'t support aggregation.' |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | $this->runSubCommand($sub_command); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Adds path to ignored directory list. |
||
184 | * |
||
185 | * @return boolean |
||
186 | * @throws CommandException When directory is already ignored. |
||
187 | * @throws CommandException When directory does not exist. |
||
188 | */ |
||
189 | protected function processIgnoreAdd() |
||
213 | |||
214 | /** |
||
215 | * Removes path from ignored directory list. |
||
216 | * |
||
217 | * @return boolean |
||
218 | * @throws CommandException When directory is not ignored. |
||
219 | */ |
||
220 | protected function processIgnoreRemove() |
||
244 | |||
245 | /** |
||
246 | * Shows ignored paths. |
||
247 | * |
||
248 | * @return boolean |
||
249 | */ |
||
250 | protected function processIgnoreShow() |
||
251 | { |
||
252 | if ( !$this->io->getOption('ignore-show') ) { |
||
253 | return false; |
||
254 | } |
||
255 | |||
256 | $ignored = $this->getIgnored(); |
||
257 | |||
258 | if ( !$ignored ) { |
||
259 | $this->io->writeln('No paths found in ignored directory list.'); |
||
260 | |||
261 | return true; |
||
262 | } |
||
263 | |||
264 | $this->io->writeln(array('Paths in ignored directory list:', '')); |
||
265 | |||
266 | foreach ( $ignored as $ignored_path ) { |
||
267 | $this->io->writeln(' * ' . $ignored_path); |
||
268 | } |
||
269 | |||
270 | $this->io->writeln(''); |
||
271 | |||
272 | return true; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns ignored paths. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | protected function getIgnored() |
||
284 | |||
285 | /** |
||
286 | * Runs sub-command. |
||
287 | * |
||
288 | * @param string $sub_command_name Sub-command name. |
||
289 | * |
||
290 | * @return void |
||
291 | * @throws \RuntimeException When command was used inside a working copy. |
||
292 | */ |
||
293 | protected function runSubCommand($sub_command_name) |
||
326 | |||
327 | /** |
||
328 | * Prepares sub-command arguments. |
||
329 | * |
||
330 | * @param string $sub_command_name Sub-command name. |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | protected function prepareSubCommandArguments($sub_command_name) |
||
351 | |||
352 | /** |
||
353 | * Returns working copies found at given path. |
||
354 | * |
||
355 | * @param string $path Path. |
||
356 | * |
||
357 | * @return array |
||
358 | * @throws CommandException When no working copies where found. |
||
359 | */ |
||
360 | protected function getWorkingCopyPaths($path) |
||
386 | |||
387 | /** |
||
388 | * Returns working copy locations recursively. |
||
389 | * |
||
390 | * @param string $path Path. |
||
391 | * |
||
392 | * @return array |
||
393 | */ |
||
394 | protected function getWorkingCopiesRecursive($path) |
||
422 | |||
423 | /** |
||
424 | * Returns list of config settings. |
||
425 | * |
||
426 | * @return AbstractConfigSetting[] |
||
427 | */ |
||
428 | public function getConfigSettings() |
||
434 | |||
435 | } |
||
436 |