Skip to content

Commit

Permalink
Use spl_object_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 21, 2020
1 parent 1b5f2c9 commit d2df726
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
class ObjectStorage implements \Countable, \IteratorAggregate, \ArrayAccess
{
/**
* The objects contained in the storage, indexed by object hash.
* The objects contained in the storage, as a map of object id to object.
*
* @psalm-var array<string, K>
* @psalm-var array<int, K>
*
* @var array<string, object>
* @var array<int, object>
*/
private $objects = [];

/**
* The data in the storage, indexed by object hash.
* The data in the storage, as a map of object id to datum.
*
* @psalm-var array<string, V>
* @psalm-var array<int, V>
*
* @var array<string, mixed>
* @var array<int, mixed>
*/
private $data = [];

Expand All @@ -46,9 +46,9 @@ class ObjectStorage implements \Countable, \IteratorAggregate, \ArrayAccess
*/
public function has(object $object) : bool
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

return isset($this->objects[$hash]);
return isset($this->objects[$id]);
}

/**
Expand All @@ -65,10 +65,10 @@ public function has(object $object) : bool
*/
public function get(object $object)
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

if (isset($this->data[$hash])) {
return $this->data[$hash];
if (isset($this->data[$id])) {
return $this->data[$id];
}

return null;
Expand All @@ -87,10 +87,10 @@ public function get(object $object)
*/
public function set(object $object, $data = null) : void
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

$this->objects[$hash] = $object;
$this->data[$hash] = $data;
$this->objects[$id] = $object;
$this->data[$id] = $data;
}

/**
Expand All @@ -106,10 +106,10 @@ public function set(object $object, $data = null) : void
*/
public function remove(object $object) : void
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

unset($this->objects[$hash]);
unset($this->data[$hash]);
unset($this->objects[$id]);
unset($this->data[$id]);
}

/**
Expand Down Expand Up @@ -147,8 +147,8 @@ public function getObjects() : array
*/
public function getIterator() : \Traversable
{
foreach ($this->objects as $hash => $object) {
yield $object => $this->data[$hash];
foreach ($this->objects as $id => $object) {
yield $object => $this->data[$id];
}
}

Expand All @@ -164,10 +164,10 @@ public function getIterator() : \Traversable
*/
public function offsetGet($object)
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

if (isset($this->objects[$hash])) {
return $this->data[$hash];
if (isset($this->objects[$id])) {
return $this->data[$id];
}

throw new \UnexpectedValueException('Object not found.');
Expand All @@ -184,10 +184,10 @@ public function offsetGet($object)
*/
public function offsetSet($object, $value) : void
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

$this->objects[$hash] = $object;
$this->data[$hash] = $value;
$this->objects[$id] = $object;
$this->data[$id] = $value;
}

/**
Expand All @@ -199,10 +199,10 @@ public function offsetSet($object, $value) : void
*/
public function offsetUnset($object) : void
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

unset($this->objects[$hash]);
unset($this->data[$hash]);
unset($this->objects[$id]);
unset($this->data[$id]);
}

/**
Expand All @@ -214,8 +214,8 @@ public function offsetUnset($object) : void
*/
public function offsetExists($object) : bool
{
$hash = spl_object_hash($object);
$id = spl_object_id($object);

return isset($this->objects[$hash]);
return isset($this->objects[$id]);
}
}

0 comments on commit d2df726

Please sign in to comment.