Passed
Push — master ( 057ebb...2912a5 )
by Maxim
03:10
created

HandlersSuit   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 45
Duplicated Lines 11.11 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 5
loc 45
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A runInjectedHandler() 4 7 2
B runStandardHandler() 0 13 9
A executeHandlers() 0 11 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->runInjectedHandler($handler, $value);
35
            } else {
36
                $value = $this->runStandardHandler($handler, $value);
37
            }
38
        }
39
        return $value;
40
    }
41
    
42
    private function runInjectedHandler(string $handler, $value)
43
    {
44
        $handler = str_replace('\\`', '`', substr($handler, 1, strlen($handler) - 2));
45 View Code Duplication
        if (is_null($this->object)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            return call_user_func("{$this->class}::__axessorsExecute", $handler, $value, false);
47
        } else {
48
            return $this->object->__axessorsExecute($handler, $value, false);
49
        }        
50
    }
51
    
52
    private function runStandardHandler(string $handler, $value)
53
    {
54
        foreach ($this->propertyData->getTypeTree() as $type => $subType) {
55
            $reflection = new \ReflectionClass('\Axessors\Types\\' . is_int($type) ? $subType : $type);
56
            foreach ($reflection->getMethods() as $method) {
57
                $isAccessible = $method->isPublic() && $method->isStatic() && !$method->isAbstract();
58
                $isThat = call_user_func([$reflection->name, 'is'], $value);
59
                if ($isAccessible && $isThat && "h_$handler" == $method->name) {
60
                    return call_user_func([$reflection->name, $method->name], $value, false);
61
                }
62
            }
63
        }
64
        throw new OopError("property {$this->class}::\${$this->propertyData->getName()} does not have handler \"$handler\"");
65
    }
66
}