Passed
Push — master ( fa7991...10a1b7 )
by 世昌
02:08
created

TargetBuilder::splitParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\runnable\target;
3
4
use nebula\runnable\target\FileTarget;
5
use nebula\runnable\target\MethodTarget;
6
use nebula\runnable\target\FunctionTarget;
7
use nebula\runnable\target\RunnableTarget;
8
9
/**
10
 * 目标构造器
11
 */
12
class TargetBuilder
13
{
14
    /**
15
     * 构建可执行对象
16
     *
17
     * @param string $command
18
     * @return RunnableTarget
19
     */
20
    public static function build(string $command):RunnableTarget
21
    {
22
        $fileStart = \strrpos($command, '@');
23
        // for @file
24
        if ($fileStart > 0) {
25
            $requireFile = substr($command, $fileStart+1);
26
            $command = substr($command, 0, $fileStart);
27
        }
28
        if ($fileStart === 0) {
29
            return new FileTarget($requireFile);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $requireFile does not seem to be defined for all execution paths leading up to this point.
Loading history...
30
        }
31
        // for parameter list
32
        list($command, $parameter) = self::splitParameter($command);
33
        // for method
34
        $dynmicsMethod = strpos($command, '->');
35
        $splitLength = strpos($command, '#');
36
        $methodStart = $splitLength ?: strpos($command, '::') ?: $dynmicsMethod;
37
        $dynmicsMethod;
38
        $parameter = self::buildParameter($parameter);
39
        if ($methodStart > 0) {
40
            $splitLength = $splitLength > 0 ? 1:2;
41
            $methodName = substr($command, $methodStart + $splitLength);
42
            $command = substr($command, 0, $methodStart);
43
            list($className, $constructParameter) = self::splitParameter($command);
44
            $constructParameter = self::buildParameter($constructParameter);
45
            return new MethodTarget($className, $dynmicsMethod? $constructParameter :null, $methodName, $parameter);
46
        } else {
47
            return new FunctionTarget(self::buildName($command), $parameter);
48
        }
49
    }
50
51
    private static function buildName(string $name)
52
    {
53
        if (preg_match('/^[\w\\\\\/.]+$/', $name) !== 1) {
54
            throw new \Exception(__('invaild command name: $0', $name));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            throw new \Exception(/** @scrutinizer ignore-call */ __('invaild command name: $0', $name));
Loading history...
55
        }
56
        return  str_replace(['.','/'], '\\', $name);
57
    }
58
59
    private static function splitParameter(string $command):array
60
    {
61
        $parameter = null;
62
        if (strrpos($command, ')') === strlen($command) -1) {
63
            $paramStart = strpos($command, '(');
64
            $parameter = substr($command, $paramStart + 1, strlen($command) - $paramStart - 2);
65
            $command = substr($command, 0, $paramStart);
66
        }
67
        return [$command,$parameter];
68
    }
69
70
    private static function buildParameter(?string $parameter)
71
    {
72
        if (is_null($parameter)) {
73
            return [];
74
        }
75
        return self::parseParameter($parameter);
76
    }
77
78
    protected static function parseParameter(string $param)
79
    {
80
        $param = trim($param);
81
        if (strpos($param, '=') === 0) {
82
            list($prefix, $value) = explode(':', $param, 2);
83
            if (strpos($value, ':') === 0) {
84
                $value = base64_decode(substr($value, 1));
85
            }
86
            if ($prefix ==='=j' || $prefix ==='=json') {
87
                $params = json_decode($value);
88
                if (json_last_error() === JSON_ERROR_NONE) {
89
                    return $params;
90
                }
91
                throw new \Exception(__('can not parse parameter $0', $param));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
                throw new \Exception(/** @scrutinizer ignore-call */ __('can not parse parameter $0', $param));
Loading history...
92
            } else {
93
                $params = unserialize($value);
94
                if (is_object($params)) {
95
                    return [$params];
96
                }
97
                return $params;
98
            }
99
        } else {
100
            $params = explode(',', trim($param, ','));
101
            foreach ($params as $index=>$value) {
102
                $params[$index]=trim($value);
103
            }
104
            return $params;
105
        }
106
    }
107
}
108