Passed
Push — master ( f73620...5b8170 )
by Maxim
02:55
created

MethodsProcessor::processAccessors()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 0
dl 0
loc 7
ccs 0
cts 5
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
    private $writingAccess;
23
    private $readingAccess;
24
    private $name;
25
    private $methods = [];
26
27
    public function __construct(string $writingAccess, string $readingAccess, string $name)
28
    {
29
        $this->name = $name;
30
        $this->writingAccess = $writingAccess;
31
        $this->readingAccess = $readingAccess;
32
    }
33
34
    /**
35
     * Generates list of methods for property.
36
     *
37
     * @param array $typeTree type tree
38
     *
39
     * @return string[] methods' names
40
     */
41
    public function processMethods(array $typeTree): array
42
    {
43
        $this->methods = [];
44
        $this->processAccessors();
45
        foreach ($typeTree as $index => $type) {
46
            $class = is_int($index) ? $type : $index;
47
            foreach ((new \ReflectionClass($class))->getMethods() as $method) {
48
                if (!($method->isStatic() && $method->isPublic() && !$method->isAbstract())) {
49
                    continue;
50
                }
51
                $this->processAxessorsMethod($method);
52
            }
53
        }
54
        return $this->methods;
55
    }
56
57
    private function processAccessors(): void
58
    {
59
        if ($this->readingAccess !== '') {
60
            $this->methods[$this->readingAccess][] = 'get' . ucfirst($this->name);
61
        }
62
        if ($this->writingAccess !== '') {
63
            $this->methods[$this->writingAccess][] = 'set' . ucfirst($this->name);
64
        }
65
    }
66
67
    private function processAxessorsMethod(\ReflectionMethod $method): void
68
    {
69
        if (substr($method->name, 0, 5) == 'm_in_' && $this->writingAccess !== '') {
70
            $this->methods[$this->writingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
71
                substr($method->name, 5));
72
        } elseif (substr($method->name, 0, 6) == 'm_out_' && $this->readingAccess !== '') {
73
            $this->methods[$this->readingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
74
                substr($method->name, 6));
75
        }
76
    }
77
}