Skip to content

Commit 7902b18

Browse files
committed
feat: setter/getter must be public
This will allow developers to implement `protected function setRelation()` without accidentally triggering on `$entity->relation = $value`.
1 parent 31a2e83 commit 7902b18

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

system/Entity/Entity.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use DateTime;
3030
use Exception;
3131
use JsonSerializable;
32+
use ReflectionMethod;
3233
use ReturnTypeWillChange;
3334

3435
/**
@@ -461,7 +462,7 @@ public function __set(string $key, $value = null)
461462
// so maybe wants to do sth with null value automatically
462463
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
463464

464-
if (method_exists($this, $method) && $method !== 'setAttributes') {
465+
if (method_exists($this, $method) && $method !== 'setAttributes' && $this->isPublic($method)) {
465466
$this->{$method}($value);
466467

467468
return $this;
@@ -476,6 +477,13 @@ public function __set(string $key, $value = null)
476477
return $this;
477478
}
478479

480+
private function isPublic(string $method): bool
481+
{
482+
$reflection = new ReflectionMethod($this, $method);
483+
484+
return $reflection->isPublic();
485+
}
486+
479487
/**
480488
* Magic method to allow retrieval of protected and private class properties
481489
* either by their name, or through a `getCamelCasedProperty()` method.
@@ -501,7 +509,7 @@ public function __get(string $key)
501509

502510
// if a get* method exists for this key,
503511
// use that method to insert this value.
504-
if (method_exists($this, $method)) {
512+
if (method_exists($this, $method) && $this->isPublic($method)) {
505513
$result = $this->{$method}();
506514
}
507515

tests/system/Entity/EntityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,12 +1114,12 @@ protected function getMappedEntity()
11141114
'orig' => 'simple',
11151115
];
11161116

1117-
protected function setSimple(string $val)
1117+
public function setSimple(string $val)
11181118
{
11191119
$this->attributes['simple'] = 'oo:' . $val;
11201120
}
11211121

1122-
protected function getSimple()
1122+
public function getSimple()
11231123
{
11241124
return $this->attributes['simple'] . ':oo';
11251125
}

0 commit comments

Comments
 (0)