Passed
Pull Request — master (#226)
by Christopher
06:17
created

AssociationStubPolymorphic::isCompatible()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 18
c 1
b 1
f 0
dl 0
loc 29
rs 6.9666
cc 12
nc 6
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 declare(strict_types=1);
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
use POData\Common\InvalidOperationException;
6
7
class AssociationStubPolymorphic extends AssociationStubBase
8
{
9
    /**
10
     * @var string
11
     */
12
    private $morphType;
13
14
    /**
15
     * @return string
16
     */
17
    public function getMorphType()
18
    {
19
        return $this->morphType;
20
    }
21
22
    /**
23
     * @param string $morphType
24
     */
25
    public function setMorphType($morphType)
26
    {
27
        $this->morphType = $morphType;
28
    }
29
30
    /**
31
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase $otherStub
32
     *
33
     * @return bool
34
     */
35
    public function isCompatible(AssociationStubBase $otherStub)
36
    {
37
        if (!parent::isCompatible($otherStub)) {
38
            return false;
39
        }
40
        $thisTarg = $this->getTargType();
41
        $thatTarg = $otherStub->getTargType();
42
        $thisNull = null === $thisTarg;
43
        $thatNull = null === $thatTarg;
44
        if ($thisNull == $thatNull && null === $thatNull) {
0 ignored issues
show
introduced by
The condition null === $thatNull is always false.
Loading history...
45
            return false;
46
        }
47
        if((!$thisNull &&
48
            !$thatNull) &&
49
            ($this->getBaseType() !== $otherStub->getTargType() ||
50
            $otherStub->getBaseType() !== $this->getTargType())
51
        ){
52
            return false;
53
        }
54
55
56
        if ($thisNull && ($thatTarg != $this->getBaseType())) {
57
            return false;
58
        }
59
        if ($thatNull && ($thisTarg != $otherStub->getBaseType())) {
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66
    /**
67
     * @throws InvalidOperationException
68
     * @return bool
69
     */
70
    public function isKnownSide()
71
    {
72
        if (!($this->isOk())) {
73
            throw new InvalidOperationException('Polymorphic stub not OK so known-side determination is meaningless');
74
        }
75
        return null === $this->targType;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function morphicType()
82
    {
83
        return 'polymorphic';
84
    }
85
}
86