Passed
Push — master ( 884bad...29d8be )
by Maxim
03:22
created

MethodsProcessor::processMethods()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 12
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\InternalError;
12
13
/**
14
 * Class MethodsProcessor.
15
 *
16
 * Processes Axessors methods.
17
 *
18
 * @package NoOne4rever\Axessors
19
 */
20
class MethodsProcessor
21
{
22
    /** @var string access modifier for setter */
23
    private $writingAccess;
24
    /** @var string access modifier for getter */
25
    private $readingAccess;
26
    /** @var string field name */
27
    private $name;
28
    /** @var array generated methods */
29
    private $methods = [];
30
31
    /**
32
     * MethodsProcessor constructor.
33
     * 
34
     * @param string $writingAccess access modifier for setter
35
     * @param string $readingAccess access modifier for getter
36
     * @param string $name field name
37
     */
38
    public function __construct(string $writingAccess, string $readingAccess, string $name)
39
    {
40
        $this->name = $name;
41
        $this->writingAccess = $writingAccess;
42
        $this->readingAccess = $readingAccess;
43
    }
44
45
    /**
46
     * Generates list of methods for property.
47
     *
48
     * @param array $typeTree type tree
49
     *
50
     * @return string[] methods' names
51
     */
52
    public function processMethods(array $typeTree): array
53
    {
54
        $this->methods = [];
55
        $this->processAccessors();
56
        foreach ($typeTree as $index => $type) {
57
            $this->addMethods(is_int($index) ? $type : $index);
58
        }
59
        return $this->methods;
60
    }
61
62
    /**
63
     * Adds Axessors methods from type to methods list.
64
     * 
65
     * @param string $type class name
66
     */
67
    private function addMethods(string $type): void
68
    {
69
        foreach ((new \ReflectionClass($type))->getMethods() as $method) {
70
            if (!($method->isStatic() && $method->isPublic() && !$method->isAbstract())) {
71
                continue;
72
            }
73
            $this->processAxessorsMethod($method);
74
        }
75
    }
76
77
    /**
78
     * Adds getter and setter to methods list.
79
     */
80
    private function processAccessors(): void
81
    {
82
        if ($this->readingAccess !== '') {
83
            $this->methods[$this->readingAccess][] = 'get' . ucfirst($this->name);
84
        }
85
        if ($this->writingAccess !== '') {
86
            $this->methods[$this->writingAccess][] = 'set' . ucfirst($this->name);
87
        }
88
    }
89
90
    /**
91
     * Adds Axessors method to methods list.
92
     * 
93
     * @param \ReflectionMethod $method reflection
94
     */
95
    private function processAxessorsMethod(\ReflectionMethod $method): void
96
    {
97
        if (substr($method->name, 0, 5) == 'm_in_' && $this->writingAccess !== '') {
98
            $this->methods[$this->writingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
99
                substr($method->name, 5));
100
        } elseif (substr($method->name, 0, 6) == 'm_out_' && $this->readingAccess !== '') {
101
            $this->methods[$this->readingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
102
                substr($method->name, 6));
103
        }
104
    }
105
}