Completed
Push — master ( e40781...e52c95 )
by Freek
13s
created

SchemalessAttributes::setMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\SchemalessAttributes;
4
5
use Countable;
6
use ArrayAccess;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Contracts\Support\Arrayable;
10
11
class SchemalessAttributes implements ArrayAccess, Countable, Arrayable
12
{
13
    /** @var \Illuminate\Database\Eloquent\Model */
14
    protected $model;
15
16
    /** @var string */
17
    protected $sourceAttributeName;
18
19
    /** @var array */
20
    protected $schemalessAttributes = [];
21
22
    public static function createForModel(Model $model, string $sourceAttributeName): self
23
    {
24
        return new static($model, $sourceAttributeName);
25
    }
26
27
    public function __construct(Model $model, string $sourceAttributeName)
28
    {
29
        $this->model = $model;
30
31
        $this->sourceAttributeName = $sourceAttributeName;
32
33
        $this->schemalessAttributes = $this->getRawSchemalessAttributes();
34
    }
35
36
    public function __get(string $name)
37
    {
38
        return $this->get($name);
39
    }
40
41
    public function get(string $name, $default = null)
42
    {
43
        return data_get($this->schemalessAttributes, $name, $default);
44
    }
45
46
    public function __set(string $name, $value)
47
    {
48
        $this->set($name, $value);
49
    }
50
51
    public function set($attribute, $value = null)
52
    {
53
        if (is_iterable($attribute)) {
54
            $this->setMany($attribute);
55
56
            return;
57
        }
58
59
        data_set($this->schemalessAttributes, $attribute, $value);
60
61
        $this->model->{$this->sourceAttributeName} = $this->schemalessAttributes;
62
    }
63
64
    public function setMany(iterable $attributes = [])
65
    {
66
        array_map(function ($key, $item) {
67
            $this->set($key, $item);
68
        }, array_keys($attributes), $attributes);
69
    }
70
71
    public function has(string $name): bool
72
    {
73
        return array_has($this->schemalessAttributes, $name);
74
    }
75
76
    public function forget(string $name): self
77
    {
78
        $this->model->{$this->sourceAttributeName} = array_except($this->schemalessAttributes, $name);
79
80
        return $this;
81
    }
82
83
    public function all(): array
84
    {
85
        return $this->getRawSchemalessAttributes();
86
    }
87
88
    public function offsetExists($offset)
89
    {
90
        return $this->has($offset);
91
    }
92
93
    public function offsetGet($offset)
94
    {
95
        return $this->$offset;
96
    }
97
98
    public function offsetSet($offset, $value)
99
    {
100
        $this->{$offset} = $value;
101
    }
102
103
    public function offsetUnset($offset)
104
    {
105
        $this->forget($offset);
106
    }
107
108
    public function count()
109
    {
110
        return count($this->schemalessAttributes);
111
    }
112
113
    public function toArray(): array
114
    {
115
        return $this->all();
116
    }
117
118
    public static function scopeWithSchemalessAttributes(string $attributeName): Builder
119
    {
120
        $arguments = debug_backtrace()[1]['args'];
121
122
        if (count($arguments) === 1) {
123
            [$builder] = $arguments;
0 ignored issues
show
Bug introduced by
The variable $builder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
124
            $schemalessAttributes = [];
125
        }
126
127
        if (count($arguments) === 2) {
128
            [$builder, $schemalessAttributes] = $arguments;
0 ignored issues
show
Bug introduced by
The variable $schemalessAttributes does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
129
        }
130
131
        if (count($arguments) >= 3) {
132
            [$builder, $name, $value] = $arguments;
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
133
            $schemalessAttributes = [$name => $value];
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
134
        }
135
136
        foreach ($schemalessAttributes as $name => $value) {
137
            $builder->where("{$attributeName}->{$name}", $value);
138
        }
139
140
        return $builder;
141
    }
142
143
    protected function getRawSchemalessAttributes(): array
144
    {
145
        return json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true);
146
    }
147
}
148