Element::applyDefaultProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/link/license
6
 * @link       https://www.flipboxfactory.com/software/link/
7
 */
8
9
namespace flipbox\link\types\traits;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\link\fields\Link;
14
use flipbox\link\types\TypeInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait Element
21
{
22
    use Base;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    protected function applyDefaultProperties()
28
    {
29
        $this->handle = 'elementId';
0 ignored issues
show
Bug introduced by
The property handle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        $this->id = static::class;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        $this->allowLimit = false;
0 ignored issues
show
Bug introduced by
The property allowLimit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->limit = 1;
0 ignored issues
show
Bug introduced by
The property limit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
    }
34
35
    /**
36
     * @var bool
37
     */
38
    public $overrideText = true;
39
40
    /**
41
     * @var int
42
     */
43
    protected $elementId;
44
45
    /**
46
     * @var ElementInterface
47
     */
48
    protected $element;
49
50
    /**
51
     * @return string
52
     */
53
    abstract protected function getElementText(): string;
54
55
    /**
56
     * @param int $id
57
     * @return ElementInterface|null
58
     */
59
    abstract protected function lookupElementById(int $id);
60
61
    /**
62
     * @return int
63
     */
64
    public function getElementId()
65
    {
66
        return $this->elementId;
67
    }
68
69
    /**
70
     * @return ElementInterface|null
71
     */
72
    protected function findElement()
73
    {
74
        if ($this->element === null) {
75
            $this->element = $this->lookupElement() ?: false;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->lookupElement() ?: false can also be of type false. However, the property $element is declared as type object<craft\base\ElementInterface>. 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...
76
        }
77
78
        return $this->element === false ? null : $this->element;
79
    }
80
81
    /**
82
     * @return ElementInterface|null
83
     */
84
    protected function lookupElement()
85
    {
86
        if ($this->elementId === null) {
87
            return null;
88
        }
89
90
        return $this->lookupElementById($this->elementId);
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function properties(): array
97
    {
98
        return [
99
            'elementId',
100
            'text'
101
        ];
102
    }
103
104
    /**
105
     * @param $elementId
106
     */
107
    public function setElementId($elementId)
108
    {
109
        if (is_array($elementId)) {
110
            $elementId = reset($elementId);
111
        }
112
        $this->elementId = (int)$elementId;
113
114
        // Clear element cache on change
115
        if ($this->element === false ||
116
            ($this->element && $this->element->getId() !== $this->elementId)
117
        ) {
118
            $this->element = null;
119
        }
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function inputHtml(Link $field, TypeInterface $type = null, ElementInterface $element = null): string
126
    {
127
        return Craft::$app->getView()->renderTemplate(
128
            'link/_components/fieldtypes/Link/types/element/input',
129
            [
130
                'value' => $type,
131
                'field' => $field,
132
                'type' => $this,
133
                'elementSelectInput' => $this->getInputHtml($type, $element)
0 ignored issues
show
Bug introduced by
The method getInputHtml() does not exist on flipbox\link\types\traits\Element. Did you maybe mean inputHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
134
            ]
135
        );
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function settingsHtml(): string
142
    {
143
        return Craft::$app->getView()->renderTemplate(
144
            'link/_components/fieldtypes/Link/types/element/settings',
145
            [
146
                'type' => $this,
147
                'elementSelectSettings' => $this->getSettingsHtml()
0 ignored issues
show
Bug introduced by
The method getSettingsHtml() does not exist on flipbox\link\types\traits\Element. Did you maybe mean settingsHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
            ]
149
        );
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function getText(): string
156
    {
157
        if ($this->text) {
158
            return $this->text;
159
        }
160
        return $this->getElementText();
161
    }
162
}
163