Passed
Pull Request — master (#226)
by Christopher
07:03 queued 03:16
created

AssociationStubPolymorphic::isCompatible()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 28
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 28
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 && $thisNull) {
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
        if ($thisNull && ($thatTarg != $this->getBaseType())) {
56
            return false;
57
        }
58
        if ($thatNull && ($thisTarg != $otherStub->getBaseType())) {
59
            return false;
60
        }
61
62
        return true;
63
    }
64
65
    /**
66
     * @throws InvalidOperationException
67
     * @return bool
68
     */
69
    public function isKnownSide()
70
    {
71
        if (!($this->isOk())) {
72
            throw new InvalidOperationException('Polymorphic stub not OK so known-side determination is meaningless');
73
        }
74
        return null === $this->targType;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function morphicType()
81
    {
82
        return 'polymorphic';
83
    }
84
}
85