Passed
Push — main ( a94f83...7c0db4 )
by Michael
03:30
created

ValueObject::notEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects;
6
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Traits\Conditionable;
9
use Illuminate\Support\Traits\Macroable;
10
11
abstract class ValueObject implements Arrayable
12
{
13
    use Macroable, Conditionable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by MichaelRubel\ValueObjects\ValueObject.
Loading history...
14
15
    /**
16
     * Get the object value.
17
     *
18
     * @return mixed
19
     */
20
    abstract public function value();
21
22
    /**
23
     * Convenient method to create a value object statically.
24
     *
25
     * @param  mixed  $values
26
     *
27
     * @return static
28
     */
29 7
    public static function make(mixed ...$values): static
30
    {
31 7
        return new static(...$values);
0 ignored issues
show
Unused Code introduced by
The call to MichaelRubel\ValueObject...ueObject::__construct() has too many arguments starting with $values. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return /** @scrutinizer ignore-call */ new static(...$values);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
32
    }
33
34
    /**
35
     * Check if objects are instances of same class
36
     * and share the same properties and values.
37
     *
38
     * @param  ValueObject  $object
39
     *
40
     * @return bool
41
     */
42 2
    public function equals(ValueObject $object): bool
43
    {
44 2
        return $this == $object;
45
    }
46
47
    /**
48
     * Inversion to `equals` method.
49
     *
50
     * @param  ValueObject  $object
51
     *
52
     * @return bool
53
     */
54 1
    public function notEquals(ValueObject $object): bool
55
    {
56 1
        return ! $this->equals($object);
57
    }
58
59
    /**
60
     * Get the length of the value.
61
     *
62
     * @return int
63
     */
64 1
    public function length(): int
65
    {
66 1
        return strlen((string) $this->value());
67
    }
68
69
    /**
70
     * Make sure value object is immutable.
71
     *
72
     * @param  string  $name
73
     * @param  mixed  $value
74
     *
75
     * @return void
76
     */
77 4
    public function __set(string $name, mixed $value): void
78
    {
79 4
        throw new \InvalidArgumentException('Value objects are immutable. You cannot modify properties. Create a new object instead.');
80
    }
81
82
    /**
83
     * Get an array representation of the value object.
84
     *
85
     * @return array
86
     */
87 4
    public function toArray(): array
88
    {
89 4
        return (array) $this->value();
90
    }
91
92
    /**
93
     * Get string representation of the value object.
94
     *
95
     * @return string
96
     */
97 14
    public function __toString(): string
98
    {
99 14
        return (string) $this->value();
100
    }
101
}
102