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

nette / utils / 22290136219

23 Feb 2026 01:47AM UTC coverage: 93.125% (-0.003%) from 93.128%
22290136219

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

99.03
/src/Utils/ObjectHelpers.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 Nette\MemberAccessException;
12
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;
13
use const PREG_SET_ORDER, SORT_REGULAR;
14

15

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

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

38

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

53

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

65
                if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
1✔
66
                        $class = get_parent_class($context);
1✔
67
                }
68

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

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

86

87
        /**
88
         * @return never
89
         * @throws MemberAccessException
90
         */
91
        public static function strictStaticCall(string $class, string $method): void
1✔
92
        {
93
                $trace = debug_backtrace(0, 3); // suppose this method is called from __callStatic()
1✔
94
                $context = ($trace[1]['function'] ?? null) === '__callStatic'
1✔
95
                        ? ($trace[2]['class'] ?? null)
1✔
96
                        : null;
1✔
97

98
                if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
1✔
99
                        $class = get_parent_class($context);
1✔
100
                }
101

102
                if (method_exists($class, $method)) { // insufficient visibility
1✔
103
                        $rm = new \ReflectionMethod($class, $method);
1✔
104
                        $visibility = $rm->isPrivate()
1✔
105
                                ? 'private '
×
106
                                : ($rm->isProtected() ? 'protected ' : '');
1✔
107
                        throw new MemberAccessException("Call to {$visibility}method $class::$method() from " . ($context ? "scope $context." : 'global scope.'));
1✔
108

109
                } else {
110
                        $hint = self::getSuggestion(
1✔
111
                                array_filter((new \ReflectionClass($class))->getMethods(\ReflectionMethod::IS_PUBLIC), fn($m) => $m->isStatic()),
1✔
112
                                $method,
113
                        );
114
                        throw new MemberAccessException("Call to undefined static method $class::$method()" . ($hint ? ", did you mean $hint()?" : '.'));
1✔
115
                }
116
        }
117

118

119
        /**
120
         * Returns array of magic properties defined by annotation @property.
121
         * @return array of [name => bit mask]
122
         * @internal
123
         */
124
        public static function getMagicProperties(string $class): array
1✔
125
        {
126
                static $cache;
1✔
127
                $props = &$cache[$class];
1✔
128
                if ($props !== null) {
1✔
129
                        return $props;
1✔
130
                }
131

132
                $rc = new \ReflectionClass($class);
1✔
133
                preg_match_all(
1✔
134
                        '~^  [ \t*]*  @property(|-read|-write|-deprecated)  [ \t]+  [^\s$]+  [ \t]+  \$  (\w+)  ()~mx',
1✔
135
                        (string) $rc->getDocComment(),
1✔
136
                        $matches,
1✔
137
                        PREG_SET_ORDER,
1✔
138
                );
139

140
                $props = [];
1✔
141
                foreach ($matches as [, $type, $name]) {
1✔
142
                        $uname = ucfirst($name);
1✔
143
                        $write = $type !== '-read'
1✔
144
                                && $rc->hasMethod($nm = 'set' . $uname)
1✔
145
                                && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
1✔
146
                        $read = $type !== '-write'
1✔
147
                                && ($rc->hasMethod($nm = 'get' . $uname) || $rc->hasMethod($nm = 'is' . $uname))
1✔
148
                                && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
1✔
149

150
                        if ($read || $write) {
1✔
151
                                $props[$name] = $read << 0 | ($nm[0] === 'g') << 1 | $rm->returnsReference() << 2 | $write << 3 | ($type === '-deprecated') << 4;
1✔
152
                        }
153
                }
154

155
                foreach ($rc->getTraits() as $trait) {
1✔
156
                        $props += self::getMagicProperties($trait->name);
1✔
157
                }
158

159
                if ($parent = get_parent_class($class)) {
1✔
160
                        $props += self::getMagicProperties($parent);
1✔
161
                }
162

163
                return $props;
1✔
164
        }
165

166

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

188
                return $best;
1✔
189
        }
190

191

192
        private static function parseFullDoc(\ReflectionClass $rc, string $pattern): array
1✔
193
        {
194
                do {
195
                        $doc[] = $rc->getDocComment();
1✔
196
                        $traits = $rc->getTraits();
1✔
197
                        while ($trait = array_pop($traits)) {
1✔
198
                                $doc[] = $trait->getDocComment();
1✔
199
                                $traits += $trait->getTraits();
1✔
200
                        }
201
                } while ($rc = $rc->getParentClass());
1✔
202

203
                return preg_match_all($pattern, implode('', $doc), $m) ? $m[1] : [];
1✔
204
        }
205

206

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

227
                return $prop;
1✔
228
        }
229
}
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