Passed
Pull Request — master (#323)
by Alexander
03:11
created

CacheObjectDataSetDecorator::hasCacheItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\DataSet;
6
7
use JetBrains\PhpStorm\ArrayShape;
8
use Yiisoft\Validator\ObjectDataSetInterface;
9
use Yiisoft\Validator\RulesProviderInterface;
10
11
use function get_class;
12
13
final class CacheObjectDataSetDecorator implements ObjectDataSetInterface, RulesProviderInterface
14
{
15
    private string $cacheKey;
16
17
    #[ArrayShape([
18
        [
19
            'rules' => 'iterable',
20
            'reflectionProperties' => 'array',
21
            'data' => 'array',
22
        ],
23
    ])]
24
    private static array $cache = [];
25
26 5
    public function __construct(private ObjectDataSetInterface|RulesProviderInterface $decorated)
27
    {
28 5
        $this->cacheKey = get_class($this->decorated->getObject());
0 ignored issues
show
Bug introduced by
The method getObject() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\DataSet\ObjectDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->cacheKey = get_class($this->decorated->/** @scrutinizer ignore-call */ getObject());
Loading history...
29
30 5
        $this->deleteCacheItem('data');
31
    }
32
33 11
    public function getAttributeValue(string $attribute): mixed
34
    {
35 11
        return $this->decorated->getAttributeValue($attribute);
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\Tests\Stub\ObjectWithCallsCount or Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\Tests\...DataSetAndRulesProvider or Yiisoft\Validator\DataSet\ObjectDataSet or Yiisoft\Validator\Tests\Stub\RulesProvidedDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        return $this->decorated->/** @scrutinizer ignore-call */ getAttributeValue($attribute);
Loading history...
36
    }
37
38 12
    public function getData(): mixed
39
    {
40 12
        if ($this->hasCacheItem('data')) {
41 9
            return $this->getCacheItem('data');
42
        }
43
44 9
        $data = $this->decorated->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\Tests\Stub\ObjectWithCallsCount or Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\Tests\...DataSetAndRulesProvider or Yiisoft\Validator\DataSet\ObjectDataSet or Yiisoft\Validator\Tests\Stub\RulesProvidedDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $data = $this->decorated->getData();
Loading history...
45 9
        $this->updateCacheItem('data', $data);
46
47 9
        return $data;
48
    }
49
50
    public function hasAttribute(string $attribute): bool
51
    {
52
        return $this->decorated->hasAttribute($attribute);
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\Tests\Stub\ObjectWithCallsCount or Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\Tests\...DataSetAndRulesProvider or Yiisoft\Validator\DataSet\ObjectDataSet or Yiisoft\Validator\Tests\Stub\RulesProvidedDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return $this->decorated->/** @scrutinizer ignore-call */ hasAttribute($attribute);
Loading history...
53
    }
54
55 12
    public function getRules(): iterable
56
    {
57
        if (
58 12
            $this->hasCacheItem('rules') &&
59 12
            $this->hasCacheItem('propertyVisibility') &&
60 12
            $this->decorated->getPropertyVisibility() === $this->getCacheItem('propertyVisibility')
0 ignored issues
show
Bug introduced by
The method getPropertyVisibility() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\DataSet\ObjectDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            $this->decorated->/** @scrutinizer ignore-call */ 
61
                              getPropertyVisibility() === $this->getCacheItem('propertyVisibility')
Loading history...
61
        ) {
62 9
            return $this->getCacheItem('rules');
63
        }
64
65 9
        $rules = $this->decorated->getRules();
0 ignored issues
show
Bug introduced by
The method getRules() does not exist on Yiisoft\Validator\ObjectDataSetInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\Validator\ObjectDataSetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        /** @scrutinizer ignore-call */ 
66
        $rules = $this->decorated->getRules();
Loading history...
66
67 9
        $this->updateCacheItem('rules', $rules);
68 9
        $this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility());
69
70 9
        return $rules;
71
    }
72
73
    public function getReflectionProperties(): array
74
    {
75
        if (
76
            $this->hasCacheItem('reflectionProperties') &&
77
            $this->hasCacheItem('propertyVisibility') &&
78
            $this->decorated->getPropertyVisibility() === $this->getCacheItem('propertyVisibility')
79
        ) {
80
            return $this->getCacheItem('reflectionProperties');
81
        }
82
83
        $reflectionProperties = $this->decorated->getReflectionProperties();
0 ignored issues
show
Bug introduced by
The method getReflectionProperties() does not exist on Yiisoft\Validator\RulesProviderInterface. It seems like you code against a sub-type of Yiisoft\Validator\RulesProviderInterface such as Yiisoft\Validator\DataSe...eObjectDataSetDecorator or Yiisoft\Validator\DataSet\ObjectDataSet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        $reflectionProperties = $this->decorated->getReflectionProperties();
Loading history...
84
85
        $this->updateCacheItem('reflectionProperties', $reflectionProperties);
86
        $this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility());
87
88
        return $reflectionProperties;
89
    }
90
91 9
    public function getPropertyVisibility(): int
92
    {
93 9
        return $this->decorated->getPropertyVisibility();
94
    }
95
96
    public function getObject(): object
97
    {
98
        return $this->decorated->getObject();
99
    }
100
101 12
    private function hasCacheItem(string $key): bool
102
    {
103 12
        return isset(self::$cache[$this->cacheKey][$key]);
104
    }
105
106 9
    #[ArrayShape([
107
        'rules' => 'iterable',
108
        'reflectionProperties' => 'array',
109
        'data' => 'array',
110
    ])]
111
    private function getCacheItem(string $key): mixed
112
    {
113 9
        return self::$cache[$this->cacheKey][$key];
114
    }
115
116 9
    private function updateCacheItem(string $key, mixed $value): void
117
    {
118 9
        self::$cache[$this->cacheKey][$key] = $value;
119
    }
120
121 5
    private function deleteCacheItem(string $key): void
122
    {
123 5
        unset(self::$cache[$this->cacheKey][$key]);
124
    }
125
}
126