Passed
Push — master ( 781476...75fa6b )
by Maxim
03:03
created

HandlersSuit   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A executeHandlers() 0 11 4
B runStandardHandler() 0 13 9
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\OopError;
12
13
/**
14
 * Class HandlersSuit.
15
 * 
16
 * Processes Axessors handlers.
17
 * 
18
 * @package NoOne4rever\Axessors
19
 */
20
class HandlersSuit extends RunningSuit
21
{
22
    /**
23
     * Executes handlers defined in the Axessors comment.
24
     *
25
     * @param $value mixed value of the property
26
     * @return mixed new value of the property
27
     * @throws OopError if the property does not have one of the handlers defined in the Axessors comment
28
     */
29
    public function executeHandlers($value)
30
    {
31
        $handlers = $this->mode == RunningSuit::OUTPUT_MODE ? $this->propertyData->getOutputHandlers() : $this->propertyData->getInputHandlers();
32
        foreach ($handlers as $handler) {
33
            if (strpos($handler, '`') !== false) {
34
                $value = $this->executeInjectedString($handler, $value, false);
35
            } else {
36
                $value = $this->runStandardHandler($handler, $value);
37
            }
38
        }
39
        return $value;
40
    }
41
42
    /**
43
     * Runs Axessors handler.
44
     * 
45
     * @param string $handler handler name
46
     * @param mixed $value the value to process
47
     * @return mixed the result of handler execution 
48
     * @throws OopError if the called handler not found
49
     */
50
    private function runStandardHandler(string $handler, $value)
51
    {
52
        foreach ($this->propertyData->getTypeTree() as $type => $subType) {
53
            $reflection = new \ReflectionClass('\Axessors\Types\\' . is_int($type) ? $subType : $type);
54
            foreach ($reflection->getMethods() as $method) {
55
                $isAccessible = $method->isPublic() && $method->isStatic() && !$method->isAbstract();
56
                $isThat = call_user_func([$reflection->name, 'is'], $value);
57
                if ($isAccessible && $isThat && "h_$handler" == $method->name) {
58
                    return call_user_func([$reflection->name, $method->name], $value, false);
59
                }
60
            }
61
        }
62
        throw new OopError("property {$this->class}::\${$this->propertyData->getName()} does not have handler \"$handler\"");
63
    }
64
}