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.

ReflectionConstant   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 178
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDeclaringClass() 0 4 1
A getValue() 0 4 1
A getName() 0 4 1
A export() 0 12 2
A __toString() 0 4 1
A getDocComment() 0 9 2
B getDocCommentFromFile() 0 27 5
C getCurrentConstantKeyFromClassTokens() 0 22 7
1
<?php
2
3
namespace Wingu\OctopusCore\Reflection;
4
5
/**
6
 * Reflection about a class constant.
7
 */
8
class ReflectionConstant implements \Reflector
9
{
10
11
    use ReflectionDocCommentTrait;
12
13
    /**
14
     * The name of the constant.
15
     *
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * The value of the constant.
22
     *
23
     * @var mixed
24
     */
25
    protected $value;
26
27
    /**
28
     * The class where the constant has been declared.
29
     *
30
     * @var \Wingu\OctopusCore\Reflection\ReflectionClass
31
     */
32
    protected $declaringClass;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string $class The name of the class where the constant has been declared.
38
     * @param string $name The name of the constant to reflect.
39
     */
40 30
    public function __construct($class, $name)
41
    {
42 30
        $this->declaringClass = new ReflectionClass($class);
43 30
        $this->name = $name;
44 30
        $this->value = $this->declaringClass->getConstant($name);
45 30
    }
46
47
    /**
48
     * Get a reflection of the class where the constant has been declared.
49
     *
50
     * @return \Wingu\OctopusCore\Reflection\ReflectionClass
51
     */
52 12
    public function getDeclaringClass()
53
    {
54 12
        return $this->declaringClass;
55
    }
56
57
    /**
58
     * Get the value of the constant.
59
     *
60
     * @return mixed
61
     */
62 3
    public function getValue()
63
    {
64 3
        return $this->value;
65
    }
66
67
    /**
68
     * Get the name of the constant.
69
     *
70
     * @return string
71
     */
72 15
    public function getName()
73
    {
74 15
        return $this->name;
75
    }
76
77
    /**
78
     * Export the current constant reflection.
79
     *
80
     * @param string $className The class name where the constant is defined.
81
     * @param string $constantName The name of the constant.
82
     * @param boolean $return Flag if the export should be returned or not.
83
     * @return string
84
     */
85 6
    public static function export($className, $constantName, $return = false)
86
    {
87 6
        $export = new self($className, $constantName);
88 6
        $export = (string)$export;
89 6
        if ($return === true) {
90 3
            return $export;
91
        } else {
92 3
            echo $export;
93
94 3
            return null;
95
        }
96
    }
97
98
    /**
99
     * Return the string representation of the ReflectionConstant object.
100
     *
101
     * @return string
102
     */
103 6
    public function __toString()
104
    {
105 6
        return 'Constant [ ' . gettype($this->value) . ' const ' . $this->getName() . ' ] { ' . $this->value . ' }';
106
    }
107
108
    /**
109
     * Return the string containing the current constant comment or false if there's no comment.
110
     *
111
     * @return string
112
     */
113 9
    public function getDocComment()
114
    {
115 9
        $fileName = $this->getDeclaringClass()->getFileName();
116 9
        if ($fileName === false) {
117 3
            return false;
118
        } else {
119 6
            return $this->getDocCommentFromFile($fileName);
120
        }
121
    }
122
123
    /**
124
     * Returns the doc comment from an existing filename or false if empty.
125
     *
126
     * @param string $fileName an existing filename
127
     * @return string
128
     */
129 6
    private function getDocCommentFromFile($fileName)
130
    {
131 6
        $lines = file($fileName, FILE_IGNORE_NEW_LINES);
132
133 6
        $declaringClassStartLine = $this->getDeclaringClass()->getStartLine() - 1;
134 6
        $declaringClassLength = $this->getDeclaringClass()->getEndLine() - $declaringClassStartLine + 1;
135
136 6
        $currentClassLines = array_slice($lines, $declaringClassStartLine, $declaringClassLength, true);
137
138
        // Need the php open tag to tokenize the class.
139 6
        $tokens = token_get_all("<?php\n" . implode("\n", $currentClassLines));
140
141 6
        $return = false;
142 6
        $constDeclarationKey = $this->getCurrentConstantKeyFromClassTokens($tokens);
143
144
        // Now we have the key value of the constant declaration, we have to pick up the comment before the declaration (if it exists).
145 6
        for ($constDeclarationKey--; $constDeclarationKey > 0; $constDeclarationKey--) {
146 6
            if ($tokens[$constDeclarationKey][0] !== T_WHITESPACE && $tokens[$constDeclarationKey][0] !== T_DOC_COMMENT) {
147 3
                break;
148 6
            } elseif ($tokens[$constDeclarationKey][0] === T_DOC_COMMENT) {
149 3
                $return = $tokens[$constDeclarationKey][1];
150 3
                break;
151
            }
152 2
        }
153
154 6
        return $return;
155
    }
156
157
    /**
158
     * Returns the current constant declaration key in the list of given tokens. Returns 0 if not found.
159
     *
160
     * @param array $tokens the array of tokens (see http://www.php.net/manual/en/ref.tokenizer.php)
161
     * @return int
162
     */
163 6
    private function getCurrentConstantKeyFromClassTokens($tokens)
164
    {
165 6
        $parsingStateConstDeclarationFound = false;
166 6
        $constDeclarationKey = 0;
167
168 6
        foreach ($tokens as $key => $token) {
169 6
            if (is_array($token) === true && $token[0] === T_CONST) {
170 6
                $parsingStateConstDeclarationFound = true;
171 6
                $constDeclarationKey = $key;
172 2
            }
173
174 6
            if ($parsingStateConstDeclarationFound === true && $token[0] === T_STRING) {
175 6
                if ($token[1] === $this->getName()) {
176 6
                    break;
177
                } else {
178 5
                    $parsingStateConstDeclarationFound = false;
179
                }
180 1
            }
181 2
        }
182
183 6
        return $constDeclarationKey;
184
    }
185
}
186