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:
1 | <?php |
||
43 | class ConsoleApplication implements Application |
||
44 | { |
||
45 | /** |
||
46 | * @var ApplicationConfig |
||
47 | */ |
||
48 | private $config; |
||
49 | |||
50 | /** |
||
51 | * @var CommandCollection |
||
52 | */ |
||
53 | private $commands; |
||
54 | |||
55 | /** |
||
56 | * @var CommandCollection |
||
57 | */ |
||
58 | private $namedCommands; |
||
59 | |||
60 | /** |
||
61 | * @var CommandCollection |
||
62 | */ |
||
63 | private $defaultCommands; |
||
64 | |||
65 | /** |
||
66 | * @var ArgsFormat |
||
67 | */ |
||
68 | private $globalArgsFormat; |
||
69 | |||
70 | /** |
||
71 | * @var EventDispatcherInterface |
||
72 | */ |
||
73 | private $dispatcher; |
||
74 | |||
75 | /** |
||
76 | * @var ConsoleIO |
||
77 | */ |
||
78 | private $preliminaryIo; |
||
79 | |||
80 | /** |
||
81 | * Creates a new console application. |
||
82 | * |
||
83 | * @param ApplicationConfig|callable $config The application configuration |
||
84 | * or a callable that creates the |
||
85 | * configuration. |
||
86 | */ |
||
87 | 138 | public function __construct($config) |
|
88 | { |
||
89 | 138 | $this->preliminaryIo = new ConsoleIO(); |
|
90 | |||
91 | // Enable trace output for exceptions thrown during boot |
||
92 | 138 | $this->preliminaryIo->setVerbosity(IO::VERY_VERBOSE); |
|
93 | |||
94 | 138 | if (is_callable($config)) { |
|
95 | try { |
||
96 | 1 | $config = $config(); |
|
97 | } catch (Exception $e) { |
||
98 | $trace = new ExceptionTrace($e); |
||
99 | $trace->render($this->preliminaryIo); |
||
100 | |||
101 | exit($this->exceptionToExitCode($e->getCode())); |
||
|
|||
102 | } |
||
103 | } |
||
104 | |||
105 | 138 | Assert::isInstanceOf($config, 'Webmozart\Console\Api\Config\ApplicationConfig', 'The $config argument must be an ApplicationConfig or a callable. Got: %s'); |
|
106 | |||
107 | try { |
||
108 | 137 | $dispatcher = $config->getEventDispatcher(); |
|
109 | |||
110 | 137 | if ($dispatcher && $dispatcher->hasListeners(ConsoleEvents::CONFIG)) { |
|
111 | 1 | $dispatcher->dispatch(ConsoleEvents::CONFIG, |
|
112 | 1 | new ConfigEvent($config)); |
|
113 | } |
||
114 | |||
115 | 137 | $this->config = $config; |
|
116 | 137 | $this->dispatcher = $config->getEventDispatcher(); |
|
117 | 137 | $this->commands = new CommandCollection(); |
|
118 | 137 | $this->namedCommands = new CommandCollection(); |
|
119 | 137 | $this->defaultCommands = new CommandCollection(); |
|
120 | |||
121 | 137 | $this->globalArgsFormat = new ArgsFormat(array_merge( |
|
122 | 137 | $config->getOptions(), |
|
123 | 137 | $config->getArguments() |
|
124 | )); |
||
125 | |||
126 | 137 | foreach ($config->getCommandConfigs() as $commandConfig) { |
|
127 | 137 | $this->addCommand($commandConfig); |
|
128 | } |
||
129 | 2 | } catch (Exception $e) { |
|
130 | 2 | if (!$config->isExceptionCaught()) { |
|
131 | 2 | throw $e; |
|
132 | } |
||
133 | |||
134 | // Render the trace to the preliminary IO |
||
135 | $trace = new ExceptionTrace($e); |
||
136 | $trace->render($this->preliminaryIo); |
||
137 | |||
138 | // Ignore isTerminatedAfterRun() setting. This is a fatal error. |
||
139 | exit($this->exceptionToExitCode($e->getCode())); |
||
140 | } |
||
141 | 135 | } |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 130 | public function getConfig() |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 130 | public function getGlobalArgsFormat() |
|
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | 76 | public function getCommand($name) |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 17 | public function getCommands() |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 1 | public function hasCommand($name) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 2 | public function hasCommands() |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 235 | public function getNamedCommands() |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | 2 | public function hasNamedCommands() |
|
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 18 | public function getDefaultCommands() |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 2 | public function hasDefaultCommands() |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 43 | public function resolveCommand(RawArgs $args) |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 42 | public function run(RawArgs $args = null, InputStream $inputStream = null, OutputStream $outputStream = null, OutputStream $errorStream = null) |
|
244 | { |
||
245 | // Render errors to the preliminary IO until the final IO is created |
||
246 | 42 | $io = $this->preliminaryIo; |
|
247 | |||
248 | try { |
||
249 | 42 | if (null === $args) { |
|
250 | $args = new ArgvArgs(); |
||
251 | } |
||
252 | |||
253 | 42 | $ioFactory = $this->config->getIOFactory(); |
|
254 | |||
255 | 42 | if (null === $ioFactory) { |
|
256 | 1 | throw new LogicException('The IO factory must be set.'); |
|
257 | } |
||
258 | |||
259 | /** @var IO $io */ |
||
260 | 41 | $io = call_user_func($ioFactory, $this, $args, $inputStream, $outputStream, $errorStream); |
|
261 | |||
262 | 41 | $resolvedCommand = $this->resolveCommand($args); |
|
263 | 41 | $command = $resolvedCommand->getCommand(); |
|
264 | 41 | $parsedArgs = $resolvedCommand->getArgs(); |
|
265 | |||
266 | 41 | $statusCode = $command->handle($parsedArgs, $io); |
|
267 | 5 | } catch (Exception $e) { |
|
268 | 5 | if (!$this->config->isExceptionCaught()) { |
|
269 | 2 | throw $e; |
|
270 | } |
||
271 | |||
272 | 3 | $trace = new ExceptionTrace($e); |
|
273 | 3 | $trace->render($io); |
|
274 | |||
275 | 3 | $statusCode = $this->exceptionToExitCode($e->getCode()); |
|
276 | } |
||
277 | |||
278 | 40 | if ($this->config->isTerminatedAfterRun()) { |
|
279 | exit($statusCode); |
||
280 | } |
||
281 | |||
282 | 40 | return $statusCode; |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * Converts an exception code to an exit code. |
||
287 | * |
||
288 | * @param int $code The exception code. |
||
289 | * |
||
290 | * @return int The exit code. |
||
291 | */ |
||
292 | 3 | private function exceptionToExitCode($code) |
|
293 | { |
||
294 | 3 | if (!is_numeric($code)) { |
|
295 | return 1; |
||
296 | } |
||
297 | |||
298 | 3 | return min(max((int) $code, 1), 255); |
|
299 | } |
||
300 | |||
301 | 121 | private function addCommand(CommandConfig $config) |
|
321 | |||
322 | 121 | private function validateCommandName(CommandConfig $config) |
|
323 | { |
||
324 | 121 | $name = $config->getName(); |
|
334 | } |
||
335 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.