Completed
Push — master ( f8fbf5...e75b27 )
by Alexey
02:21
created

ReflectionUseStatements::createRawUseStatements()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 31
cts 31
cp 1
rs 6.4055
c 0
b 0
f 0
cc 14
eloc 34
nc 12
nop 0
crap 14

How to fix   Long Method    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
declare(strict_types = 1);
4
5
namespace Reflection;
6
7
use \ReflectionClass;
8
use \RuntimeException;
9
10
/**
11
 * Class ReflectionUseStatements
12
 * @package UsesReflection
13
 */
14
class ReflectionUseStatements extends ReflectionClass {
15
16
    /**
17
     * @const string
18
     */
19
    const CLASS_STATEMENT_TYPE = 'class';
20
21
    /**
22
     * @const string
23
     */
24
    const ALIAS_STATEMENT_TYPE = 'alias';
25
26
    /**
27
     * @var UseStatements
28
     */
29
    private $useStatements;
30
31
    /**
32
     * @var boolean
33
     */
34
    private $isUseStatementsParsed = false;
35
36
    /**
37
     * @return UseStatements
38
     */
39 1
    public function getUseStatements(): UseStatements {
40 1
        if ($this->isUseStatementsNotParsed()) {
41 1
            $this->useStatements = $this->createUseStatements();
42
        }
43
44 1
        return $this->useStatements;
45
    }
46
47
    /**
48
     * @param string $class
49
     * @return boolean
50
     */
51 1
    public function hasUseStatement(string $class): bool {
52 1
        return $this->getUseStatements()
53 1
            ->hasClass($class);
54
    }
55
56
    /**
57
     * @return bool
58
     */
59 1
    public function isNotUserDefined(): bool {
60 1
        return !$this->isUserDefined();
61
    }
62
63
    /**
64
     * @return boolean
65
     */
66 1
    private function isUseStatementsNotParsed(): bool {
67 1
        return !$this->isUseStatementsParsed;
68
    }
69
70
    /**
71
     * @return ReflectionUseStatements
72
     */
73 1
    private function setUseStatementsIsParsed(): ReflectionUseStatements {
74 1
        $this->isUseStatementsParsed = true;
75
76 1
        return $this;
77
    }
78
79
    /**
80
     * @return UseStatements
81
     */
82 1
    private function createUseStatements(): UseStatements {
83 1
        if ($this->isNotUserDefined()) {
84 1
            throw new RuntimeException('Can get use statements from user defined classes only.');
85
        }
86
87 1
        $this->setUseStatementsIsParsed();
88
89 1
        $rawUseStatements = $this->createRawUseStatements();
90
91 1
        $useStatements = new UseStatements();
92
93 1
        foreach ($rawUseStatements as $rawUseStatement) {
94 1
            $useStatements->add(new UseStatement(
95 1
                $rawUseStatement[self::CLASS_STATEMENT_TYPE],
96 1
                $rawUseStatement[self::ALIAS_STATEMENT_TYPE]
97
            ));
98
        }
99
100 1
        return $useStatements;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 1
    private function readFileSource(): string {
107 1
        $file = fopen($this->getFileName(), 'r');
108 1
        $line = 0;
109 1
        $source = '';
110
111 1
        while (!feof($file)) {
112 1
            ++$line;
113
114 1
            if ($line >= $this->getStartLine()) {
115 1
                break;
116
            }
117
118 1
            $source .= fgets($file);
119
        }
120
121 1
        fclose($file);
122
123 1
        return $source;
124
    }
125
126
    /**
127
     * @return array
128
     */
129 1
    private function createRawUseStatements(): array {
130 1
        $tokens = token_get_all($this->readFileSource());
131
132 1
        $useStatementIsBuilding = false;
133 1
        $statementType = '';
134 1
        $useStatements = [];
135
        $useStatement = [
136 1
            self::CLASS_STATEMENT_TYPE => '',
137 1
            self::ALIAS_STATEMENT_TYPE => ''
138
        ];
139
140 1
        foreach ($tokens as $token) {
141 1
            if (is_array($token)) {
142 1
                if ($token[0] === T_USE) {
143 1
                    $useStatementIsBuilding = true;
144
145 1
                    $statementType = self::CLASS_STATEMENT_TYPE;
146
147 1
                    continue;
148
                }
149
150 1
                if ($token[0] === T_AS) {
151 1
                    $statementType = self::ALIAS_STATEMENT_TYPE;
152
153 1
                    continue;
154
                }
155
156 1
                if ($useStatementIsBuilding && $statementType) {
157 1
                    switch ($token[0]) {
158 1
                        case T_NS_SEPARATOR:
159 1
                        case T_STRING:
160 1
                            $useStatement[$statementType] .= $token[1];
161
162 1
                            break;
163
164
                    }
165
                }
166
            } else {
167 1
                if (($token === ';' || $token === ',') && $useStatementIsBuilding) {
168 1
                    $useStatements[] = $useStatement;
169
170
                    $useStatement = [
171 1
                        self::CLASS_STATEMENT_TYPE => '',
172 1
                        self::ALIAS_STATEMENT_TYPE => ''
173
                    ];
174
175 1
                    if ($token === ';') {
176 1
                        $useStatementIsBuilding = false;
177
                    }
178
179 1
                    if ($token === ',') {
180 1
                        $statementType = self::CLASS_STATEMENT_TYPE;
181
                    }
182
                }
183
            }
184
        }
185
186 1
        return $useStatements;
187
    }
188
189
}