Passed
Push — main ( 250a5e...66e4ab )
by Michael
03:55
created

TaxNumber::fullTaxNumber()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects\Collection\Complex;
6
7
use MichaelRubel\Formatters\Collection\TaxNumberFormatter;
8
use MichaelRubel\ValueObjects\ValueObject;
9
10
/**
11
 * @method static static make(string $number, string $country = null)
12
 */
13
class TaxNumber extends ValueObject
14
{
15
    /**
16
     * Create a new instance of the value object.
17
     *
18
     * @param  string|null  $number
19
     * @param  string|null  $country
20
     */
21 23
    public function __construct(
22
        protected ?string $number = null,
23
        protected ?string $country = null,
24
    ) {
25 23
        $this->number = $this->format();
26
27 23
        if ($this->isWithCountry()) {
28 17
            $this->split();
29
        }
30
    }
31
32
    /**
33
     * Check if the tax number length is less or equal two.
34
     *
35
     * @return bool
36
     */
37 23
    public function isWithCountry(): bool
38
    {
39 23
        return strlen($this->number) >= 2 && ! is_numeric($this->number);
0 ignored issues
show
Bug introduced by
It seems like $this->number can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        return strlen(/** @scrutinizer ignore-type */ $this->number) >= 2 && ! is_numeric($this->number);
Loading history...
40
    }
41
42
    /**
43
     * Get the tax number with a country prefix.
44
     *
45
     * @return string
46
     */
47 13
    public function fullTaxNumber(): string
48
    {
49 13
        return $this->country() . $this->taxNumber();
50
    }
51
52
    /**
53
     * Get the tax number without country prefix.
54
     *
55
     * @return string
56
     */
57 23
    public function taxNumber(): string
58
    {
59 23
        return str($this->number)
60 23
            ->upper()
61 23
            ->value();
62
    }
63
64
    /**
65
     * Get the country prefix for a given tax number.
66
     *
67
     * @return string
68
     */
69 23
    public function country(): string
70
    {
71 23
        return str($this->country)
72 23
            ->upper()
73 23
            ->value();
74
    }
75
76
    /**
77
     * Get the object value.
78
     *
79
     * @return string
80
     */
81 5
    public function value(): string
82
    {
83 5
        return $this->fullTaxNumber();
84
    }
85
86
    /**
87
     * Format the value.
88
     *
89
     * @return string
90
     */
91 23
    protected function format(): string
92
    {
93 23
        return format(TaxNumberFormatter::class, $this->taxNumber(), $this->country());
94
    }
95
96
    /**
97
     * Split the value.
98
     *
99
     * @return void
100
     */
101 17
    protected function split(): void
102
    {
103 17
        $this->country = str($this->number)
104 17
            ->substr(0, 2)
105 17
            ->upper()
106 17
            ->value();
107
108 17
        $this->number = str($this->number)
109 17
            ->substr(2)
110 17
            ->value();
111
    }
112
113
    /**
114
     * Get an array representation of the value object.
115
     *
116
     * @return array
117
     */
118 1
    public function toArray(): array
119
    {
120
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('fullTaxNum...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...
121 1
            'fullTaxNumber' => $this->fullTaxNumber(),
122 1
            'taxNumber'     => $this->taxNumber(),
123 1
            'country'       => $this->country(),
124
        ];
125
    }
126
}
127