Failed Conditions
Push — master ( 3ca65e...6e095f )
by Luís
20s
created

ORMInvalidArgumentException   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Test Coverage

Coverage 77.61%

Importance

Changes 0
Metric Value
dl 0
loc 242
ccs 52
cts 67
cp 0.7761
rs 10
c 0
b 0
f 0
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
A scheduleInsertTwice() 0 3 1
A scheduleInsertForRemovedEntity() 0 3 1
A entityHasNoIdentity() 0 3 1
A newEntityFoundThroughRelationship() 0 3 1
A invalidIdentifierBindingEntity() 0 3 1
A detachedEntityCannot() 0 3 1
A entityNotManaged() 0 4 1
A detachedEntityFoundThroughRelationship() 0 5 1
A readOnlyRequiresManagedEntity() 0 3 1
A invalidCompositeIdentifier() 0 4 1
A invalidObject() 0 4 1
A entityWithoutIdentity() 0 5 1
A scheduleInsertForManagedEntity() 0 3 1
A entityIsRemoved() 0 3 1
A newEntitiesFoundThroughRelationships() 0 19 2
A newEntityFoundThroughRelationshipMessage() 0 12 2
A invalidAssociation() 0 10 2
A invalidEntityName() 0 3 1
A objToStr() 0 3 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM;
21
use Doctrine\ORM\Mapping\ClassMetadata;
22
23
/**
24
 * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
25
 *
26
 * @author Benjamin Eberlei <[email protected]>
27
 */
