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<string> |
||||
13 | */ |
||||
14 | private $commandArgs; |
||||
15 | |||||
16 | /** |
||||
17 | * @param array<string>|string $command |
||||
18 | */ |
||||
19 | 19 | public function __construct($command) |
|||
20 | { |
||||
21 | 19 | if (\is_array($command)) { |
|||
22 | 18 | $this->commandArgs = $command; |
|||
23 | } elseif (\is_string($command)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
24 | 16 | $this->commandArgs = [$command]; |
|||
25 | 15 | } else { |
|||
26 | throw new InvalidArgumentException( |
||||
27 | 1 | \Safe\sprintf( |
|||
0 ignored issues
–
show
The function
Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
28 | 1 | 'Command should be an "array<string>" or a "string", "%s" given', |
|||
29 | 1 | \is_object($command) ? \get_class($command) : \gettype($command) |
|||
30 | 1 | ) |
|||
31 | ); |
||||
32 | } |
||||
33 | } |
||||
34 | |||||
35 | 18 | public function addArg(string $arg): self |
|||
36 | { |
||||
37 | 14 | $args = $this->commandArgs; |
|||
38 | $args[] = $arg; |
||||
39 | 14 | ||||
40 | 14 | return new self($args); |
|||
41 | } |
||||
42 | 14 | ||||
43 | /** |
||||
44 | * @param iterable<string> $argsToAdd |
||||
45 | */ |
||||
46 | public function addArgs(iterable $argsToAdd): self |
||||
47 | { |
||||
48 | 8 | $args = $this->commandArgs; |
|||
49 | |||||
50 | 8 | foreach ($argsToAdd as $arg) { |
|||
51 | $args[] = $arg; |
||||
52 | 8 | } |
|||
53 | 8 | ||||
54 | return new self($args); |
||||
55 | } |
||||
56 | 8 | ||||
57 | /** |
||||
58 | * @return array<string> |
||||
59 | 16 | */ |
|||
60 | public function getArgs(): array |
||||
61 | 16 | { |
|||
62 | return $this->commandArgs; |
||||
63 | } |
||||
64 | 1 | ||||
65 | public function asString(): string |
||||
66 | 1 | { |
|||
67 | return implode(' ', array_map([$this, 'escapeArgument'], $this->commandArgs)); |
||||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * Escapes a string to be used as a shell argument. |
||||
72 | 1 | */ |
|||
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) !== 0) { |
||||
85 | return $argument; |
||||
86 | } |
||||
87 | $argument = \Safe\preg_replace('/(\\\\+)$/', '$1$1', $argument); |
||||
88 | |||||
89 | return '"' . str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument) . '"'; |
||||
90 | } |
||||
91 | } |
||||
92 |