Completed
Push — master ( 7a38c7...a3c5ae )
by Dmitry
11:27
created

UnknownCommandException::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\console;
9
10
use yii\console\controllers\HelpController;
11
12
/**
13
 * UnknownCommandException represents an exception caused by incorrect usage of a console command.
14
 *
15
 * @author Carsten Brandt <[email protected]>
16
 * @since 2.0.11
17
 */
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)
40
    {
41 17
        $this->command = $route;
42 17
        $this->application = $application;
43 17
        parent::__construct("Unknown command \"$route\".", $code, $previous);
44 17
    }
45
46
    /**
47
     * @return string the user-friendly name of this exception
48
     */
49 1
    public function getName()
50
    {
51 1
        return 'Unknown command';
52
    }
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $actionID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $actionID is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
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)
116
    {
117 15
        $alternatives = [];
118
119
        // suggest alternatives that begin with $command first
120 15
        foreach ($actions as $action) {
121 15
            if (strpos($action, $command) === 0) {
122 15
                $alternatives[] = $action;
123
            }
124
        }
125
126
        // calculate the Levenshtein distance between the unknown command and all available commands.
127
        $distances = array_map(function ($action) use ($command) {
128 15
            $action = strlen($action) > 255 ? substr($action, 0, 255) : $action;
129 15
            $command = strlen($command) > 255 ? substr($command, 0, 255) : $command;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $command, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
130 15
            return levenshtein($action, $command);
131 15
        }, array_combine($actions, $actions));
132
133
        // we assume a typo if the levensthein distance is no more than 3, i.e. 3 replacements needed
134 15
        $relevantTypos = array_filter($distances, function ($distance) {
135 15
            return $distance <= 3;
136 15
        });
137 15
        asort($relevantTypos);
138 15
        $alternatives = array_merge($alternatives, array_flip($relevantTypos));
139
140 15
        return array_unique($alternatives);
141
    }
142
}
143