CacheItem::isHit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Crossword\Infrastructure\Cache;
6
7
use DateInterval;
8
use DateTimeImmutable;
9
use DateTimeInterface;
10
use Psr\Cache\CacheItemInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Cache\CacheItemInterface 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...
11
12
final class CacheItem implements CacheItemInterface
13
{
14
    private const DEFAULT_EXPIRATION = 'now +1 year';
15
16
    private string $key;
17
    private mixed $value;
18
    private bool $hit;
19
    private DateTimeInterface | int | null $expiration;
0 ignored issues
show
Bug introduced by
The type App\Crossword\Infrastructure\Cache\null 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...
20
21
    public function __construct(string $key, mixed $value)
22
    {
23
        $this->key = $key;
24
        $this->value = $value;
25
        $this->hit = null !== $value;
26
    }
27
28
    public function getKey(): string
29
    {
30
        return $this->key;
31
    }
32
33
    public function get(): mixed
34
    {
35
        return $this->value;
36
    }
37
38
    public function isHit(): bool
39
    {
40
        return $this->hit;
41
    }
42
43
    public function set(mixed $value): void
44
    {
45
        $this->value = $value;
46
    }
47
48
    public function expiresAt(?DateTimeInterface $expiration): void
49
    {
50
        $this->expiration = $expiration;
51
        if (null === $expiration) {
52
            $this->expiration = new DateTimeImmutable(self::DEFAULT_EXPIRATION);
53
        }
54
    }
55
56
    public function expiresAfter(DateInterval | int | null $time): void
57
    {
58
        if ($time instanceof DateInterval) {
59
            $this->expiration = new DateTimeImmutable();
60
            $this->expiration->add($time);
61
62
            return;
63
        }
64
65
        if (is_int($time)) {
0 ignored issues
show
introduced by
The condition is_int($time) is always true.
Loading history...
66
            $this->expiration = new DateTimeImmutable(sprintf('now +%d seconds', $time));
67
68
            return;
69
        }
70
71
        $this->expiration = new DateTimeImmutable(self::DEFAULT_EXPIRATION);
72
    }
73
74
    public function expiration(): DateTimeInterface | int | null
75
    {
76
        return $this->expiration;
77
    }
78
}
79