Passed
Push — main ( dd0152...6175ee )
by Michael
03:30
created

FullName::getFullName()   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
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
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\Stringable;
10
use Illuminate\Support\Traits\Conditionable;
11
use Illuminate\Support\Traits\Macroable;
12
use Illuminate\Support\Traits\Tappable;
13
use MichaelRubel\Formatters\Collection\FullNameFormatter;
14
use MichaelRubel\ValueObjects\ValueObject;
15
16
class FullName implements ValueObject, Arrayable
17
{
18
    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...
19
20
    /**
21
     * @var Collection
22
     */
23
    protected Collection $split;
24
25
    /**
26
     * Create a new value object instance.
27
     *
28
     * @param  string|null  $full_name
29
     */
30 9
    final public function __construct(public ?string $full_name)
31
    {
32 9
        $this->full_name = format(FullNameFormatter::class, $this->full_name);
33
34 9
        $this->split = str($this->full_name)->split('/\s/');
35
    }
36
37
    /**
38
     * Return a new instance of TaxNumber.
39
     *
40
     * @param  string|null  $name
41
     *
42
     * @return static
43
     */
44 4
    public static function make(?string $name = null): static
45
    {
46 4
        return new static($name);
47
    }
48
49
    /**
50
     * Get the first name.
51
     *
52
     * @return string
53
     */
54 3
    public function getFirstName(): string
55
    {
56 3
        return $this->split->first();
57
    }
58
59
    /**
60
     * Get the last name.
61
     *
62
     * @return string
63
     */
64 2
    public function getLastName(): string
65
    {
66 2
        return $this->split->last();
67
    }
68
69
    /**
70
     * Get the last name.
71
     *
72
     * @return string
73
     */
74 5
    public function getFullName(): string
75
    {
76 5
        return $this->full_name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->full_name could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
    /**
80
     * @return array
81
     */
82 1
    public function toArray(): array
83
    {
84
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('full_name'...> $this->getLastName()) 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...
85 1
            'full_name'  => $this->getFullName(),
86 1
            'first_name' => $this->getFirstName(),
87 1
            'last_name'  => $this->getLastName(),
88
        ];
89
    }
90
91
    /**
92
     * Return the first UUID if cast to string.
93
     *
94
     * @return string
95
     */
96 1
    public function __toString(): string
97
    {
98 1
        return $this->getFullName();
99
    }
100
}
101