Passed
Push — develop ( 3a86a8...a74a25 )
by Andrew
06:23
created

Field::fieldsOfTypeFromLayout()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 3
1
<?php
2
/**
3
 * Instant Analytics plugin for Craft CMS
4
 *
5
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
6
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 2 should be the @author tag
Loading history...
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @link      http://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @copyright tag
Loading history...
8
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
The tag in position 4 should be the @link tag
Loading history...
9
 * @since     1.0.0
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\instantanalytics\helpers;
13
14
use Craft;
15
use craft\base\Element;
16
use craft\base\Field as BaseField;
17
use craft\base\Volume;
18
use craft\elements\User;
19
use craft\ckeditor\Field as CKEditorField;
0 ignored issues
show
Bug introduced by
The type craft\ckeditor\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use craft\elements\Category;
21
use craft\elements\Entry;
22
use craft\elements\MatrixBlock;
23
use craft\fields\Assets as AssetsField;
24
use craft\fields\Matrix as MatrixField;
25
use craft\fields\PlainText as PlainTextField;
26
use craft\fields\Tags as TagsField;
27
use craft\models\FieldLayout;
28
use craft\redactor\Field as RedactorField;
0 ignored issues
show
Bug introduced by
The type craft\redactor\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
use craft\commerce\Plugin as CommercePlugin;
0 ignored issues
show
Bug introduced by
The type craft\commerce\Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use craft\commerce\elements\Product;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
use yii\base\InvalidConfigException;
34
35
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
37
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
38
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
39
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
40
class Field
41
{
42
    // Constants
43
    // =========================================================================
44
45
    const TEXT_FIELD_CLASS_KEY = 'text';
46
    const ASSET_FIELD_CLASS_KEY = 'asset';
47
    const BLOCK_FIELD_CLASS_KEY = 'block';
48
49
    const FIELD_CLASSES = [
50
        self::TEXT_FIELD_CLASS_KEY  => [
51
            CKEditorField::class,
52
            PlainTextField::class,
53
            MatrixField::class,
54
            RedactorField::class,
55
            TagsField::class,
56
        ],
57
        self::ASSET_FIELD_CLASS_KEY => [
58
            AssetsField::class,
59
        ],
60
        self::BLOCK_FIELD_CLASS_KEY => [
61
            MatrixField::class,
62
        ],
63
    ];
64
65
    // Static Methods
66
    // =========================================================================
67
68
    /**
69
     * Return all of the fields from the $layout that are of the type
70
     * $fieldClassKey
71
     *
72
     * @param string      $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     * @param FieldLayout $layout
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
74
     * @param bool        $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     *
76
     * @return array
77
     */
78
    public static function fieldsOfTypeFromLayout(
79
        string $fieldClassKey,
80
        FieldLayout $layout,
81
        bool $keysOnly = true
82
    ): array {
83
        $foundFields = [];
84
        if (!empty(self::FIELD_CLASSES[$fieldClassKey])) {
85
            $fieldClasses = self::FIELD_CLASSES[$fieldClassKey];
86
            $fields = $layout->getFields();
87
            /** @var  $field BaseField */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
88
            foreach ($fields as $field) {
89
                /** @var array $fieldClasses */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
90
                foreach ($fieldClasses as $fieldClass) {
91
                    if ($field instanceof $fieldClass) {
92
                        $foundFields[$field->handle] = $field->name;
93
                    }
94
                }
95
            }
96
        }
97
98
        // Return only the keys if asked
99
        if ($keysOnly) {
100
            $foundFields = array_keys($foundFields);
101
        }
102
103
        return $foundFields;
104
    }
105
106
    /**
107
     * Return all of the fields in the $element of the type $fieldClassKey
108
     *
109
     * @param Element $element
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     * @param string  $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @param bool    $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     *
113
     * @return array
114
     */
