Field::fieldsOfTypeFromAssetVolumes()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 9.8666
cc 4
nc 5
nop 2
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\MatrixBlock;
21
use craft\fields\Assets as AssetsField;
22
use craft\fields\Categories as CategoriesField;
23
use craft\fields\Matrix as MatrixField;
24
use craft\fields\PlainText as PlainTextField;
25
use craft\fields\Tags as TagsField;
26
use craft\models\FieldLayout;
27
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...
28
29
use yii\base\InvalidConfigException;
30
31
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
 * @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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
33
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
34
 * @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 for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
35
 */
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...
36
class Field
37
{
38
    // Constants
39
    // =========================================================================
40
41
    const TEXT_FIELD_CLASS_KEY = 'text';
42
    const ASSET_FIELD_CLASS_KEY = 'asset';
43
    const BLOCK_FIELD_CLASS_KEY = 'block';
44
45
    const FIELD_CLASSES = [
46
        self::TEXT_FIELD_CLASS_KEY  => [
47
            CKEditorField::class,
48
            PlainTextField::class,
49
            RedactorField::class,
50
            TagsField::class,
51
            CategoriesField::class,
52
        ],
53
        self::ASSET_FIELD_CLASS_KEY => [
54
            AssetsField::class,
55
        ],
56
        self::BLOCK_FIELD_CLASS_KEY => [
57
            MatrixField::class,
58
        ],
59
    ];
60
61
    // Static Methods
62
    // =========================================================================
63
64
    /**
65
     * Return all of the fields from the $layout that are of the type
66
     * $fieldClassKey
67
     *
68
     * @param string      $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     * @param FieldLayout $layout
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @param bool        $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     *
72
     * @return array
73
     */
74
    public static function fieldsOfTypeFromLayout(
75
        string $fieldClassKey,
76
        FieldLayout $layout,
77
        bool $keysOnly = true
78
    ): array {
79
        $foundFields = [];
80
        if (!empty(self::FIELD_CLASSES[$fieldClassKey])) {
81
            $fieldClasses = self::FIELD_CLASSES[$fieldClassKey];
82
            $fields = $layout->getFields();
83
            /** @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
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Tag value for @var tag indented incorrectly; expected 1 spaces but found 2
Loading history...
84
            foreach ($fields as $field) {
85
                /** @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...
86
                foreach ($fieldClasses as $fieldClass) {
87
                    if ($field instanceof $fieldClass) {
88
                        $foundFields[$field->handle] = $field->name;
89
                    }
90
                }
91
            }
92
        }
93
94
        // Return only the keys if asked
95
        if ($keysOnly) {
96
            $foundFields = array_keys($foundFields);
97
        }
98
99
        return $foundFields;
100
    }
101
102
    /**
103
     * Return all of the fields in the $element of the type $fieldClassKey
104
     *
105
     * @param Element $element
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @param string  $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     * @param bool    $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
     *
109
     * @return array
110
     */
111
    public static function fieldsOfTypeFromElement(
112
        Element $element,
113
        string $fieldClassKey,
114
        bool $keysOnly = true
115
    ): array {
116
        $foundFields = [];
117
        $layout = $element->getFieldLayout();
118
        if ($layout !== null) {
119
            $foundFields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
120
        }
121
122
        return $foundFields;
123
    }
124
125
    /**
126
     * Return all of the fields from Users layout of the type $fieldClassKey
127
     *
128
     * @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...
129
     * @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...
130
     *
131
     * @return array
132
     */
133
    public static function fieldsOfTypeFromUsers(string $fieldClassKey, bool $keysOnly = true): array
134
    {
135
        $layout = Craft::$app->getFields()->getLayoutByType(User::class);
136
137
        return self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
138
    }
139
140
    /**
141
     * Return all of the fields from all Asset Volume layouts of the type
142
     * $fieldClassKey
143
     *
144
     * @param string $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
145
     * @param bool   $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
146
     *
147
     * @return array
148
     */
149
    public static function fieldsOfTypeFromAssetVolumes(string $fieldClassKey, bool $keysOnly = true): array
150
    {
151
        $foundFields = [];
152
        $volumes = Craft::$app->getVolumes()->getAllVolumes();
153
        foreach ($volumes as $volume) {
154
            /** @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...
155
            try {
156
                $layout = $volume->getFieldLayout();
157
            } catch (InvalidConfigException $e) {
158
                $layout = null;
159
            }
160
            if ($layout) {
161
                /** @noinspection SlowArrayOperationsInLoopInspection */
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...
162
                $foundFields = array_merge(
163
                    $foundFields,
164
                    self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly)
165
                );
166
            }
167
        }
168
169
        return $foundFields;
170
    }
171
172
    /**
173
     * Return all of the fields from all Global Set layouts of the type
174
     * $fieldClassKey
175
     *
176
     * @param string $fieldClassKey
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
177
     * @param bool   $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
178
     *
179
     * @return array
180
     */
181
    public static function fieldsOfTypeFromGlobals(string $fieldClassKey, bool $keysOnly = true): array
182
    {
183
        $foundFields = [];
184
        $globals = Craft::$app->getGlobals()->getAllSets();
185
        foreach ($globals as $global) {
186
            $layout = $global->getFieldLayout();
187
            if ($layout) {
188
                $fields = self::fieldsOfTypeFromLayout($fieldClassKey, $layout, $keysOnly);
189
                // Prefix the keys with the global set name
190
                $prefix = $global->handle;
191
                $fields = array_combine(
192
                    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...
193
                        return $prefix.'.'.$key;
194
                    }, 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...
195
                    $fields
196
                );
197
                // Merge with any fields we've already found
198
                /** @noinspection SlowArrayOperationsInLoopInspection */
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...
199
                $foundFields = array_merge(
200
                    $foundFields,
201
                    $fields
202
                );
203
            }
204
        }
205
206
        return $foundFields;
207
    }
208
209
    /**
210
     * Return all of the fields in the $matrixBlock of the type $fieldType class
211
     *
212
     * @param MatrixBlock $matrixBlock
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
213
     * @param string      $fieldType
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
214
     * @param bool        $keysOnly
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
215
     *
216
     * @return array
217
     */
218
    public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType, bool $keysOnly = true): array
219
    {
220
        $foundFields = [];
221
222
        try {
223
            $matrixBlockTypeModel = $matrixBlock->getType();
224
        } catch (InvalidConfigException $e) {
225
            $matrixBlockTypeModel = null;
226
        }
227
        if ($matrixBlockTypeModel) {
228
            $fields = $matrixBlockTypeModel->getFields();
229
            /** @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
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Tag value for @var tag indented incorrectly; expected 1 spaces but found 2
Loading history...
230
            foreach ($fields as $field) {
231
                if ($field instanceof $fieldType) {
232
                    $foundFields[$field->handle] = $field->name;
233
                }
234
            }
235
        }
236
237
        // Return only the keys if asked
238
        if ($keysOnly) {
239
            $foundFields = array_keys($foundFields);
240
        }
241
242
        return $foundFields;
243
    }
244
}
245