Passed
Push — main ( 01d627...8c0949 )
by Michael
03:55
created

FullName::__toString()   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\Support\Collection;
8
use Illuminate\Support\Traits\Conditionable;
9
use Illuminate\Support\Traits\Macroable;
10
use MichaelRubel\Formatters\Collection\FullNameFormatter;
11
use MichaelRubel\ValueObjects\ValueObject;
12
13
/**
14
 * @method static static make(string $taxNumber)
15
 */
16
class FullName extends ValueObject
17
{
18
    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\Complex\FullName.
Loading history...
19
20
    /**
21
     * @var Collection
22
     */
23
    protected Collection $split;
24
25
    /**
26
     * @param  string|null  $fullName
27
     */
28 10
    public function __construct(protected ?string $fullName)
29
    {
30 10
        $this->fullName = format(FullNameFormatter::class, $this->fullName);
31
32 10
        $this->split = str($this->fullName)->split('/\s/');
33
    }
34
35
    /**
36
     * Get the full name.
37
     *
38
     * @return string
39
     */
40 6
    public function fullName(): string
41
    {
42 6
        return (string) $this->fullName;
43
    }
44
45
    /**
46
     * Get the first name.
47
     *
48
     * @return string
49
     */
50 3
    public function firstName(): string
51
    {
52 3
        return $this->split->first();
53
    }
54
55
    /**
56
     * Get the last name.
57
     *
58
     * @return string
59
     */
60 2
    public function lastName(): string
61
    {
62 2
        return $this->split->last();
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function value(): string
69
    {
70
        return $this->fullName();
71
    }
72
73
    /**
74
     * Get array representation of the value object.
75
     *
76
     * @return array
77
     */
78 1
    public function toArray(): array
79
    {
80
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('fullName' ...' => $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...
81 1
            'fullName'  => $this->fullName(),
82 1
            'firstName' => $this->firstName(),
83 1
            'lastName'  => $this->lastName(),
84
        ];
85
    }
86
87
    /**
88
     * Get string representation of the value object.
89
     *
90
     * @return string
91
     */
92 1
    public function __toString(): string
93
    {
94 1
        return $this->fullName();
95
    }
96
}
97