115
    public static function fieldsOfTypeFromElement(
116
        Element $element,
117
        string $fieldClassKey,
118
        bool $keysOnly = true
119
    ): array {
120
        $foundFields = [];
121
        $layout = $element->getFieldLayout();
122
        if ($layout !== null) {
123
            $foundFields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
124
        }
125
126
        return $foundFields;
127
    }
128
129
    /**
130
     * Return all of the fields from Users layout of the type $fieldClassKey
131
     *
132
     * @param string  $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
133
     * @param bool    $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 4 found
Loading history...
134
     *
135
     * @return array
136
     */
137
    public static function fieldsOfTypeFromUsers(string $fieldClassKey, bool $keysOnly = true): array
138
    {
139
        $layout = Craft::$app->getFields()->getLayoutByType(User::class);
140
141
        return self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
142
    }
143
144
    /**
145
     * Return all of the fields from all Asset Volume layouts of the type
146
     * $fieldClassKey
147
     *
148
     * @param string $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
149
     * @param bool   $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
150
     *
151
     * @return array
152
     */
153
    public static function fieldsOfTypeFromAssetVolumes(string $fieldClassKey, bool $keysOnly = true): array
154
    {
155
        $foundFields = [];
156
        $volumes = Craft::$app->getVolumes()->getAllVolumes();
157
        foreach ($volumes as $volume) {
158
            /** @var Volume $volume */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
159
            try {
160
                $layout = $volume->getFieldLayout();
161
            } catch (InvalidConfigException $e) {
162
                $layout = null;
163
            }
164
            if ($layout) {
165
                $foundFields = array_merge(
166
                    $foundFields,
167
                    self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly)
168
                );
169
            }
170
        }
171
172
        return $foundFields;
173
    }
174
175
    /**
176
     * Return all of the fields from all Global Set layouts of the type
177
     * $fieldClassKey
178
     *
179
     * @param string $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
180
     * @param bool   $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
181
     *
182
     * @return array
183
     */
184
    public static function fieldsOfTypeFromGlobals(string $fieldClassKey, bool $keysOnly = true): array
185
    {
186
        $foundFields = [];
187
        $globals = Craft::$app->getGlobals()->getAllSets();
188
        foreach ($globals as $global) {
189
            $layout = $global->getFieldLayout();
190
            if ($layout) {
191
                $fields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
192
                // Prefix the keys with the global set name
193
                $prefix = $global->handle;
194
                $fields = array_combine(
195
                    array_map(function ($key) use ($prefix) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
196
                        return $prefix.'.'.$key;
197
                    }, array_keys($fields)),
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
198
                    $fields
199
                );
200
                // Merge with any fields we've already found
201
                $foundFields = array_merge(
202
                    $foundFields,
203
                    $fields
204
                );
205
            }
206
        }
207
208
        return $foundFields;
209
    }
210
211
    /**
212
     * Return all of the fields in the $matrixBlock of the type $fieldType class
213
     *
214
     * @param MatrixBlock $matrixBlock
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
215
     * @param string      $fieldType
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
216
     * @param bool        $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
217
     *
218
     * @return array
219
     */
220
    public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType, bool $keysOnly = true): array
221
    {
222
        $foundFields = [];
223
224
        try {
225
            $matrixBlockTypeModel = $matrixBlock->getType();
226
        } catch (InvalidConfigException $e) {
227
            $matrixBlockTypeModel = null;
228
        }
229
        if ($matrixBlockTypeModel) {
230
            $fields = $matrixBlockTypeModel->getFields();
231
            /** @var  $field BaseField */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
232
            foreach ($fields as $field) {
233
                if ($field instanceof $fieldType) {
234
                    $foundFields[$field->handle] = $field->name;
235
                }
236
            }
237
        }
238
239
        // Return only the keys if asked
240
        if ($keysOnly) {
241
            $foundFields = array_keys($foundFields);
242
        }
243
244
        return $foundFields;
245
    }
246
}
247