From d63584523112dceb4d3506bb2b93d5fc52b45d1c Mon Sep 17 00:00:00 2001 From: Spenser Hale Date: Tue, 14 May 2024 23:01:31 -0700 Subject: [PATCH] Feat: Using concrete instead of abstract classes for callable string Allows the usage of Hook and Shortcode on abstract methods to be wired with their concrete class implementation. --- src/AttributeResolver.php | 32 ++++++++++++++++---------------- src/Hook.php | 4 ++-- src/Shortcode.php | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/AttributeResolver.php b/src/AttributeResolver.php index a1a95b5..fe2f6d2 100644 --- a/src/AttributeResolver.php +++ b/src/AttributeResolver.php @@ -41,17 +41,17 @@ public static function processClassesToList(array $classes): array $hooks = []; $errors = []; - foreach ($classes as $class) { + foreach ($classes as $classNamme) { try { - $reflector = new ReflectionClass($class); - if ($reflector->isAbstract() || $reflector->isInterface() || $reflector->isTrait()) { + $reflectorClass = new ReflectionClass($classNamme); + if ($reflectorClass->isAbstract() || $reflectorClass->isInterface() || $reflectorClass->isTrait()) { continue; } - self::processClassAttributes($hooks, $reflector); + self::processClassAttributes($hooks, $reflectorClass); - foreach ($reflector->getMethods() as $method) { - self::processMethodAttributes($hooks, $method); + foreach ($reflectorClass->getMethods() as $method) { + self::processMethodAttributes($hooks, $reflectorClass, $method); } } catch (Throwable $e) { $errors[] = $e; @@ -61,25 +61,25 @@ public static function processClassesToList(array $classes): array return [$hooks, $errors]; } - private static function processClassAttributes(array &$hooks, ReflectionClass $reflector): void + private static function processClassAttributes(array &$hooks, ReflectionClass $class): void { - foreach (self::getAttributeInstances($reflector, Hook::class) as $hook) { - $hooks[] = $hook->setByClass($reflector); + foreach (self::getAttributeInstances($class, Hook::class) as $hook) { + $hooks[] = $hook->setByClass($class); } - foreach (self::getAttributeInstances($reflector, Shortcode::class) as $shortcode) { - $hooks[] = $shortcode->setByClass($reflector); + foreach (self::getAttributeInstances($class, Shortcode::class) as $shortcode) { + $hooks[] = $shortcode->setByClass($class); } } - private static function processMethodAttributes(array &$hooks, ReflectionMethod $reflector): void + private static function processMethodAttributes(array &$hooks, ReflectionClass $class, ReflectionMethod $method): void { - foreach (self::getAttributeInstances($reflector, Hook::class) as $hook) { - $hooks[] = $hook->setByMethod($reflector); + foreach (self::getAttributeInstances($method, Hook::class) as $hook) { + $hooks[] = $hook->setByMethod($class, $method); } - foreach (self::getAttributeInstances($reflector, Shortcode::class) as $shortcode) { - $hooks[] = $shortcode->setByMethod($reflector); + foreach (self::getAttributeInstances($method, Shortcode::class) as $shortcode) { + $hooks[] = $shortcode->setByMethod($class, $method); } } diff --git a/src/Hook.php b/src/Hook.php index 755b626..669b1ab 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -20,9 +20,9 @@ public function __construct( public string $type = 'add_filter' ) {} - public function setByMethod(\ReflectionMethod $method): static + public function setByMethod(\ReflectionClass $class, \ReflectionMethod $method): static { - $this->callback = "{$method->getDeclaringClass()->getName()}::{$method->getName()}"; + $this->callback = "{$class->getName()}::{$method->getName()}"; $this->arguments = $method->getNumberOfParameters(); if($method->getReturnType()?->getName() === 'void') { $this->type = 'add_action'; diff --git a/src/Shortcode.php b/src/Shortcode.php index bd3390d..88a17b7 100644 --- a/src/Shortcode.php +++ b/src/Shortcode.php @@ -17,9 +17,9 @@ public function __construct( public ?string $callback = null, ) {} - public function setByMethod(\ReflectionMethod $method): static + public function setByMethod(\ReflectionClass $class, \ReflectionMethod $method): static { - $this->callback = "{$method->getDeclaringClass()->getName()}::{$method->getName()}"; + $this->callback = "{$class->getName()}::{$method->getName()}"; return $this; }