Skip to content

Commit

Permalink
fix Extender finding extension method for subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Maometos committed Nov 26, 2023
1 parent a90f9ed commit 7521233
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/Extender.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,27 @@ public static function getImports(object $target): array
$count = 0;
$trace = debug_backtrace();
foreach ($trace as $info) {
$class = $info['class'] ?? null;
$function = $info['function'] ?? null;
// Looking for the file where the target calls the method __call().
if ($class == get_class($target) && $function == '__call') {
$file = $info['file'] ?? '';
break;
$object = $info['object'] ?? null;
if ($object) {
// Looking for the file where the target object calls the method __call().
$function = $info['function'] ?? null;
if ($object::class == $target::class && $function == '__call') {
$file = $info['file'] ?? '';
break;
}
}
$count++;
}

$classes = self::$classes[$file] ?? [];
$classes = static::$classes[$file] ?? [];
// Return the imports if they are already stored.
if ($classes) return $classes;

// Add the case where the extension method used inside the extension class.
$callerClass = $trace[$count + 1]['class'] ?? null;
if ($callerClass) {
$classes[] = $callerClass;
self::$classes[$file] = $classes;
static::$classes[$file] = $classes;
}

// Looking for all the imports in the file.
Expand All @@ -71,7 +73,7 @@ public static function getImports(object $target): array
preg_match_all("%(?i)use\s+([A-Za-z_\\\]+);%", $contents, $matches);
if (isset($matches[1])) {
$classes = array_merge($classes, $matches[1]);
self::$classes[$file] = $classes;
static::$classes[$file] = $classes;
}
}

Expand Down

0 comments on commit 7521233

Please sign in to comment.