Street::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Temidaio\ValueObjects;
6
7
use Illuminate\Support\Str;
8
use MichaelRubel\ValueObjects\ValueObject;
9
use Temidaio\ValueObjects\Formatters\StreetFormatter;
10
11
class Street extends ValueObject
12
{
13
    /**
14
     * Internal properties.
15
     *
16
     * @var string|null
17
     */
18
    protected ?string $prefix = null;
19
    protected ?string $street = null;
20
    protected ?string $number = null;
21
    protected ?string $local  = null;
22
23
    /**
24
     * @param array|null $data
25
     */
26 25
    final public function __construct(?array $data)
27
    {
28 25
        collect($data)->each(
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

28
        collect(/** @scrutinizer ignore-type */ $data)->each(
Loading history...
29 25
            fn ($value, $key) => ! property_exists($this, $key)
30 25
                ?: $this->{$key} = $this->sanitize($value)
31 25
        );
32
    }
33
34
    /**
35
     * @return string
36
     */
37 22
    public function value(): string
38
    {
39 22
        return $this->format();
40
    }
41
42
    /**
43
     * Sanitize the passed property.
44
     *
45
     * @param string|null $value
46
     *
47
     * @return string|null
48
     */
49 17
    protected function sanitize(?string $value): ?string
50
    {
51 17
        return ! is_null($value)
52 17
            ? Str::squish($value)
53 17
            : null;
54
    }
55
56
    /**
57
     * @return string
58
     */
59 22
    protected function format(): string
60
    {
61 22
        return format(StreetFormatter::class, [
62 22
            'prefix' => $this->prefix,
63 22
            'street' => $this->street,
64 22
            'number' => $this->number,
65 22
            'local'  => $this->local,
66 22
        ]);
67
    }
68
69
    /**
70
     * @return string
71
     */
72 22
    public function __toString(): string
73
    {
74 22
        return $this->value();
75
    }
76
}
77