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

nette / utils / 39638515326

21 Oct 2026 04:50PM UTC coverage: 92.495% (-0.06%) from 92.551%
39638515326

push

github

dg
added support for PHP 8.6

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

114 existing lines in 17 files now uncovered.

1861 of 2012 relevant lines covered (92.5%)

0.92 hits per line

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

88.46
/src/SmartObject.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;
9

10
use Nette\Utils\ObjectHelpers;
11

12

13
/**
14
 * Strict class for better experience.
15
 * - 'did you mean' hints
16
 * - access to undeclared members throws exceptions
17
 * - support for @property annotations
18
 * - support for calling event handlers stored in $onEvent via onEvent()
19
 */
20
trait SmartObject
21
{
22
        /**
23
         * @return mixed
24
         * @throws MemberAccessException
25
         */
26
        public function __call(string $name, array $args)
27
        {
28
                $class = static::class;
1 ✔
29

30
                if (ObjectHelpers::hasProperty($class, $name) === 'event') { // calling event handlers
1 ✔
31
                        $handlers = $this->$name ?? null;
1 ✔
32
                        if (is_iterable($handlers)) {
1 ✔
33
                                foreach ($handlers as $handler) {
1 ✔
34
                                        $handler(...$args);
1 ✔
35
                                }
36
                        } elseif ($handlers !== null) {
1 ✔
37
                                throw new UnexpectedValueException("Property $class::$$name must be iterable or null, " . get_debug_type($handlers) . ' given.');
1 ✔
38
                        }
39

40
                        return null;
1 ✔
41
                }
42

43
                ObjectHelpers::strictCall($class, $name);
1 ✔
UNCOV
44
        }
×
45

46

47
        /**
48
         * @throws MemberAccessException
49
         */
50
        public static function __callStatic(string $name, array $args)
51
        {
52
                ObjectHelpers::strictStaticCall(static::class, $name);
1 ✔
UNCOV
53
        }
×
54

55

56
        /**
57
         * @return mixed
58
         * @throws MemberAccessException if the property is not defined.
59
         */
60
        public function &__get(string $name)
61
        {
62
                $class = static::class;
1 ✔
63

64
                if ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) { // property getter
1 ✔
65
                        if (!($prop & 0b0001)) {
1 ✔
66
                                throw new MemberAccessException("Cannot read a write-only property $class::\$$name.");
1 ✔
67
                        }
68

69
                        $m = ($prop & 0b0010 ? 'get' : 'is') . ucfirst($name);
1 ✔
70
                        if ($prop & 0b10000) {
1 ✔
71
                                $trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
1 ✔
72
                                $loc = isset($trace['file'], $trace['line'])
1 ✔
73
                                        ? " in $trace[file] on line $trace[line]"
1 ✔
UNCOV
74
                                        : '';
×
75
                                trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
1 ✔
76
                        }
77

78
                        if ($prop & 0b0100) { // return by reference
1 ✔
79
                                return $this->$m();
1 ✔
80
                        } else {
81
                                $val = $this->$m();
1 ✔
82
                                return $val;
1 ✔
83
                        }
84
                } else {
85
                        ObjectHelpers::strictGet($class, $name);
1 ✔
86
                }
UNCOV
87
        }
×
88

89

90
        /**
91
         * @throws MemberAccessException if the property is not defined or is read-only
92
         */
93
        public function __set(string $name, mixed $value): void
94
        {
95
                $class = static::class;
1 ✔
96

97
                if (ObjectHelpers::hasProperty($class, $name)) { // unsetted property
1 ✔
98
                        $this->$name = $value;
1 ✔
99

100
                } elseif ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) { // property setter
1 ✔
101
                        if (!($prop & 0b1000)) {
1 ✔
102
                                throw new MemberAccessException("Cannot write to a read-only property $class::\$$name.");
1 ✔
103
                        }
104

105
                        $m = 'set' . ucfirst($name);
1 ✔
106
                        if ($prop & 0b10000) {
1 ✔
107
                                $trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
1 ✔
108
                                $loc = isset($trace['file'], $trace['line'])
1 ✔
109
                                        ? " in $trace[file] on line $trace[line]"
1 ✔
UNCOV
110
                                        : '';
×
111
                                trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
1 ✔
112
                        }
113

114
                        $this->$m($value);
1 ✔
115

116
                } else {
117
                        ObjectHelpers::strictSet($class, $name);
1 ✔
118
                }
119
        }
1 ✔
120

121

122
        /**
123
         * @throws MemberAccessException
124
         */
125
        public function __unset(string $name): void
126
        {
127
                $class = static::class;
1 ✔
128
                if (!ObjectHelpers::hasProperty($class, $name)) {
1 ✔
UNCOV
129
                        throw new MemberAccessException("Cannot unset the property $class::\$$name.");
×
130
                }
131
        }
1 ✔
132

133

134
        public function __isset(string $name): bool
135
        {
136
                return isset(ObjectHelpers::getMagicProperties(static::class)[$name]);
1 ✔
137
        }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc