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

SchemalessAttributesTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCasts() 0 11 3
A initializeSchemalessAttributesTrait() 0 6 2
A getSchemalessAttributes() 0 4 2
A __get() 0 8 2
1
<?php
2
3
namespace Spatie\SchemalessAttributes;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
8
/**
9
 * @property array $schemalessAttributes
10
 *
11
 * @mixin Collection
12
 */
13
trait SchemalessAttributesTrait
14
{
15
    /**
16
     * @var
17
     */
18
    private $initializeSchemalessAttributesTrait;
19
20
    /**
21
     * Get the casts array.
22
     *
23
     * @return array
24
     */
25
    public function getCasts()
26
    {
27
        if (Str::startsWith(app()->version(), '5.6')) {
28
            if (! $this->initializeSchemalessAttributesTrait) {
29
                $this->initializeSchemalessAttributesTrait();
30
                $this->initializeSchemalessAttributesTrait = true;
31
            }
32
        }
33
34
        return parent::getCasts();
35
    }
36
37
    public function initializeSchemalessAttributesTrait()
38
    {
39
        foreach ($this->getSchemalessAttributes() as $attribute) {
40
            $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...
41
        }
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getSchemalessAttributes()
48
    {
49
        return isset($this->schemalessAttributes) ? $this->schemalessAttributes : [];
50
    }
51
52
    /**
53
     * @param $key
54
     * @return mixed|SchemalessAttributes
55
     */
56
    public function __get($key)
57
    {
58
        if (in_array($key, $this->getSchemalessAttributes())) {
59
            return SchemalessAttributes::createForModel($this, $key);
60
        }
61
62
        return parent::__get($key);
63
    }
64
}
65