8000 Replace spl_object_hash() with spl_object_id() by greg0ire · Pull Request #8837 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Replace spl_object_hash() with spl_object_id() #8837

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

Merged
merged 1 commit into from
Jul 20, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"doctrine/persistence": "^2.2",
"psr/cache": "^1 || ^2 || ^3",
"symfony/console": "^3.0 || ^4.0 || ^5.0 || ^6.0",
"symfony/polyfill-php72": "^1.23",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\Cache\CollectionCacheKey;
use Doctrine\ORM\PersistentCollection;

use function spl_object_hash;
use function spl_object_id;

class NonStrictReadWriteCachedCollectionPersister extends AbstractCollectionPersister
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public function delete(PersistentCollection $collection)

$this->persister->delete($collection);

$this->queuedCache['delete'][spl_object_hash($collection)] = $key;
$this->queuedCache['delete'][spl_object_id($collection)] = $key;
}

/**
Expand All @@ -69,14 +69,14 @@ public function update(PersistentCollection $collection)
if ($isDirty && ! $isInitialized || isset($this->association['orderBy'])) {
$this->persister->update($collection);

$this->queuedCache['delete'][spl_object_hash($collection)] = $key;
$this->queuedCache['delete'][spl_object_id($collection)] = $key;

return;
}

$this->persister->update($collection);

$this->queuedCache['update'][spl_object_hash($collection)] = [
$this->queuedCache['update'][spl_object_id($collection)] = [
'key' => $key,
'list' => $collection,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Persisters\Collection\CollectionPersister;

use function spl_object_hash;
use function spl_object_id;

class ReadWriteCachedCollectionPersister extends AbstractCollectionPersister
{
Expand Down Expand Up @@ -78,7 +78,7 @@ public function delete(PersistentCollection $collection)
return;
}

$this->queuedCache['delete'][spl_object_hash($collection)] = [
$this->queuedCache['delete'][spl_object_id($collection)] = [
'key' => $key,
'lock' => $lock,
];
Expand Down Expand Up @@ -106,7 +106,7 @@ public function update(PersistentCollection $collection)
return;
}

$this->queuedCache['update'][spl_object_hash($collection)] = [
$this->queuedCache['update'][spl_object_id($collection)] = [
'key' => $key,
'lock' => $lock,
];
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use function is_array;
use function key;
use function ltrim;
use function spl_object_hash;
use function spl_object_id;

/**
* The ObjectHydrator constructs an object graph out of an SQL result set.
Expand Down Expand Up @@ -162,7 +162,7 @@ private function initRelatedCollection(
string $fieldName,
string $parentDqlAlias
): PersistentCollection {
$oid = spl_object_hash($entity);
$oid = spl_object_id($entity);
$relation = $class->associationMappings[$fieldName];
$value = $class->reflFields[$fieldName]->getValue($entity);

Expand Down Expand Up @@ -356,7 +356,7 @@ protected function hydrateRowData(array $row, array &$result)
continue;
}

$oid = spl_object_hash($parentObject);
$oid = spl_object_id($parentObject);

// Check the type of the relation (many or single-valued)
if (! ($relation['type'] & ClassMetadata::TO_ONE)) {
Expand Down Expand Up @@ -429,7 +429,7 @@ protected function hydrateRowData(array $row, array &$result)
$inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']];
if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) {
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_id($element), $inverseAssoc['fieldName'], $parentObject);
}
} elseif ($parentClass === $targetClass && $relation['mappedBy']) {
// Special case: bi-directional self-referencing one-one on the same class
Expand All @@ -438,7 +438,7 @@ protected function hydrateRowData(array $row, array &$result)
} else {
// For sure bidirectional, as there is no inverse side in unidirectional mappings
$targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject);
$this->_uow->setOriginalEntityProperty(spl_object_id($element), $relation['mappedBy'], $parentObject);
}

// Update result pointer
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
use function is_subclass_of;
use function ltrim;
use function method_exists;
use function spl_object_hash;
use function spl_object_id;
use function str_replace;
use function strpos;
use function strtolower;
Expand Down Expand Up @@ -833,7 +833,7 @@ public function getFieldValue($entity, $field)
*/
public function __toString()
{
return self::class . '@' . spl_object_hash($this);
return self::class . '@' . spl_object_id($this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use function is_object;
use function method_exists;
use function reset;
use function spl_object_hash;
use function spl_object_id;
use function sprintf;

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ public static function invalidEntityName($entityName)
*/
private static function objToStr($obj): string
{
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_id($obj);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use function array_walk;
use function get_class;
use function is_object;
use function spl_object_hash;
use function spl_object_id;

/**
* A PersistentCollection represents a collection of elements that have persistent state.
Expand Down Expand Up @@ -158,7 +158,7 @@ public function hydrateAdd($element): void
);

$this->em->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($element),
spl_object_id($element),
$this->backRefFieldName,
$this->owner
);
Expand Down Expand Up @@ -687,16 +687,16 @@ protected function doInitialize(): void
/**
* @param object[] $newObjects
*
* Note: the only reason why this entire looping/complexity is performed via `spl_object_hash`
* Note: the only reason why this entire looping/complexity is performed via `spl_object_id`
* is because we want to prevent using `array_udiff()`, which is likely to cause very
* high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in
* core, which is faster than using a callback for comparisons
*/
private function restoreNewObjectsInDirtyCollection(array $newObjects): void
{
$loadedObjects = $this->collection->toArray();
$newObjectsByOid = array_combine(array_map('spl_object_hash', $newObjects), $newObjects);
$loadedObjectsByOid = array_combine(array_map('spl_object_hash', $loadedObjects), $loadedObjects);
$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) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
use function is_array;
use function is_object;
use function reset;
use function spl_object_hash;
use function spl_object_id;
use function sprintf;
use function strpos;
use function strtoupper;
Expand Down Expand Up @@ -132,7 +132,7 @@ class BasicEntityPersister implements EntityPersister
/**
* Queued inserts.
*
* @psalm-var array<string, object>
* @psalm-var array<int, object>
*/
protected $queuedInserts = [];

Expand Down Expand Up @@ -233,7 +233,7 @@ public function getResultSetMapping()
*/
public function addInsert($entity)
{
$this->queuedInserts[spl_object_hash($entity)] = $entity;
$this->queuedInserts[spl_object_id($entity)] = $entity;
}

/**
Expand Down Expand Up @@ -640,7 +640,7 @@ protected function prepareUpdateData($entity)
}

if ($newVal !== null) {
$oid = spl_object_hash($newVal);
$oid = spl_object_id($newVal);

if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
// The associated entity $newVal is not yet persisted, so we must
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Persisters/Entity/EntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getResultSetMapping();
/**
* Get all queued inserts.
*
* @psalm-return array<string, object>
* @psalm-return array<string|int, object>
*/
public function getInserts();

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;

use function spl_object_hash;
use function spl_object_id;

/**
* This factory is used to create default repository objects for entities at runtime.
Expand All @@ -24,7 +24,7 @@ final class DefaultRepositoryFactory implements RepositoryFactory
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository
{
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_id($entityManager);

if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/ORM/Tools/DebugUnitOfWorkListener.php
< 69EF /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use function fwrite;
use function gettype;
use function is_object;
use function spl_object_hash;
use function spl_object_id;

/**
* Use this logger to dump the identity map during the onFlush event. This is useful for debugging
Expand Down Expand Up @@ -74,7 +74,7 @@ public function dumpIdentityMap(EntityManagerInterface $em)
fwrite($fh, 'Class: ' . $className . "\n");

foreach ($map as $entity) {
fwrite($fh, ' Entity: ' . $this->getIdString($entity, $uow) . ' ' . spl_object_hash($entity) . "\n");
fwrite($fh, ' Entity: ' . $this->getIdString($entity, $uow) . ' ' . spl_object_id($entity) . "\n");
fwrite($fh, " Associations:\n");

$cm = $em->getClassMetadata($className);
Expand All @@ -91,7 +91,7 @@ public function dumpIdentityMap(EntityManagerInterface $em)
fwrite($fh, '[PROXY] ');
}

fwrite($fh, $this->getIdString($value, $uow) . ' ' . spl_object_hash($value) . "\n");
fwrite($fh, $this->getIdString($value, $uow) . ' ' . spl_object_id($value) . "\n");
}
} else {
$initialized = ! ($value instanceof PersistentCollection) || $value->isInitialized();
Expand All @@ -101,12 +101,12 @@ public function dumpIdentityMap(EntityManagerInterface $em)
fwrite($fh, '[INITIALIZED] ' . $this->getType($value) . ' ' . count($value) . " elements\n");

foreach ($value as $obj) {
fwrite($fh, ' ' . $this->getIdString($obj, $uow) . ' ' . spl_object_hash($obj) . "\n");
fwrite($fh, ' ' . $this->getIdString($obj, $uow) . ' ' . spl_object_id($obj) . "\n");
}
} else {
fwrite($fh, '[PROXY] ' . $this->getType($value) . " unknown element size\n");
foreach ($value->unwrap() as $obj) {
fwrite($fh, ' ' . $this->getIdString($obj, $uow) . ' ' . spl_object_hash($obj) . "\n");
fwrite($fh, ' ' . $this->getIdString($obj, $uow) . ' ' . spl_object_id($obj) . "\n");
}
}
}
Expand Down
Loading
0