Passed
Push — master ( c2e0e3...51d343 )
by Maxim
02:05 queued 21s
created

HandlersSuit   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C executeHandlers() 0 29 16
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
                $handler = str_replace('\\`', '`', substr($handler, 1, strlen($handler) - 2));
35
                if (is_null($this->object)) {
36
                    $value = call_user_func("{$this->class}::__axessorsExecute", $handler, $value, false);
37
                } else {
38
                    $value = $this->object->__axessorsExecute($handler, $value, false);
39
                }
40
            } else {
41
                foreach ($this->propertyData->getTypeTree() as $type => $subType) {
42
                    $reflection = new \ReflectionClass('\Axessors\Types\\' . is_int($type) ? $subType : $type);
43
                    foreach ($reflection->getMethods() as $method) {
44
                        $isAccessible = $method->isPublic() && $method->isStatic() && !$method->isAbstract();
45
                        $isThat = call_user_func([$reflection->name, 'is'], $value);
46
                        if ($isAccessible && $isThat && "h_$handler" == $method->name) {
47
                            $value = call_user_func([$reflection->name, $method->name], $value, false);
48
                            continue 3;
49
                        } elseif ($isAccessible && "h_$handler" == $method->name && !$isThat) {
50
                            continue 3;
51
                        }
52
                    }
53
                }
54
                throw new OopError("property {$this->class}::\${$this->propertyData->getName()} does not have handler \"$handler\"");
55
            }
56
        }
57
        return $value;
58
    }
59
}