OptionCommandConfig::setName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
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\Config;
13
14
use LogicException;
15
use Webmozart\Assert\Assert;
16
use Webmozart\Console\Api\Args\Format\ArgsFormat;
17
use Webmozart\Console\Api\Args\Format\CommandOption;
18
19
/**
20
 * The configuration of an option command.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class OptionCommandConfig extends SubCommandConfig
27
{
28
    /**
29
     * @var string
30
     */
31
    private $shortName;
32
33
    /**
34
     * @var bool
35
     */
36
    private $longNamePreferred;
37
38
    /**
39
     * Creates a new configuration.
40
     *
41
     * @param string            $name              The long option name of the command.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

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...
42
     * @param string            $shortName         The short option name of the command.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $shortName not be string|null?

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...
43
     * @param CommandConfig     $parentConfig      The parent configuration.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $parentConfig not be null|CommandConfig?

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...
44
     * @param ApplicationConfig $applicationConfig The application configuration.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $applicationConfig not be null|ApplicationConfig?

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...
45
     */
46 53
    public function __construct($name = null, $shortName = null, CommandConfig $parentConfig = null, ApplicationConfig $applicationConfig = null)
47
    {
48 53
        parent::__construct($name, $parentConfig, $applicationConfig);
0 ignored issues
show
Unused Code introduced by
The call to SubCommandConfig::__construct() has too many arguments starting with $applicationConfig.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
50 53
        $this->setShortName($shortName);
51 53
    }
52
53
    /**
54
     * Sets the name of the command.
55
     *
56
     * Contrary to the base implementation, the name of an option command must
57
     * contain at least two characters.
58
     *
59
     * @param string $name The name of the command.
60
     *
61
     * @return static The current instance.
62
     */
63 32
    public function setName($name)
64
    {
65 32
        if (null !== $name) {
66 32
            Assert::string($name, 'The command name must be a string or null. Got: %s');
67 30
            Assert::notEmpty($name, 'The command name must not be empty.');
68 29
            Assert::greaterThan(strlen($name), 1, sprintf('The command name should contain at least two characters. Got: "%s"', $name));
69
        }
70
71 26
        parent::setName($name);
72
73 26
        return $this;
74
    }
75
76
    /**
77
     * Alias of {@link getName()}.
78
     *
79
     * @return string The command name.
80
     */
81 7
    public function getLongName()
82
    {
83 7
        return $this->getName();
84
    }
85
86
    /**
87
     * Alias of {@link setName()}.
88
     *
89
     * @param string $name The command name.
90
     *
91
     * @return static The current instance.
92
     */
93 1
    public function setLongName($name)
94
    {
95 1
        return $this->setName($name);
96
    }
97
98
    /**
99
     * Returns the short option name of the command.
100
     *
101
     * @return string The short option name.
102
     */
103 26
    public function getShortName()
104
    {
105 26
        return $this->shortName;
106
    }
107
108
    /**
109
     * Sets the short option name of the command.
110
     *
111
     * The short name must consist of a single letter. The short name is
112
     * preceded by a single dash "-" when calling the command:
113
     *
114
     * ```
115
     * $ server -d localhost
116
     * ```
117
     *
118
     * In the example above, "d" is the short name of the "server --delete"
119
     * command.
120
     *
121
     * @param string $shortName The short option name.
122
     *
123
     * @return static The current instance.
124
     */
125 53
    public function setShortName($shortName)
126
    {
127 53
        if (null !== $shortName) {
128 36
            Assert::string($shortName, 'The short command name must be a string or null. Got: %s');
129 34
            Assert::notEmpty($shortName, 'The short command name must not be empty.');
130 33
            Assert::regex($shortName, '~^[a-zA-Z]$~', 'The short command name must contain a single letter. Got: %s');
131
        }
132
133
        // Reset short name preference when unsetting the short name
134 53
        if (null === $shortName && false === $this->longNamePreferred) {
135 1
            $this->longNamePreferred = null;
136
        }
137
138 53
        $this->shortName = $shortName;
139
140 53
        return $this;
141
    }
142
143
    /**
144
     * Marks the long name to be preferred over the short name.
145
     *
146
     * This information is mainly used in the help where the preferred name is
147
     * listed before alternative names.
148
     *
149
     * @return static The current instance.
150
     *
151
     * @see isLongNamePreferred(), setPreferShortName()
152
     */
153 2
    public function setPreferLongName()
154
    {
155 2
        $this->longNamePreferred = true;
156
157 2
        return $this;
158
    }
159
160
    /**
161
     * Marks the short name to be preferred over the long name.
162
     *
163
     * This information is mainly used in the help where the preferred name is
164
     * listed before alternative names.
165
     *
166
     * @return static The current instance.
167
     *
168
     * @see isShortNamePreferred(), setPreferLongName()
169
     */
170 3
    public function setPreferShortName()
171
    {
172 3
        if (null === $this->shortName) {
173 1
            throw new LogicException('No short name is set.');
174
        }
175
176 2
        $this->longNamePreferred = false;
177
178 2
        return $this;
179
    }
180
181
    /**
182
     * Returns whether the long name should be preferred over the short name.
183
     *
184
     * If no preference was set, the short name is preferred by default if one
185
     * is set. If no short name is set, the long name is preferred by default.
186
     *
187
     * @return bool Returns `true` if the long name should be preferred over the
188
     *              short name.
189
     *
190
     * @see setPreferLongName(), isShortNamePreferred()
191
     */
192 23
    public function isLongNamePreferred()
193
    {
194 23
        if (null === $this->longNamePreferred) {
195
            // If no preference is set, prefer the short name (if one is set)
196 20
            return null === $this->shortName;
197
        }
198
199 3
        return $this->longNamePreferred;
200
    }
201
202
    /**
203
     * Returns whether the short name should be preferred over the long name.
204
     *
205
     * If no preference was set, the short name is preferred by default if one
206
     * is set. If no short name is set, the long name is preferred by default.
207
     *
208
     * @return bool Returns `true` if the short name should be preferred over
209
     *              the long name.
210
     *
211
     * @see setPreferShortName(), isLongNamePreferred()
212
     */
213 5
    public function isShortNamePreferred()
214
    {
215 5
        return !$this->isLongNamePreferred();
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 19
    public function buildArgsFormat(ArgsFormat $baseFormat = null)
222
    {
223 19
        $formatBuilder = ArgsFormat::build($baseFormat);
224
225 19
        if (!$this->isAnonymous()) {
226 18
            $flags = $this->isLongNamePreferred()
227 7
                ? CommandOption::PREFER_LONG_NAME
228 18
                : CommandOption::PREFER_SHORT_NAME;
229
230 18
            $formatBuilder->addCommandOption(new CommandOption(
231 18
                $this->getName(),
232 18
                $this->getShortName(),
233 18
                $this->getAliases(),
234
                $flags
235
            ));
236
        }
237
238 19
        $formatBuilder->addOptions($this->getOptions());
239 19
        $formatBuilder->addArguments($this->getArguments());
240
241 19
        return $formatBuilder->getFormat();
242
    }
243
}
244