Passed
Pull Request — master (#154)
by Alex
07:05
created

AssociationStubPolymorphic::isCompatible()   C

Complexity

Conditions 13
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 5.1234
cc 13
eloc 19
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
class AssociationStubPolymorphic extends AssociationStubBase
6
{
7
    /**
8
     * @var string
9
     */
10
    private $morphType;
11
12
    /**
13
     * @return string
14
     */
15
    public function getMorphType()
16
    {
17
        return $this->morphType;
18
    }
19
20
    /**
21
     * @param string $morphType
22
     */
23
    public function setMorphType($morphType)
24
    {
25
        $this->morphType = $morphType;
26
    }
27
28
    /**
29
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase $otherStub
30
     *
31
     * @return bool
32
     */
33
    public function isCompatible(AssociationStubBase $otherStub)
34
    {
35
        if (!parent::isCompatible($otherStub)) {
36
            return false;
37
        }
38
        $thisTarg = $this->getTargType();
39
        $thatTarg = $otherStub->getTargType();
40
        $thisNull = null === $thisTarg;
41
        $thatNull = null === $thatTarg;
42
        if ($thisNull == $thatNull) {
43
            return false;
44
        }
45
        if ($thisNull && ($thatTarg != $this->getBaseType())) {
46
            return false;
47
        }
48
        if ($thatNull && ($thisTarg != $otherStub->getBaseType())) {
49
            return false;
50
        }
51
52
        if (AssociationStubRelationType::MANY() == $this->getMultiplicity()
53
            && AssociationStubRelationType::MANY() == $otherStub->getMultiplicity()) {
54
            if ($thisNull && ($otherStub->getForeignField() != $this->getKeyField())) {
55
                return false;
56
            }
57
58
            if ($thatNull && ($this->getForeignField() != $otherStub->getKeyField())) {
59
                return false;
60
            }
61
        }
62
63
        return true;
64
    }
65
66
    public function isKnownSide()
67
    {
68
        assert($this->isOk(), 'Polymorphic stub not OK so known-side determination is meaningless');
69
        return null === $this->targType;
70
    }
71
}
72