• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / utils / 22834359508

09 Mar 2026 01:14AM UTC coverage: 93.125%. Remained the same
22834359508

push

github

dg
added CLAUDE.md

2086 of 2240 relevant lines covered (93.13%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.41
/src/Utils/Reflection.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Utils;
9

10
use Nette;
11
use function constant, current, defined, end, explode, file_get_contents, implode, ltrim, next, ord, strrchr, strtolower, substr;
12
use const T_AS, T_CLASS, T_COMMENT, T_CURLY_OPEN, T_DOC_COMMENT, T_DOLLAR_OPEN_CURLY_BRACES, T_ENUM, T_INTERFACE, T_NAME_FULLY_QUALIFIED, T_NAME_QUALIFIED, T_NAMESPACE, T_NS_SEPARATOR, T_STRING, T_TRAIT, T_USE, T_WHITESPACE, TOKEN_PARSE;
13

14

15
/**
16
 * PHP reflection helpers.
17
 */
18
final class Reflection
19
{
20
        use Nette\StaticClass;
21

22
        /** @deprecated use Nette\Utils\Validators::isBuiltinType() */
23
        public static function isBuiltinType(string $type): bool
24
        {
25
                return Validators::isBuiltinType($type);
×
26
        }
27

28

29
        #[\Deprecated('use Nette\Utils\Validators::isClassKeyword()')]
30
        public static function isClassKeyword(string $name): bool
31
        {
32
                return Validators::isClassKeyword($name);
×
33
        }
34

35

36
        /**
37
         * Returns the default value of a parameter. Resolves constants and class constants used as default values.
38
         * @throws \ReflectionException if the constant cannot be resolved
39
         */
40
        public static function getParameterDefaultValue(\ReflectionParameter $param): mixed
1✔
41
        {
42
                if ($param->isDefaultValueConstant()) {
1✔
43
                        $const = $orig = $param->getDefaultValueConstantName() ?? throw new Nette\ShouldNotHappenException;
1✔
44
                        $pair = explode('::', $const);
1✔
45
                        if (isset($pair[1])) {
1✔
46
                                $pair[0] = Type::resolve($pair[0], $param);
1✔
47
                                try {
48
                                        $rcc = new \ReflectionClassConstant($pair[0], $pair[1]);
1✔
49
                                } catch (\ReflectionException $e) {
1✔
50
                                        $name = self::toString($param);
1✔
51
                                        throw new \ReflectionException("Unable to resolve constant $orig used as default value of $name.", 0, $e);
1✔
52
                                }
53

54
                                return $rcc->getValue();
1✔
55

56
                        } elseif (!defined($const)) {
1✔
57
                                $const = substr((string) strrchr($const, '\\'), 1);
1✔
58
                                if (!defined($const)) {
1✔
59
                                        $name = self::toString($param);
1✔
60
                                        throw new \ReflectionException("Unable to resolve constant $orig used as default value of $name.");
1✔
61
                                }
62
                        }
63

64
                        return constant($const);
1✔
65
                }
66

67
                return $param->getDefaultValue();
×
68
        }
69

70

71
        /**
72
         * Returns a reflection of a class or trait that contains a declaration of given property. Property can also be declared in the trait.
73
         * @return \ReflectionClass<object>
74
         */
75
        public static function getPropertyDeclaringClass(\ReflectionProperty $prop): \ReflectionClass
1✔
76
        {
77
                foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
1✔
78
                        if ($trait->hasProperty($prop->name)
1✔
79
                                // doc-comment guessing as workaround for insufficient PHP reflection
80
                                && $trait->getProperty($prop->name)->getDocComment() === $prop->getDocComment()
1✔
81
                        ) {
82
                                return self::getPropertyDeclaringClass($trait->getProperty($prop->name));
1✔
83
                        }
84
                }
85

86
                return $prop->getDeclaringClass();
1✔
87
        }
88

89

90
        /**
91
         * Returns a reflection of a method that contains a declaration of $method.
92
         * Usually, each method is its own declaration, but the body of the method can also be in the trait and under a different name.
93
         */
94
        public static function getMethodDeclaringMethod(\ReflectionMethod $method): \ReflectionMethod
1✔
95
        {
96
                // file & line guessing as workaround for insufficient PHP reflection
97
                $decl = $method->getDeclaringClass();
1✔
98
                if ($decl->getFileName() === $method->getFileName()
1✔
99
                        && $decl->getStartLine() <= $method->getStartLine()
1✔
100
                        && $decl->getEndLine() >= $method->getEndLine()
1✔
101
                ) {
102
                        return $method;
1✔
103
                }
104

105
                $hash = [$method->getFileName(), $method->getStartLine(), $method->getEndLine()];
1✔
106
                if (($alias = $decl->getTraitAliases()[$method->name] ?? null)
1✔
107
                        && ($m = new \ReflectionMethod(...explode('::', $alias, 2)))
1✔
108
                        && $hash === [$m->getFileName(), $m->getStartLine(), $m->getEndLine()]
1✔
109
                ) {
110
                        return self::getMethodDeclaringMethod($m);
1✔
111
                }
112

113
                foreach ($decl->getTraits() as $trait) {
1✔
114
                        if ($trait->hasMethod($method->name)
1✔
115
                                && ($m = $trait->getMethod($method->name))
1✔
116
                                && $hash === [$m->getFileName(), $m->getStartLine(), $m->getEndLine()]
1✔
117
                        ) {
118
                                return self::getMethodDeclaringMethod($m);
1✔
119
                        }
120
                }
121

122
                return $method;
×
123
        }
124

125

126
        /**
127
         * Finds out if reflection has access to PHPdoc comments. Comments may not be available due to the opcode cache.
128
         */
129
        public static function areCommentsAvailable(): bool
130
        {
131
                static $res;
×
132
                return $res ?? $res = (bool) (new \ReflectionMethod(self::class, __FUNCTION__))->getDocComment();
×
133
        }
134

135

136
        /**
137
         * Returns a human-readable string representation of a reflection object.
138
         */
139
        public static function toString(\Reflector $ref): string
1✔
140
        {
141
                if ($ref instanceof \ReflectionClass) {
1✔
142
                        return $ref->name;
1✔
143
                } elseif ($ref instanceof \ReflectionMethod) {
1✔
144
                        return $ref->getDeclaringClass()->name . '::' . $ref->name . '()';
1✔
145
                } elseif ($ref instanceof \ReflectionFunction) {
1✔
146
                        return $ref->isAnonymous() ? '{closure}()' : $ref->name . '()';
1✔
147
                } elseif ($ref instanceof \ReflectionProperty) {
1✔
148
                        return self::getPropertyDeclaringClass($ref)->name . '::$' . $ref->name;
1✔
149
                } elseif ($ref instanceof \ReflectionParameter) {
1✔
150
                        return '$' . $ref->name . ' in ' . self::toString($ref->getDeclaringFunction());
1✔
151
                } else {
152
                        throw new Nette\InvalidArgumentException;
×
153
                }
154
        }
155

156

157
        /**
158
         * Expands the name of the class to full name in the given context of given class.
159
         * Thus, it returns how the PHP parser would understand $name if it were written in the body of the class $context.
160
         * @param  \ReflectionClass<object>  $context
161
         * @throws Nette\InvalidArgumentException
162
         */
163
        public static function expandClassName(string $name, \ReflectionClass $context): string
1✔
164
        {
165
                $lower = strtolower($name);
1✔
166
                if (empty($name)) {
1✔
167
                        throw new Nette\InvalidArgumentException('Class name must not be empty.');
1✔
168

169
                } elseif (Validators::isBuiltinType($lower)) {
1✔
170
                        return $lower;
1✔
171

172
                } elseif ($lower === 'self' || $lower === 'static') {
1✔
173
                        return $context->name;
1✔
174

175
                } elseif ($lower === 'parent') {
1✔
176
                        return $context->getParentClass()
1✔
177
                                ? $context->getParentClass()->name
1✔
178
                                : 'parent';
1✔
179

180
                } elseif ($name[0] === '\\') { // fully qualified name
1✔
181
                        return ltrim($name, '\\');
1✔
182
                }
183

184
                $uses = self::getUseStatements($context);
1✔
185
                $parts = explode('\\', $name, 2);
1✔
186
                if (isset($uses[$parts[0]])) {
1✔
187
                        $parts[0] = $uses[$parts[0]];
1✔
188
                        return implode('\\', $parts);
1✔
189

190
                } elseif ($context->inNamespace()) {
1✔
191
                        return $context->getNamespaceName() . '\\' . $name;
1✔
192

193
                } else {
194
                        return $name;
1✔
195
                }
196
        }
197

198

199
        /**
200
         * Returns the use statements from the file where the class is defined.
201
         * @param  \ReflectionClass<object>  $class
202
         * @return array<string, class-string>  Map of alias to fully qualified class name
203
         */
204
        public static function getUseStatements(\ReflectionClass $class): array
1✔
205
        {
206
                if ($class->isAnonymous()) {
1✔
207
                        throw new Nette\NotImplementedException('Anonymous classes are not supported.');
1✔
208
                }
209

210
                static $cache = [];
1✔
211
                if (!isset($cache[$name = $class->name])) {
1✔
212
                        if ($class->isInternal()) {
1✔
213
                                $cache[$name] = [];
1✔
214
                        } else {
215
                                $code = (string) file_get_contents((string) $class->getFileName());
1✔
216
                                $cache = self::parseUseStatements($code, $name) + $cache;
1✔
217
                        }
218
                }
219

220
                return $cache[$name];
1✔
221
        }
222

223

224
        /**
225
         * Parses PHP code to [class => [alias => class, ...]]
226
         * @return array<string, array<string, string>>
227
         */
228
        private static function parseUseStatements(string $code, ?string $forClass = null): array
1✔
229
        {
230
                try {
231
                        $tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
1✔
232
                } catch (\ParseError $e) {
×
233
                        trigger_error($e->getMessage(), E_USER_NOTICE);
×
234
                        $tokens = [];
×
235
                }
236

237
                $namespace = $class = null;
1✔
238
                $classLevel = $level = 0;
1✔
239
                $res = $uses = [];
1✔
240

241
                $nameTokens = [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED];
1✔
242

243
                while ($token = current($tokens)) {
1✔
244
                        next($tokens);
1✔
245
                        switch ($token->id) {
1✔
246
                                case T_NAMESPACE:
247
                                        $namespace = ltrim(self::fetch($tokens, $nameTokens) . '\\', '\\');
1✔
248
                                        $uses = [];
1✔
249
                                        break;
1✔
250

251
                                case T_CLASS:
252
                                case T_INTERFACE:
253
                                case T_TRAIT:
254
                                case T_ENUM:
255
                                        if ($name = self::fetch($tokens, T_STRING)) {
1✔
256
                                                $class = $namespace . $name;
1✔
257
                                                $classLevel = $level + 1;
1✔
258
                                                $res[$class] = $uses;
1✔
259
                                                if ($class === $forClass) {
1✔
260
                                                        return $res;
1✔
261
                                                }
262
                                        }
263

264
                                        break;
1✔
265

266
                                case T_USE:
267
                                        while (!$class && ($name = self::fetch($tokens, $nameTokens))) {
1✔
268
                                                $name = ltrim($name, '\\');
1✔
269
                                                if (self::fetch($tokens, '{')) {
1✔
270
                                                        while ($suffix = self::fetch($tokens, $nameTokens)) {
1✔
271
                                                                if (self::fetch($tokens, T_AS) && ($alias = self::fetch($tokens, T_STRING))) {
1✔
272
                                                                        $uses[$alias] = $name . $suffix;
1✔
273
                                                                } else {
274
                                                                        $tmp = explode('\\', $suffix);
1✔
275
                                                                        $uses[end($tmp)] = $name . $suffix;
1✔
276
                                                                }
277

278
                                                                if (!self::fetch($tokens, ',')) {
1✔
279
                                                                        break;
1✔
280
                                                                }
281
                                                        }
282
                                                } elseif (self::fetch($tokens, T_AS) && ($alias = self::fetch($tokens, T_STRING))) {
1✔
283
                                                        $uses[$alias] = $name;
1✔
284

285
                                                } else {
286
                                                        $tmp = explode('\\', $name);
1✔
287
                                                        $uses[end($tmp)] = $name;
1✔
288
                                                }
289

290
                                                if (!self::fetch($tokens, ',')) {
1✔
291
                                                        break;
1✔
292
                                                }
293
                                        }
294

295
                                        break;
1✔
296

297
                                case T_CURLY_OPEN:
298
                                case T_DOLLAR_OPEN_CURLY_BRACES:
299
                                case ord('{'):
1✔
300
                                        $level++;
1✔
301
                                        break;
1✔
302

303
                                case ord('}'):
1✔
304
                                        if ($level === $classLevel) {
1✔
305
                                                $class = $classLevel = 0;
1✔
306
                                        }
307

308
                                        $level--;
1✔
309
                        }
310
                }
311

312
                return $res;
×
313
        }
314

315

316
        /**
317
         * @param  \PhpToken[]  $tokens
318
         * @param  string|int|int[]  $take
319
         */
320
        private static function fetch(array &$tokens, string|int|array $take): ?string
1✔
321
        {
322
                $res = null;
1✔
323
                while ($token = current($tokens)) {
1✔
324
                        if ($token->is($take)) {
1✔
325
                                $res .= $token->text;
1✔
326
                        } elseif (!$token->is([T_DOC_COMMENT, T_WHITESPACE, T_COMMENT])) {
1✔
327
                                break;
1✔
328
                        }
329

330
                        next($tokens);
1✔
331
                }
332

333
                return $res;
1✔
334
        }
335
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc