Completed
Push — master ( 69ca06...8af5d6 )
by Nate
03:36
created

ElementMutator::internalResolveElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\traits;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
15
/**
16
 * @property int|null $elementId
17
 * @property Element|ElementInterface|null $element
18
 *
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait ElementMutator
23
{
24
    /**
25
     * @var Element|null
26
     */
27
    private $element;
28
29
    /**
30
     * Set associated elementId
31
     *
32
     * @param $id
33
     * @return $this
34
     */
35
    public function setElementId(int $id)
36
    {
37
        $this->elementId = $id;
38
        return $this;
39
    }
40
41
    /**
42
     * Get associated elementId
43
     *
44
     * @return int|null
45
     */
46
    public function getElementId()
47
    {
48
        if (null === $this->elementId && null !== $this->element) {
49
            $this->elementId = $this->element->id;
50
        }
51
52
        return $this->elementId;
53
    }
54
55
    /**
56
     * Associate a element
57
     *
58
     * @param mixed $element
59
     * @return $this
60
     */
61
    public function setElement($element = null)
62
    {
63
        $this->element = null;
64
65
        if (!$element = $this->internalResolveElement($element)) {
66
            $this->element = $this->elementId = null;
67
        } else {
68
            $this->elementId = $element->id;
1 ignored issue
show
Bug introduced by
Accessing id on the interface craft\base\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
            $this->element = $element;
0 ignored issues
show
Documentation Bug introduced by
It seems like $element of type object<craft\base\ElementInterface> is incompatible with the declared type object<craft\base\Element>|null of property $element.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return ElementInterface|null
77
     */
78
    public function getElement(): ElementInterface
79
    {
80
        if ($this->element === null) {
81
            $element = $this->resolveElement();
82
            $this->setElement($element);
83
            return $element;
84
        }
85
86
        $elementId = $this->elementId;
87
        if ($elementId !== null &&
88
            $elementId !== $this->element->id
89
        ) {
90
            $this->element = null;
91
            return $this->getElement();
92
        }
93
94
        return $this->element;
95
    }
96
97
    /**
98
     * @return ElementInterface|null
99
     */
100
    protected function resolveElement()
101
    {
102
        if ($model = $this->resolveElementFromId()) {
103
            return $model;
104
        }
105
106
        return null;
107
    }
108
109
    /**
110
     * @return ElementInterface|null
111
     */
112
    private function resolveElementFromId()
113
    {
114
        if (null === $this->elementId) {
115
            return null;
116
        }
117
118
        return Craft::$app->getElements()->getElementById($this->elementId);
119
    }
120
121
    /**
122
     * @param $element
123
     * @return ElementInterface|Element|null
124
     */
125
    protected function internalResolveElement($element = null)
126
    {
127
        if ($element instanceof ElementInterface) {
128
            return $element;
129
        }
130
131
        if (is_numeric($element)) {
132
            return Craft::$app->getElements()->getElementById($element);
133
        }
134
135
        if (is_string($element)) {
136
            return Craft::$app->getElements()->getElementByUri($element);
137
        }
138
139
        return Craft::$app->getElements()->createElement($element);
140
    }
141
}
142