Skip to content

Commit

Permalink
Handle PHP 8.1 types
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Dec 21, 2021
1 parent b2d26a9 commit 61a2330
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ODM\MongoDB\Configuration;
use ReflectionClass;
use ReflectionException;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
Expand All @@ -22,6 +23,7 @@
use function file_exists;
use function file_put_contents;
use function implode;
use function in_array;
use function interface_exists;
use function is_dir;
use function is_writable;
Expand Down Expand Up @@ -193,11 +195,7 @@ private function generateMethod(ReflectionMethod $method): string
{
$parametersString = $this->buildParametersString($method);
$callParamsString = implode(', ', $this->getParameterNamesForDecoratedCall($method->getParameters()));

$return = 'return ';
if ($method->getReturnType() !== null && $this->formatType($method->getReturnType(), $method) === 'void') {
$return = '';
}
$return = $this->shouldMethodSkipReturnKeyword($method) ? '' : 'return ';

return <<<CODE
Expand All @@ -213,6 +211,15 @@ public function {$method->name}($parametersString){$this->getMethodReturnType($m
CODE;
}

private function shouldMethodSkipReturnKeyword(ReflectionMethod $method): bool
{
if ($method->getReturnType() === null) {
return false;
}

return in_array($this->formatType($method->getReturnType(), $method), ['void', 'never'], true);
}

private function buildParametersString(ReflectionMethod $method): string
{
$parameters = $method->getParameters();
Expand Down Expand Up @@ -303,6 +310,15 @@ function (ReflectionType $unionedType) use ($method, $parameter) {
));
}

if ($type instanceof ReflectionIntersectionType) {
return implode('&', array_map(
function (ReflectionType $intersectedType) use ($method, $parameter) {
return $this->formatType($intersectedType, $method, $parameter);
},
$type->getTypes()
));
}

assert($type instanceof ReflectionNamedType);
$name = $type->getName();
$nameLower = strtolower($name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Tests\PersistentCollection;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

/**
* @template TKey of array-key
* @template TElement
* @template-extends ArrayCollection<TKey, TElement>
*/
class CollWithPHP81Types extends ArrayCollection
{
/**
* @param Collection<TKey, TElement>&ArrayCollection<TKey, TElement> $param
* @return Collection<TKey, TElement>&ArrayCollection<TKey, TElement>
*/
public function intersection(Collection&ArrayCollection $param) : Collection&ArrayCollection
{
return $param;
}

public function never() : never
{
die('You shall not pass');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public function testPHP80Types(): void
$coll = new $class(new CollWithPHP80Types(), $this->dm, $this->uow);
$this->assertInstanceOf(CollWithPHP80Types::class, $coll);
}

/**
* @requires PHP 8.1
*/
public function testPHP81Types(): void
{
$class = $this->generator->loadClass(CollWithPHP81Types::class, Configuration::AUTOGENERATE_EVAL);
$coll = new $class(new CollWithPHP81Types(), $this->dm, $this->uow);
$this->assertInstanceOf(CollWithPHP81Types::class, $coll);
}
}

0 comments on commit 61a2330

Please sign in to comment.