Skip to content

Commit 70c62b1

Browse files
committed
Resolve Interceptor generation bugs
- If intercepting an interface, set implemented interfaces - otherwise set extended class - Add Interceptor trait
1 parent b6f2c46 commit 70c62b1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/bitExpert/PHPStan/Magento/Autoload/InterceptorAutoloader.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function getFileContents(string $class): string
7676
$originalClassname = implode('\\', $namespace);
7777

7878
if (!class_exists($originalClassname)) {
79-
throw new \RuntimeException("Class ${class} for Interceptor does not exist");
79+
throw new \RuntimeException("Class ${originalClassname} for Interceptor does not exist");
8080
}
8181
$reflectionClass = new ReflectionClass($originalClassname);
8282

@@ -90,12 +90,20 @@ protected function getFileContents(string $class): string
9090

9191
$generator = new ClassGenerator();
9292
$generator->setName($class)
93-
->setExtendedClass($originalClassname)
94-
->addProperties([])
9593
->addMethods($methods);
9694

95+
$interfaces = [];
96+
if ($reflectionClass->isInterface()) {
97+
$interfaces[] = $originalClassname;
98+
} else {
99+
$generator->setExtendedClass($originalClassname);
100+
}
101+
$generator->addTrait('\\' . \Magento\Framework\Interception\Interceptor::class);
102+
$interfaces[] = '\\' . \Magento\Framework\Interception\InterceptorInterface::class;
103+
$generator->setImplementedInterfaces($interfaces);
104+
97105
$code = $generator->generate();
98-
return $this->_fixCodeStyle($code);
106+
return '<?php'.PHP_EOL.PHP_EOL.$this->_fixCodeStyle($code);
99107
}
100108

101109
protected function _fixCodeStyle(string $sourceCode): string

0 commit comments

Comments
 (0)