Completed
Pull Request — master (#65)
by
unknown
01:03
created

SchemalessAttributesTrait::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SchemalessAttributes;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * @property array $schemalessAttributes
9
 *
10
 * @mixin Collection
11
 */
12
trait SchemalessAttributesTrait
13
{
14
    /**
15
     *
16
     */
17
    public function initializeSchemalessAttributesTrait()
18
    {
19
        foreach ($this->getSchemalessAttributes() as $attribute) {
20
            $this->casts[$attribute] = 'array';
0 ignored issues
show
Documentation introduced by
The property casts does not exist on object<Spatie\Schemaless...emalessAttributesTrait>. 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...
21
        }
22
    }
23
24
    /**
25
     * @return array
26
     */
27
    public function getSchemalessAttributes()
28
    {
29
        return $this->schemalessAttributes ? $this->schemalessAttributes : [];
30
    }
31
32
    /**
33
     * @param $key
34
     * @return mixed|SchemalessAttributes
35
     */
36
    public function __get($key)
37
    {
38
        if (in_array($key, $this->getSchemalessAttributes())) {
39
            return SchemalessAttributes::createForModel($this, $key);
40
        }
41
42
        return parent::__get($key);
43
    }
44
}
45