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

nette / utils / 21636962765

03 Feb 2026 03:39PM UTC coverage: 93.312% (+0.05%) from 93.264%
21636962765

push

github

dg
added CLAUDE.md

2065 of 2213 relevant lines covered (93.31%)

0.93 hits per line

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

97.14
/src/Utils/ObjectHelpers.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\Utils;
11

12
use Nette;
13
use Nette\MemberAccessException;
14
use function array_filter, array_merge, array_pop, array_unique, get_class_methods, get_parent_class, implode, is_a, levenshtein, method_exists, preg_match_all, preg_replace, strlen, ucfirst;
15
use const PREG_SET_ORDER, SORT_REGULAR;
16

17

18
/**
19
 * Nette\SmartObject helpers.
20
 * @internal
21
 */
22
final class ObjectHelpers
23
{
24
        use Nette\StaticClass;
25

26
        /**
27
         * @param  class-string  $class
28
         * @return never
29
         * @throws MemberAccessException
30
         */
31
        public static function strictGet(string $class, string $name): never
1✔
32
        {
33
                $rc = new \ReflectionClass($class);
1✔
34
                $hint = self::getSuggestion(array_merge(
1✔
35
                        array_filter($rc->getProperties(\ReflectionProperty::IS_PUBLIC), fn($p) => !$p->isStatic()),
1✔
36
                        self::parseFullDoc($rc, '~^[ \t*]*@property(?:-read)?[ \t]+(?:\S+[ \t]+)??\$(\w+)~m'),
1✔
37
                ), $name);
38
                throw new MemberAccessException("Cannot read an undeclared property $class::\$$name" . ($hint ? ", did you mean \$$hint?" : '.'));
1✔
39
        }
×
40

41

42
        /**
43
         * @param  class-string  $class
44
         * @return never
45
         * @throws MemberAccessException
46
         */
47
        public static function strictSet(string $class, string $name): never
1✔
48
        {
49
                $rc = new \ReflectionClass($class);
1✔
50
                $hint = self::getSuggestion(array_merge(
1✔
51
                        array_filter($rc->getProperties(\ReflectionProperty::IS_PUBLIC), fn($p) => !$p->isStatic()),
1✔
52
                        self::parseFullDoc($rc, '~^[ \t*]*@property(?:-write)?[ \t]+(?:\S+[ \t]+)??\$(\w+)~m'),
1✔
53
                ), $name);
54
                throw new MemberAccessException("Cannot write to an undeclared property $class::\$$name" . ($hint ? ", did you mean \$$hint?" : '.'));
1✔
55
        }
×
56

57

58
        /**
59
         * @param  class-string  $class
60
         * @return never
61
         * @throws MemberAccessException
62
         */
63
        public static function strictCall(string $class, string $method, array $additionalMethods = []): void
1✔
64
        {
65
                $trace = debug_backtrace(0, 3); // suppose this method is called from __call()
1✔
66
                $context = ($trace[1]['function'] ?? null) === '__call'
1✔
67
                        ? ($trace[2]['class'] ?? null)
1✔
68
                        : null;
1✔
69

70
                if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
1✔
71
                        $class = get_parent_class($context);
1✔
72
                }
73

74
                if (method_exists($class, $method)) { // insufficient visibility
1✔
75
                        $rm = new \ReflectionMethod($class, $method);
1✔
76
                        $visibility = $rm->isPrivate()
1✔
77
                                ? 'private '
1✔
78
                                : ($rm->isProtected() ? 'protected ' : '');
1✔
79
                        throw new MemberAccessException("Call to {$visibility}method $class::$method() from " . ($context ? "scope $context." : 'global scope.'));
1✔
80

81
                } else {
82
                        $hint = self::getSuggestion(array_merge(
1✔
83
                                get_class_methods($class),
1✔
84
                                self::parseFullDoc(new \ReflectionClass($class), '~^[ \t*]*@method[ \t]+(?:static[ \t]+)?(?:\S+[ \t]+)??(\w+)\(~m'),
1✔
85
                                $additionalMethods,
1✔
86
                        ), $method);
87
                        throw new MemberAccessException("Call to undefined method $class::$method()" . ($hint ? ", did you mean $hint()?" : '.'));
1✔
88
                }
89
        }
90

91

92
        /**
93
         * @param  class-string  $class
94
         * @return never
95
         * @throws MemberAccessException
96
         */
97
        public static function strictStaticCall(string $class, string $method): void
1✔
98
        {
99
                $trace = debug_backtrace(0, 3); // suppose this method is called from __callStatic()
1✔
100
                $context = ($trace[1]['function'] ?? null) === '__callStatic'
1✔
101
                        ? ($trace[2]['class'] ?? null)
1✔
102
                        : null;
1✔
103

104
                if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
1✔
105
                        $class = get_parent_class($context);
1✔
106
                }
107

