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

nette / utils / 20431395313

22 Dec 2025 12:06PM UTC coverage: 93.164% (+71.8%) from 21.324%
20431395313

push

github

dg
Html::addText() accepts int|null for back compatibility [Closes #332][Closes #333]

1 of 1 new or added line in 1 file covered. (100.0%)

140 existing lines in 15 files now uncovered.

2058 of 2209 relevant lines covered (93.16%)

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

40

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

55

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

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

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

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

88

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

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

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

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

120

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

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

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

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

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

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

165
                return $props;
1✔
166
        }
167

168

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

190
                return $best;
1✔
191
        }
192

193

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

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

208

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

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

© 2025 Coveralls, Inc