Passed
Push — main ( 0df40c...ce1342 )
by Michael
04:28 queued 52s
created

FullName   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 12
c 2
b 0
f 1
dl 0
loc 70
ccs 15
cts 15
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 6 1
A value() 0 3 1
A firstName() 0 3 1
A __construct() 0 5 1
A fullName() 0 3 1
A lastName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects\Complex;
6
7
use Illuminate\Support\Collection;
8
use MichaelRubel\Formatters\Collection\FullNameFormatter;
9
use MichaelRubel\ValueObjects\ValueObject;
10
11
/**
12
 * @method static static make(string $taxNumber)
13
 */
14
class FullName extends ValueObject
15
{
16
    /**
17
     * @var Collection
18
     */
19
    protected Collection $split;
20
21
    /**
22
     * Create a new instance of the value object.
23
     *
24
     * @param  string|null  $fullName
25
     */
26 10
    public function __construct(protected ?string $fullName)
27
    {
28 10
        $this->fullName = format(FullNameFormatter::class, $this->fullName);
29
30 10
        $this->split = str($this->fullName)->split('/\s/');
31
    }
32
33
    /**
34
     * Get the full name.
35
     *
36
     * @return string
37
     */
38 6
    public function fullName(): string
39
    {
40 6
        return (string) $this->fullName;
41
    }
42
43
    /**
44
     * Get the first name.
45
     *
46
     * @return string
47
     */
48 3
    public function firstName(): string
49
    {
50 3
        return $this->split->first();
51
    }
52
53
    /**
54
     * Get the last name.
55
     *
56
     * @return string
57
     */
58 2
    public function lastName(): string
59
    {
60 2
        return $this->split->last();
61
    }
62
63
    /**
64
     * Get the object value.
65
     *
66
     * @return string
67
     */
68 1
    public function value(): string
69
    {
70 1
        return $this->fullName();
71
    }
72
73
    /**
74
     * Get an 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