Completed
Pull Request — master (#111)
by Jan Philipp
02:16
created

ParameterParser::extractParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Application;
4
5
use function array_slice;
6
use function count;
7
use function explode;
8
use function mb_strpos;
9
use function mb_substr;
10
use function sprintf;
11
use function str_replace;
12
13
14
/**
15
 * ./psh --no-header unit --filter once
16
 */
17
class ParameterParser
18
{
19
    public function parseAllParams(array $params): RuntimeParameters
20
    {
21
        if (count($params) === 0) {
22
            return new RuntimeParameters(
23
                [],
24
                [],
25
                []
26
            );
27
        }
28
29
        $params = array_splice($params, 1);
30
        $commandsAreAt = 0;
31
32
        $appOptions = [];
33
        foreach($params as $commandsAreAt => $param) {
34
            if(!in_array($param, ApplicationOptions::getAllFlags())) {
35
                break;
36
            }
37
38
            $appOptions[] = $param;
39
            $commandsAreAt++;
40
        }
41
42
        $scriptNames = [[], $this->explodeScriptNames($params, $commandsAreAt)];
43
        $overwrites = [[], $this->extractParams(array_slice($params, $commandsAreAt + 1))];
44
45
        return new RuntimeParameters(
46
            array_unique($appOptions),
47
            array_merge(...$scriptNames),
48
            array_merge(...$overwrites)
49
        );
50
    }
51
52
    private function extractParams(array $params): array
53
    {
54
        $reformattedParams = [];
55
        $paramsCount = count($params);
56
        for ($i = 0; $i < $paramsCount; $i++) {
57
            $key = $params[$i];
58
59
            $this->testParameterFormat($key);
60
61
            if ($this->isKeyValuePair($key)) {
62
                list($key, $value) = explode('=', $key, 2);
63
64
                if ($this->isEnclosedInAmpersand($value)) {
65
                    $value = mb_substr($value, 1, -1);
66
                }
67
            } else {
68
                $i++;
69
                $value = $params[$i];
70
            }
71
72
            $key = str_replace('--', '', $key);
73
            $reformattedParams[$key] = $value;
74
        }
75
76
        return $reformattedParams;
77
    }
78
79
    private function testParameterFormat(string $key): void
80
    {
81
        if (mb_strpos($key, '--') !== 0) {
82
            throw new InvalidParameter(
83
                sprintf('Unable to parse parameter "%s". Use -- for correct usage', $key)
84
            );
85
        }
86
    }
87
88
    private function isKeyValuePair(string $key): bool
89
    {
90
        return mb_strpos($key, '=') !== false;
91
    }
92
93
    private function isEnclosedInAmpersand(string $value): bool
94
    {
95
        return mb_strpos($value, '"') === 0;
96
    }
97
98
    private function explodeScriptNames(array $params, int $position): array
99
    {
100
        if(!isset($params[$position])) {
101
            return [];
102
        }
103
104
        return explode(',', $params[$position]);
105
    }
106
}
107