Skip to content

Commit

Permalink
Merge pull request doctrine#10006 from derrabus/fix/build
Browse files Browse the repository at this point in the history
Fix build
  • Loading branch information
derrabus authored Aug 26, 2022
2 parents 5283e14 + 46ec865 commit 4c253f2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 65 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"require-dev": {
"doctrine/annotations": "^1.13",
"doctrine/coding-standard": "^9.0",
"doctrine/coding-standard": "^9.0.2",
"phpbench/phpbench": "^0.16.10 || ^1.0",
"phpstan/phpstan": "~1.4.10 || 1.8.2",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
Expand Down
46 changes: 24 additions & 22 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function array_map;
use function array_values;
use function array_walk;
use function assert;
use function get_class;
use function is_object;
use function spl_object_id;
Expand All @@ -33,7 +34,8 @@
*
* @psalm-template TKey of array-key
* @psalm-template T
* @template-implements Collection<TKey,T>
* @template-extends AbstractLazyCollection<TKey,T>
* @template-implements Selectable<TKey,T>
*/
final class PersistentCollection extends AbstractLazyCollection implements Selectable
{
Expand Down Expand Up @@ -71,7 +73,7 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
* The name of the field on the target entities that points to the owner
* of the collection. This is only set if the association is bi-directional.
*
* @var string
* @var string|null
*/
private $backRefFieldName;

Expand Down Expand Up @@ -148,7 +150,7 @@ public function getTypeClass(): Mapping\ClassMetadataInfo
*/
public function hydrateAdd($element): void
{
$this->collection->add($element);
$this->unwrap()->add($element);

// If _backRefFieldName is set and its a one-to-many association,
// we need to set the back reference.
Expand Down Expand Up @@ -176,7 +178,7 @@ public function hydrateAdd($element): void
*/
public function hydrateSet($key, $element): void
{
$this->collection->set($key, $element);
$this->unwrap()->set($key, $element);

// If _backRefFieldName is set, then the association is bidirectional
// and we need to set the back reference.
Expand Down Expand Up @@ -210,7 +212,7 @@ public function initialize(): void
*/
public function takeSnapshot(): void
{
$this->snapshot = $this->collection->toArray();
$this->snapshot = $this->unwrap()->toArray();
$this->isDirty = false;
}

Expand All @@ -233,7 +235,7 @@ public function getSnapshot(): array
*/
public function getDeleteDiff(): array
{
$collectionItems = $this->collection->toArray();
$collectionItems = $this->unwrap()->toArray();

return array_values(array_diff_key(
array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot),
Expand All @@ -249,7 +251,7 @@ public function getDeleteDiff(): array
*/
public function getInsertDiff(): array
{
$collectionItems = $this->collection->toArray();
$collectionItems = $this->unwrap()->toArray();

return array_values(array_diff_key(
array_combine(array_map('spl_object_id', $collectionItems), $collectionItems),
Expand Down Expand Up @@ -322,8 +324,6 @@ public function setInitialized($bool): void

/**
* {@inheritdoc}
*
* @return object
*/
public function remove($key)
{
Expand Down Expand Up @@ -387,7 +387,7 @@ public function containsKey($key): bool
) {
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);

return $this->collection->containsKey($key) || $persister->containsKey($this, $key);
return $this->unwrap()->containsKey($key) || $persister->containsKey($this, $key);
}

return parent::containsKey($key);
Expand All @@ -401,7 +401,7 @@ public function contains($element): bool
if (! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);

return $this->collection->contains($element) || $persister->contains($this, $element);
return $this->unwrap()->contains($element) || $persister->contains($this, $element);
}

return parent::contains($element);
Expand Down Expand Up @@ -432,7 +432,7 @@ public function count(): int
if (! $this->initialized && $this->association !== null && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) {
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);

return $persister->count($this) + ($this->isDirty ? $this->collection->count() : 0);
return $persister->count($this) + ($this->isDirty ? $this->unwrap()->count() : 0);
}

return parent::count();
Expand All @@ -457,7 +457,7 @@ public function set($key, $value): void
*/
public function add($value): bool
{
$this->collection->add($value);
$this->unwrap()->add($value);

$this->changed();

Expand Down Expand Up @@ -514,13 +514,13 @@ public function offsetUnset($offset)

public function isEmpty(): bool
{
return $this->collection->isEmpty() && $this->count() === 0;
return $this->unwrap()->isEmpty() && $this->count() === 0;
}

public function clear(): void
{
if ($this->initialized && $this->isEmpty()) {
$this->collection->clear();
$this->unwrap()->clear();

return;
}
Expand All @@ -536,12 +536,12 @@ public function clear(): void
// hence for event listeners we need the objects in memory.
$this->initialize();

foreach ($this->collection as $element) {
foreach ($this->unwrap() as $element) {
$uow->scheduleOrphanRemoval($element);
}
}

$this->collection->clear();
$this->unwrap()->clear();

$this->initialized = true; // direct call, {@link initialize()} is too expensive

Expand Down Expand Up @@ -633,7 +633,7 @@ public function matching(Criteria $criteria): Collection
}

if ($this->initialized) {
return $this->collection->matching($criteria);
return $this->unwrap()->matching($criteria);
}

if ($this->association['type'] === ClassMetadata::MANY_TO_MANY) {
Expand Down Expand Up @@ -665,6 +665,8 @@ public function matching(Criteria $criteria): Collection
*/
public function unwrap(): Collection
{
assert($this->collection !== null);

return $this->collection;
}

Expand All @@ -674,10 +676,10 @@ protected function doInitialize(): void
$newlyAddedDirtyObjects = [];

if ($this->isDirty) {
$newlyAddedDirtyObjects = $this->collection->toArray();
$newlyAddedDirtyObjects = $this->unwrap()->toArray();
}

$this->collection->clear();
$this->unwrap()->clear();
$this->em->getUnitOfWork()->loadCollection($this);
$this->takeSnapshot();

Expand All @@ -696,14 +698,14 @@ protected function doInitialize(): void
*/
private function restoreNewObjectsInDirtyCollection(array $newObjects): void
{
$loadedObjects = $this->collection->toArray();
$loadedObjects = $this->unwrap()->toArray();
$newObjectsByOid = array_combine(array_map('spl_object_id', $newObjects), $newObjects);
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $loadedObjects), $loadedObjects);
$newObjectsThatWereNotLoaded = array_diff_key($newObjectsByOid, $loadedObjectsByOid);

if ($newObjectsThatWereNotLoaded) {
// Reattach NEW objects added through add(), if any.
array_walk($newObjectsThatWereNotLoaded, [$this->collection, 'add']);
array_walk($newObjectsThatWereNotLoaded, [$this->unwrap(), 'add']);

$this->isDirty = true;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Doctrine/ORM/Tools/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
use Symfony\Component\Cache\Adapter\RedisAdapter;

use function class_exists;
use function dirname;
use function extension_loaded;
use function file_exists;
use function md5;
use function sys_get_temp_dir;

Expand All @@ -52,7 +54,11 @@ class Setup
public static function registerAutoloadDirectory($directory)
{
if (! class_exists('Doctrine\Common\ClassLoader', false)) {
require_once $directory . '/Doctrine/Common/ClassLoader.php';
if (file_exists($directory . '/Doctrine/Common/ClassLoader.php')) {
require_once $directory . '/Doctrine/Common/ClassLoader.php';
} elseif (file_exists(dirname($directory) . '/src/ClassLoader.php')) {
require_once dirname($directory) . '/src/ClassLoader.php';
}
}

$loader = new ClassLoader('Doctrine', $directory);
Expand Down
12 changes: 1 addition & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,7 @@ parameters:
path: lib/Doctrine/ORM/NativeQuery.php

-
message: "#^Call to an undefined method Doctrine\\\\Common\\\\Collections\\\\Collection\\<\\(int\\|string\\), mixed\\>\\:\\:matching\\(\\)\\.$#"
count: 1
path: lib/Doctrine/ORM/PersistentCollection.php

-
message: "#^Method Doctrine\\\\ORM\\\\PersistentCollection\\:\\:remove\\(\\) should return object but returns array\\|float\\|int\\|string\\|false\\|null\\.$#"
message: "#^Call to an undefined method Doctrine\\\\Common\\\\Collections\\\\Collection\\<TKey of \\(int\\|string\\), T\\>\\:\\:matching\\(\\)\\.$#"
count: 1
path: lib/Doctrine/ORM/PersistentCollection.php

Expand All @@ -265,11 +260,6 @@ parameters:
count: 2
path: lib/Doctrine/ORM/PersistentCollection.php

-
message: "#^The @implements tag of class Doctrine\\\\ORM\\\\PersistentCollection describes Doctrine\\\\Common\\\\Collections\\\\Collection but the class implements\\: Doctrine\\\\Common\\\\Collections\\\\Selectable$#"
count: 1
path: lib/Doctrine/ORM/PersistentCollection.php

-
message: "#^Method Doctrine\\\\ORM\\\\Persisters\\\\Collection\\\\OneToManyPersister\\:\\:delete\\(\\) should return int\\|null but empty return statement found\\.$#"
count: 1
Expand Down
2 changes: 0 additions & 2 deletions phpstan-dbal2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ parameters:
reportUnmatchedIgnoredErrors: false

ignoreErrors:
# https://github.com/doctrine/collections/pull/282
- '/Variable \$offset in isset\(\) always exists and is not nullable\./'
# PHPStan doesn't understand our method_exists() safeguards.
- '/Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\./'
# Class name will change in DBAL 3.
Expand Down
3 changes: 0 additions & 3 deletions phpstan-persistence2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ parameters:
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php

# False positive
-
message: '/^Variable \$offset in isset\(\) always exists and is not nullable\.$/'
path: lib/Doctrine/ORM/PersistentCollection.php
-
message: '/^Call to an undefined method Doctrine\\Common\\Cache\\Cache::deleteAll\(\)\.$/'
count: 1
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ parameters:
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php

# False positive
-
message: '/^Variable \$offset in isset\(\) always exists and is not nullable\.$/'
path: lib/Doctrine/ORM/PersistentCollection.php
-
message: '/^Call to an undefined method Doctrine\\Common\\Cache\\Cache::deleteAll\(\)\.$/'
count: 1
Expand Down
Loading

0 comments on commit 4c253f2

Please sign in to comment.