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

Proxy: Added support for PHP 7 return & parameter type hints #376

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ private function generateMethods(ClassMetadata $class)
}

$methods .= $name . '(' . $this->buildParametersString($class, $method, $method->getParameters()) . ')';
$methods .= $this->getMethodReturnType($method);
$methods .= "\n" . ' {' . "\n";

if ($this->isShortIdentifierGetter($method, $class)) {
Expand Down Expand Up @@ -945,6 +946,10 @@ private function getParameterType(ClassMetadata $class, \ReflectionMethod $metho
return 'callable';
}

if (PHP_VERSION_ID >= 70000 && $parameter->hasType() && $parameter->getType()->isBuiltin()) {
return (string) $parameter->getType();
}

try {
$parameterClass = $parameter->getClass();

Expand Down Expand Up @@ -1002,4 +1007,28 @@ function (\ReflectionParameter $parameter) {
$parameters
);
}

/**
* @Param \ReflectionMethod $method
*
* @return string
*/
private function getMethodReturnType(\ReflectionMethod $method)
{
if (PHP_VERSION_ID < 70000 || !$method->hasReturnType()) {
return '';
}

$returnType = $method->getReturnType();
$nameLower = strtolower((string) $returnType);

if ($nameLower === 'self') {
return ': \\' . $method->getDeclaringClass()->getName();
}
if ($nameLower === 'parent') {
return ': \\' . $method->getDeclaringClass()->getParentClass()->getName();
}

return ': ' . (!$returnType->isBuiltin() ? '\\' : '') . (string) $returnType;
}
}
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ProxyClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,52 @@ public function testClassWithVariadicArgumentOnProxiedMethod()
$this->assertEquals(1, substr_count($classCode, 'parent::addType(...$types)'));
}

public function testClassWithScalarTypeHintsOnProxiedMethods()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Scalar type hints are only supported in PHP >= 7.0.0.');
}

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

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

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyScalarTypeHintsClass.php');

$this->assertEquals(1, substr_count($classCode, 'function singleTypeHint(string $param)'));
$this->assertEquals(1, substr_count($classCode, 'function multipleTypeHints(int $a, float $b, bool $c, string $d)'));
$this->assertEquals(1, substr_count($classCode, 'function combinationOfTypeHintsAndNormal(\stdClass $a, $b, int $c)'));
$this->assertEquals(1, substr_count($classCode, 'function typeHintsWithVariadic(int ...$foo)'));
}

public function testClassWithReturnTypesOnProxiedMethods()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Method return types are only supported in PHP >= 7.0.0.');
}

$className = 'Doctrine\Tests\Common\Proxy\ReturnTypesClass';
Copy link
Contributor

Choose a reason for hiding this comment

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

This can use PHP 5.6 ::class, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, that is a syntax error on PHP <5.5 (and this package is >=5.3.2).

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, just checked.

Copy link
Member

Choose a reason for hiding this comment

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

It would fail at parse time, fyi ;-)

Copy link
Contributor

Choose a reason for hiding this comment

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

I realized, to bad :/

Copy link
Member

Choose a reason for hiding this comment

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

Gonna change it anyway: bumping the library dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, let me know if/when you need rebase or other changes here.

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

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

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyReturnTypesClass.php');

$this->assertEquals(1, substr_count($classCode, 'function returnsClass(): \stdClass'));
$this->assertEquals(1, substr_count($classCode, 'function returnsScalar(): int'));
$this->assertEquals(1, substr_count($classCode, 'function returnsArray(): array'));
$this->assertEquals(1, substr_count($classCode, 'function returnsCallable(): callable'));
$this->assertEquals(1, substr_count($classCode, 'function returnsSelf(): \\' . $className));
$this->assertEquals(1, substr_count($classCode, 'function returnsParent(): \stdClass'));
}

public function testClassWithInvalidTypeHintOnProxiedMethod()
{
$className = 'Doctrine\Tests\Common\Proxy\InvalidTypeHintClass';
Expand Down
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ReturnTypesClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test PHP 7 return types class.
*/
class ReturnTypesClass extends \stdClass
{
public function returnsClass(): \stdClass
{
}

public function returnsScalar(): int
{
}

public function returnsArray(): array
{
}

public function returnsCallable(): callable
{
}

public function returnsSelf(): self
{
}

public function returnsParent(): parent
{
}
}
25 changes: 25 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ScalarTypeHintsClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

/**
* Test PHP 7 scalar type hints class.
*/
class ScalarTypeHintsClass
{
public function singleTypeHint(string $param)
{
}

public function multipleTypeHints(int $a, float $b, bool $c, string $d)
{
}

public function combinationOfTypeHintsAndNormal(\stdClass $a, $b, int $c)
{
}

public function typeHintsWithVariadic(int ...$foo)
{
}
}