Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

CommodityCache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 35
ccs 2
cts 12
cp 0.1666
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 2
A getAll() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Commodity\Lib;
6
7
use Stu\Orm\Entity\CommodityInterface;
8
use Stu\Orm\Repository\CommodityRepositoryInterface;
9
10
final class CommodityCache implements CommodityCacheInterface
11
{
12
    private CommodityRepositoryInterface $commodityRepository;
13
14
    /**
15
     * @var array<int, CommodityInterface>|null
16
     */
17
    private ?array $commodityArray = null;
18
19 4
    public function __construct(
20
        CommodityRepositoryInterface $commodityRepository
21
    ) {
22 4
        $this->commodityRepository = $commodityRepository;
23
    }
24
25
    public function get(int $commodityId): CommodityInterface
26
    {
27
        if ($this->commodityArray === null) {
28
            $this->commodityArray = $this->commodityRepository->getAll();
29
        }
30
31
        return $this->commodityArray[$commodityId];
32
    }
33
34
    public function getAll(int $type = null): array
35
    {
36
        if ($this->commodityArray === null) {
37
            $this->commodityArray = $this->commodityRepository->getAll();
38
        }
39
40
        if ($type !== null) {
41
            return array_filter($this->commodityArray, fn (CommodityInterface $commodity) => $commodity->getType() === $type);
0 ignored issues
show
Bug introduced by
It seems like $this->commodityArray can also be of type null; however, parameter $array of array_filter() does only seem to accept array, 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

41
            return array_filter(/** @scrutinizer ignore-type */ $this->commodityArray, fn (CommodityInterface $commodity) => $commodity->getType() === $type);
Loading history...
42
        }
43
44
        return $this->commodityArray;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->commodityArray could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
45
    }
46
}
47