Skip to content

Commit 63f70f7

Browse files
committed
SmartObject: supports property deprecation
1 parent 257227b commit 63f70f7

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/SmartObject.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ public function &__get(string $name): mixed
6464
if (!($prop & 0b0001)) {
6565
throw new MemberAccessException("Cannot read a write-only property $class::\$$name.");
6666
}
67-
$m = ($prop & 0b0010 ? 'get' : 'is') . $name;
67+
$m = ($prop & 0b0010 ? 'get' : 'is') . ucfirst($name);
68+
if ($prop & 0b10000) {
69+
$trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
70+
$loc = isset($trace['file'], $trace['line'])
71+
? " in $trace[file] on line $trace[line]"
72+
: '';
73+
trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
74+
}
6875
if ($prop & 0b0100) { // return by reference
6976
return $this->$m();
7077
} else {
@@ -91,7 +98,15 @@ public function __set(string $name, mixed $value): void
9198
if (!($prop & 0b1000)) {
9299
throw new MemberAccessException("Cannot write to a read-only property $class::\$$name.");
93100
}
94-
$this->{'set' . $name}($value);
101+
$m = 'set' . ucfirst($name);
102+
if ($prop & 0b10000) {
103+
$trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
104+
$loc = isset($trace['file'], $trace['line'])
105+
? " in $trace[file] on line $trace[line]"
106+
: '';
107+
trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
108+
}
109+
$this->$m($value);
95110

96111
} else {
97112
ObjectHelpers::strictSet($class, $name);

src/Utils/ObjectHelpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static function getMagicProperties(string $class): array
130130

131131
$rc = new \ReflectionClass($class);
132132
preg_match_all(
133-
'~^ [ \t*]* @property(|-read|-write) [ \t]+ [^\s$]+ [ \t]+ \$ (\w+) ()~mx',
133+
'~^ [ \t*]* @property(|-read|-write|-deprecated) [ \t]+ [^\s$]+ [ \t]+ \$ (\w+) ()~mx',
134134
(string) $rc->getDocComment(),
135135
$matches,
136136
PREG_SET_ORDER,
@@ -147,7 +147,7 @@ public static function getMagicProperties(string $class): array
147147
&& ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
148148

149149
if ($read || $write) {
150-
$props[$name] = $read << 0 | ($nm[0] === 'g') << 1 | $rm->returnsReference() << 2 | $write << 3;
150+
$props[$name] = $read << 0 | ($nm[0] === 'g') << 1 | $rm->returnsReference() << 2 | $write << 3 | ($type === '-deprecated') << 4;
151151
}
152152
}
153153

tests/Utils/SmartObject.property.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require __DIR__ . '/../bootstrap.php';
1616
* @property int $bar
1717
* @property int $bazz
1818
* @property int $s
19+
* @property-deprecated int $depr
1920
*/
2021
class TestClass
2122
{
@@ -69,6 +70,16 @@ class TestClass
6970
echo __METHOD__;
7071
return 'ERROR';
7172
}
73+
74+
75+
public function setDepr($value)
76+
{
77+
}
78+
79+
80+
public function getDepr()
81+
{
82+
}
7283
}
7384

7485

@@ -115,3 +126,15 @@ Assert::same('World', $obj->bar);
115126
Assert::exception(function () use ($obj) {
116127
$val = $obj->bazz;
117128
}, Nette\MemberAccessException::class, 'Cannot read a write-only property TestClass::$bazz.');
129+
130+
131+
// deprecated property
132+
Assert::error(function () {
133+
$obj = new TestClass;
134+
$obj->depr = 10;
135+
}, E_USER_DEPRECATED, 'Property TestClass::$depr is deprecated, use TestClass::setDepr() method in %a%SmartObject.property.phpt on line %d%.');
136+
137+
Assert::error(function () {
138+
$obj = new TestClass;
139+
$val = $obj->depr;
140+
}, E_USER_DEPRECATED, 'Property TestClass::$depr is deprecated, use TestClass::getDepr() method in %a%SmartObject.property.phpt on line %d%.');

0 commit comments

Comments
 (0)