• 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

88.73
/src/PropertyMapping/EntityMapping.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\ORM\PropertyMapping;
6

7
use Brick\ORM\EntityMetadata;
8
use Brick\ORM\Gateway;
9
use Brick\ORM\PropertyMapping;
10

11
use function array_slice;
12
use function get_class;
13

14
/**
15
 * @internal
16
 */
17
class EntityMapping implements PropertyMapping
18
{
19
    /**
20
     * The class metadata of the target entity.
21
     */
22
    public EntityMetadata $classMetadata;
23

24
    public string $fieldNamePrefix;
25

26
    public bool $isNullable;
27

28
    /**
29
     * @param EntityMetadata $classMetadata   The target entity class metadata.
30
     * @param string         $fieldNamePrefix The prefix for field names.
31
     * @param bool           $isNullable      Whether the property is nullable.
32
     */
33
    public function __construct(EntityMetadata $classMetadata, string $fieldNamePrefix, bool $isNullable)
34
    {
UNCOV
35
        $this->classMetadata = $classMetadata;
×
UNCOV
36
        $this->fieldNamePrefix = $fieldNamePrefix;
×
UNCOV
37
        $this->isNullable = $isNullable;
×
38
    }
39

40
    public function getType(): ?string
41
    {
UNCOV
42
        return $this->classMetadata->className;
×
43
    }
44

45
    public function isNullable(): bool
46
    {
47
        return $this->isNullable;
1✔
48
    }
49

50
    /**
51
     * @todo precompute for better performance
52
     */
53
    public function getFieldNames(): array
54
    {
55
        $names = [];
57✔
56

57
        if ($this->classMetadata->discriminatorColumn !== null) {
57✔
58
            $names[] = $this->fieldNamePrefix . $this->classMetadata->discriminatorColumn;
7✔
59
        }
60

61
        foreach ($this->classMetadata->idProperties as $prop) {
57✔
62
            foreach ($this->classMetadata->propertyMappings[$prop]->getFieldNames() as $name) {
57✔
63
                $names[] = $this->fieldNamePrefix . $name;
57✔
64
            }
65
        }
66

67
        return $names;
57✔
68
    }
69

70
    /**
71
     * @todo precompute for better performance
72
     */
73
    public function getInputValuesCount(): int
74
    {
75
        $count = 0;
34✔
76

77
        if ($this->classMetadata->discriminatorColumn !== null) {
34✔
78
            $count++;
3✔
79
        }
80

81
        foreach ($this->classMetadata->idProperties as $prop) {
34✔
82
            $propertyMapping = $this->classMetadata->propertyMappings[$prop];
34✔
83
            $count += $propertyMapping->getInputValuesCount();
34✔
84
        }
85

86
        return $count;
34✔
87
    }
88

89
    /**
90
     * @todo precompute for better performance
91
     */
92
    public function getFieldToInputValuesSQL(array $fieldNames): array
93
    {
94
        $wrappedFields = [];
52✔
95
        $currentIndex = 0;
52✔
96

97
        if ($this->classMetadata->discriminatorColumn !== null) {
52✔
98
            $wrappedFields[] = $fieldNames[$currentIndex++];
4✔
99
        }
100

101
        foreach ($this->classMetadata->idProperties as $prop) {
52✔
102
            $propertyMapping = $this->classMetadata->propertyMappings[$prop];
52✔
103
            $readFieldCount = $propertyMapping->getInputValuesCount();
52✔
104

105
            $currentFieldNames = array_slice($fieldNames, $currentIndex, $readFieldCount);
52✔
106
            $currentIndex += $readFieldCount;
52✔
107

108
            foreach ($propertyMapping->getFieldToInputValuesSQL($currentFieldNames) as $wrappedField) {
52✔
109
                $wrappedFields[] = $wrappedField;
52✔
110
            }
111
        }
112

113
        return $wrappedFields;
52✔
114
    }
115

116
    public function convertInputValuesToProp(Gateway $gateway, array $values): mixed
117
    {
118
        $currentIndex = 0;
23✔
119

120
        if ($this->classMetadata->discriminatorColumn !== null) {
23✔
121
            /** @var int|string|null $discriminatorValue */
122
            $discriminatorValue = $values[$currentIndex++];
3✔
123

124
            if ($discriminatorValue === null) {
3✔
125
                return null;
3✔
126
            }
127

UNCOV
128
            $className = $this->classMetadata->discriminatorMap[$discriminatorValue];
×
129
        } else {
130
            $className = $this->classMetadata->className;
23✔
131
        }
132

133
        $id = [];
23✔
134

135
        foreach ($this->classMetadata->idProperties as $prop) {
23✔
136
            $propertyMapping = $this->classMetadata->propertyMappings[$prop];
23✔
137
            $readFieldCount = $propertyMapping->getInputValuesCount();
23✔
138

139
            $currentInputValues = array_slice($values, $currentIndex, $readFieldCount);
23✔
140
            $currentIndex += $readFieldCount;
23✔
141

142
            $value = $propertyMapping->convertInputValuesToProp($gateway, $currentInputValues);
23✔
143

144
            if ($value === null) {
23✔
145
                return null;
2✔
146
            }
147

148
            $id[$prop] = $value;
22✔
149
        }
150

151
        return $gateway->getReference($className, $id);
22✔
152
    }
153

154
    /**
155
     * @todo use Gateway::getIdentity() instead; currently does not check that the object has an identity
156
     */
157
    public function convertPropToFields(mixed $propValue): array
158
    {
159
        $result = [];
7✔
160

161
        /** @var object|null $entity */
162
        $entity = $propValue;
7✔
163

164
        if ($this->classMetadata->discriminatorColumn !== null) {
7✔
165
            if ($entity === null) {
4✔
166
                $result[] = ['NULL'];
4✔
167
            } else {
UNCOV
168
                $class = get_class($entity);
×
UNCOV
169
                $discriminatorValue = $this->classMetadata->inverseDiscriminatorMap[$class];
×
UNCOV
170
                $result[] = ['?', $discriminatorValue];
×
171
            }
172
        }
173

174
        $idProperties = $this->classMetadata->idProperties;
7✔
175

176
        $identity = [];
7✔
177

178
        if ($entity !== null) {
7✔
179
            (function () use ($idProperties, &$identity): void {
7✔
180
                foreach ($idProperties as $prop) {
7✔
181
                    $identity[$prop] = $this->{$prop};
7✔
182
                }
183
            })->bindTo($entity, $entity)();
7✔
184
        } else {
185
            foreach ($idProperties as $prop) {
4✔
186
                $identity[$prop] = null;
4✔
187
            }
188
        }
189

190
        foreach ($idProperties as $prop) {
7✔
191
            $propertyMapping = $this->classMetadata->propertyMappings[$prop];
7✔
192

193
            foreach ($propertyMapping->convertPropToFields($identity[$prop]) as $expressionAndValues) {
7✔
194
                $result[] = $expressionAndValues;
7✔
195
            }
196
        }
197

198
        return $result;
7✔
199
    }
200
}
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