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

Add support for PHP 8.0 types in custom collection #2263

Merged
merged 3 commits into from
Jan 8, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Prepare PHP 8 dependencies"
run: "composer remove --no-update --ignore-platform-req=php --dev doctrine/coding-standard phpstan/phpstan squizlabs/php_codesniffer"
run: "composer remove --no-update --ignore-platform-req=php --dev doctrine/coding-standard squizlabs/php_codesniffer"
if: "${{ matrix.php-version == '8.0' }}"

- name: "Install dependencies with composer"
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout code"
Expand Down Expand Up @@ -54,6 +54,9 @@ jobs:
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Prepare PHP 8 dependencies"
run: "composer remove --no-update --ignore-platform-req=php --dev doctrine/coding-standard squizlabs/php_codesniffer"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --no-progress"

Expand All @@ -67,7 +70,7 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout code"
Expand All @@ -87,6 +90,9 @@ jobs:
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Prepare PHP 8 dependencies"
run: "composer remove --no-update --ignore-platform-req=php --dev doctrine/coding-standard squizlabs/php_codesniffer"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --no-progress"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionType;
use ReflectionUnionType;
use const DIRECTORY_SEPARATOR;
use function array_map;
use function array_pop;
Expand All @@ -24,7 +25,6 @@
use function interface_exists;
use function is_dir;
use function is_writable;
use function method_exists;
use function mkdir;
use function rename;
use function sprintf;
Expand Down Expand Up @@ -254,15 +254,10 @@ private function getParameterType(ReflectionParameter $parameter) : string
throw new ReflectionException(sprintf('Parameter "%s" has no type. Please file a bug report.', $parameter->getName()));
}

$type = $parameter->getType();
assert($type instanceof ReflectionNamedType);
$method = $parameter->getDeclaringFunction();
assert($method instanceof ReflectionMethod);

return sprintf(
'%s%s%s',
$type->allowsNull() ? '?' : '',
$type->isBuiltin() ? '' : '\\',
$type->getName()
);
return $this->formatType($parameter->getType(), $method, $parameter);
}

/**
Expand Down Expand Up @@ -301,7 +296,17 @@ private function formatType(
ReflectionMethod $method,
?ReflectionParameter $parameter = null
) : string {
$name = method_exists($type, 'getName') ? $type->getName() : (string) $type;
if ($type instanceof ReflectionUnionType) {
return implode('|', array_map(
function (ReflectionType $unionedType) use ($method, $parameter) {
return $this->formatType($unionedType, $method, $parameter);
},
$type->getTypes()
));
}

assert($type instanceof ReflectionNamedType);
$name = $type->getName();
$nameLower = strtolower($name);
if ($nameLower === 'self') {
$name = $method->getDeclaringClass()->getName();
Expand All @@ -314,7 +319,7 @@ private function formatType(

$name = $parentClass->getName();
}
if (! $type->isBuiltin() && ! class_exists($name) && ! interface_exists($name)) {
if ($nameLower !== 'static' && ! $type->isBuiltin() && ! class_exists($name) && ! interface_exists($name)) {
if ($parameter !== null) {
throw PersistentCollectionException::invalidParameterTypeHint(
$method->getDeclaringClass()->getName(),
Expand All @@ -327,11 +332,12 @@ private function formatType(
$method->getName()
);
}
if (! $type->isBuiltin()) {
if ($nameLower !== 'static' && ! $type->isBuiltin()) {
$name = '\\' . $name;
}
if ($type->allowsNull()
&& ($parameter === null || ! $parameter->isDefaultValueAvailable() || $parameter->getDefaultValue() !== null)
&& $name !== 'mixed'
) {
$name = '?' . $name;
}
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<exclude-pattern>tests/Proxies*</exclude-pattern>
<exclude-pattern>tests/Hydrators*</exclude-pattern>
<exclude-pattern>tests/PersistentCollections*</exclude-pattern>
<exclude-pattern>tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/CollWithPHP80Types.php</exclude-pattern>

<rule ref="Doctrine">
<!-- Traversable type hints often end up as mixed[], so we skip them for now -->
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ parameters:
paths:
- %rootDir%/../../../benchmark
- %rootDir%/../../../lib
excludes_analyse:
- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup/Match.php
- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Tests\PersistentCollection;

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

class CollWithPHP80Types extends ArrayCollection
{
public function mixed(mixed $param) : mixed
{
return $param;
}

public function union(Collection|ArrayCollection $param) : Collection|ArrayCollection
{
return $param;
}

public function static() : static
{
return $this;
}

public function nullableStatic() : ?static
{
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use function phpversion;
use function version_compare;

/**
* Tests aims to check if classes generated for various PHP versions are correct (i.e. parses).
Expand Down Expand Up @@ -45,4 +47,15 @@ public function testWithNullableReturnType()
$coll = new $class(new CollWithNullableReturnType(), $this->dm, $this->uow);
$this->assertInstanceOf(CollWithNullableReturnType::class, $coll);
}

public function testPHP80Types()
{
if (version_compare((string) phpversion(), '8.0.0', '<')) {
$this->markTestSkipped('PHP 8.0 is required to run this test');
}

$class = $this->generator->loadClass(CollWithPHP80Types::class, Configuration::AUTOGENERATE_EVAL);
$coll = new $class(new CollWithPHP80Types(), $this->dm, $this->uow);
$this->assertInstanceOf(CollWithPHP80Types::class, $coll);
}
}