Passed
Push — main ( ec6948...9b9c4a )
by Michael
04:01
created

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