Completed
Push — master ( bd5ff6...90c2c7 )
by Bernhard
07:04
created

CannotResolveCommandException::noDefaultCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Api\Resolver;
13
14
use Exception;
15
use RuntimeException;
16
use Webmozart\Console\Api\Command\CommandCollection;
17
use Webmozart\Console\Util\SimilarCommandName;
18
19
/**
20
 * Thrown when a command cannot be resolved.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class CannotResolveCommandException extends RuntimeException
27
{
28
    /**
29
     * Code: The passed command name was not found.
30
     */
31
    const NAME_NOT_FOUND = 1;
32
33
    /**
34
     * Code: No command was passed and no default was configured.
35
     */
36
    const NO_DEFAULT_COMMAND = 2;
37
38
    /**
39
     * Creates an exception for the code {@link NAME_NOT_FOUND}.
40
     *
41
     * Suggested alternatives are searched in the passed commands.
42
     *
43
     * @param string            $commandName The command name that was not found.
44
     * @param CommandCollection $commands    A list of available commands that
45
     *                                       is searched for similar names.
46
     * @param Exception         $cause       The exception that caused this
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cause not be null|Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
47
     *                                       exception.
48
     *
49
     * @return static The created exception.
50
     */
51 1
    public static function nameNotFound($commandName, CommandCollection $commands, Exception $cause = null)
52
    {
53 1
        $message = sprintf('The command "%s" is not defined.', $commandName);
54
55 1
        $suggestedNames = SimilarCommandName::find($commandName, $commands);
56
57 1
        if (count($suggestedNames) > 0) {
58 1
            if (1 === count($suggestedNames)) {
59
                $message .= "\n\nDid you mean this?\n    ";
60
            } else {
61 1
                $message .= "\n\nDid you mean one of these?\n    ";
62
            }
63 1
            $message .= implode("\n    ", $suggestedNames);
64
        }
65
66 1
        return new static($message, self::NAME_NOT_FOUND, $cause);
67
    }
68
69
    /**
70
     * Creates an exception for the code {@link NO_DEFAULT_COMMAND}.
71
     *
72
     * @param Exception $cause The exception that caused this exception.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cause not be null|Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
73
     *
74
     * @return static The created exception.
75
     */
76
    public static function noDefaultCommand(Exception $cause = null)
77
    {
78
        return new static('No default command is defined.', self::NO_DEFAULT_COMMAND, $cause);
79
    }
80
}
81