Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

CommandLine::escapeArgument()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 17.8

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 1
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
ccs 2
cts 10
cp 0.2
crap 17.8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Utils;
6
7
use PhpSpellcheck\Exception\InvalidArgumentException;
8
9
class CommandLine
10
{
11
    /**
12
     * @var array
13
     */
14
    private $commandArgs;
15
16
    /**
17
     * @param string|array $command
18
     */
19 19
    public function __construct($command)
20
    {
21 19
        if (is_array($command)) {
22 18
            $this->commandArgs = $command;
23
        } else {
24 16
            if (is_string($command)) {
0 ignored issues
show
introduced by
The condition is_string($command) is always true.
Loading history...
25 15
                $this->commandArgs = [$command];
26
            } else {
27 1
                throw new InvalidArgumentException(
28 1
                    \Safe\sprintf(
29 1
                        'Command should be an "array" or a "string", "%s" given',
30 1
                        is_object($command) ? get_class($command) : gettype($command)
31
                    )
32
                );
33
            }
34
        }
35 18
    }
36
37 14
    public function addArg(string $arg): self
38
    {
39 14
        $args = $this->commandArgs;
40 14
        $args[] = $arg;
41
42 14
        return new self($args);
43
    }
44
45
    /**
46
     * @param string[] $argsToAdd
47
     */
48 8
    public function addArgs(iterable $argsToAdd): self
49
    {
50 8
        $args = $this->commandArgs;
51
52 8
        foreach ($argsToAdd as $arg) {
53 8
            $args[] = $arg;
54
        }
55
56 8
        return new self($args);
57
    }
58
59 16
    public function getArgs(): array
60
    {
61 16
        return $this->commandArgs;
62
    }
63
64 1
    public function asString(): string
65
    {
66 1
        return implode(' ', array_map([$this, 'escapeArgument'], $this->commandArgs));
67
    }
68
69
    /**
70
     * Escapes a string to be used as a shell argument.
71
     */
72 1
    //@codingStandardsIgnoreLine SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
73
    private function escapeArgument(string $argument): string
74 1
    {
75 1
        if ('\\' !== \DIRECTORY_SEPARATOR) {
76
            return "'" . str_replace("'", "'\\''", $argument) . "'";
77
        }
78
        if ('' === $argument) {
79
            return '""';
80
        }
81
        if (false !== strpos($argument, "\0")) {
82
            $argument = str_replace("\0", '?', $argument);
83
        }
84
        if (!\Safe\preg_match('/[\/()%!^"<>&|\s]/', $argument)) {
85
            return $argument;
86
        }
87
        $argument = \Safe\preg_replace('/(\\\\+)$/', '$1$1', $argument);
88
89
        return '"' . str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument) . '"';
90
    }
91
    //@codingStandardsIgnoreEnd
92
}
93