Completed
Push — master ( 09273f...7566be )
by Maxim
03:11
created

HierarchyLexer::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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 HierarchyLexer
15
 *
16
 * Searches for Axessors methods in abstract classes and interfaces.
17
 *
18
 * @package NoOne4rever\Axessors
19
 */
20
class HierarchyLexer extends Lexer
21
{
22
    /** @var string[] patterns of necessary tokens */
23
    private $expectations;
24
    /** @var int[] numbers of required tokens */
25
    private $requiredItems;
26
27
    /**
28
     * HierarchyLexer constructor.
29
     *
30
     * @param \ReflectionClass $reflection class' reflection
31
     * @throws InternalError if given class is not abstract or is not an interface
32
     */
33 1
    public function __construct(\ReflectionClass $reflection)
34
    {
35 1
        parent::__construct($reflection);
36 1
        if ($reflection->isInterface()) {
37 1
            $this->expectations = [
38
                '{^#}',
39
                '{^public}',
40
                '{^function}',
41
                '{^[a-zA-Z_][a-zA-Z0-9_]*}'
42
            ];
43 1
            $this->requiredItems = [0, 1, 2, 3];
44 1
        } elseif ($reflection->isAbstract()) {
45 1
            $this->expectations = [
46
                '{^#}',
47
                '{^abstract}',
48
                '{^(public|protected)}',
49
                '{^function}',
50
                '{^[a-zA-Z_][a-zA-Z0-9_]*}'
51
            ];
52 1
            $this->requiredItems = [0, 1, 2, 3, 4];
53
        } else {
54
            throw new InternalError("\"{$reflection->name}\" is not an interface or abstract class");
55
        }
56 1
    }
57
58
    /**
59
     * Returns Axessors methods' names.
60
     *
61
     * @return string[] Axessors methods' names
62
     */
63 1
    public function getMethods(): array
64
    {
65 1
        $methods = [];
66 1
        for ($i = $this->startLine; $i <= $this->endLine; ++$i) {
67 1
            $this->readLine();
68 1
            if (!$this->isAxsMethod()) {
69 1
                continue;
70
            }
71 1
            $method = $this->getMethod();
72 1
            if ($this->reflection->isInterface()) {
73 1
                $accessModifier = $method[1];
74 1
                $methodName = $method[3];
75
            } else {
76 1
                $accessModifier = $method[2];
77 1
                $methodName = $method[4];
78
            }
79 1
            $methods[$accessModifier][] = $methodName;
80
        }
81 1
        return $methods;
82
    }
83
84
    /**
85
     * Returns Axessors method slit into array of tokens.
86
     *
87
     * @return string[] found tokens
88
     */
89 1
    private function getMethod(): array
90
    {
91 1
        return $this->parse(
92 1
            $this->currentLine,
93 1
            $this->expectations,
94 1
            $this->requiredItems
95
        );
96
    }
97
98
    /**
99
     * Checks if the line of code given is an Axessors method declaration.
100
     *
101
     * @return bool result of the checkout
102
     */
103 1
    private function isAxsMethod(): bool
104
    {
105 1
        return (bool)preg_match('{^\s*#}', $this->currentLine);
106
    }
107
}
108