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

TaxNumber::country()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
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\Traits\Conditionable;
9
use Illuminate\Support\Traits\Macroable;
10
use Illuminate\Support\Traits\Tappable;
11
use MichaelRubel\Formatters\Collection\TaxNumberFormatter;
12
use MichaelRubel\ValueObjects\ValueObject;
13
14
class TaxNumber implements ValueObject, Arrayable
15
{
16
    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\TaxNumber.
Loading history...
17
18
    /**
19
     * Create a new value object instance.
20
     *
21
     * @param  string|null  $tax_number
22
     * @param  string|null  $country
23
     */
24 21
    final public function __construct(
25
        public ?string $tax_number = null,
26
        public ?string $country = null,
27
    ) {
28 21
        $this->tax_number = format(
29
            TaxNumberFormatter::class,
30 21
            [$this->tax_number, $this->country]
31
        );
32
33 21
        $this->when($this->isWithCountry(), function () {
34 15
            $this->country = str($this->tax_number)
35 15
                ->substr(0, 2)
36 15
                ->upper()
37 15
                ->value();
38
39 15
            $this->tax_number = str($this->tax_number)
40 15
                ->substr(2)
41 15
                ->value();
42
        });
43
    }
44
45
    /**
46
     * Return a new instance of TaxNumber.
47
     *
48
     * @param  string|null  $tax_number
49
     * @param  string|null  $country
50
     *
51
     * @return static
52
     */
53 12
    public static function make(?string $tax_number, ?string $country = null): static
54
    {
55 12
        return new static($tax_number, $country);
56
    }
57
58
    /**
59
     * Check if the tax number length is less or equal two.
60
     *
61
     * @return bool
62
     */
63 21
    public function isWithCountry(): bool
64
    {
65 21
        return strlen((string) $this->tax_number) >= 2
66 21
            && ! is_numeric($this->tax_number);
67
    }
68
69
    /**
70
     * Get the tax number.
71
     *
72
     * @return string
73
     */
74 19
    public function taxNumber(): string
75
    {
76 19
        return str($this->tax_number)
77 19
            ->upper()
78 19
            ->value();
79
    }
80
81
    /**
82
     * Get the country prefix.
83
     *
84
     * @return string
85
     */
86 20
    public function country(): string
87
    {
88 20
        return str($this->country)
89 20
            ->upper()
90 20
            ->value();
91
    }
92
93
    /**
94
     * Get a full tax number for a given value object.
95
     * The tax number with a country prefix.
96
     *
97
     * @return string
98
     */
99 11
    public function fullTaxNumber(): string
100
    {
101 11
        return $this->country() . $this->taxNumber();
102
    }
103
104
    /**
105
     * Get the array representation of the value object.
106
     *
107
     * @return array
108
     */
109 1
    public function toArray(): array
110
    {
111
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('full_tax_n...y' => $this->country()) 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...
112 1
            'full_tax_number' => $this->fullTaxNumber(),
113 1
            'tax_number'      => $this->taxNumber(),
114 1
            'country'         => $this->country(),
115
        ];
116
    }
117
118
    /**
119
     * Get the raw string value.
120
     *
121
     * @return string
122
     */
123 3
    public function __toString(): string
124
    {
125 3
        return $this->fullTaxNumber();
126
    }
127
}
128