Passed
Push — develop ( ed2959...eaea54 )
by Andrew
04:30
created

addMagicGetterProperties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 9.9332
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\twigfield\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 craft\elements\Entry;
16
use nystudio107\twigfield\base\ObjectParserAutocomplete;
17
use nystudio107\twigfield\models\CompleteItem;
18
use nystudio107\twigfield\types\AutocompleteTypes;
19
use nystudio107\twigfield\types\CompleteItemKind;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @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...
24
 * @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...
25
 */
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...
26
class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete
27
{
28
    // Constants
29
    // =========================================================================
30
31
    public const OPTIONS_DATA_KEY = 'SectionShorthandFields';
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @array properties that are defined only using the `@property` docblock annotation
35
     */
36
    public const MAGIC_GETTER_PROPERTIES = [
37
        Entry::class => [
38
            'typeId' => "the entry type’s ID",
39
            'authorId' => "the entry author’s ID",
40
            'type' => "the entry type",
41
            'section' => "the entry’s section",
42
            'author' => "the entry’s author",
43
        ]
44
    ];
45
46
    // Public Properties
47
    // =========================================================================
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var string The name of the autocomplete
51
     */
52
    public $name = 'SectionShorthandFieldsAutocomplete';
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var string The type of the autocomplete
56
     */
57
    public $type = AutocompleteTypes::TwigExpressionAutocomplete;
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
61
     */
62
    public $hasSubProperties = true;
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @inheritdoc
66
     */
67
    public $parseBehaviors = false;
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @inheritdoc
71
     */
72
    public $customPropertySortPrefix = '';
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @inheritdoc
76
     */
77
    public $propertySortPrefix = '';
78
79
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
80
     * @inheritdoc
81
     */
82
    public $methodSortPrefix = '';
83
84
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
85
     * @var ?int The section id. A sectionId of 0 denotes we don't know what this section is, so use
86
     * a generic `Entry` and don't generate any complete items for custom fields
87
     */
88
    public $sectionId = null;
89
90
    // Public Methods
91
    // =========================================================================
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @inerhitDoc
95
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
96
    public function init(): void
97
    {
98
        $this->sectionId = $this->twigfieldOptions[self::OPTIONS_DATA_KEY] ?? null;
99
    }
100
101
    /**
102
     * Core function that generates the autocomplete array
103
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
104
    public function generateCompleteItems(): void
105
    {
106
        if ($this->sectionId !== null) {
107
            // A sectionId of 0 denotes we don't know what this section is, so use
108
            // a generic `Entry` and don't generate any complete items for custom fields
109
            if ($this->sectionId === 0) {
110
                $element = new Entry();
111
                $this->parseObject('', $element, 0);
112
                $this->addMagicGetterProperties($element);
113
114
                return;
115
            }
116
            // Find the entry types used in the passed in sectionId
117
            $sections = Craft::$app->getSections();
118
            $section = $sections->getSectionById($this->sectionId);
119
            if ($section) {
120
                $entryTypes = $section->getEntryTypes();
121
                foreach ($entryTypes as $entryType) {
122
                    // Add the native fields in
123
                    if ($entryType->elementType) {
124
                        $element = new $entryType->elementType;
125
                        /* @var ElementInterface $element */
126
                        $this->parseObject('', $element, 0);
127
                        $this->addMagicGetterProperties($element);
128
                    }
129
                    // Add the custom fields in
130
                    $customFields = $entryType->getCustomFields();
131
                    foreach ($customFields as $customField) {
132
                        $name = $customField->handle;
133
                        $docs = $customField->instructions ?? '';
134
                        if ($name) {
135
                            CompleteItem::create()
136
                                ->insertText($name)
137
                                ->label($name)
138
                                ->detail(Craft::t('twigfield', 'Custom Field Shorthand'))
139
                                ->documentation($docs)
140
                                ->kind(CompleteItemKind::FieldKind)
141
                                ->add($this);
142
                        }
143
                    }
144
                }
145
            }
146
        }
147
    }
148
149
    /**
150
     * Add in magic getter properties that are defined only in the `@property` docklock annotation
151
     *
152
     * @param ElementInterface $element
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
153
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
154
     */
155
    protected function addMagicGetterProperties(ElementInterface $element): void
156
    {
157
        foreach (self::MAGIC_GETTER_PROPERTIES as $key => $value) {
158
            if ($key === $element::class) {
159
                foreach ($value as $name => $docs) {
160
                    CompleteItem::create()
161
                        ->insertText($name)
162
                        ->label($name)
163
                        ->detail(Craft::t('twigfield', 'Custom Field Shorthand'))
164
                        ->documentation($docs)
165
                        ->kind(CompleteItemKind::FieldKind)
166
                        ->add($this);
167
                }
168
            }
169
        }
170
    }
171
}
172