Completed
Push — master ( 33ec04...ed9cfc )
by Bart
11s
created

src/Behaviors/FieldLayoutBehavior.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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