Descriptor::describeInputArgument()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php namespace nyx\console;
2
3
// External dependencies
4
use nyx\diagnostics;
5
6
// Internal dependencies
7
use nyx\console\input\parameter\definitions;
8
9
/**
10
 * Descriptor
11
 *
12
 * The particular methods are public for simplicity's sake but if you intend to use them directly, ensure you are
13
 * checking for the type of this class, not for the interface, as the interface does not require them to be present
14
 * for the contract to be fulfilled.
15
 *
16
 * @version     0.1.0
17
 * @author      Michal Chojnacki <[email protected]>
18
 * @copyright   2012-2017 Nyx Dev Team
19
 * @link        https://github.com/unyx/nyx
20
 */
21
abstract class Descriptor implements interfaces\Descriptor
22
{
23
    /**
24
     * @var array   A map of supported types to their respective instance methods, minus the 'describe' prefix.
25
     */
26
    protected $types = [
27
        Application::class      => 'Application',
28
        Suite::class            => 'Suite',
29
        Command::class          => 'Command',
30
        input\Definition::class => 'InputDefinition',
31
        input\Argument::class   => 'InputArgument',
32
        input\Option::class     => 'InputOption',
33
        input\Value::class      => 'InputValue',
34
    ];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function describe($object, array $options = null)
40
    {
41
        foreach ($this->types as $class => $type) {
42
            if ($object instanceof $class) {
43
                return $this->{'describe'.$type}($object, $options);
44
            }
45
        }
46
47
        throw new \InvalidArgumentException("The type [".diagnostics\Debug::getTypeName($object)."] is not supported by this Descriptor.");
48
    }
49
50
    /**
51
     * Describes an Application.
52
     *
53
     * @param   Application $application    The Application to describe.
54
     * @param   array       $options        Additional options to be considered by the Descriptor.
55
     * @return  mixed
56
     */
57
    abstract public function describeApplication(Application $application, array $options = null);
58
59
    /**
60
     * Describes a Suite.
61
     *
62
     * @param   Suite   $suite      The Suite to describe.
63
     * @param   array   $options    Additional options to be considered by the Descriptor.
64
     * @return  mixed
65
     */
66
    abstract public function describeSuite(Suite $suite, array $options = null);
67
68
    /**
69
     * Describes a Command.
70
     *
71
     * @param   Command $command    The Command to describe.
72
     * @param   array   $options    Additional options to be considered by the Descriptor.
73
     * @return  mixed
74
     */
75
    abstract public function describeCommand(Command $command, array $options = null);
76
77
    /**
78
     * Describes an Input Definition.
79
     *
80
     * @param   input\Definition    $definition The Input Definition to describe.
81
     * @param   array               $options    Additional options to be considered by the Descriptor.
82
     * @return  mixed
83
     */
84
    abstract public function describeInputDefinition(input\Definition $definition, array $options = null);
85
86
    /**
87
     * Describes an Input Argument.
88
     *
89
     * @param   input\Argument  $argument   The Input Argument to describe.
90
     * @param   array           $options    Additional options to be considered by the Descriptor.
91
     * @return  mixed
92
     */
93
    abstract public function describeInputArgument(input\Argument $argument, array $options = null);
94
95
    /**
96
     * Describes an Input Option.
97
     *
98
     * @param   input\Option    $option     The Input Option to describe.
99
     * @param   array           $options    Additional options to be considered by the Descriptor.
100
     * @return  mixed
101
     */
102
    abstract public function describeInputOption(input\Option $option, array $options = null);
103
104
    /**
105
     * Describes an Input Value.
106
     *
107
     * @param   input\Value     $value      The Input Value to describe.
108
     * @param   array           $options    Additional options to be considered by the Descriptor.
109
     * @return  mixed
110
     */
111
    abstract public function describeInputValue(input\Value $value, array $options = null);
112
113
    /**
114
     * Provides a synopsis (ie. a string describing the usage) for the given Command.
115
     *
116
     * Kept in the abstract Descriptor as the generated string is rather generic, but may require overrides for
117
     * specific formats and therefore should not be kept within the Command class.
118
     *
119
     * @param   Command     $command    The Command to provide the synopsis for.
120
     * @return  mixed
121
     */
122
    public function getCommandSynopsis(Command $command, array $options = null)
123
    {
124
        $definition     = $command->getDefinition();
125
        $inputOptions   = $definition->options();
126
        $inputArguments = $definition->arguments();
127
128
        // We are following the docopt specification here. Options are followed by '--', followed by arguments.
129
        // @link http://docopt.org
130
        $output = $this->getInputOptionsSynopsis($inputOptions, $options);
131
132
        if (!$inputOptions->isEmpty() && !$inputArguments->isEmpty()) {
133
            $output .= ' [--] ';
134
        }
135
136
        return $output . $this->getInputArgumentsSynopsis($inputArguments, $options);
137
    }
138
139
    /**
140
     * Provides a synopsis for the given Input Option Definitions.
141
     *
142
     * @param   definitions\Options     $definitions    The Input Option Definitions to provide a synopsis for.
143
     * @param   array                   $options        Additional options to be considered by the Descriptor.
144
     * @return  mixed
145
     */
146
    public function getInputOptionsSynopsis(definitions\Options $definitions, array $options = null)
147
    {
148
        if ($definitions->isEmpty()) {
149
            return '';
150
        }
151
152
        // The 'short_synopsis' option is primarily used by the Help command which lists all Input Options
153
        // separately from the synopsis for brevity.
154
        if ($options['short_synopsis'] ?? false) {
155
            return '[options]';
156
        }
157
158
        $items = [];
159
160
        foreach ($definitions as $option) {
161
            $items[] = $this->getInputOptionSynopsis($option, $options);
162
        }
163
164
        return implode(' ', $items);
165
    }
166
167
    /**
168
     * Provides a synopsis for the given Input Option.
169
     *
170
     * @param   input\Option    $definition     The Input Option to provide a synopsis for.
171
     * @param   array           $options        Additional options to be considered by the Descriptor.
172
     * @return  mixed
173
     */
174
    public function getInputOptionSynopsis(input\Option $definition, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        $shortcut = $definition->getShortcut() ? sprintf('-%s|', $definition->getShortcut()) : '';
177
178
        if ($value = $definition->getValue()) {
179
            $format = $value->is(input\Value::REQUIRED) ? '%s--%s="..."' : '%s--%s[="..."]';
180
        } else {
181
            $format = '%s--%s';
182
        }
183
184
        return sprintf('['.$format.']', $shortcut, $definition->getName());
185
    }
186
187
    /**
188
     * Provides a synopsis for the given Input Argument Definitions.
189
     *
190
     * @param   definitions\Arguments   $definitions    The Input Argument Definitions to provide a synopsis for.
191
     * @param   array                   $options        Additional options to be considered by the Descriptor.
192
     * @return  mixed
193
     */
194
    public function getInputArgumentsSynopsis(definitions\Arguments $definitions, array $options = null)
195
    {
196
        if ($definitions->isEmpty()) {
197
            return '';
198
        }
199
200
        $items = [];
201
202
        /** @var input\Argument $argument */
203
        foreach ($definitions as $argument) {
204
            $items[] = $this->getInputArgumentSynopsis($argument, $options);
205
        }
206
207
        return implode(' ', $items);
208
    }
209
210
    /**
211
     * Provides a synopsis for the given Input Argument.
212
     *
213
     * @param   input\Argument  $definition     The Input Argument to provide a synopsis for.
214
     * @param   array           $options        Additional options to be considered by the Descriptor.
215
     * @return  mixed
216
     */
217
    public function getInputArgumentSynopsis(input\Argument $definition, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
218
    {
219
        $output = '\<'.$definition->getName().'>';
220
        $value  = $definition->getValue();
221
222
        if (!$value->is(input\Value::REQUIRED)) {
223
            $output = '['.$output.']';
224
        } elseif ($value instanceof input\values\Multiple) {
225
            $output = $output.' ('.$output.')';
226
        }
227
228
        if ($value instanceof input\values\Multiple) {
229
            $output .= '...';
230
        }
231
232
        return $output;
233
    }
234
}
235