Skip to content

Commit

Permalink
[5.x] Removes reflection calls from FluentGetterSetter (#9584)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
JohnathonKoster and jasonvarga authored Apr 2, 2024
1 parent d00fedb commit af95040
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/Support/FluentGetterSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Statamic\Support;

use Closure;
use ReflectionException;
use ReflectionObject;

class FluentGetterSetter
{
Expand Down Expand Up @@ -105,11 +103,7 @@ public function args($args)
*/
protected function runGetterLogic()
{
try {
$value = $this->reflectedProperty()->getValue($this->object);
} catch (ReflectionException $exception) {
$value = $this->object->{$this->property} ?? null;
}
$value = $this->getProperty($this->object, $this->property);

if ($getter = $this->getter) {
$value = $getter($value);
Expand All @@ -129,27 +123,20 @@ protected function runSetterLogic($value)
$value = $setter($value);
}

try {
$this->reflectedProperty()->setValue($this->object, $value);
} catch (ReflectionException $exception) {
$this->object->{$this->property} = $value;
}
$this->setProperty($this->object, $this->property, $value);

if ($afterSetter = $this->afterSetter) {
$afterSetter($value);
}
}

/**
* Get reflected property.
*
* @return \ReflectionProperty
*/
protected function reflectedProperty()
private function getProperty($object, $property)
{
$property = (new ReflectionObject($this->object))->getProperty($this->property);
$property->setAccessible(true);
return (fn () => $this->{$property})->call($object);
}

return $property;
private function setProperty($object, $property, $value)
{
(fn () => $this->{$property} = $value)->call($object);
}
}

0 comments on commit af95040

Please sign in to comment.