Passed
Push — master ( 4420f2...c422ec )
by Maxim
04:12 queued 27s
created

MethodRunner::is()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
use NoOne4rever\Axessors\Exceptions\{
12
    AxessorsError,
13
    TypeError
14
};
15
16
/**
17
 * Class MethodRunner.
18
 *
19
 * Runs a method generated by Axessors.
20
 *
21
 * @package NoOne4rever\Axessors
22
 */
23
class MethodRunner extends RunningSuit
24
{
25
    /** @var string method name */
26
    private $method;
27
28
    /**
29
     * MethodRunner constructor.
30
     *
31
     * @param int $mode running mode
32
     * @param PropertyData $data property data
33
     * @param string $class class
34
     * @param string $method method name
35
     * @param object|null $object object
36
     */
37 3
    public function __construct(int $mode, PropertyData $data, string $class, string $method, $object = null)
38
    {
39 3
        parent::__construct($mode, $data, $class, $object);
40 3
        $this->method = $method;
41 3
    }
42
43
    /**
44
     * Emulates execution of the method.
45
     *
46
     * @param array $args the arguments of the method called
47
     * @param string $file filename
48
     * @param int $line line number
49
     * 
50
     * @return mixed return value of the method called
51
     * @throws AxessorsError if conditions for executing an accessor did not pass
52
     * @throws AxessorsError if Axessors method not found
53
     */
54 3
    public function run(array $args, string $file, int $line)
55
    {
56 3
        $prefix = substr($this->method, 0, 3);
57 3
        if ($prefix == 'get') {
58 1
            $this->propertyData->reflection->setAccessible(true);
59 1
            $value = is_null($this->object) ? $this->propertyData->reflection->getValue() : $this->propertyData->reflection->getValue($this->object);
60 1
            $this->propertyData->reflection->setAccessible(false);
61 1
            return $this->executeAccessor(RunningSuit::OUTPUT_MODE, $value);
62 2
        } elseif ($prefix == 'set') {
63 2
            if (!isset($args[0])) {
64 1
                throw new AxessorsError("setter could not be called without arguments at $file:$line");
65
            }
66 1
            $value = $this->executeAccessor(RunningSuit::INPUT_MODE, $args[0]);
67 1
            $this->propertyData->reflection->setAccessible(true);
68 1
            is_null($this->object) ? $this->propertyData->reflection->setValue($value) : $this->propertyData->reflection->setValue($this->object,
69 1
                $value);
70 1
            $this->propertyData->reflection->setAccessible(false);
71 1
            return;
72
        } else {
73
            $this->method = str_replace(ucfirst($this->propertyData->getName()), 'PROPERTY', $this->method);
74
            return $this->runAxessorsMethod($args);
75
        }
76
    }
77
78
    /**
79
     * Searches and runs Axessors method.
80
     *
81
     * @param array $args method parameters
82
     * @return mixed result of function
83
     * @throws AxessorsError if requested method not found
84
     */
85
    private function runAxessorsMethod(array $args)
86
    {
87
        $this->propertyData->reflection->setAccessible(true);
88
        $value = $this->propertyData->reflection->getValue($this->object);
89
        $this->checkType($this->propertyData->getTypeTree(), $value);
90
        foreach ($this->propertyData->getTypeTree() as $type => $subType) {
91
            $type = is_int($type) ? $subType : $type;
92
            $reflection = new \ReflectionClass($type);
93
            foreach ($reflection->getMethods() as $method) {
94
                if ($this->isCalledAxessorsMethod($method)) {
95
                    return $this->executeAxessorsMethod($type, $method->name, $value, $args);
96
                }
97
            }
98
        }
99
        throw new AxessorsError("method {$this->class}::{$this->method}() not found");
100
    }
101
102
    /**
103
     * Checks if the method given is called method.
104
     * 
105
     * @param \ReflectionMethod $method method
106
     * @return bool the result of the checkout
107
     */
108
    private function isCalledAxessorsMethod(\ReflectionMethod $method): bool 
109
    {
110
        $isAccessible = $method->isStatic() && $method->isPublic() && !$method->isAbstract();
111
        $isCalled = $method->name === "m_in_$this->method" || $method->name === "m_out_$this->method";
112
        return $isAccessible && $isCalled;
113
    }
114
    
115
    /**
116
     * Executes Axessors method.
117
     *
118
     * @param string $type type name
119
     * @param string $name method name
120
     * @param mixed $value value to read/write
121
     * @param array $args arguments
122
     * @return mixed function result
123
     */
124
    private function executeAxessorsMethod(string $type, string $name, $value, array $args)
125
    {
126
        if ($name == "m_in_$this->method") {
127
            // add support for static properties
128
            $this->propertyData->reflection->setValue($this->object,
129
                call_user_func([$type, "m_in_$this->method"], $value, $args));
130
            $this->propertyData->reflection->setAccessible(false);
131
            return;
132
        } elseif ($name == "m_out_$this->method") {
133
            $result = call_user_func([$type, "m_out_$this->method"], $value, $args);
134
            $this->propertyData->reflection->setAccessible(false);
135
            return $result;
136
        }
137
    }
138
139
    /**
140
     * Executes complex accessor.
141
     *
142
     * @param int $mode running mode
143
     * @param mixed $value field or argument value
144
     * @return mixed new value
145
     * @throws AxessorsError if conditions for accessor did not pass
146
     */
147 2
    private function executeAccessor(int $mode, $value)
148
    {
149 2
        $this->checkType($this->propertyData->getTypeTree(), $value);
150 2
        $conditionsSuit = new ConditionsRunner($mode, $this->propertyData, $this->class, $this->method, $this->object);
151 2
        if ($conditionsSuit->processConditions($value)) {
152 2
            $handlersSuit = new HandlersRunner($mode, $this->propertyData, $this->class, $this->object);
153 2
            $value = $handlersSuit->executeHandlers($value);
154 2
            return $value;
155
        } else {
156
            throw new AxessorsError("conditions for {$this->class}::{$this->method}() did not pass");
157
        }
158
    }
159
160
    /**
161
     * Checks if the variable belongs to defined type.
162
     * 
163
     * @param mixed $var variable
164
     * @param string $type type
165
     * @return bool the result of the checkout
166
     */
167 2
    private function is($var, string $type): bool
168
    {
169 2
        $isExactClass = $var instanceof $type;
170 2
        $isAxessorsType = ((new \ReflectionClass($type))->hasMethod('is') && call_user_func([$type, 'is'], $var));
171 2
        return $isExactClass || $isAxessorsType;
172
    }
173
174
    /**
175
     * Checks if the type of new property's value is correct.
176
     *
177
     * @param array $typeTree all possible types
178
     * @param $var mixed new value of the property
179
     * @throws TypeError if the type of new property's value does not match the type defined in Axessors comment
180
     */
181 2
    private function checkType(array $typeTree, $var): void
182
    {
183 2
        foreach ($typeTree as $type => $subType) {
184 2
            if (is_int($type) && $this->is($var, $subType)) {
185 2
                return;
186
            } elseif (is_iterable($var) && $this->is($var, $type)) {
187
                foreach ($var as $subVar) {
188
                    $this->checkType($subType, $subVar);
189
                }
190
                return;
191
            }
192
        }
193
        throw new TypeError("not a valid type of {$this->class}::\${$this->propertyData->getName()}");
194
    }
195
}