HasCustomProperties::getCustomProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace LaTevaWeb\CustomProperties;
4
5
use Illuminate\Support\Arr;
6
7
trait HasCustomProperties
8
{
9 5
    public function __construct(array $attributes = [])
10
    {
11 5
        parent::__construct($attributes);
12 5
        $this->casts['custom_properties'] = 'array';
0 ignored issues
show
Bug introduced by
The property casts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13 5
    }
14
15 1
    public function forgetCustomProperty(string $name): self
16
    {
17 1
        $customProperties = $this->custom_properties;
0 ignored issues
show
Bug introduced by
The property custom_properties does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18 1
        Arr::forget($customProperties, $name);
19 1
        $this->custom_properties = $customProperties;
20
21 1
        return $this;
22
    }
23
24
    /**
25
     * Get the value of custom property with the given name.
26
     *
27
     * @param string $propertyName
28
     * @param mixed $default
29
     *
30
     * @return mixed
31
     */
32 1
    public function getCustomProperty(string $propertyName, $default = null)
33
    {
34 1
        return Arr::get($this->custom_properties, $propertyName, $default);
35
    }
36
37
    /*
38
     * Determine if the model item has a custom property with the given name.
39
     */
40 1
    public function hasCustomProperty(string $propertyName): bool
41
    {
42 1
        return Arr::has($this->custom_properties, $propertyName);
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param mixed $value
48
     *
49
     * @return $this
50
     */
51 5
    public function setCustomProperty(string $name, $value): self
52
    {
53 5
        $customProperties = $this->custom_properties;
54 5
        Arr::set($customProperties, $name, $value);
55 5
        $this->custom_properties = $customProperties;
56
57 5
        return $this;
58
    }
59
}
60