Skip to content

Commit 47e82e4

Browse files
authored
Merge pull request #2465 from greg0ire/evaluate-collections-2
2 parents 74c477f + b5f5bbd commit 47e82e4

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"ext-mongodb": "^1.5",
2727
"doctrine/annotations": "^1.12",
2828
"doctrine/cache": "^1.11 || ^2.0",
29-
"doctrine/collections": "^1.5",
29+
"doctrine/collections": "^1.5 || ^2.0",
3030
"doctrine/event-manager": "^1.0",
3131
"doctrine/instantiator": "^1.1",
3232
"doctrine/persistence": "^2.4 || ^3.0",

lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\PersistentCollection;
66

7+
use BadMethodCallException;
78
use Closure;
89
use Doctrine\Common\Collections\Collection as BaseCollection;
910
use Doctrine\ODM\MongoDB\DocumentManager;
@@ -22,6 +23,7 @@
2223
use function count;
2324
use function get_class;
2425
use function is_object;
26+
use function method_exists;
2527

2628
/**
2729
* Trait with methods needed to implement PersistentCollectionInterface.
@@ -742,4 +744,36 @@ private function needsSchedulingForSynchronization(): bool
742744
return $this->owner && $this->dm && ! empty($this->mapping['isOwningSide'])
743745
&& $this->dm->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify();
744746
}
747+
748+
/**
749+
* @psalm-param Closure(TKey, T):bool $p
750+
*
751+
* @psalm-return T|null
752+
*/
753+
public function findFirst(Closure $p)
754+
{
755+
if (! method_exists($this->coll, 'findFirst')) {
756+
throw new BadMethodCallException('findFirst() is only available since doctrine/collections v2');
757+
}
758+
759+
return $this->coll->findFirst($p);
760+
}
761+
762+
/**
763+
* @psalm-param Closure(TReturn|TInitial|null, T):(TInitial|TReturn) $func
764+
* @psalm-param TInitial|null $initial
765+
*
766+
* @psalm-return TReturn|TInitial|null
767+
*
768+
* @psalm-template TReturn
769+
* @psalm-template TInitial
770+
*/
771+
public function reduce(Closure $func, $initial = null)
772+
{
773+
if (! method_exists($this->coll, 'reduce')) {
774+
throw new BadMethodCallException('reduce() is only available since doctrine/collections v2');
775+
}
776+
777+
return $this->coll->reduce($func, $initial);
778+
}
745779
}

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ parameters:
6363
count: 1
6464
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
6565

66+
# Support for doctrine/collections v1
67+
-
68+
message: '#^Method Doctrine\\ODM\\MongoDB\\PersistentCollection\:\:add\(\) with return type void returns true but should not return anything\.$#'
69+
count: 1
70+
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php
71+
6672
# import type in PHPStan does not work, see https://github.com/phpstan/phpstan/issues/5091
6773
-
6874
message: "#^Access to offset '.+' on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\PersistentCollection\\\\FieldMapping\\.$#"

psalm.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@
4040
<referencedClass name="ReturnTypeWillChange" />
4141
</errorLevel>
4242
</UndefinedAttributeClass>
43+
<InvalidReturnType>
44+
<errorLevel type="suppress">
45+
<!-- Remove it when dropping support for doctrine/collections v1 -->
46+
<file name="lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php"/>
47+
</errorLevel>
48+
</InvalidReturnType>
4349
</issueHandlers>
4450
</psalm>

tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM29Test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
1010
use Doctrine\ODM\MongoDB\Tests\BaseTest;
1111

12+
use function assert;
13+
1214
class MODM29Test extends BaseTest
1315
{
1416
public function testTest(): void
@@ -25,6 +27,7 @@ public function testTest(): void
2527
$this->dm->persist($doc);
2628
$this->dm->flush();
2729

30+
assert(isset($collection[0], $collection[1], $collection[2]));
2831
// place element '0' after '1'
2932
$collection = new ArrayCollection([
3033
$collection[1],

0 commit comments

Comments
 (0)