Passed
Push — develop ( 975095...7eadbb )
by Andrew
14:03
created

generateCompleteItems()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 29
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 36
rs 8.2114
1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API autocomplete
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) 2022 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\seomatic\autocompletes;
12
13
use Craft;
14
use craft\base\ElementInterface;
0 ignored issues
show
Bug introduced by
The type craft\base\ElementInterface 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...
15
use nystudio107\twigfield\base\Autocomplete;
16
use nystudio107\twigfield\models\CompleteItem;
17
use nystudio107\twigfield\types\AutocompleteTypes;
18
use nystudio107\twigfield\types\CompleteItemKind;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @package   twigfield
0 ignored issues
show
Coding Style introduced by
Package name "twigfield" is not valid; consider "Twigfield" instead
Loading history...
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.0.13
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...
24
 */
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...
25
class SectionShorthandFieldsAutocomplete extends Autocomplete
26
{
27
    // Constants
28
    // =========================================================================
29
30
    public const OPTIONS_DATA_KEY = 'SectionShorthandFieldsAutocomplete';
31
32
    // Public Properties
33
    // =========================================================================
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var string The name of the autocomplete
37
     */
38
    public $name = 'SectionShorthandFieldsAutocomplete';
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var string The type of the autocomplete
42
     */
43
    public $type = AutocompleteTypes::TwigExpressionAutocomplete;
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
47
     */
48
    public $hasSubProperties = false;
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @var ?int The section id
52
     */
53
    public $sectionId = null;
54
55
    // Public Methods
56
    // =========================================================================
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @inerhitDoc
60
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
61
    public function init(): void
62
    {
63
        $this->sectionId = $this->twigfieldOptions[self::OPTIONS_DATA_KEY] ?? null;
64
    }
65
66
    /**
67
     * Core function that generates the autocomplete array
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function generateCompleteItems(): void
70
    {
71
        if ($this->sectionId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sectionId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72
            $sections = Craft::$app->getSections();
73
            $section = $sections->getSectionById($this->sectionId);
74
            if ($section) {
75
                $entryTypes = $section->getEntryTypes();
76
                foreach ($entryTypes as $entryType) {
77
                    // Add the native fields in
78
                    if ($entryType->elementType) {
79
                        $element = new $entryType->elementType;
80
                        /* @var ElementInterface $element */
81
                        $nativeFields = $element->attributeLabels();
82
                        foreach ($nativeFields as $key => $value) {
83
                            CompleteItem::create()
84
                                ->insertText($key)
85
                                ->label($key)
86
                                ->detail(Craft::t('twigfield', 'Field'))
87
                                ->documentation($element::displayName() . ' ' . $value)
88
                                ->kind(CompleteItemKind::PropertyKind)
89
                                ->add($this);
90
                        }
91
                    }
92
                    // Add the custom fields in
93
                    $customFields = $entryType->getCustomFields();
94
                    foreach ($customFields as $customField) {
95
                        $name = $customField->handle;
96
                        $docs = $customField->instructions ?? '';
97
                        if ($name) {
98
                            CompleteItem::create()
99
                                ->insertText($name)
100
                                ->label($name)
101
                                ->detail(Craft::t('twigfield', 'Custom Field'))
102
                                ->documentation($docs)
103
                                ->kind(CompleteItemKind::FieldKind)
104
                                ->add($this);
105
                        }
106
                    }
107
                }
108
            }
109
        }
110
    }
111
}
112