Skip to content

Commit c317634

Browse files
committed
refactor(app): extract cmd not found to outputhelper
1 parent 763e259 commit c317634

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/Application.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,8 @@ protected function doAction(Command $command)
367367
*/
368368
protected function notFound()
369369
{
370-
$closest = [];
371-
$attempted = $this->argv[1];
372370
$available = \array_keys($this->commands() + $this->aliases);
373-
374-
foreach ($available as $cmd) {
375-
$lev = \levenshtein($attempted, $cmd);
376-
if ($lev > 0 || $lev < 5) {
377-
$closest[$cmd] = $lev;
378-
}
379-
}
380-
381-
$this->io()->error("Command $attempted not found", true);
382-
if ($closest) {
383-
\asort($closest);
384-
$closest = \key($closest);
385-
$this->io()->bgRed("Did you mean $closest?", true);
386-
}
371+
$this->outputHelper()->showCommandNotFound($this->argv[1], $available);
387372

388373
return ($this->onExit)(127);
389374
}

src/Helper/OutputHelper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,26 @@ public function showUsage(string $usage): self
227227
return $this;
228228
}
229229

230+
public function showCommandNotFound(string $attempted, array $available): self
231+
{
232+
$closest = [];
233+
foreach ($available as $cmd) {
234+
$lev = \levenshtein($attempted, $cmd);
235+
if ($lev > 0 || $lev < 5) {
236+
$closest[$cmd] = $lev;
237+
}
238+
}
239+
240+
$this->writer->error("Command $attempted not found", true);
241+
if ($closest) {
242+
\asort($closest);
243+
$closest = \key($closest);
244+
$this->writer->bgRed("Did you mean $closest?", true);
245+
}
246+
247+
return $this;
248+
}
249+
230250
/**
231251
* Sort items by name. As a side effect sets max length of all names.
232252
*

0 commit comments

Comments
 (0)