Completed
Pull Request — master (#114)
by Bart
07:39
created

FieldLayoutBehavior::getPrepareFieldLayout()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
namespace NerdsAndCompany\Schematic\Behaviors;
3
4
use Craft;
5
use yii\base\Behavior;
6
use craft\models\FieldLayout;
7
8
/**
9
 * Schematic FieldLayout Behavior.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class FieldLayoutBehavior extends Behavior
20
{
21
    /**
22
     * Get field layout definition.
23
     *
24
     * @param FieldLayout $fieldLayout
25
     *
26
     * @return array
27
     */
28
    public function getFieldLayoutDefinition(FieldLayout $fieldLayout)
29
    {
30
        if ($fieldLayout->getTabs()) {
31
            $tabDefinitions = [];
32
33
            foreach ($fieldLayout->getTabs() as $tab) {
34
                $tabDefinitions[$tab->name] = $this->getFieldLayoutFieldsDefinition($tab->getFields());
0 ignored issues
show
Documentation introduced by
$tab->getFields() is of type array<integer,object<craft\base\FieldInterface>>, but the function expects a array<integer,object<Ner...FieldLayoutFieldModel>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
            }
36
37
            return ['tabs' => $tabDefinitions];
38
        }
39
40
        return ['fields' => $this->getFieldLayoutFieldsDefinition($fieldLayout->getFields())];
0 ignored issues
show
Documentation introduced by
$fieldLayout->getFields() is of type array<integer,object<craft\base\FieldInterface>>, but the function expects a array<integer,object<Ner...FieldLayoutFieldModel>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
    }
42
43
    /**
44
     * Get field layout fields definition.
45
     *
46
     * @param FieldLayoutFieldModel[] $fields
47
     *
48
     * @return array
49
     */
50
    private function getFieldLayoutFieldsDefinition(array $fields)
51
    {
52
        $fieldDefinitions = [];
53
54
        foreach ($fields as $field) {
55
            $fieldDefinitions[$field->handle] = $field->required;
56
        }
57
58
        return $fieldDefinitions;
59
    }
60
61
    /**
62
     * Attempt to import a field layout.
63
     *
64
     * @param array $fieldLayoutDef
65
     *
66
     * @return FieldLayout
67
     */
68
    public function getFieldLayout(array $fieldLayoutDef)
69
    {
70
        $layoutFields = [];
71
        $requiredFields = [];
72
73
        if (array_key_exists('tabs', $fieldLayoutDef)) {
74
            foreach ($fieldLayoutDef['tabs'] as $tabName => $tabDef) {
75
                $layoutTabFields = $this->getPrepareFieldLayout($tabDef);
76
                $requiredFields = array_merge($requiredFields, $layoutTabFields['required']);
77
                $layoutFields[$tabName] = $layoutTabFields['fields'];
78
            }
79
        } elseif (array_key_exists('fields', $fieldLayoutDef)) {
80
            $layoutTabFields = $this->getPrepareFieldLayout($fieldLayoutDef);
81
            $requiredFields = $layoutTabFields['required'];
82
            $layoutFields = $layoutTabFields['fields'];
83
        }
84
85
        $fieldLayout = Craft::$app->fields->assembleLayout($layoutFields, $requiredFields);
86
        $fieldLayout->type = ElementType::Entry;
87
88
        return $fieldLayout;
89
    }
90
91
    /**
92
     * Get a prepared fieldLayout for the craft assembleLayout function.
93
     *
94
     * @param array $fieldLayoutDef
95
     *
96
     * @return array
97
     */
98
    private function getPrepareFieldLayout(array $fieldLayoutDef)
99
    {
100
        $layoutFields = [];
101
        $requiredFields = [];
102
103
        foreach ($fieldLayoutDef as $fieldHandle => $required) {
104
            $field = Craft::$app->fields->getFieldByHandle($fieldHandle);
105
            if ($field instanceof FieldModel) {
0 ignored issues
show
Bug introduced by
The class NerdsAndCompany\Schematic\Behaviors\FieldModel does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
106
                $layoutFields[] = $field->id;
107
108
                if ($required) {
109
                    $requiredFields[] = $field->id;
110
                }
111
            }
112
        }
113
114
        return [
115
          'fields' => $layoutFields,
116
          'required' => $requiredFields,
117
        ];
118
    }
119
}
120