DefaultController::actionIndex()   B
last analyzed

Complexity

Conditions 9
Paths 18

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 31
c 7
b 0
f 0
dl 0
loc 53
rs 8.0555
cc 9
nc 18
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Rich Variables plugin for Craft CMS 3.x
4
 *
5
 * Allows you to easily use Craft Globals as variables in Rich Text fields
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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 @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\richvariables\controllers;
12
13
use aelvan\preparsefield\fields\PreparseFieldType as PreparseField;
0 ignored issues
show
Bug introduced by
The type aelvan\preparsefield\fields\PreparseFieldType 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...
14
use Craft;
15
use craft\base\Field;
16
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...
17
use craft\fields\Date as DateField;
18
use craft\fields\Dropdown as DropdownField;
19
use craft\fields\Number as NumberField;
20
use craft\fields\PlainText as PlainTextField;
21
use craft\helpers\Json;
22
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...
23
use craft\web\Controller;
24
use nystudio107\richvariables\assetbundles\richvariables\RichVariablesAsset;
25
use nystudio107\richvariables\RichVariables;
26
use yii\base\InvalidConfigException;
27
28
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
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
The tag in position 1 should be the @package tag
Loading history...
30
 * @package   RichVariables
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @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...
32
 */
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...
33
class DefaultController extends Controller
34
{
35
    // Constants
36
    // =========================================================================
37
38
    const VALID_FIELD_CLASSES = [
39
        PlainTextField::class,
40
        NumberField::class,
41
        DateField::class,
42
        DropdownField::class,
43
        RedactorField::class,
44
        CKEditorField::class,
45
        PreparseField::class,
46
    ];
47
48
    // Public Methods
49
    // =========================================================================
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @return mixed
53
     */
54
    public function actionIndex()
55
    {
56
        $result = [];
57
        $variablesList = [];
58
59
        // Get the global set to use
60
        $settings = RichVariables::$plugin->getSettings();
61
        $globalsSet = Craft::$app->getGlobals()->getSetByHandle($settings['globalSetHandle']);
62
        // Grab the first global set if they haven't specified one yet
63
        if (!$globalsSet) {
64
            $allGlobalsSetIds = Craft::$app->getGlobals()->getAllSetIds();
65
            if (!empty($allGlobalsSetIds)) {
66
                $globalsSet = Craft::$app->globals->getSetById($allGlobalsSetIds[0]);
0 ignored issues
show
Bug introduced by
The method getSetById() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
                /** @scrutinizer ignore-call */ 
67
                $globalsSet = Craft::$app->globals->getSetById($allGlobalsSetIds[0]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            }
68
        }
69
        if ($globalsSet) {
70
            // Get the field layout fields used for this global set
71
            $layout = $globalsSet->getFieldLayout();
72
            if ($layout) {
73
                $fieldLayoutFields = $layout->getFields();
74
                /** @var Field $field */
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
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
                foreach ($fieldLayoutFields as $field) {
76
                    foreach (self::VALID_FIELD_CLASSES as $fieldClass) {
77
                        if ($field instanceof $fieldClass) {
78
                            // Add the field title and Reference Tag as per https://craftcms.com/docs/reference-tags
79
                            $thisVar = [
80
                                'title' => $field->name,
81
                                'text' => '{globalset:' . $globalsSet->attributes['id'] . ':' . $field->handle . '}',
82
                            ];
83
                            $variablesList[] = $thisVar;
84
                        }
85
                    }
86
                }
87
            }
88
        }
89
90
        // Get the URL to our menu icon from our resource bundle
91
        try {
92
            Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class);
93
        } catch (InvalidConfigException $e) {
94
            Craft::error($e->getMessage(), __METHOD__);
95
        }
96
        $menuIconUrl = Craft::$app->assetManager->getPublishedUrl(
0 ignored issues
show
Bug introduced by
Are you sure Craft::app->assetManager...web/assets/dist', true) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $menuIconUrl = /** @scrutinizer ignore-type */ Craft::$app->assetManager->getPublishedUrl(
Loading history...
97
                '@nystudio107/richvariables/web/assets/dist',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
98
                true
0 ignored issues
show
Unused Code introduced by
The call to yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $menuIconUrl = Craft::$app->assetManager->/** @scrutinizer ignore-call */ getPublishedUrl(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
99
            ) . '/img/RichVariables-menu-icon.svg';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
100
101
        // Return everything to our JavaScript encoded as JSON
102
        $result['variablesList'] = $variablesList;
103
        $result['menuIconUrl'] = $menuIconUrl;
104
        $result['useIconForMenu'] = $settings['useIconForMenu'];
105
106
        return Json::encode($result);
107
    }
108
}
109