Skip to content

Commit

Permalink
Merge pull request #2398 from malarzm/php-8-1
Browse files Browse the repository at this point in the history
PHP 8.1 types in custom collections
  • Loading branch information
malarzm authored Dec 25, 2021
2 parents a520aae + 61a2330 commit 47642e7
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 13 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- "7.3"
- "7.4"
- "8.0"
- "8.1"
mongodb-version:
- "4.4"
- "4.2"
Expand All @@ -42,11 +43,6 @@ jobs:
mongodb-version: "4.4"
driver-version: "stable"
dependencies: "highest"
- topology: "server"
php-version: "8.1"
mongodb-version: "4.4"
driver-version: "alpha"
dependencies: "highest"

steps:
- name: "Checkout"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.0"
- "8.1"

steps:
- name: "Checkout code"
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.0"
- "8.1"

steps:
- name: "Checkout code"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"doctrine/coding-standard": "^9.0",
"jmikola/geojson": "^1.0",
"phpbench/phpbench": "^1.0.0",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5 || ^9",
"squizlabs/php_codesniffer": "^3.5",
Expand Down
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
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<exclude-pattern>tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/CollWithPHP80Types.php</exclude-pattern>
<exclude-pattern>lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup/Match.php</exclude-pattern>
<exclude-pattern>lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Match.php</exclude-pattern>
<!-- Figure out what to do with PHP 8.1 errors in coding standard -->
<exclude-pattern>tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/CollWithPHP81Types.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 psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<file name="lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup/Match.php" />
<file name="lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Match.php" />
<file name="tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/User.php" />
<!-- Remove CollWithPHP81Types once Psalm supports native intersection https://github.com/vimeo/psalm/issues/6413 -->
<file name="tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/DefaultPersistentCollectionGeneratorTest.php" />
<file name="tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/CollWithPHP81Types.php" />
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
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 47642e7

Please sign in to comment.