1 | <?php |
||
8 | abstract class MorphOneOrMany extends HasOneOrMany |
||
9 | { |
||
10 | /** |
||
11 | * The foreign key type for the relationship. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $morphType; |
||
16 | |||
17 | /** |
||
18 | * The class name of the parent model. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $morphClass; |
||
23 | |||
24 | /** |
||
25 | * Create a new has many relationship instance. |
||
26 | * |
||
27 | * @param Mapper $mapper |
||
28 | * @param \Analogue\ORM\Mappable $parent |
||
29 | * @param string $type |
||
30 | * @param string $id |
||
31 | * @param string $localKey |
||
32 | * @throws \Analogue\ORM\Exceptions\MappingException |
||
33 | */ |
||
34 | public function __construct(Mapper $mapper, $parent, $type, $id, $localKey) |
||
35 | { |
||
36 | $this->morphType = $type; |
||
37 | $this->morphClass = $mapper->getManager()->getMapper($parent)->getEntityMap()->getMorphClass(); |
||
|
|||
38 | |||
39 | parent::__construct($mapper, $parent, $id, $localKey); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Set the base constraints on the relation query. |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | public function addConstraints() |
||
48 | { |
||
49 | if (static::$constraints) { |
||
50 | parent::addConstraints(); |
||
51 | |||
52 | $this->query->where($this->morphType, $this->morphClass); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get the relationship count query. |
||
58 | * |
||
59 | * @param Query $query |
||
60 | * @param Query $parent |
||
61 | * @return Query |
||
62 | */ |
||
63 | public function getRelationCountQuery(Query $query, Query $parent) |
||
64 | { |
||
65 | $query = parent::getRelationCountQuery($query, $parent); |
||
66 | |||
67 | return $query->where($this->morphType, $this->morphClass); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Set the constraints for an eager load of the relation. |
||
72 | * |
||
73 | * @param array $entities |
||
74 | * @return void |
||
75 | */ |
||
76 | public function addEagerConstraints(array $entities) |
||
77 | { |
||
78 | parent::addEagerConstraints($entities); |
||
79 | |||
80 | $this->query->where($this->morphType, $this->morphClass); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get the foreign key "type" name. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getMorphType() |
||
89 | { |
||
90 | return $this->morphType; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the plain morph type name without the table. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getPlainMorphType() |
||
99 | { |
||
100 | return last(explode('.', $this->morphType)); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get the class name of the parent model. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getMorphClass() |
||
109 | { |
||
110 | return $this->morphClass; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get the foreign key as value pair for this relation |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getForeignKeyValuePair() |
||
125 | } |
||
126 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.