Skip to content

Commit

Permalink
Support new in initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Aug 22, 2022
1 parent 1a5b448 commit a45d437
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,10 +1091,7 @@ private function buildParametersString(array $parameters, array $renameParameter
}

$parameterDefinition .= '$' . ($renameParameters ? $renameParameters[$i] : $param->getName());

if ($param->isDefaultValueAvailable()) {
$parameterDefinition .= ' = ' . var_export($param->getDefaultValue(), true);
}
$parameterDefinition .= $this->getParameterDefaultValue($param);

$parameterDefinitions[] = $parameterDefinition;
}
Expand All @@ -1118,6 +1115,24 @@ private function getParameterType(ReflectionParameter $parameter)
return $this->formatType($parameter->getType(), $declaringFunction, $parameter);
}

/**
* @return string
*/
private function getParameterDefaultValue(ReflectionParameter $parameter)
{
if (! $parameter->isDefaultValueAvailable()) {
return '';
}

if (PHP_VERSION_ID < 80100) {
return ' = ' . var_export($parameter->getDefaultValue(), true);
}

$value = rtrim(substr(explode('$' . $parameter->getName() . ' = ', (string) $parameter, 2)[1], 0, -2));

return ' = ' . $value;
}

/**
* @param ReflectionParameter[] $parameters
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Proxy;

class PHP81NewInInitializers
{
public function onlyInitializer($foo = new \stdClass()): void
{

}

public function typed(\DateTimeInterface $foo = new \DateTimeImmutable('now')): void
{

}

public function arrayInDefault(array $foo = [new \DateTimeImmutable('2022-08-22 16:20', new \DateTimeZone('Europe/Warsaw'))]): void
{

}
}
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,36 @@ public function testEnumDefaultInPublicProperty() : void
$this->assertSame($object->isEnum, \Doctrine\Tests\Common\Proxy\YesOrNo::YES);
}

/**
* @requires PHP >= 8.1.0
*/
public function testPhp81NewInInitializers()
{
$className = PHP81NewInInitializers::class;

if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\PHP81NewInInitializers', false)) {
$metadata = $this->createClassMetadata($className, ['id']);

$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy');
$this->generateAndRequire($proxyGenerator, $metadata);
}

self::assertStringContainsString(
'onlyInitializer($foo = new \stdClass()): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
);

self::assertStringContainsString(
'typed(\DateTimeInterface $foo = new \DateTimeImmutable(\'now\')): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
);

self::assertStringContainsString(
'arrayInDefault(array $foo = [new \DateTimeImmutable(\'2022-08-22 16:20\', new \DateTimeZone(\'Europe/Warsaw\'))]): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
);
}

/**
* @param string $className
* @param mixed[] $ids
Expand Down

0 comments on commit a45d437

Please sign in to comment.