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 | /** |
||
26 | * @var Application |
||
27 | */ |
||
28 | protected $application; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Construct the exception. |
||
33 | * |
||
34 | * @param string $route the route of the command that could not be found. |
||
35 | * @param Application $application the console application instance involved. |
||
36 | * @param int $code the Exception code. |
||
37 | * @param \Exception $previous the previous exception used for the exception chaining. |
||
38 | */ |
||
39 | 17 | public function __construct($route, $application, $code = 0, \Exception $previous = null) |
|
45 | |||
46 | /** |
||
47 | * @return string the user-friendly name of this exception |
||
48 | */ |
||
49 | 1 | public function getName() |
|
53 | |||
54 | /** |
||
55 | * Suggest alternative commands for [[$command]] based on string similarity. |
||
56 | * |
||
57 | * Alternatives are searched using the following steps: |
||
58 | * |
||
59 | * - suggest alternatives that begin with `$command` |
||
60 | * - find typos by calculating the Levenshtein distance between the unknown command and all |
||
61 | * available commands. The Levenshtein distance is defined as the minimal number of |
||
62 | * characters you have to replace, insert or delete to transform str1 into str2. |
||
63 | * |
||
64 | * @see http://php.net/manual/en/function.levenshtein.php |
||
65 | * @return array a list of suggested alternatives sorted by similarity. |
||
66 | */ |
||
67 | 16 | public function getSuggestedAlternatives() |
|
68 | { |
||
69 | 16 | $help = $this->application->createController('help'); |
|
70 | 16 | if ($help === false || $this->command === '') { |
|
71 | 1 | return []; |
|
72 | } |
||
73 | /** @var $helpController HelpController */ |
||
74 | 15 | list($helpController, $actionID) = $help; |
|
|
|||
75 | |||
76 | 15 | $availableActions = []; |
|
77 | 15 | foreach ($helpController->getCommands() 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 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | 15 | return $this->filterBySimilarity($availableActions, $this->command); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Find suggest alternative commands based on string similarity. |
||
102 | * |
||
103 | * Alternatives are searched using the following steps: |
||
104 | * |
||
105 | * - suggest alternatives that begin with `$command` |
||
106 | * - find typos by calculating the Levenshtein distance between the unknown command and all |
||
107 | * available commands. The Levenshtein distance is defined as the minimal number of |
||
108 | * characters you have to replace, insert or delete to transform str1 into str2. |
||
109 | * |
||
110 | * @see http://php.net/manual/en/function.levenshtein.php |
||
111 | * @param array $actions available command names. |
||
112 | * @param string $command the command to compare to. |
||
113 | * @return array a list of suggested alternatives sorted by similarity. |
||
114 | */ |
||
115 | 15 | private function filterBySimilarity($actions, $command) |
|
142 | } |
||
143 |
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.