GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ReflectionClassUse   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 2
dl 0
loc 244
ccs 85
cts 85
cp 1
rs 9
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getName() 0 4 1
A getConflictResolutions() 0 4 1
A findConflictResolutions() 0 21 4
C extractConflictsFromUseStatement() 0 53 14
B isSearchedTrait() 0 14 5
B next() 0 13 5
A export() 0 12 2
A __toString() 0 9 2
1
<?php
2
3
namespace Wingu\OctopusCore\Reflection;
4
5
use Wingu\OctopusCore\Reflection\Exceptions\InvalidArgumentException;
6
7
/**
8
 * Reflection about a class use statement.
9
 */
10
class ReflectionClassUse implements \Reflector
11
{
12
13
    /**
14
     * The name of the trait.
15
     *
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * The conflict resolutions of the trait.
22
     *
23
     * @var array
24
     */
25
    private $conflictResolutions = array();
26
27
    /**
28
     * The class where the use statement has been declared.
29
     *
30
     * @var \Wingu\OctopusCore\Reflection\ReflectionClass
31
     */
32
    protected $declaringClass;
33
34
    /**
35
     * The tokens list of the parsed PHP code.
36
     *
37
     * @var array
38
     */
39
    private $tokens = array();
40
41
    /**
42
     * The number of tokens found in the code.
43
     *
44
     * @var integer
45
     */
46
    private $tokensCount = 0;
47
48
    /**
49
     * The current token position.
50
     *
51
     * @var integer
52
     */
53
    private $tokenPos = 0;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param string $class The class name where the use statement is defined.
59
     * @param string $name The name of the trait.
60
     */
61 36
    public function __construct($class, $name)
62
    {
63 36
        $this->declaringClass = new ReflectionClass($class);
64 36
        $this->name = $name;
65
66 36
        $this->findConflictResolutions();
67 33
        $this->tokens = array();
68 33
        $this->tokensCount = $this->tokenPos = 0;
69 33
    }
70
71
    /**
72
     * Get the name of the trait.
73
     *
74
     * @return string
75
     */
76 24
    public function getName()
77
    {
78 24
        return $this->name;
79
    }
80
81
    /**
82
     * Get the conflict resolutions.
83
     *
84
     * @return array
85
     */
86 24
    public function getConflictResolutions()
87
    {
88 24
        return $this->conflictResolutions;
89
    }
90
91
    /**
92
     * Find the conflict resolutions of a trait.
93
     *
94
     * @throws \Wingu\OctopusCore\Reflection\Annotation\Exceptions\InvalidArgumentException If the trait is not found.
95
     */
96 39
    protected function findConflictResolutions()
97
    {
98 39
        $contents = '<?php' . PHP_EOL . $this->declaringClass->getBody();
99 39
        $this->tokens = token_get_all($contents);
100 39
        $this->tokensCount = count($this->tokens);
101
102 39
        while (($token = $this->next()) !== null) {
103 39
            if ($token[0] === T_USE) {
104 39
                $conflicts = $this->extractConflictsFromUseStatement();
105 39
                if ($conflicts !== null) {
106 33
                    $this->conflictResolutions = explode(';', implode('', $conflicts));
107 33
                    $this->conflictResolutions = array_map('trim', $this->conflictResolutions);
108 33
                    $this->conflictResolutions = array_filter($this->conflictResolutions);
109
110 33
                    return;
111
                }
112 8
            }
113 13
        }
114
115 6
        throw new InvalidArgumentException('Could not find the trait "' . $this->name . '".');
116
    }
117
118
    /**
119
     * Extract the conflicts from the current use statement.
120
     *
121
     * @return array
122
     */
123 39
    private function extractConflictsFromUseStatement()
124
    {
125 39
        $class = '';
126 39
        $conflicts = array();
127 39
        $inConflicts = false;
128 39
        while (($token = $this->next($inConflicts)) !== null) {
129 39
            if ($token !== '}' && $inConflicts === true) {
130 15
                if (is_string($token) === true) {
131 15
                    $conflicts[] = trim($token);
132 5
                } else {
133 15
                    $conflicts[] = $token[1];
134
                }
135
136 15
                continue;
137
            }
138
139 39
            if (($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR) === true) {
140 36
                $class .= $token[1];
141 12
            } else {
142 39
                if ($token === ',') {
143 33
                    if ($this->isSearchedTrait($class) === true) {
144 9
                        return $conflicts;
145
                    }
146
147 24
                    $class = '';
148 8
                } else {
149 30
                    if ($token === ';') {
150 24
                        if ($this->isSearchedTrait($class) === true) {
151 15
                            return $conflicts;
152
                        } else {
153 21
                            return null;
154
                        }
155
                    } else {
156 18
                        if ($token === '{') {
157 15
                            $inConflicts = true;
158 5
                        } else {
159 18
                            if ($token === '}') {
160 15
                                if ($this->isSearchedTrait($class) === true) {
161 9
                                    return $conflicts;
162
                                } else {
163 6
                                    return null;
164
                                }
165
                            } else {
166 3
                                break;
167
                            }
168
                        }
169
                    }
170
                }
171
            }
172 12
        }
173
174 3
        return null;
175
    }
176
177
    /**
178
     * Check if the found trait name is the one that we need to reflect.
179
     *
180
     * @param string $name The name of the trait found.
181
     * @return boolean
182
     */
183 36
    private function isSearchedTrait($name)
184
    {
185 36
        if ($this->name === $name) {
186 6
            return true;
187
        }
188
189 36
        if (strpos($name, '\\') === 0) {
190 12
            return $this->name === $name || '\\' . $this->name === $name;
191
        }
192
193 36
        $name = $this->declaringClass->getNamespaceName() . '\\' . $name;
194
195 36
        return $this->name === $name || $this->name === '\\' . $name;
196
    }
197
198
    /**
199
     * Return the next token that is not a whitespace or comment.
200
     *
201
     * @param boolean $includeWhiteSpace Flag if the whitespace should also be returned.
202
     * @return mixed
203
     */
204 39
    private function next($includeWhiteSpace = false)
205
    {
206 39
        for ($i = $this->tokenPos; $i < $this->tokensCount; $i++) {
207 39
            $this->tokenPos++;
208 39
            if (($includeWhiteSpace === false && $this->tokens[$i][0] === T_WHITESPACE) || $this->tokens[$i][0] === T_COMMENT) {
209 39
                continue;
210
            }
211
212 39
            return $this->tokens[$i];
213
        }
214
215 6
        return null;
216
    }
217
218
    /**
219
     * Export the current use statement reflection.
220
     *
221
     * @param string $className The class name where the use statement is defined.
222
     * @param string $name The name of the trait.
223
     * @param boolean $return Flag if the export should be returned or not.
224
     * @return string
225
     */
226 9
    public static function export($className, $name, $return = false)
227
    {
228 9
        $export = new self($className, $name);
229 9
        $export = (string)$export;
230 9
        if ($return === true) {
231 6
            return $export;
232
        } else {
233 3
            echo $export;
234
235 3
            return null;
236
        }
237
    }
238
239
    /**
240
     * Return the string representation of the ReflectionConstant object.
241
     *
242
     * @return string
243
     */
244 9
    public function __toString()
245
    {
246 9
        $return = 'ClassUse [ trait ' . $this->name . ' ]';
247 9
        if (count($this->conflictResolutions) > 0) {
248 3
            return $return . ' {' . PHP_EOL . implode(';' . PHP_EOL, $this->conflictResolutions) . ';' . PHP_EOL . '}';
249
        } else {
250 6
            return $return . ' { }';
251
        }
252
    }
253
}
254