Completed
Pull Request — master (#114)
by Bart
06:18
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
use craft\elements\Entry;
8
9
/**
10
 * Schematic FieldLayout Behavior.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class FieldLayoutBehavior extends Behavior
21
{
22
    /**
23
     * Get field layout definition.
24
     *
25
     * @param FieldLayout $fieldLayout
26
     *
27
     * @return array
28
     */
29
    public function getFieldLayoutDefinition(FieldLayout $fieldLayout)
30
    {
31
        if ($fieldLayout->getTabs()) {
32
            $tabDefinitions = [];
33
34
            foreach ($fieldLayout->getTabs() as $tab) {
35
                $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...
36
            }
37
38
            return ['tabs' => $tabDefinitions];
39
        }
40
41
        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...
42
    }
43
44
    /**
45
     * Get field layout fields definition.
46
     *
47
     * @param FieldLayoutFieldModel[] $fields
48
     *
49
     * @return array
50
     */
51
    private function getFieldLayoutFieldsDefinition(array $fields)
52
    {
53
        $fieldDefinitions = [];
54
55
        foreach ($fields as $field) {
56
            $fieldDefinitions[$field->handle] = $field->required;
57
        }
58
59
        return $fieldDefinitions;
60
    }
61
62
    /**
63
     * Attempt to import a field layout.
64
     *
65
     * @param array $fieldLayoutDef
66
     *
67
     * @return FieldLayout
68
     */
69
    public function getFieldLayout(array $fieldLayoutDef)
70
    {
71
        $layoutFields = [];
72
        $requiredFields = [];
73
74
        if (array_key_exists('tabs', $fieldLayoutDef)) {
75
            foreach ($fieldLayoutDef['tabs'] as $tabName => $tabDef) {
76
                $layoutTabFields = $this->getPrepareFieldLayout($tabDef);
77
                $requiredFields = array_merge($requiredFields, $layoutTabFields['required']);
78
                $layoutFields[$tabName] = $layoutTabFields['fields'];
79
            }
80
        } elseif (array_key_exists('fields', $fieldLayoutDef)) {
81
            $layoutTabFields = $this->getPrepareFieldLayout($fieldLayoutDef);
82
            $requiredFields = $layoutTabFields['required'];
83
            $layoutFields = $layoutTabFields['fields'];
84
        }
85
86
        $fieldLayout = Craft::$app->fields->assembleLayout($layoutFields, $requiredFields);
87
        $fieldLayout->type = Entry::class;
88
89
        return $fieldLayout;
90
    }
91
92
    /**
93
     * Get a prepared fieldLayout for the craft assembleLayout function.
94
     *
95
     * @param array $fieldLayoutDef
96
     *
97
     * @return array
98
     */
99
    private function getPrepareFieldLayout(array $fieldLayoutDef)
100
    {
101
        $layoutFields = [];
102
        $requiredFields = [];
103
104
        foreach ($fieldLayoutDef as $fieldHandle => $required) {
105
            $field = Craft::$app->fields->getFieldByHandle($fieldHandle);
106
            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...
107
                $layoutFields[] = $field->id;
108
109
                if ($required) {
110
                    $requiredFields[] = $field->id;
111
                }
112
            }
113
        }
114
115
        return [
116
          'fields' => $layoutFields,
117
          'required' => $requiredFields,
118
        ];
119
    }
120
}
121