Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12d0865

Browse files
committed
Fix static analysis errors for Collections 1.7
1 parent 5283e14 commit 12d0865

6 files changed

Lines changed: 44 additions & 62 deletions

File tree

lib/Doctrine/ORM/PersistentCollection.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function array_map;
1919
use function array_values;
2020
use function array_walk;
21+
use function assert;
2122
use function get_class;
2223
use function is_object;
2324
use function spl_object_id;
@@ -33,7 +34,8 @@
3334
*
3435
* @psalm-template TKey of array-key
3536
* @psalm-template T
36-
* @template-implements Collection<TKey,T>
37+
* @template-extends AbstractLazyCollection<TKey,T>
38+
* @template-implements Selectable<TKey,T>
3739
*/
3840
final class PersistentCollection extends AbstractLazyCollection implements Selectable
3941
{
@@ -71,7 +73,7 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
7173
* The name of the field on the target entities that points to the owner
7274
* of the collection. This is only set if the association is bi-directional.
7375
*
74-
* @var string
76+
* @var string|null
7577
*/
7678
private $backRefFieldName;
7779

@@ -148,7 +150,7 @@ public function getTypeClass(): Mapping\ClassMetadataInfo
148150
*/
149151
public function hydrateAdd($element): void
150152
{
151-
$this->collection->add($element);
153+
$this->unwrap()->add($element);
152154

153155
// If _backRefFieldName is set and its a one-to-many association,
154156
// we need to set the back reference.
@@ -176,7 +178,7 @@ public function hydrateAdd($element): void
176178
*/
177179
public function hydrateSet($key, $element): void
178180
{
179-
$this->collection->set($key, $element);
181+
$this->unwrap()->set($key, $element);
180182

181183
// If _backRefFieldName is set, then the association is bidirectional
182184
// and we need to set the back reference.
@@ -210,7 +212,7 @@ public function initialize(): void
210212
*/
211213
public function takeSnapshot(): void
212214
{
213-
$this->snapshot = $this->collection->toArray();
215+
$this->snapshot = $this->unwrap()->toArray();
214216
$this->isDirty = false;
215217
}
216218

@@ -233,7 +235,7 @@ public function getSnapshot(): array
233235
*/
234236
public function getDeleteDiff(): array
235237
{
236-
$collectionItems = $this->collection->toArray();
238+
$collectionItems = $this->unwrap()->toArray();
237239

238240
return array_values(array_diff_key(
239241
array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot),
@@ -249,7 +251,7 @@ public function getDeleteDiff(): array
249251
*/
250252
public function getInsertDiff(): array
251253
{
252-
$collectionItems = $this->collection->toArray();
254+
$collectionItems = $this->unwrap()->toArray();
253255

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

323325
/**
324326
* {@inheritdoc}
325-
*
326-
* @return object
327327
*/
328328
public function remove($key)
329329
{
@@ -387,7 +387,7 @@ public function containsKey($key): bool
387387
) {
388388
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
389389

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

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

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

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

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

438438
return parent::count();
@@ -457,7 +457,7 @@ public function set($key, $value): void
457457
*/
458458
public function add($value): bool
459459
{
460-
$this->collection->add($value);
460+
$this->unwrap()->add($value);
461461

462462
$this->changed();
463463

@@ -514,13 +514,13 @@ public function offsetUnset($offset)
514514

515515
public function isEmpty(): bool
516516
{
517-
return $this->collection->isEmpty() && $this->count() === 0;
517+
return $this->unwrap()->isEmpty() && $this->count() === 0;
518518
}
519519

520520
public function clear(): void
521521
{
522522
if ($this->initialized && $this->isEmpty()) {
523-
$this->collection->clear();
523+
$this->unwrap()->clear();
524524

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

539-
foreach ($this->collection as $element) {
539+
foreach ($this->unwrap() as $element) {
540540
$uow->scheduleOrphanRemoval($element);
541541
}
542542
}
543543

544-
$this->collection->clear();
544+
$this->unwrap()->clear();
545545

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

@@ -633,7 +633,7 @@ public function matching(Criteria $criteria): Collection
633633
}
634634

635635
if ($this->initialized) {
636-
return $this->collection->matching($criteria);
636+
return $this->unwrap()->matching($criteria);
637637
}
638638

639639
if ($this->association['type'] === ClassMetadata::MANY_TO_MANY) {
@@ -665,6 +665,8 @@ public function matching(Criteria $criteria): Collection
665665
*/
666666
public function unwrap(): Collection
667667
{
668+
assert($this->collection !== null);
669+
668670
return $this->collection;
669671
}
670672

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

676678
if ($this->isDirty) {
677-
$newlyAddedDirtyObjects = $this->collection->toArray();
679+
$newlyAddedDirtyObjects = $this->unwrap()->toArray();
678680
}
679681

680-
$this->collection->clear();
682+
$this->unwrap()->clear();
681683
$this->em->getUnitOfWork()->loadCollection($this);
682684
$this->takeSnapshot();
683685

@@ -696,14 +698,14 @@ protected function doInitialize(): void
696698
*/
697699
private function restoreNewObjectsInDirtyCollection(array $newObjects): void
698700
{
699-
$loadedObjects = $this->collection->toArray();
701+
$loadedObjects = $this->unwrap()->toArray();
700702
$newObjectsByOid = array_combine(array_map('spl_object_id', $newObjects), $newObjects);
701703
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $loadedObjects), $loadedObjects);
702704
$newObjectsThatWereNotLoaded = array_diff_key($newObjectsByOid, $loadedObjectsByOid);
703705

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

708710
$this->isDirty = true;
709711
}

phpstan-baseline.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,7 @@ parameters:
251251
path: lib/Doctrine/ORM/NativeQuery.php
252252

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

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

268-
-
269-
message: "#^The @implements tag of class Doctrine\\\\ORM\\\\PersistentCollection describes Doctrine\\\\Common\\\\Collections\\\\Collection but the class implements\\: Doctrine\\\\Common\\\\Collections\\\\Selectable$#"
270-
count: 1
271-
path: lib/Doctrine/ORM/PersistentCollection.php
272-
273263
-
274264
message: "#^Method Doctrine\\\\ORM\\\\Persisters\\\\Collection\\\\OneToManyPersister\\:\\:delete\\(\\) should return int\\|null but empty return statement found\\.$#"
275265
count: 1

phpstan-dbal2.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ parameters:
66
reportUnmatchedIgnoredErrors: false
77

88
ignoreErrors:
9-
# https://github.com/doctrine/collections/pull/282
10-
- '/Variable \$offset in isset\(\) always exists and is not nullable\./'
119
# PHPStan doesn't understand our method_exists() safeguards.
1210
- '/Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\./'
1311
# Class name will change in DBAL 3.

phpstan-persistence2.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ parameters:
3232
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
3333

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

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ parameters:
3232
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
3333

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

psalm-baseline.xml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
</PossiblyNullArrayOffset>
8484
<PossiblyUndefinedArrayOffset occurrences="3">
8585
<code>$assoc['joinColumnFieldNames']</code>
86+
<code>$assoc['targetToSourceKeyColumns']</code>
87+
<code>$owningAssociation['targetToSourceKeyColumns']</code>
8688
</PossiblyUndefinedArrayOffset>
8789
<UndefinedInterfaceMethod occurrences="1">
8890
<code>getCacheRegion</code>
@@ -472,11 +474,6 @@
472474
<code>$class</code>
473475
</PropertyNotSetInConstructor>
474476
</file>
475-
<file src="lib/Doctrine/ORM/LazyCriteriaCollection.php">
476-
<PropertyNotSetInConstructor occurrences="1">
477-
<code>LazyCriteriaCollection</code>
478-
</PropertyNotSetInConstructor>
479-
</file>
480477
<file src="lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php">
481478
<ArgumentTypeCoercion occurrences="1">
482479
<code>$repositoryClassName</code>
@@ -1070,28 +1067,25 @@
10701067
</PossiblyInvalidArgument>
10711068
</file>
10721069
<file src="lib/Doctrine/ORM/PersistentCollection.php">
1073-
<DocblockTypeContradiction occurrences="1">
1074-
<code>isset($offset)</code>
1075-
</DocblockTypeContradiction>
1076-
<ImplementedReturnTypeMismatch occurrences="1">
1070+
<ImplementedReturnTypeMismatch occurrences="2">
1071+
<code>Collection&lt;TKey, T&gt;</code>
10771072
<code>object|null</code>
10781073
</ImplementedReturnTypeMismatch>
1079-
<InvalidDocblock occurrences="1">
1080-
<code>final class PersistentCollection extends AbstractLazyCollection implements Selectable</code>
1081-
</InvalidDocblock>
1082-
<InvalidReturnStatement occurrences="1"/>
1083-
<MissingParamType occurrences="2">
1084-
<code>$key</code>
1074+
<InvalidReturnStatement occurrences="2">
1075+
<code>$this-&gt;em-&gt;find($this-&gt;typeClass-&gt;name, $key)</code>
1076+
</InvalidReturnStatement>
1077+
<MissingParamType occurrences="1">
10851078
<code>$offset</code>
10861079
</MissingParamType>
10871080
<ParamNameMismatch occurrences="1">
10881081
<code>$value</code>
10891082
</ParamNameMismatch>
1090-
<PossiblyNullArgument occurrences="4">
1083+
<PossiblyNullArgument occurrences="5">
10911084
<code>$this-&gt;association</code>
10921085
<code>$this-&gt;association</code>
10931086
<code>$this-&gt;association</code>
10941087
<code>$this-&gt;association['targetEntity']</code>
1088+
<code>$this-&gt;backRefFieldName</code>
10951089
</PossiblyNullArgument>
10961090
<PossiblyNullArrayAccess occurrences="12">
10971091
<code>$this-&gt;association['fetch']</code>
@@ -1111,13 +1105,9 @@
11111105
<code>setValue</code>
11121106
<code>setValue</code>
11131107
</PossiblyNullReference>
1114-
<PropertyNotSetInConstructor occurrences="1">
1115-
<code>$backRefFieldName</code>
1116-
</PropertyNotSetInConstructor>
1117-
<RedundantConditionGivenDocblockType occurrences="5">
1108+
<RedundantConditionGivenDocblockType occurrences="4">
11181109
<code>$this-&gt;em</code>
11191110
<code>$this-&gt;em</code>
1120-
<code>is_object($this-&gt;collection)</code>
11211111
<code>is_object($value) &amp;&amp; $this-&gt;em</code>
11221112
<code>is_object($value) &amp;&amp; $this-&gt;em</code>
11231113
</RedundantConditionGivenDocblockType>
@@ -1353,6 +1343,8 @@
13531343
<code>$association['joinTable']</code>
13541344
<code>$association['joinTable']</code>
13551345
<code>$association['joinTable']</code>
1346+
<code>$owningAssoc['targetToSourceKeyColumns']</code>
1347+
<code>$owningAssoc['targetToSourceKeyColumns']</code>
13561348
<code>$this-&gt;class-&gt;associationMappings[$fieldName]['joinColumns']</code>
13571349
<code>$this-&gt;class-&gt;associationMappings[$idField]['joinColumns']</code>
13581350
</PossiblyUndefinedArrayOffset>
@@ -1384,6 +1376,7 @@
13841376
<code>executeInserts</code>
13851377
</MoreSpecificReturnType>
13861378
<PossiblyUndefinedArrayOffset occurrences="3">
1379+
<code>$assoc['targetToSourceKeyColumns']</code>
13871380
<code>$mapping['joinColumns']</code>
13881381
<code>$mapping['joinColumns']</code>
13891382
</PossiblyUndefinedArrayOffset>
@@ -1703,6 +1696,7 @@
17031696
</PossiblyNullArrayOffset>
17041697
<PossiblyUndefinedArrayOffset occurrences="2">
17051698
<code>$owningAssoc['joinTable']</code>
1699+
<code>$owningAssoc['targetToSourceKeyColumns']</code>
17061700
</PossiblyUndefinedArrayOffset>
17071701
<PropertyNotSetInConstructor occurrences="1">
17081702
<code>$collectionPathExpression</code>
@@ -2428,8 +2422,11 @@
24282422
<code>$assoc['joinColumns']</code>
24292423
<code>$assoc['joinColumns']</code>
24302424
<code>$assoc['sourceToTargetKeyColumns']</code>
2425+
<code>$assoc['targetToSourceKeyColumns']</code>
24312426
<code>$association['sourceToTargetKeyColumns']</code>
2427+
<code>$association['targetToSourceKeyColumns']</code>
24322428
<code>$owningAssoc['joinTable']</code>
2429+
<code>$owningAssoc['targetToSourceKeyColumns']</code>
24332430
</PossiblyUndefinedArrayOffset>
24342431
<RedundantConditionGivenDocblockType occurrences="2">
24352432
<code>$whereClause !== null</code>
@@ -3092,6 +3089,7 @@
30923089
<PossiblyUndefinedArrayOffset occurrences="3">
30933090
<code>$assoc['joinColumns']</code>
30943091
<code>$assoc['orphanRemoval']</code>
3092+
<code>$assoc['targetToSourceKeyColumns']</code>
30953093
</PossiblyUndefinedArrayOffset>
30963094
<PossiblyUndefinedMethod occurrences="3">
30973095
<code>unwrap</code>

0 commit comments

Comments
 (0)