108
                if (method_exists($class, $method)) { // insufficient visibility
1✔
109
                        $rm = new \ReflectionMethod($class, $method);
1✔
110
                        $visibility = $rm->isPrivate()
1✔
111
                                ? 'private '
×
112
                                : ($rm->isProtected() ? 'protected ' : '');
1✔
113
                        throw new MemberAccessException("Call to {$visibility}method $class::$method() from " . ($context ? "scope $context." : 'global scope.'));
1✔
114

115
                } else {
116
                        $hint = self::getSuggestion(
1✔
117
                                array_filter((new \ReflectionClass($class))->getMethods(\ReflectionMethod::IS_PUBLIC), fn($m) => $m->isStatic()),
1✔
118
                                $method,
119
                        );
120
                        throw new MemberAccessException("Call to undefined static method $class::$method()" . ($hint ? ", did you mean $hint()?" : '.'));
1✔
121
                }
122
        }
123

124

125
        /**
126
         * Returns array of magic properties defined by annotation @property.
127
         * @param  class-string  $class
128
         * @return array<string, int>  [name => bit mask]
129
         * @internal
130
         */
131
        public static function getMagicProperties(string $class): array
1✔
132
        {
133
                static $cache;
1✔
134
                $props = &$cache[$class];
1✔
135
                if ($props !== null) {
1✔
136
                        return $props;
1✔
137
                }
138

139
                $rc = new \ReflectionClass($class);
1✔
140
                preg_match_all(
1✔
141
                        '~^  [ \t*]*  @property(|-read|-write|-deprecated)  [ \t]+  [^\s$]+  [ \t]+  \$  (\w+)  ()~mx',
1✔
142
                        (string) $rc->getDocComment(),
1✔
143
                        $matches,
1✔
144
                        PREG_SET_ORDER,
1✔
145
                );
146

147
                $props = [];
1✔
148
                foreach ($matches as [, $type, $name]) {
1✔
149
                        $uname = ucfirst($name);
1✔
150
                        $write = $type !== '-read'
1✔
151
                                && $rc->hasMethod($nm = 'set' . $uname)
1✔
152
                                && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
1✔
153
                        $read = $type !== '-write'
1✔
154
                                && ($rc->hasMethod($nm = 'get' . $uname) || $rc->hasMethod($nm = 'is' . $uname))
1✔
155
                                && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
1✔
156

157
                        if ($read || $write) {
1✔
158
                                $props[$name] = $read << 0 | ($nm[0] === 'g') << 1 | $rm->returnsReference() << 2 | $write << 3 | ($type === '-deprecated') << 4;
1✔
159
                        }
160
                }
161

162
                foreach ($rc->getTraits() as $trait) {
1✔
163
                        $props += self::getMagicProperties($trait->name);
1✔
164
                }
165

166
                if ($parent = get_parent_class($class)) {
1✔
167
                        $props += self::getMagicProperties($parent);
1✔
168
                }
169

170
                return $props;
1✔
171
        }
172

173

174
        /**
175
         * Finds the best suggestion (for 8-bit encoding).
176
         * @param  (\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionClass|\ReflectionProperty|string)[]  $possibilities
177
         * @internal
178
         */
179
        public static function getSuggestion(array $possibilities, string $value): ?string
1✔
180
        {
181
                $norm = preg_replace($re = '#^(get|set|has|is|add)(?=[A-Z])#', '+', $value);
1✔
182
                $best = null;
1✔
183
                $min = (strlen($value) / 4 + 1) * 10 + .1;
1✔
184
                foreach (array_unique($possibilities, SORT_REGULAR) as $item) {
1✔
185
                        $item = $item instanceof \Reflector ? $item->name : $item;
1✔
186
                        if ($item !== $value && (
1✔
187
                                ($len = levenshtein($item, $value, 10, 11, 10)) < $min
1✔
188
                                || ($len = levenshtein(preg_replace($re, '*', $item), $norm, 10, 11, 10)) < $min
1✔
189
                        )) {
190
                                $min = $len;
1✔
191
                                $best = $item;
1✔
192
                        }
193
                }
194

195
                return $best;
1✔
196
        }
197

198

199
        private static function parseFullDoc(\ReflectionClass $rc, string $pattern): array
1✔
200
        {
201
                do {
202
                        $doc[] = $rc->getDocComment();
1✔
203
                        $traits = $rc->getTraits();
1✔
204
                        while ($trait = array_pop($traits)) {
1✔
205
                                $doc[] = $trait->getDocComment();
1✔
206
                                $traits += $trait->getTraits();
1✔
207
                        }
208
                } while ($rc = $rc->getParentClass());
1✔
209

210
                return preg_match_all($pattern, implode('', $doc), $m) ? $m[1] : [];
1✔
211
        }
212

213

214
        /**
215
         * Checks if the public non-static property exists.
216
         * Returns 'event' if the property exists and has event like name.
217
         * @param  class-string  $class
218
         * @internal
219
         */
220
        public static function hasProperty(string $class, string $name): bool|string
1✔
221
        {
222
                static $cache;
1✔
223
                $prop = &$cache[$class][$name];
1✔
224
                if ($prop === null) {
1✔
225
                        $prop = false;
1✔
226
                        try {
227
                                $rp = new \ReflectionProperty($class, $name);
1✔
228
                                if ($rp->isPublic() && !$rp->isStatic()) {
1✔
229
                                        $prop = $name >= 'onA' && $name < 'on_' ? 'event' : true;
1✔
230
                                }
231
                        } catch (\ReflectionException) {
1✔
232
                        }
233
                }
234

235
                return $prop;
1✔
236
        }
237
}
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