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

CommodityCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
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