Completed
Push — master ( bcc437...987b8d )
by Freek
01:58
created

HasCustomProperties::hasCustomProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ServerMonitor\Models\Concerns;
4
5
trait HasCustomProperties
6
{
7
    public function hasCustomProperty(string $propertyName): bool
8
    {
9
        return array_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...
10
    }
11
12
    /**
13
     * Get the value of custom property with the given name.
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 array_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
        array_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
        array_forget($customProperties, $name);
52
53
        $this->custom_properties = $customProperties;
54
55
        return $this;
56
    }
57
}