Completed
Pull Request — master (#178)
by
unknown
10:55
created

GitCommand::renderOption()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php declare(strict_types=1);
2
3
namespace GitWrapper;
4
5
final class GitCommand
6
{
7
    /**
8
     * Path to the directory containing the working copy. If this variable is
9
     * set, then the process will change into this directory while the Git
10
     * command is being run.
11
     *
12
     * @var string|null
13
     */
14
    private $directory;
15
16
    /**
17
     * The command being run, e.g. "clone", "commit", etc.
18
     *
19
     * @var string
20
     */
21
    private $command = '';
22
23
    /**
24
     * Whether command execution should be bypassed.
25
     *
26
     * @var bool
27
     */
28
    private $bypass = false;
29
30
    /**
31
     * Whether to execute the raw command without escaping it. This is useful
32
     * for executing arbitrary commands, e.g. "status -s". If this is true,
33
     * any options and arguments are ignored.
34
     *
35
     * @var bool
36
     */
37
    private $executeRaw = false;
38
39
    /**
40
     * @var mixed[]
41
     */
42
    private $options = [];
43
44
    /**
45
     * @var mixed[]
46
     */
47
    private $args = [];
48
49
    /**
50
     * @param mixed ...$argsAndOptions
51
     */
52
    public function __construct(string $command = '', ...$argsAndOptions)
53
    {
54
        $this->command = $command;
55
56
        foreach ($argsAndOptions as $argOrOption) {
57
            if (is_array($argOrOption)) {
58
                // If item is array, set it as the options
59
                $this->setOptions($argOrOption);
60
            } else {
61
                // Pass all other as the Git command arguments
62
                $this->addArgument($argOrOption);
63
            }
64
        }
65
    }
66
67
    /**
68
     * Returns Git command being run, e.g. "clone", "commit", etc.
69
     */
70
    public function getCommand(): string
71
    {
72
        return $this->command;
73
    }
74
75
    public function setDirectory(?string $directory): void
76
    {
77
        $this->directory = $directory;
78
    }
79
80
    public function getDirectory(): ?string
81
    {
82
        return $this->directory;
83
    }
84
85
    /**
86
     * A bool flagging whether to skip running the command.
87
     */
88
    public function bypass(bool $bypass = true): void
89
    {
90
        $this->bypass = $bypass;
91
    }
92
93
    /**
94
     * Set whether to execute the command as-is without escaping it.
95
     */
96
    public function executeRaw(bool $executeRaw = true): void
97
    {
98
        $this->executeRaw = $executeRaw;
99
    }
100
101
    /**
102
     * Returns true if the Git command should be run.
103
     */
104
    public function notBypassed(): bool
105
    {
106
        return ! $this->bypass;
107
    }
108
109
    /**
110
     * Builds the command line options for use in the Git command.
111
     *
112
     * @return mixed[]
113
     */
114
    public function buildOptions(): array
115
    {
116
        $options = [];
117
        foreach ($this->options as $option => $values) {
118
            // PHP always casts numeric array keys to integer
119
            $option = (string) $option;
120
            foreach ((array) $values as $value) {
121
                $options = $this->renderOption($options, $option, $value);
122
            }
123
        }
124
125
        return $options;
126
    }
127
128
    /**
129
     * @param mixed[]|string|true $value The option's value, pass true if the options is a flag.
130
     */
131
    public function setOption(string $option, $value): void
132
    {
133
        $this->options[$option] = $value;
134
    }
135
136
    /**
137
     * @param mixed[] $options
138
     */
139
    public function setOptions(array $options): void
140
    {
141
        foreach ($options as $option => $value) {
142
            $this->setOption($option, $value);
143
        }
144
    }
145
146
    public function setFlag(string $option): void
147
    {
148
        $this->setOption($option, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array<integer,*>|string|object<GitWrapper\true>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
    }
150
151
    /**
152
     * @param mixed $default
153
     * @return mixed
154
     */
155
    public function getOption(string $option, $default = null)
156
    {
157
        return $this->options[$option] ?? $default;
158
    }
159
160
    public function unsetOption(string $option): void
161
    {
162
        unset($this->options[$option]);
163
    }
164
165
    public function addArgument(string $arg): void
166
    {
167
        $this->args[] = $arg;
168
    }
169
170
    /**
171
     * Renders the arguments and options for the Git command.
172
     *
173
     * @return string|mixed[]
174
     */
175
    public function getCommandLine()
176
    {
177
        if ($this->executeRaw) {
178
            return $this->getCommand();
179
        }
180
181
        $command = array_merge([$this->getCommand()], $this->buildOptions(), $this->args);
182
183
        return array_filter($command, 'strlen');
184
    }
185
186
    /**
187
     * Render the option.
188
     *
189
     * @param string[] $options
190
     * @param string $option
191
     * @param mixed $value
192
     * @return string[]
193
     */
194
    private function renderOption(array $options, string $option, $value): array
195
    {
196
        if (strlen($option) > 1) {
197
            $options[] = '--' . $option . ($value !== true ? '=' . $value : '');
198
        } else {
199
            $options[] = '-' . $option;
200
            if ($value !== true) {
201
                $options[] = $value;
202
            }
203
        }
204
205
        return $options;
206
    }
207
}
208