Passed
Push — main ( ec6948...9b9c4a )
by Michael
04:01
created

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

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