CannotAddCommandException::nameExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
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\Command;
13
14
use Exception;
15
use RuntimeException;
16
17
/**
18
 * Thrown when two commands have the same name.
19
 *
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24
class CannotAddCommandException extends RuntimeException
25
{
26
    /**
27
     * Code: A command with the same name exists.
28
     */
29
    const NAME_EXISTS = 1;
30
31
    /**
32
     * Code: An option with the same name exists.
33
     */
34
    const OPTION_EXISTS = 2;
35
36
    /**
37
     * Code: The command name is empty.
38
     */
39
    const NAME_EMPTY = 3;
40
41
    /**
42
     * Creates an exception for the code {@link NAME_EXISTS}.
43
     *
44
     * @param string    $name  The command name.
45
     * @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...
46
     *
47
     * @return static The created exception.
48
     */
49 6
    public static function nameExists($name, Exception $cause = null)
50
    {
51 6
        return new static(sprintf(
52 6
            'A command named "%s" exists already.',
53
            $name
54 6
        ), self::NAME_EXISTS, $cause);
55
    }
56
57
    /**
58
     * Creates an exception for the code {@link OPTION_EXISTS}.
59
     *
60
     * @param string    $name  The command name.
61
     * @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...
62
     *
63
     * @return static The created exception.
64
     */
65 2
    public static function optionExists($name, Exception $cause = null)
66
    {
67 2
        return new static(sprintf(
68 2
            'An option named "%s%s" exists already.',
69 2
            strlen($name) > 1 ? '--' : '-',
70
            $name
71 2
        ), self::OPTION_EXISTS, $cause);
72
    }
73
74
    /**
75
     * Creates an exception for the code {@link NAME_EMPTY}.
76
     *
77
     * @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...
78
     *
79
     * @return static The created exception.
80
     */
81 2
    public static function nameEmpty(Exception $cause = null)
82
    {
83 2
        return new static('The command name must be set.', self::NAME_EMPTY, $cause);
84
    }
85
}
86