Passed
Push — main ( 752a2b...79148c )
by Michael
03:17
created

FullName::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects\Complex;
6
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Traits\Conditionable;
10
use Illuminate\Support\Traits\Macroable;
11
use Illuminate\Support\Traits\Tappable;
12
use MichaelRubel\Formatters\Collection\FullNameFormatter;
13
use MichaelRubel\ValueObjects\ValueObject;
14
15
class FullName implements ValueObject, Arrayable
16
{
17
    use Macroable, Conditionable, Tappable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by MichaelRubel\ValueObjects\Complex\FullName.
Loading history...
18
19
    /**
20
     * @var Collection
21
     */
22
    protected Collection $split;
23
24
    /**
25
     * Create a new value object instance.
26
     *
27
     * @param  string|null  $full_name
28
     */
29 10
    final public function __construct(public ?string $full_name)
30
    {
31 10
        $this->full_name = format(FullNameFormatter::class, $this->full_name);
32
33 10
        $this->split = str($this->full_name)->split('/\s/');
34
    }
35
36
    /**
37
     * Return a new instance of TaxNumber.
38
     *
39
     * @param  string|null  $name
40
     *
41
     * @return static
42
     */
43 5
    public static function make(?string $name): static
44
    {
45 5
        return new static($name);
46
    }
47
48
    /**
49
     * Get the first name.
50
     *
51
     * @return string
52
     */
53 3
    public function firstName(): string
54
    {
55 3
        return $this->split->first();
56
    }
57
58
    /**
59
     * Get the last name.
60
     *
61
     * @return string
62
     */
63 2
    public function lastName(): string
64
    {
65 2
        return $this->split->last();
66
    }
67
68
    /**
69
     * Get the last name.
70
     *
71
     * @return string
72
     */
73 6
    public function fullName(): string
74
    {
75 6
        return (string) $this->full_name;
76
    }
77
78
    /**
79
     * @return array
80
     */
81 1
    public function toArray(): array
82
    {
83
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('full_name'...' => $this->lastName()) returns the type array<string,string> which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
84 1
            'full_name'  => $this->fullName(),
85 1
            'first_name' => $this->firstName(),
86 1
            'last_name'  => $this->lastName(),
87
        ];
88
    }
89
90
    /**
91
     * Return the first UUID if cast to string.
92
     *
93
     * @return string
94
     */
95 1
    public function __toString(): string
96
    {
97 1
        return $this->fullName();
98
    }
99
}
100