Skip to content

Commit

Permalink
Feat: Using concrete instead of abstract classes for callable string
Browse files Browse the repository at this point in the history
Allows the usage of Hook and Shortcode on abstract methods to be wired with their concrete class implementation.
  • Loading branch information
spenserhale committed May 15, 2024
1 parent fe70e5a commit d635845
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions src/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d635845

Please sign in to comment.