Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new in initializers #966

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a native type declaration here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason, I was mimicking other methods in the generator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's deal with those globally later? We will be able to add them to private methods only though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doctrine/common is probably going to die soon, so that's really not a big deal, we can deal with it later or not at all.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! @greg0ire are you referring to #826 here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm referring to a discussion on a private Slack.

*/
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