HasCustomProperties   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 53
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A forgetCustomProperty() 0 8 1
A getCustomProperty() 0 4 1
A hasCustomProperty() 0 4 1
A setCustomProperty() 0 8 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