GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DefaultVariableResolver::invokeProperty()   D
last analyzed

Complexity

Conditions 9
Paths 11

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 16.9615

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
ccs 14
cts 26
cp 0.5385
rs 4.909
cc 9
eloc 25
nc 11
nop 2
crap 16.9615
1
<?php
2
/**
3
 * @link    https://github.com/old-town/old-town-workflow
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\Util;
7
8
use OldTown\PropertySet\PropertySetInterface;
9
use OldTown\Workflow\Exception\InternalWorkflowException;
10
use OldTown\Workflow\TransientVars\TransientVarsInterface;
11
12
/**
13
 * Class DefaultVariableResolver
14
 *
15
 * @package OldTown\Workflow\Util
16
 */
17
class  DefaultVariableResolver implements VariableResolverInterface
18
{
19
    /**
20
     * Регулярка для поиска перменных в аргументе
21
     *
22
     * @var string
23
     */
24
    protected $variablePatterns = '/.*?(\\${[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_.\\x7f-\\xff]*})/i';
25
26
    /**
27
     *
28
     * @param string                 $s
29
     * @param TransientVarsInterface $transientVars
30
     * @param PropertySetInterface   $ps
31
     *
32
     * @return mixed
33
     *
34
     * @throws \OldTown\Workflow\Exception\InternalWorkflowException
35
     * @throws \OldTown\PropertySet\Exception\PropertyException
36
     */
37 22
    public function translateVariables($s, TransientVarsInterface $transientVars, PropertySetInterface $ps)
38
    {
39 22
        if (0 === strpos($s, '${') && '}' === substr($s, -1) && 1 === substr_count($s, '$')) {
40 3
            $var = substr($s, 2, -1);
41
42 3
            return $this->getVariableFromMaps($var, $transientVars, $ps);
43
        }
44
45 20
        $resultVariable = $s;
46
47 20
        $matches = [];
48 20
        preg_match_all($this->variablePatterns, $s, $matches);
49
50
51 20
        if (array_key_exists(1, $matches) && is_array($matches[1])) {
52 20
            $prepareVariables = $matches[1];
53 20
            $variables = array_unique($prepareVariables);
54
55 20
            $pattern = [];
56 20
            $replacement = [];
57 20
            foreach ($variables as $variable) {
58 2
                $variableName = substr($variable, 2, -1);
59 2
                $pattern[] = $variable;
60
61 2
                $variableValue = $this->getVariableFromMaps($variableName, $transientVars, $ps);
62 2
                $replacement[] = (string)$variableValue;
63 20
            }
64
65 20
            $resultVariable = str_replace($pattern, $replacement, $s);
66 20
        }
67
68 20
        return $resultVariable;
69
    }
70
71
    /**
72
     * @param mixed                  $var
73
     * @param TransientVarsInterface $transientVars
74
     * @param PropertySetInterface   $ps
75
     *
76
     * @return mixed
77
     * @throws \OldTown\PropertySet\Exception\PropertyException
78
     * @throws \OldTown\Workflow\Exception\InternalWorkflowException
79
     */
80 5
    public function getVariableFromMaps($var, TransientVarsInterface $transientVars, PropertySetInterface $ps)
81
    {
82 5
        $firstDot = strpos($var, '.');
83 5
        $actualVar = $var;
84
85 5
        if (false !== $firstDot) {
86 2
            $actualVar = substr($var, 0, $firstDot);
87 2
        }
88
89
90 5
        $o = null;
91 5
        if ($transientVars->offsetExists($actualVar)) {
92 5
            $o = $transientVars->offsetGet($actualVar);
93 5
        }
94
95 5
        if (null === $o) {
96
            $o = $ps->getAsActualType($actualVar);
97
        }
98
99 5
        if (false !== $firstDot) {
100 2
            $property = substr($var, $firstDot + 1);
101 2
            $o = $this->getPropertyObject($o, $property);
102 2
        }
103
104 5
        return $o;
105
    }
106
107
    /**
108
     * @param mixed       $object
109
     * @param string|null $property
110
     *
111
     * @return mixed
112
     *
113
     * @throws \OldTown\Workflow\Exception\InternalWorkflowException
114
     */
115 2
    protected function getPropertyObject($object = null, $property = null)
116
    {
117 2
        if (null === $object || null === $property || !is_object($object)) {
118
            return null;
119
        }
120
121 2
        $property = (string)$property;
122
123 2
        $st = explode('.', $property);
124
125 2
        $result = $object;
126
127 2
        foreach ($st as $currentPropertyName) {
128
            try {
129 2
                $result = $this->invokeProperty($result, $currentPropertyName);
130 2
            } catch (\Exception $e) {
131
                $errMsg = sprintf(
132
                    'Ошика при получение свойства %s',
133
                    $currentPropertyName
134
                );
135
                throw new InternalWorkflowException($errMsg, $e->getCode(), $e);
136
            }
137 2
        }
138
139 2
        return $result;
140
    }
141
142
    /**
143
     * @param mixed       $obj
144
     * @param string|null $property
145
     *
146
     * @return mixed
147
     */
148 2
    protected function invokeProperty($obj, $property = null)
149
    {
150 2
        if (null === $property) {
151
            return null;
152
        }
153
154 2
        if (!settype($property, 'string')) {
155
            return null;
156
        }
157
158 2
        $property = trim($property);
159 2
        if (0 === mb_strlen($property)) {
160
            return null;
161
        }
162
163 2
        if (!is_object($obj)) {
164
            return null;
165
        }
166
167
        $methods = [
168 2
            $this->createMethodName('get', $property),
169 2
            $this->createMethodName('is', $property),
170
            $property
171 2
        ];
172
173 2
        $r = new \ReflectionObject($obj);
174 2
        foreach ($methods as $method) {
175 2
            if ($r->hasMethod($method)) {
176 2
                $result = $r->getMethod($method)->invoke($obj);
177
178 2
                return $result;
179
            }
180
        }
181
182
        if ($r->hasProperty($property)) {
183
            $rProperty = $r->getProperty($property);
184
            if ($rProperty->isPublic()) {
185
                $result = $rProperty->getValue($obj);
186
187
                return $result;
188
            }
189
        }
190
191
        return null;
192
    }
193
194
    /**
195
     * Подготавливает название метода
196
     *
197
     * @param $prefix
198
     * @param $propertyName
199
     *
200
     * @return string
201
     */
202 2
    protected function createMethodName($prefix, $propertyName)
203
    {
204 2
        return $prefix . ucfirst($propertyName);
205
    }
206
}
207