HierarchyLexer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 86
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAxsMethod() 0 3 1
A getMethods() 0 19 4
A __construct() 0 22 3
A getMethod() 0 6 1
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 2
    public function __construct(\ReflectionClass $reflection)
34
    {
35 2
        parent::__construct($reflection);
36 2
        if ($reflection->isInterface()) {
37 2
            $this->expectations = [
38
                '#',
39
                'public',
40
                'function',
41
                '[a-zA-Z_][a-zA-Z0-9_]*'
42
            ];
43 2
            $this->requiredItems = [0, 1, 2, 3];
44 2
        } elseif ($reflection->isAbstract()) {
45 2
            $this->expectations = [
46
                '#',
47
                'abstract',
48
                '(public|protected)',
49
                'function',
50
                '[a-zA-Z_][a-zA-Z0-9_]*'
51
            ];
52 2
            $this->requiredItems = [0, 1, 2, 3, 4];
53
        } else {
54
            throw new InternalError("\"{$reflection->name}\" is not an interface or abstract class");
55
        }
56 2
    }
57
58
    /**
59
     * Returns Axessors methods' names.
60
     *
61
     * @return string[] Axessors methods' names
62
     */
63 2
    public function getMethods(): array
64
    {
65 2
        $methods = [];
66 2
        for ($i = $this->startLine; $i <= $this->endLine; ++$i) {
67 2
            $this->readLine();
68 2
            if (!$this->isAxsMethod()) {
69 2
                continue;
70
            }
71 2
            $method = $this->getMethod();
72 2
            if ($this->reflection->isInterface()) {
73 2
                $accessModifier = $method[1];
74 2
                $methodName = $method[3];
75
            } else {
76 2
                $accessModifier = $method[2];
77 2
                $methodName = $method[4];
78
            }
79 2
            $methods[$accessModifier][] = $methodName;
80
        }
81 2
        return $methods;
82
    }
83
84
    /**
85
     * Returns Axessors method slit into array of tokens.
86
     *
87
     * @return string[] found tokens
88
     */
89 2
    private function getMethod(): array
90
    {
91 2
        return $this->parse(
92 2
            $this->currentLine,
93 2
            $this->expectations,
94 2
            $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 2
    private function isAxsMethod(): bool
104
    {
105 2
        return (bool)preg_match('{^\s*#}', $this->currentLine);
106
    }
107
}
108