HasCustomProperties::getCustomProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Models\Concerns;
4
5
use Illuminate\Support\Arr;
6
7
trait HasCustomProperties
8
{
9
    public function hasCustomProperty(string $propertyName): bool
10
    {
11
        return Arr::has($this->custom_properties, $propertyName);
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...
12
    }
13
14
    /**
15
     * @param string $propertyName
16
     * @param mixed $default
17
     *
18
     * @return mixed
19
     */
20
    public function getCustomProperty(string $propertyName, $default = null)
21
    {
22
        return Arr::get($this->custom_properties, $propertyName, $default);
23
    }
24
25
    /**
26
     * @param string $name
27
     * @param mixed $value
28
     *
29
     * @return $this
30
     */
31
    public function setCustomProperty(string $name, $value)
32
    {
33
        $customProperties = $this->custom_properties;
34
35
        Arr::set($customProperties, $name, $value);
36
37
        $this->custom_properties = $customProperties;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param string $name
44
     *
45
     * @return $this
46
     */
47
    public function forgetCustomProperty(string $name)
48
    {
49
        $customProperties = $this->custom_properties;
50
51
        Arr::forget($customProperties, $name);
52
53
        $this->custom_properties = $customProperties;
54
55
        return $this;
56
    }
57
}
58