Passed
Push — main ( 267d80...2eae33 )
by Michael
03:37
created

TaxNumber::split()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
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|null $number, string|null $prefix = null)
12
 * @method static static from(string|null $number, string|null $prefix = null)
13
 */
14
class TaxNumber extends ValueObject
15
{
16
    /**
17
     * Create a new instance of the value object.
18
     *
19
     * @param  string|null  $number
20
     * @param  string|null  $prefix
21
     */
22 23
    public function __construct(
23
        protected ?string $number = null,
24
        protected ?string $prefix = null,
25
    ) {
26 23
        $this->number = $this->format();
27
28 23
        if ($this->isWithPrefix()) {
29 17
            $this->split();
30
        }
31
    }
32
33
    /**
34
     * Check if the tax number length is less or equal two.
35
     *
36
     * @return bool
37
     */
38 23
    public function isWithPrefix(): bool
39
    {
40 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

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