Completed
Pull Request — master (#114)
by Bart
06:44
created

Fields   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 2
dl 0
loc 264
rs 9
c 0
b 0
f 0
ccs 0
cts 89
cp 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecords() 0 4 1
A export() 0 9 3
A getRecordDefinition() 0 11 2
B import() 0 34 5
A saveFieldGroupModel() 0 8 2
A saveFieldModel() 0 12 3
A deleteFields() 0 7 2
A deleteGroups() 0 7 2
A deleteFieldsAndGroups() 0 5 1
A createFieldGroupModel() 0 9 2
A getFieldModel() 0 4 2
A validateFieldModel() 0 11 3
A importFields() 0 19 3
A unsetData() 0 9 3
A setGlobalContext() 0 5 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\base\Field;
8
use craft\models\FieldLayout;
9
10
/**
11
 * Schematic Fields Service.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Fields extends Base
22
{
23
    //==============================================================================================================
24
    //================================================  EXPORT  ====================================================
25
    //==============================================================================================================
26
27
    /**
28
     * Get all field groups
29
     *
30
     * @return FieldGroup[]
31
     */
32
    protected function getRecords()
33
    {
34
        return Craft::$app->fields->getAllGroups();
35
    }
36
37
    /**
38
     * Get all field definitions per group
39
     *
40
     * @return array
41
     */
42
    public function export(array $records = null)
43
    {
44
        $fieldGroups = $records ?: $this->getRecords();
45
        $result = [];
46
        foreach ($fieldGroups as $group) {
47
            $result[$group->name] = parent::export($group->getFields());
48
        }
49
        return $result;
50
    }
51
52
    /**
53
     * Get section definition.
54
     *
55
     * @param Model $record
56
     *
57
     * @return array
58
     */
59
    protected function getRecordDefinition(Model $record)
60
    {
61
        $attributes = parent::getRecordDefinition($record);
62
        if ($record instanceof Field) {
63
            unset($attributes['groupId']);
64
            unset($attributes['layoutId']);
65
            unset($attributes['tabId']);
66
        }
67
68
        return $attributes;
69
    }
70
71
    //==============================================================================================================
72
    //================================================  IMPORT  ====================================================
73
    //==============================================================================================================
74
75
    /**
76
     * Attempt to import fields.
77
     *
78
     * @param array $groupDefinitions
79
     * @param bool  $force            if set to true items not in the import will be deleted
80
     *
81
     * @return Result
82
     */
83
    public function import($force = false, array $groupDefinitions = null)
84
    {
85
        Craft::info('Importing Fields', 'schematic');
86
87
        if (!empty($groupDefinitions)) {
88
            $this->setGlobalContext();
89
            $this->groups = Craft::$app->fields->getAllGroups('name');
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
            $this->fields = Craft::$app->fields->getAllFields('handle');
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
92
            foreach ($groupDefinitions as $name => $fieldDefinitions) {
93
                try {
94
                    $this->beginTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method beginTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
95
96
                    $group = $this->createFieldGroupModel($name);
97
98
                    $this->importFields($fieldDefinitions, $group, $force);
99
100
                    $this->commitTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method commitTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
101
                } catch (\Exception $e) {
102
                    $this->rollbackTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method rollbackTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
104
                    $this->addError($e->getMessage());
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
                }
106
107
                $this->unsetData($name, $fieldDefinitions);
108
            }
109
110
            if ($force) { // Remove not imported data
111
                $this->deleteFieldsAndGroups();
112
            }
113
        }
114
115
        return $this->getResultModel();
0 ignored issues
show
Documentation Bug introduced by
The method getResultModel does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
116
    }
117
118
    /**
119
     * Save field group.
120
     *
121
     * @param FieldGroupModel $group
122
     *
123
     * @throws Exception
124
     */
125
    private function saveFieldGroupModel(FieldGroupModel $group)
126
    {
127
        if (!Craft::$app->fields->saveGroup($group)) {
128
            $this->addErrors($group->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
129
130
            throw new Exception('Failed to save group');
131
        }
132
    }
133
134
    /**
135
     * Save field.
136
     *
137
     * @param FieldModel $field
138
     *
139
     * @throws \Exception
140
     */
141
    private function saveFieldModel(FieldModel $field)
142
    {
143
        $this->validateFieldModel($field); // Validate field
144
        if ($field->context === 'global') {
145
            $this->setGlobalContext();
146
        }
147
        if (!Craft::$app->fields->saveField($field)) {
148
            $this->addErrors($field->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
150
            throw new Exception('Failed to save field');
151
        }
152
    }
153
154
    /**
155
     * Removes fields that where not imported.
156
     */
157
    private function deleteFields()
158
    {
159
        $fieldsService = Craft::$app->fields;
160
        foreach ($this->fields as $field) {
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
161
            $fieldsService->deleteFieldById($field->id);
162
        }
163
    }
164
165
    /**
166
     * Removes groups that where not imported.
167
     */
168
    private function deleteGroups()
169
    {
170
        $fieldsService = Craft::$app->fields;
171
        foreach ($this->groups as $group) {
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
172
            $fieldsService->deleteGroupById($group->id);
173
        }
174
    }
175
176
    /**
177
     * Removes fields and groups that where not imported.
178
     */
179
    private function deleteFieldsAndGroups()
180
    {
181
        $this->deleteFields();
182
        $this->deleteGroups();
183
    }
184
185
    /**
186
     * Creates new or updates existing group model.
187
     *
188
     * @param string $group
189
     *
190
     * @return FieldGroupModel
191
     */
192
    private function createFieldGroupModel($group)
193
    {
194
        $groupModel = (array_key_exists($group, $this->groups) ? $this->groups[$group] : new FieldGroupModel());
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
195
        $groupModel->name = $group;
196
197
        $this->saveFieldGroupModel($groupModel);
198
199
        return $groupModel;
200
    }
201
202
    /**
203
     * @param string $field
204
     *
205
     * @return FieldModel
206
     */
207
    private function getFieldModel($field)
208
    {
209
        return array_key_exists($field, $this->fields) ? $this->fields[$field] : new FieldModel();
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
210
    }
211
212
    /**
213
     * Validates field type, throw error when it's incorrect.
214
     *
215
     * @param FieldModel $field
216
     *
217
     * @throws \Exception
218
     */
219
    private function validateFieldModel(FieldModel $field)
220
    {
221
        if (!$field->getFieldType()) {
222
            $fieldType = $field->type;
223
            ($fieldType == 'Matrix')
224
                ? $this->addError("One of the field's types does not exist. Are you missing a plugin?")
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
225
                : $this->addError("Field type '$fieldType' does not exist. Are you missing a plugin?");
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
226
227
            throw new Exception('Failed to save field');
228
        }
229
    }
230
231
    /**
232
     * Import field group fields.
233
     *
234
     * @param array           $fieldDefinitions
235
     * @param FieldGroupModel $group
236
     * @param bool            $force
237
     *
238
     * @throws \Exception
239
     */
240
    private function importFields(array $fieldDefinitions, FieldGroupModel $group, $force = false)
241
    {
242
        $fieldFactory = $this->getFieldFactory();
0 ignored issues
show
Documentation Bug introduced by
The method getFieldFactory does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
243
244
        foreach ($fieldDefinitions as $fieldHandle => $fieldDef) {
245
            $field = $this->getFieldModel($fieldHandle);
246
            $schematicFieldModel = $fieldFactory->build($fieldDef['type']);
247
248
            if ($schematicFieldModel->getDefinition($field, true) === $fieldDef) {
249
                Craft::info('Skipping `{name}`, no changes detected', ['name' => $field->name], 'schematic');
0 ignored issues
show
Documentation introduced by
array('name' => $field->name) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Craft::info() has too many arguments starting with 'schematic'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
250
                continue;
251
            }
252
253
            Craft::info('Importing `{name}`', ['name' => $fieldDef['name']], 'schematic');
0 ignored issues
show
Documentation introduced by
array('name' => $fieldDef['name']) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Craft::info() has too many arguments starting with 'schematic'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
254
255
            $schematicFieldModel->populate($fieldDef, $field, $fieldHandle, $group, $force);
256
            $this->saveFieldModel($field);
257
        }
258
    }
259
260
    /**
261
     * Unset group and field data else $force flag will delete it.
262
     *
263
     * @param string $name
264
     * @param array  $definitions
265
     */
266
    private function unsetData($name, array $definitions)
267
    {
268
        if (array_key_exists($name, $this->groups)) {
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
269
            unset($this->groups[$name]);
270
            foreach ($definitions as $handle => $definition) {
271
                unset($this->fields[$handle]);
272
            }
273
        }
274
    }
275
276
    /**
277
     * Set global field context.
278
     */
279
    private function setGlobalContext()
280
    {
281
        Craft::$app->content->fieldContext = 'global';
282
        Craft::$app->content->contentTable = 'content';
283
    }
284
}
285