• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / orm / 23255296146

18 Mar 2026 04:24PM UTC coverage: 47.104%. Remained the same
23255296146

push

github

BenMorel
Avoid \Exception that somehow confuses ECS

1 of 5 new or added lines in 1 file covered. (20.0%)

402 existing lines in 24 files now uncovered.

553 of 1174 relevant lines covered (47.1%)

10.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/IdentityMap.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\ORM;
6

7
use Brick\ORM\Exception\IdentityConflictException;
8

9
/**
10
 * The identity map.
11
 *
12
 * For performance reasons, no consistency checks are performed. In particular, it is assumed that:
13
 *
14
 * - identity arrays contain integers and strings only;
15
 * - multiple calls to set() and get() for a given class name always contain the same number of elements for the
16
 *   identity, and always in the same order.
17
 *
18
 * These conditions must always be met by the ORM classes that consume the identity map.
19
 * Failure to meet these conditions would result in PHP errors.
20
 */
21
class IdentityMap
22
{
23
    /**
24
     * The entities, indexed by root class name and identity (one nested array per identity value).
25
     */
26
    private array $entities = [];
27

28
    /**
29
     * Retrieves an entity from the identity map.
30
     *
31
     * @param class-string     $class    The root entity class name.
32
     * @param list<int|string> $identity The list of scalar values that form the entity's identity.
33
     *
34
     * @return object|null The entity, or null if not found.
35
     */
36
    public function get(string $class, array $identity): ?object
37
    {
UNCOV
38
        $ref = &$this->entities[$class];
×
39

UNCOV
40
        foreach ($identity as $key) {
×
UNCOV
41
            $ref = &$ref[$key];
×
42
        }
43

UNCOV
44
        return $ref;
×
45
    }
46

47
    /**
48
     * Adds an entity to the identity map.
49
     *
50
     * If the entity already exists in the identity map, this method does nothing.
51
     * If another entity already exists under this identity, an exception is thrown.
52
     *
53
     * @param class-string     $class    The root entity class name.
54
     * @param list<int|string> $identity The list of scalar values that form the entity's identity.
55
     * @param object           $entity   The entity to add.
56
     *
57
     * @throws IdentityConflictException If another instance with the same identity already exists.
58
     */
59
    public function set(string $class, array $identity, object $entity): void
60
    {
UNCOV
61
        $ref = &$this->entities[$class];
×
62

UNCOV
63
        foreach ($identity as $key) {
×
UNCOV
64
            $ref = &$ref[$key];
×
65
        }
66

UNCOV
67
        if ($ref !== null && $ref !== $entity) {
×
UNCOV
68
            throw IdentityConflictException::identityMapConflict($class, $identity);
×
69
        }
70

UNCOV
71
        $ref = $entity;
×
72
    }
73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc