DefaultTransportCostCollection::getCosts()   A
last analyzed

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
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\BaseListCollection;
8
use RuntimeException;
9
use function array_map;
10
11
/**
12
 * @extends \Inspirum\Arrayable\BaseListCollection<string,mixed,\Inspirum\Balikobot\Model\TransportCost\TransportCost>
13
 */
14
final class DefaultTransportCostCollection extends BaseListCollection implements TransportCostCollection
15
{
16
    /**
17
     * @param list<\Inspirum\Balikobot\Model\TransportCost\TransportCost> $items
0 ignored issues
show
Bug introduced by
The type Inspirum\Balikobot\Model\TransportCost\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
     */
19 5
    public function __construct(
20
        private readonly ?string $carrier,
21
        array $items = [],
22
    ) {
23 5
        parent::__construct($items);
24
    }
25
26 1
    public function getCarrier(): string
27
    {
28 1
        return $this->carrier ?? throw new RuntimeException('Collection is empty');
29
    }
30
31
    /** @inheritDoc */
32 1
    public function getCosts(): array
33
    {
34 1
        return $this->getItems();
35
    }
36
37
    /**
38
     * @return list<string>
39
     */
40 1
    public function getBatchIds(): array
41
    {
42 1
        return array_map(static fn (TransportCost $transportCost) => $transportCost->getBatchId(), $this->getItems());
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_map(functio.../ }, $this->getItems()) returns the type array which is incompatible with the documented return type Inspirum\Balikobot\Model\TransportCost\list.
Loading history...
43
    }
44
45 2
    public function getTotalCost(): float
46
    {
47 2
        $totalCost = 0.0;
48 2
        $currencyCode = $this->getCurrencyCode();
49
50 2
        foreach ($this->getItems() as $cost) {
51 2
            if ($cost->getCurrencyCode() !== $currencyCode) {
52 1
                throw new RuntimeException('Package cost currency codes are not the same');
53
            }
54
55 2
            $totalCost += $cost->getTotalCost();
56
        }
57
58 1
        return $totalCost;
59
    }
60
61 3
    public function getCurrencyCode(): string
62
    {
63 3
        if (empty($this->items)) {
64 1
            throw new RuntimeException('Collection is empty');
65
        }
66
67 2
        return $this->items[0]->getCurrencyCode();
68
    }
69
}
70