Passed
Push — master ( 52c22c...f73620 )
by Maxim
03:05 queued 11s
created

MethodsProcessor::processMethods()   C

Complexity

Conditions 15
Paths 20

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
eloc 21
nc 20
nop 1
dl 0
loc 32
ccs 0
cts 21
cp 0
crap 240
rs 5.0504
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    
26
    public function __construct(string $writingAccess, string $readingAccess, string $name)
27
    {
28
        $this->name = $name;
29
        $this->writingAccess = $writingAccess;
30
        $this->readingAccess = $readingAccess;
31
    }
32
33
    /**
34
     * Generates list of methods for property.
35
     *
36
     * @param array $typeTree type tree
37
     *
38
     * @return string[] methods' names
39
     */
40
    public function processMethods(array $typeTree): array
41
    {
42
        $methods = [];
43
44
        if ($this->readingAccess !== '') {
45
            $methods[$this->readingAccess][] = 'get' . ucfirst($this->name);
46
        }
47
        if ($this->writingAccess !== '') {
48
            $methods[$this->writingAccess][] = 'set' . ucfirst($this->name);
49
        }
50
51
        foreach ($typeTree as $index => $type) {
52
            $class = is_int($index) ? $type : $index;
53
            try {
54
                class_exists($class);
55
            } catch (InternalError $error) {
56
                continue;
57
            }
58
            foreach ((new \ReflectionClass($class))->getMethods() as $method) {
59
                $isAccessible = $method->isStatic() && $method->isPublic() && !$method->isAbstract();
60
                if ($isAccessible && preg_match('{^m_(in|out)_.*?PROPERTY.*}', $method->name)) {
61
                    if (substr($method->name, 0, 5) == 'm_in_' && $this->writingAccess !== '') {
62
                        $methods[$this->writingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
63
                            substr($method->name, 5));
64
                    } elseif (substr($method->name, 0, 6) == 'm_out_' && $this->readingAccess !== '') {
65
                        $methods[$this->readingAccess][] = str_replace('PROPERTY', ucfirst($this->name),
66
                            substr($method->name, 6));
67
                    }
68
                }
69
            }
70
        }
71
        return $methods;
72
    }
73
}