MorphOneOrMany   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 10
Bugs 4 Features 1
Metric Value
wmc 9
c 10
b 4
f 1
lcom 1
cbo 5
dl 0
loc 118
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getForeignKeyValuePair() 0 7 1
A __construct() 0 7 1
A addConstraints() 0 8 2
A getRelationCountQuery() 0 6 1
A addEagerConstraints() 0 6 1
A getMorphType() 0 4 1
A getPlainMorphType() 0 4 1
A getMorphClass() 0 4 1
1
<?php
2
3
namespace Analogue\ORM\Relationships;
4
5
use Analogue\ORM\System\Mapper;
6
use Analogue\ORM\System\Query;
7
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $mapper->getManager()->g...yMap()->getMorphClass() can also be of type integer. However, the property $morphClass is declared as type string. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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()
119
    {
120
        return [
121
            $this->getPlainForeignKey() => $this->getParentKey(),
122
            $this->getPlainMorphType() => $this->morphClass,
123
        ];
124
    }
125
}
126