From 07af6f6a0c8a911d9e08870bdd54946757c0e958 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 20 Oct 2021 23:01:22 -0500 Subject: [PATCH] Additional optimizations to magic properties --- lib/MagicProperties.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/MagicProperties.php b/lib/MagicProperties.php index b7fcbeb..b8ee75e 100644 --- a/lib/MagicProperties.php +++ b/lib/MagicProperties.php @@ -57,14 +57,25 @@ trait MagicProperties { if (!isset($protectedMethodsList[$this::class])) { $reflector = new \ReflectionClass($this); // Magic property methods are protected - $protectedMethodsList[$this::class] = $reflector->getMethods(\ReflectionMethod::IS_PROTECTED); - } + $methodsList = $reflector->getMethods(\ReflectionMethod::IS_PROTECTED); + $temp = []; + $valid = false; + // Only cache the magic methods + foreach ($methodsList as $m) { + if (str_starts_with($m->name, '__get_') || str_starts_with($m->name, '__set_')) { + $temp[] = $m->name; - foreach ($protectedMethodsList[$this::class] as $method) { - if ($method->name === $methodName) { - return $methodName; + if (!$valid && $m->name === $methodName) { + $valid = true; + } + } } + + $protectedMethodsList[$this::class] = $temp; + return ($valid) ? $methodName : null; } + + return (in_array($methodName, $protectedMethodsList[$this::class])) ? $methodName : null; } return null;