Shell::validateOp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Consolidation\SiteProcess\Util;
3
4
/**
5
 * Shell::op is a static factory that will create shell operators for use
6
 * in command line arguments list. Shell operators are characters that have
7
 * special meaning to the shell, such as "output redirection". When a shell
8
 * operator object is used, it indicates that this element is intended to
9
 * be used as an operator, and is not simply some other parameter to be escaped.
10
 */
11
class Shell implements ShellOperatorInterface
12
{
13
    protected $value;
14
15
    public static function op($operator)
16
    {
17
        static::validateOp($operator);
18
        return new self($operator);
19
    }
20
21
    public static function preEscaped($value)
22
    {
23
        return new self($value);
24
    }
25
26
    public function __construct($value)
27
    {
28
        $this->value = $value;
29
    }
30
31
    public function __toString()
32
    {
33
        return $this->value;
34
    }
35
36
    protected static function validateOp($operator)
37
    {
38
        $valid = [
39
            '&&',
40
            '||',
41
            '|',
42
            '<',
43
            '>',
44
            '>>',
45
            ';',
46
        ];
47
48
        if (!in_array($operator, $valid)) {
49
            throw new \Exception($operator . ' is not a valid shell operator.');
50
        }
51
    }
52
}
53