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.

ReflectionConst::export()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Minime\Annotations\Reflector;
4
5
use PhpParser\ParserFactory;
6
use PhpParser\Node\Stmt\Class_;
7
use PhpParser\Node\Stmt\ClassConst;
8
use PhpParser\Node\Stmt\Namespace_;
9
use PhpParser\Error as ParserError;
10
11
/**
12
 * We need this class in the annotation reader because there is no builtin constant reflector
13
 * We only implemented getDocComment because that's the only feature we need
14
 * @see \Minime\Annotations\Reader::getConstantAnnotations()
15
 */
16
class ReflectionConst implements \Reflector
17
{
18
19
    protected $classConstNode = null;
20
    protected $constNode = null;
21
22
    protected $docComment = null;
23
    private $docCommentProcessed = false;
24
25
    /**
26
     * @param  string|object        $class     fully qualified name or instance of the class
27
     * @param  string               $constName name of the constant
28
     * @throws \ReflectionException
29
     */
30
    public function __construct($class, $constName)
31
    {
32
33
        $classReflection = new \ReflectionClass($class);
34
        $className = $classReflection->getName();
35
        $fileName = $classReflection->getFileName();
36
37
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
38
        try {
39
            $stmts = $parser->parse(file_get_contents($fileName));
40
        } catch (ParserError $e) {
41
            throw new \ReflectionException("Cannot parse the class ${fileName}", 0, $e);
42
        }
43
44
        // Class can be in a namespace or at the root of the statement
45
        $classNode = $this->findClassNode($stmts);
46
        if (!$classNode) {
47
            throw new \ReflectionException("Class ${className} not found in file ${fileName}");
48
        }
49
50
51
        // Find the constant we are looking for
52
        foreach ($classNode->stmts as $classSubNode) {
53
            if ($classSubNode instanceof ClassConst) {
54
                foreach ($classSubNode->consts as $constNode) {
55
                    if ($constNode->name == $constName) {
56
                        $this->classConstNode = $classSubNode;
57
                        $this->constNode = $constNode;
58
                        break 2;
59
                    }
60
                }
61
            }
62
        }
63
64
        if (!$this->constNode) {
65
            throw new \ReflectionException("Class constant ${constName} does not exist in class ${className}");
66
        }
67
68
    }
69
70
    /**
71
     * @param $stmts
72
     * @return Class_
73
     */
74
    private function findClassNode($stmts)
75
    {
76
        foreach ($stmts as $node) {
77
            if ($node instanceof Namespace_) {
78
                return $this->findClassNode($node->stmts);
79
            } else {
80
                if ($node instanceof Class_) {
81
                    return $node;
82
                }
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getDocComment()
93
    {
94
95
        if (false === $this->docCommentProcessed) {
96
            $this->docComment = null;
97
98
99
            /**
100
             *
101
             * The first constant can have additional docblock
102
             *
103
             * /**
104
             *  * This belongs to the first
105
             *  * /
106
             * const
107
             *
108
             *      FOO = 'foo',
109
             *      BAR = 'bar'
110
             *
111
             *
112
             * const
113
             *      /**
114
             *       * This belongs to the first
115
             *       * /
116
             *      FOO = "foo";
117
             *
118
             */
119
            // Then we take every comments from the constant node
120
            // Then if it's the first of the list we tank everything from the classConstNode
121
            // (and we order it from the closest to the further
122
123
            $comments = array_reverse($this->constNode->getAttribute('comments', []));
124
            if ($this->classConstNode->consts[0] == $this->constNode) {
125
                $comments += array_reverse($this->classConstNode->getAttribute('comments', []));
126
            }
127
128
            if (count($comments) > 0) {
129
                // we can have many doc comment for one statement
130
                // We only take the closest one
131
                while ($this->docComment === null && $currentComment = current($comments)) {
132
                    if (substr($currentComment, 0, 3) == '/**') {
133
                        $this->docComment = $currentComment;
134
                    }
135
                    next($comments);
136
                }
137
138
            }
139
            $this->docCommentProcessed = true;
140
        }
141
142
        return $this->docComment;
143
    }
144
145
    /**
146
     * No need to implement it, we only need the getDocComment
147
     */
148
    public static function export()
149
    {
150
    }
151
152
    /**
153
     * No need to implement it, we only need the getDocComment
154
     */
155
    public function __toString()
156
    {
157
    }
158
}
159