Issues (121)

src/Model/BasePerCarrierCollection.php (4 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model;
6
7
use Inspirum\Arrayable\BaseListCollection;
8
use InvalidArgumentException;
9
use RuntimeException;
10
use function array_map;
11
use function sprintf;
12
13
/**
14
 * @template TItemKey of array-key
15
 * @template TItemValue
16
 * @template TValue of \Inspirum\Arrayable\Arrayable<TItemKey,TItemValue>&\Inspirum\Balikobot\Model\WithCarrierId
17
 * @extends \Inspirum\Arrayable\BaseListCollection<TItemKey,TItemValue,TValue>
18
 * @implements \Inspirum\Balikobot\Model\PerCarrierCollection<TValue>
19
 */
20
abstract class BasePerCarrierCollection extends BaseListCollection implements PerCarrierCollection
21
{
22
    /**
23
     * @param list<TValue> $items
0 ignored issues
show
The type Inspirum\Balikobot\Model\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...
24
     */
25 34
    public function __construct(
26
        private ?string $carrier = null,
27
        array $items = [],
28
    ) {
29 34
        parent::__construct();
30
31 34
        foreach ($items as $key => $value) {
32 18
            $this->offsetSet($key, $value);
33
        }
34
    }
35
36
    /**
37
     * @throws \RuntimeException
38
     */
39 19
    public function getCarrier(): string
40
    {
41 19
        return $this->carrier ?? throw new RuntimeException('Collection is empty');
42
    }
43
44
    /**
45
     * @param TValue $item
0 ignored issues
show
The type Inspirum\Balikobot\Model\TValue 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...
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49 16
    public function add(WithCarrierId $item): void
50
    {
51 16
        $this->offsetAdd($item);
52
    }
53
54
    /**
55
     * @param non-negative-int $key
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-negative-int at position 0 could not be parsed: Unknown type name 'non-negative-int' at position 0 in non-negative-int.
Loading history...
56
     * @param TValue $value
57
     */
58 18
    public function offsetSet(mixed $key, mixed $value): void
59
    {
60 18
        $this->validateCarrier($value);
61
62 17
        parent::offsetSet($key, $value);
63
    }
64
65
    /**
66
     * @param TValue $value
67
     */
68 16
    public function offsetAdd(mixed $value): void
69
    {
70 16
        $this->validateCarrier($value);
71
72 15
        parent::offsetAdd($value);
73
    }
74
75
    /**
76
     * @return TValue|null
77
     */
78 1
    public function getForCarrierId(string $carrierId): ?WithCarrierId
79
    {
80 1
        return $this->first(static fn (WithCarrierId $item) => $item->getCarrierId() === $carrierId);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->first(function(...) { /* ... */ }) also could return the type Inspirum\Balikobot\Model\WithCarrierId which is incompatible with the documented return type Inspirum\Balikobot\Model\TValue|null.
Loading history...
81
    }
82
83
    /**
84
     * @param callable(TValue): bool $filter
85
     *
86
     * @return TValue|null
87
     */
88 1
    protected function first(callable $filter): ?WithCarrierId
89
    {
90 1
        foreach ($this->items as $package) {
91 1
            if ($filter($package)) {
92 1
                return $package;
93
            }
94
        }
95
96 1
        return null;
97
    }
98
99
    /** @inheritDoc */
100 7
    public function getCarrierIds(): array
101
    {
102 7
        return array_map(static fn (WithCarrierId $item) => $item->getCarrierId(), $this->getItems());
103
    }
104
105
    /**
106
     * @throws \InvalidArgumentException
107
     */
108 32
    private function validateCarrier(WithCarrierId $item): void
109
    {
110 32
        if ($this->carrier === null) {
111 1
            $this->carrier = $item->getCarrier();
112
113 1
            return;
114
        }
115
116 31
        if ($this->carrier !== $item->getCarrier()) {
117 3
            throw new InvalidArgumentException(
118 3
                sprintf(
119 3
                    'Item carrier mismatch ("%s" instead "%s")',
120 3
                    $item->getCarrier(),
121 3
                    $this->carrier,
122 3
                ),
123 3
            );
124
        }
125
    }
126
}
127