Passed
Push — master ( 0a21cd...0ad97c )
by Janko
05:01
created

CommodityCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Commodity\Lib;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
8
use Stu\Orm\Entity\Commodity;
9
use Stu\Orm\Repository\CommodityRepositoryInterface;
10
11
final class CommodityCache implements CommodityCacheInterface
12
{
13
    /**
14
     * @var array<int, Commodity>|null
15
     */
16
    private ?array $commodityArray = null;
17
18 2
    public function __construct(private CommodityRepositoryInterface $commodityRepository)
19
    {
20 2
    }
21
22 18
    #[Override]
23
    public function get(int $commodityId): Commodity
24
    {
25 18
        if ($this->commodityArray === null) {
26 1
            $this->commodityArray = $this->commodityRepository->getAll();
27
        }
28
29 18
        return $this->commodityArray[$commodityId];
30
    }
31
32 5
    #[Override]
33
    public function getAll(?int $type = null): array
34
    {
35 5
        if ($this->commodityArray === null) {
36
            $this->commodityArray = $this->commodityRepository->getAll();
37
        }
38
39 5
        if ($type !== null) {
40 5
            return array_filter($this->commodityArray, fn (Commodity $commodity): bool => $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

40
            return array_filter(/** @scrutinizer ignore-type */ $this->commodityArray, fn (Commodity $commodity): bool => $commodity->getType() === $type);
Loading history...
41
        }
42
43
        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...
44
    }
45
}
46