Completed
Branch develop (17e8cf)
by Nate
10:01
created

InputTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 111
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
elementType() 0 1 ?
A inputTemplateVariables() 0 22 3
A getInputJs() 0 28 3
A getIndexJs() 0 30 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\element\lists\fields;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use craft\elements\db\ElementQueryInterface;
15
use flipbox\craft\elements\nestedIndex\web\assets\index\NestedElementIndex;
16
use flipbox\craft\ember\helpers\SiteHelper;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 *
22
 * @property string $inputTemplate
23
 * @property string $inputJsClass
24
 */
25
trait InputTrait
26
{
27
    /**
28
     * Returns the element class associated with this field type.
29
     *
30
     * @return string The Element class name
31
     */
32
    abstract protected static function elementType(): string;
33
34
    /**
35
     * @param null $value
36
     * @param ElementInterface|null $element
37
     * @return array
38
     * @throws \yii\base\InvalidConfigException
39
     */
40
    protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
41
    {
42
        if ($value instanceof ElementQueryInterface) {
43
            $value = $value
44
                ->anyStatus()
45
                ->ids();
46
        } elseif (!is_array($value)) {
47
            $value = [];
48
        }
49
50
        Craft::$app->getView()->registerAssetBundle(NestedElementIndex::class);
51
52
        return [
53
            'element' => $element,
54
            'container' => 'nested-index-' . $this->handle,
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...
55
            'elementType' => static::elementType(),
56
            'inputJsClass' => $this->inputJsClass,
57
            'inputJs' => $this->getInputJs($value, $element),
0 ignored issues
show
Bug introduced by
It seems like $element defined by parameter $element on line 40 can be null; however, flipbox\craft\element\li...nputTrait::getInputJs() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
58
            'indexJsClass' => 'Craft.NestedElementIndex',
59
            'indexJs' => $this->getIndexJs($element)
0 ignored issues
show
Bug introduced by
It seems like $element defined by parameter $element on line 40 can be null; however, flipbox\craft\element\li...nputTrait::getIndexJs() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
60
        ];
61
    }
62
63
    /*******************************************
64
     * JS CONFIGS
65
     *******************************************/
66
67
    /**
68
     * @param array $elementIds
69
     * @param ElementInterface $element
70
     * @return array
71
     */
72
    private function getInputJs(array $elementIds, ElementInterface $element): array
73
    {
74
        /** @var Element $element */
75
        $siteId = SiteHelper::ensureSiteId($element->siteId);
76
77
        $selectionCriteria = [
78
            'enabledForSite' => null,
79
            'siteId' => $siteId
80
        ];
81
82
        return [
83
            'elementType' => static::elementType(),
84
            'sources' => $this->inputSources($element),
0 ignored issues
show
Bug introduced by
It seems like inputSources() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
85
            'criteria' => $selectionCriteria,
86
            'sourceElementId' => $element->getId() ?: null,
87
            'viewMode' => $this->viewMode,
0 ignored issues
show
Bug introduced by
The property viewMode 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...
88
            'limit' => $this->limit,
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...
89
            'selectionLabel' => $this->selectionLabel,
0 ignored issues
show
Bug introduced by
The property selectionLabel 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...
90
            'storageKey' => 'nested.index.input.' . $this->handle,
91
            'elements' => $elementIds,
92
            'addAction' => 'element-lists/source/associate',
93
            'selectTargetAttribute' => 'target',
94
            'selectParams' => [
95
                'source' => $element->getId() ?: null,
96
                'field' => $this->id
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...
97
            ]
98
        ];
99
    }
100
101
    /**
102
     * @param ElementInterface $element
103
     * @return array
104
     */
105
    private function getIndexJs(
106
        ElementInterface $element
107
    ): array {
108
109
        /** @var Element $element */
110
        $elementId = $element->getId() !== null ? $element->getId() : false;
111
112
        return [
113
            'source' => 'nested',
114
            'context' => 'index',
115
            'viewMode' => $this->viewMode,
116
            'showStatusMenu' => true,
117
            'showSiteMenu' => true,
118
            'hideSidebar' => false,
119
            'toolbarFixed' => false,
120
            'storageKey' => 'nested.index.' . $this->handle,
121
            'updateElementsAction' => 'element-lists/element-indexes/get-elements',
122
            'submitActionsAction' => 'element-lists/element-indexes/perform-action',
123
            'loadMoreElementsAction' => 'element-lists/element-indexes/get-more-elements',
124
            'criteria' => [
125
                'enabledForSite' => null,
126
                'siteId' => SiteHelper::ensureSiteId($element->siteId),
127
                $this->handle => $elementId
128
            ],
129
            'viewParams' => [
130
                'sourceId' => $elementId,
131
                'fieldId' => $this->id
132
            ]
133
        ];
134
    }
135
}
136