Skip to content

Commit

Permalink
Fix generating default values
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 24, 2023
1 parent d93bd79 commit ecadbdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
38 changes: 2 additions & 36 deletions src/ProxyManager/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,14 @@
namespace ProxyManager\Generator;

use Laminas\Code\Generator\ClassGenerator as LaminasClassGenerator;
use ReflectionParameter;

use function method_exists;

/**
* Class generator that ensures that interfaces/classes that are implemented/extended are FQCNs
*
* @internal do not use this in your code: it is only here for internal use
* @deprecated this class was in use due to parent implementation not receiving prompt bugfixes, but
* `laminas/laminas-code` is actively maintained and receives quick release iterations.
*/
class ClassGenerator extends LaminasClassGenerator
{
public function generate(): string
{
$extendedClass = $this->getExtendedClass();

foreach ($this->getMethods() as $method) {
$class = $extendedClass;

if ($class === null) {
foreach ($this->getImplementedInterfaces() as $interface) {
if (method_exists($interface, $method->getName())) {
$class = $interface;
break;
}
}
}

if ($class === null || ! method_exists($class, $method->getName())) {
continue;
}

foreach ($method->getParameters() as $parameter) {
$default = $parameter->getDefaultValue();

if ($default === null) {
continue;
}

$parameter->setDefaultValue(new ValueGenerator($default, new ReflectionParameter([$class, $method->getName()], $parameter->getName())));
}
}

return parent::generate();
}
}
12 changes: 9 additions & 3 deletions src/ProxyManager/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laminas\Code\Reflection\MethodReflection;
use ReflectionException;
use ReflectionMethod;
use ReflectionParameter;

/**
* Method generator that fixes minor quirks in ZF2's method generator
Expand Down Expand Up @@ -61,9 +62,14 @@ public static function copyMethodSignature(MethodReflection $reflectionMethod):
$method = parent::copyMethodSignature($reflectionMethod);

foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
$method->setParameter(
ParameterGenerator::fromReflection($reflectionParameter)
);
$parameter = ParameterGenerator::fromReflection($reflectionParameter);
$default = $parameter->getDefaultValue();

if ($default !== null) {
$parameter->setDefaultValue(new ValueGenerator($default, $reflectionParameter));
}

$method->setParameter($parameter);
}

return $method;
Expand Down

0 comments on commit ecadbdc

Please sign in to comment.