1 | <?php |
||
18 | class UnknownCommandException extends Exception |
||
19 | { |
||
20 | /** |
||
21 | * @var string the name of the command that could not be recognized. |
||
22 | */ |
||
23 | public $command; |
||
24 | /** |
||
25 | * @var Application |
||
26 | */ |
||
27 | protected $application; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Construct the exception. |
||
32 | * |
||
33 | * @param string $route the route of the command that could not be found. |
||
34 | * @param Application $application the console application instance involved. |
||
35 | * @param int $code the Exception code. |
||
36 | * @param \Exception $previous the previous exception used for the exception chaining. |
||
37 | */ |
||
38 | 16 | public function __construct($route, $application, $code = 0, \Exception $previous = null) |
|
44 | |||
45 | /** |
||
46 | * @return string the user-friendly name of this exception |
||
47 | */ |
||
48 | 1 | public function getName() |
|
52 | |||
53 | /** |
||
54 | * Suggest alternative commands for [[$command]] based on string similarity. |
||
55 | * |
||
56 | * Alternatives are searched using the following steps: |
||
57 | * |
||
58 | * - suggest alternatives that begin with `$command` |
||
59 | * - find typos by calculating the Levenshtein distance between the unknown command and all |
||
60 | * available commands. The Levenshtein distance is defined as the minimal number of |
||
61 | * characters you have to replace, insert or delete to transform str1 into str2. |
||
62 | * |
||
63 | * @see http://php.net/manual/en/function.levenshtein.php |
||
64 | * @return array a list of suggested alternatives sorted by similarity. |
||
65 | */ |
||
66 | 15 | public function getSuggestedAlternatives() |
|
67 | { |
||
68 | 15 | $help = $this->application->createController('help'); |
|
69 | 15 | if ($help === false) { |
|
70 | return []; |
||
71 | } |
||
72 | /** @var $helpController HelpController */ |
||
73 | 15 | list($helpController, $actionID) = $help; |
|
|
|||
74 | |||
75 | 15 | $availableActions = []; |
|
76 | 15 | $commands = $helpController->getCommands(); |
|
77 | 15 | foreach ($commands as $command) { |
|
78 | 15 | $result = $this->application->createController($command); |
|
79 | 15 | if ($result === false) { |
|
80 | continue; |
||
81 | } |
||
82 | // add the command itself (default action) |
||
83 | 15 | $availableActions[] = $command; |
|
84 | |||
85 | // add all actions of this controller |
||
86 | /** @var $controller Controller */ |
||
87 | 15 | list($controller, $actionID) = $result; |
|
88 | 15 | $actions = $helpController->getActions($controller); |
|
89 | 15 | if (!empty($actions)) { |
|
90 | 15 | $prefix = $controller->getUniqueId(); |
|
91 | 15 | foreach ($actions as $action) { |
|
92 | 15 | $availableActions[] = $prefix . '/' . $action; |
|
93 | 15 | } |
|
94 | 15 | } |
|
95 | 15 | } |
|
96 | 15 | return $this->filterBySimilarity($availableActions, $this->command); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Find suggest alternative commands based on string similarity. |
||
101 | * |
||
102 | * Alternatives are searched using the following steps: |
||
103 | * |
||
104 | * - suggest alternatives that begin with `$command` |
||
105 | * - find typos by calculating the Levenshtein distance between the unknown command and all |
||
106 | * available commands. The Levenshtein distance is defined as the minimal number of |
||
107 | * characters you have to replace, insert or delete to transform str1 into str2. |
||
108 | * |
||
109 | * @see http://php.net/manual/en/function.levenshtein.php |
||
110 | * @param array $actions available command names. |
||
111 | * @param string $command the command to compare to. |
||
112 | * @return array a list of suggested alternatives sorted by similarity. |
||
113 | */ |
||
114 | 15 | private function filterBySimilarity($actions, $command) |
|
141 | } |
||
142 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.