28
class ORMInvalidArgumentException extends \InvalidArgumentException
29
{
30
    /**
31
     * @param object $entity
32
     *
33
     * @return ORMInvalidArgumentException
34
     */
35 1
    static public function scheduleInsertForManagedEntity($entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
36
    {
37 1
        return new self("A managed+dirty entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
38
    }
39
40
    /**
41
     * @param object $entity
42
     *
43
     * @return ORMInvalidArgumentException
44
     */
45 1
    static public function scheduleInsertForRemovedEntity($entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
46
    {
47 1
        return new self("Removed entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
48
    }
49
50
    /**
51
     * @param object $entity
52
     *
53
     * @return ORMInvalidArgumentException
54
     */
55 1
    static public function scheduleInsertTwice($entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
56
    {
57 1
        return new self("Entity " . self::objToStr($entity) . " can not be scheduled for insertion twice.");
58
    }
59
60
    /**
61
     * @param string $className
62
     * @param object $entity
63
     *
64
     * @return ORMInvalidArgumentException
65
     */
66 6
    static public function entityWithoutIdentity($className, $entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
67
    {
68 6
        return new self(
69 6
            "The given entity of type '" . $className . "' (".self::objToStr($entity).") has no identity/no " .
70 6
            "id values set. It cannot be added to the identity map."
71
        );
72
    }
73
74
    /**
75
     * @param object $entity
76
     *
77
     * @return ORMInvalidArgumentException
78
     */
79 1
    static public function readOnlyRequiresManagedEntity($entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
80
    {
81 1
        return new self("Only managed entities can be marked or checked as read only. But " . self::objToStr($entity) . " is not");
82
    }
83
84
    /**
85
     * @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array
86
 *                                                              of [array $associationMapping, object $entity] pairs
87
     *
88
     * @return ORMInvalidArgumentException
89
     */
90 8
    static public function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
91
    {
92 8
        $errorMessages = array_map(
93 8
            function (array $newEntityWithAssociation) : string {
94 8
                [$associationMapping, $entity] = $newEntityWithAssociation;
95
96 8
                return self::newEntityFoundThroughRelationshipMessage($associationMapping, $entity);
97 8
            },
98 8
            $newEntitiesWithAssociations
99
        );
100
101 8
        if (1 === count($errorMessages)) {
102 7
            return new self(reset($errorMessages));
103
        }
104
105 1
        return new self(
106
            'Multiple non-persisted new entities were found through the given association graph:'
107
            . "\n\n * "
108 1
            . implode("\n * ", $errorMessages)
109
        );
110
    }
111
112
    /**
113
     * @param array  $associationMapping
114
     * @param object $entry
115
     *
116
     * @return ORMInvalidArgumentException
117
     */
118
    static public function newEntityFoundThroughRelationship(array $associationMapping, $entry)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
119
    {
120
        return new self(self::newEntityFoundThroughRelationshipMessage($associationMapping, $entry));
121
    }
122
123
    /**
124
     * @param array  $assoc
125
     * @param object $entry
126
     *
127
     * @return ORMInvalidArgumentException
128
     */
129
    static public function detachedEntityFoundThroughRelationship(array $assoc, $entry)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
130
    {
131
        return new self("A detached entity of type " . $assoc['targetEntity'] . " (" . self::objToStr($entry) . ") "
132
            . " was found through the relationship '" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' "
133
            . "during cascading a persist operation.");
134
    }
135
136
    /**
137
     * @param object $entity
138
     *
139
     * @return ORMInvalidArgumentException
140
     */
141 1
    static public function entityNotManaged($entity)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
142
    {
143 1
        return new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " .
144 1
            "from the database or registered as new through EntityManager#persist");
145
    }
146
147
    /**
148
     * @param object $entity
149
     * @param string $operation
150
     *
151
     * @return ORMInvalidArgumentException
152
     */
153
    static public function entityHasNoIdentity($entity, $operation)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
154
    {
155
        return new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
156
    }
157
158
    /**
159
     * @param object $entity
160
     * @param string $operation
161
     *
162
     * @return ORMInvalidArgumentException
163
     */
164
    static public function entityIsRemoved($entity, $operation)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
165
    {
166
        return new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
167
    }
168
169
    /**
170
     * @param object $entity
171
     * @param string $operation
172
     *
173
     * @return ORMInvalidArgumentException
174
     */
175
    static public function detachedEntityCannot($entity, $operation)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
176
    {
177
        return new self("Detached entity " . self::objToStr($entity) . " cannot be " . $operation);
178
    }
179
180
    /**
181
     * @param string $context
182
     * @param mixed  $given
183
     * @param int    $parameterIndex
184
     *
185
     * @return ORMInvalidArgumentException
186
     */
187 5
    public static function invalidObject($context, $given, $parameterIndex = 1)
188
    {
189 5
        return new self($context . ' expects parameter ' . $parameterIndex .
190 5
            ' to be an entity object, '. gettype($given) . ' given.');
191
    }
192
193
    /**
194
     * @return ORMInvalidArgumentException
195
     */
196
    public static function invalidCompositeIdentifier()
197
    {
198
        return new self("Binding an entity with a composite primary key to a query is not supported. " .
199
            "You should split the parameter into the explicit fields and bind them separately.");
200
    }
201
202
    /**
203
     * @return ORMInvalidArgumentException
204
     */
205 1
    public static function invalidIdentifierBindingEntity()
206
    {
207 1
        return new self("Binding entities to query parameters only allowed for entities that have an identifier.");
208
    }
209
210
    /**
211
     * @param ClassMetadata $targetClass
212
     * @param array         $assoc
213
     * @param mixed         $actualValue
214
     *
215
     * @return self
216
     */
217 15
    public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue)
218
    {
219 15
        $expectedType = $targetClass->getName();
220
221 15
        return new self(sprintf(
222 15
            'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.',
223 15
            $expectedType,
224 15
            $assoc['sourceEntity'],
225 15
            $assoc['fieldName'],
226 15
            is_object($actualValue) ? get_class($actualValue) : gettype($actualValue)
227
        ));
228
    }
229
230
    /**
231
     * Used when a given entityName hasn't the good type
232
     *
233
     * @param mixed $entityName The given entity (which shouldn't be a string)
234
     *
235
     * @return self
236
     */
237 6
    public static function invalidEntityName($entityName)
238
    {
239 6
        return new self(sprintf('Entity name must be a string, %s given', gettype($entityName)));
240
    }
241
242
    /**
243
     * Helper method to show an object as string.
244
     *
245
     * @param object $obj
246
     *
247
     * @return string
248
     */
249 19
    private static function objToStr($obj) : string
250
    {
251 19
        return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj);
252
    }
253
254
    /**
255
     * @param array  $associationMapping
256
     * @param object $entity
257
     */
258 8
    private static function newEntityFoundThroughRelationshipMessage(array $associationMapping, $entity) : string
259
    {
260
        return 'A new entity was found through the relationship \''
261 8
            . $associationMapping['sourceEntity'] . '#' . $associationMapping['fieldName'] . '\' that was not'
262 8
            . ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.'
263 8
            . ' To solve this issue: Either explicitly call EntityManager#persist()'
264 8
            . ' on this unknown entity or configure cascade persist'
265 8
            . ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).'
266 8
            . (method_exists($entity, '__toString')
267 2
                ? ''
268
                : ' If you cannot find out which entity causes the problem implement \''
269 8
                . $associationMapping['targetEntity'] . '#__toString()\' to get a clue.'
270
            );
271
    }
272
}
273