DefaultTransportCost::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 5
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\TransportCost;
6
7
use Inspirum\Arrayable\BaseModel;
8
use function array_map;
9
10
/**
11
 * @extends \Inspirum\Arrayable\BaseModel<string,mixed>
12
 */
13
final class DefaultTransportCost extends BaseModel implements TransportCost
14
{
15
    /**
16
     * @param array<\Inspirum\Balikobot\Model\TransportCost\TransportCostPart> $costsBreakdown
17
     */
18 5
    public function __construct(
19
        private readonly string $batchId,
20
        private readonly string $carrier,
21
        private readonly float $totalCost,
22
        private readonly string $currencyCode,
23
        private readonly array $costsBreakdown = [],
24
    ) {
25 5
    }
26
27 2
    public function getBatchId(): string
28
    {
29 2
        return $this->batchId;
30
    }
31
32 1
    public function getCarrier(): string
33
    {
34 1
        return $this->carrier;
35
    }
36
37 3
    public function getTotalCost(): float
38
    {
39 3
        return $this->totalCost;
40
    }
41
42 3
    public function getCurrencyCode(): string
43
    {
44 3
        return $this->currencyCode;
45
    }
46
47
    /** @inheritDoc */
48 1
    public function getCostsBreakdown(): array
49
    {
50 1
        return $this->costsBreakdown;
51
    }
52
53
    /**
54
     * @return array<string,mixed>
55
     */
56 2
    public function __toArray(): array
57
    {
58 2
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('batchId' =...$this->costsBreakdown)) returns the type array<string,array|double|string> which is incompatible with the return type mandated by Inspirum\Arrayable\BaseModel::__toArray() of Inspirum\Arrayable\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...
59 2
            'batchId' => $this->batchId,
60 2
            'carrier' => $this->carrier,
61 2
            'totalCost' => $this->totalCost,
62 2
            'currencyCode' => $this->currencyCode,
63 2
            'costsBreakdown' => array_map(static fn (TransportCostPart $costPart): array => $costPart->__toArray(), $this->costsBreakdown),
64 2
        ];
65
    }
66
}
67