Passed
Push — main ( 0df40c...ce1342 )
by Michael
04:28 queued 52s
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 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
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 MichaelRubel\Formatters\Collection\TaxNumberFormatter;
8
use MichaelRubel\ValueObjects\ValueObject;
9
10
/**
11
 * @method static static make(string $taxNumber, string $country)
12
 */
13
class TaxNumber extends ValueObject
14
{
15
    /**
16
     * Create a new instance of the value object.
17
     *
18
     * @param  string|null  $taxNumber
19
     * @param  string|null  $country
20
     */
21 21
    public function __construct(
22
        protected ?string $taxNumber = null,
23
        protected ?string $country = null,
24
    ) {
25 21
        $this->taxNumber = format(TaxNumberFormatter::class, $this->taxNumber, $this->country);
26
27 21
        $this->when($this->isWithCountry(), function () {
28 15
            $this->country = str($this->taxNumber)
29 15
                ->substr(0, 2)
30 15
                ->upper()
31 15
                ->value();
32
33 15
            $this->taxNumber = str($this->taxNumber)
34 15
                ->substr(2)
35 15
                ->value();
36
        });
37
    }
38
39
    /**
40
     * Get the tax number with a country prefix.
41
     *
42
     * @return string
43
     */
44 11
    public function fullTaxNumber(): string
45
    {
46 11
        return $this->country() . $this->taxNumber();
47
    }
48
49
    /**
50
     * Get the tax number without country prefix.
51
     *
52
     * @return string
53
     */
54 19
    public function taxNumber(): string
55
    {
56 19
        return str($this->taxNumber)
57 19
            ->upper()
58 19
            ->value();
59
    }
60
61
    /**
62
     * Get the country prefix for a given tax number.
63
     *
64
     * @return string
65
     */
66 20
    public function country(): string
67
    {
68 20
        return str($this->country)
69 20
            ->upper()
70 20
            ->value();
71
    }
72
73
    /**
74
     * Check if the tax number length is less or equal two.
75
     *
76
     * @return bool
77
     */
78 21
    public function isWithCountry(): bool
79
    {
80 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

80
        return strlen(/** @scrutinizer ignore-type */ $this->taxNumber) >= 2 && ! is_numeric($this->taxNumber);
Loading history...
81
    }
82
83
    /**
84
     * Get the object value.
85
     *
86
     * @return string
87
     */
88 3
    public function value(): string
89
    {
90 3
        return $this->fullTaxNumber();
91
    }
92
93
    /**
94
     * Get an 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