Passed
Push — main ( 01d627...8c0949 )
by Michael
03:55
created

TaxNumber::__toString()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
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\Support\Traits\Conditionable;
8
use Illuminate\Support\Traits\Macroable;
9
use MichaelRubel\Formatters\Collection\TaxNumberFormatter;
10
use MichaelRubel\ValueObjects\ValueObject;
11
12
/**
13
 * @method static static make(string $taxNumber, string $country)
14
 */
15
class TaxNumber extends ValueObject
16
{
17
    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\TaxNumber.
Loading history...
18
19
    /**
20
     * @param  string|null  $taxNumber
21
     * @param  string|null  $country
22
     */
23 21
    public function __construct(
24
        protected ?string $taxNumber = null,
25
        protected ?string $country = null,
26
    ) {
27 21
        $this->taxNumber = format(TaxNumberFormatter::class, $this->taxNumber, $this->country);
28
29 21
        $this->when($this->isWithCountry(), function () {
30 15
            $this->country = str($this->taxNumber)
31 15
                ->substr(0, 2)
32 15
                ->upper()
33 15
                ->value();
34
35 15
            $this->taxNumber = str($this->taxNumber)
36 15
                ->substr(2)
37 15
                ->value();
38
        });
39
    }
40
41
    /**
42
     * Get the tax number with a country prefix.
43
     *
44
     * @return string
45
     */
46 11
    public function fullTaxNumber(): string
47
    {
48 11
        return $this->country() . $this->taxNumber();
49
    }
50
51
    /**
52
     * Get the tax number without country prefix.
53
     *
54
     * @return string
55
     */
56 19
    public function taxNumber(): string
57
    {
58 19
        return str($this->taxNumber)
59 19
            ->upper()
60 19
            ->value();
61
    }
62
63
    /**
64
     * Get the country prefix for a given tax number.
65
     *
66
     * @return string
67
     */
68 20
    public function country(): string
69
    {
70 20
        return str($this->country)
71 20
            ->upper()
72 20
            ->value();
73
    }
74
75
    /**
76
     * Check if the tax number length is less or equal two.
77
     *
78
     * @return bool
79
     */
80 21
    public function isWithCountry(): bool
81
    {
82 21
        return strlen($this->taxNumber) >= 2 && ! is_numeric($this->taxNumber);
0 ignored issues
show
Bug introduced by
It seems like $this->taxNumber 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

82
        return strlen(/** @scrutinizer ignore-type */ $this->taxNumber) >= 2 && ! is_numeric($this->taxNumber);
Loading history...
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function value(): string
89
    {
90
        return $this->fullTaxNumber();
91
    }
92
93
    /**
94
     * Get array representation of the value object.
95
     *
96
     * @return array
97
     */
98 1
    public function toArray(): array
99
    {
100
        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...
101 1
            'fullTaxNumber' => $this->fullTaxNumber(),
102 1
            'taxNumber'     => $this->taxNumber(),
103 1
            'country'       => $this->country(),
104
        ];
105
    }
106
107
    /**
108
     * Get string representation of the value object.
109
     *
110
     * @return string
111
     */
112 3
    public function __toString(): string
113
    {
114 3
        return $this->fullTaxNumber();
115
    }
116
}
117