Passed
Pull Request — master (#323)
by Dmitriy
02:46
created

CacheObjectDataSetDecorator::getAttributeValue()   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
            'propertyVisibility' => 'int',
21
        ],
22
    ])]
23
    private static array $cache = [];
24
25 5
    public function __construct(private ObjectDataSetInterface|RulesProviderInterface $decorated)
26
    {
27 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

27
        $this->cacheKey = get_class($this->decorated->/** @scrutinizer ignore-call */ getObject());
Loading history...
28
    }
29
30 11
    public function getAttributeValue(string $attribute): mixed
31
    {
32 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

32
        return $this->decorated->/** @scrutinizer ignore-call */ getAttributeValue($attribute);
Loading history...
33
    }
34
35 14
    public function getData(): mixed
36
    {
37 14
        return $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

37
        return $this->decorated->/** @scrutinizer ignore-call */ getData();
Loading history...
38
    }
39
40
    public function hasAttribute(string $attribute): bool
41
    {
42
        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

42
        return $this->decorated->/** @scrutinizer ignore-call */ hasAttribute($attribute);
Loading history...
43
    }
44
45 12
    public function getRules(): iterable
46
    {
47
        if (
48 12
            $this->hasCacheItem('rules') &&
49 12
            $this->hasCacheItem('propertyVisibility') &&
50 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

50
            $this->decorated->/** @scrutinizer ignore-call */ 
51
                              getPropertyVisibility() === $this->getCacheItem('propertyVisibility')
Loading history...
51
        ) {
52 9
            return $this->getCacheItem('rules');
53
        }
54
55 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

55
        /** @scrutinizer ignore-call */ 
56
        $rules = $this->decorated->getRules();
Loading history...
56
57 9
        $this->updateCacheItem('rules', $rules);
58 9
        $this->updateCacheItem('propertyVisibility', $this->getPropertyVisibility());
59
60 9
        return $rules;
61
    }
62
63 9
    public function getPropertyVisibility(): int
64
    {
65 9
        return $this->decorated->getPropertyVisibility();
66
    }
67
68
    public function getObject(): object
69
    {
70
        return $this->decorated->getObject();
71
    }
72
73 12
    private function hasCacheItem(string $key): bool
74
    {
75 12
        return isset(self::$cache[$this->cacheKey][$key]);
76
    }
77
78 9
    #[ArrayShape([
79
        'rules' => 'iterable',
80
        'propertyVisibility' => 'int',
81
    ])]
82
    private function getCacheItem(string $key): mixed
83
    {
84 9
        return self::$cache[$this->cacheKey][$key];
85
    }
86
87 9
    private function updateCacheItem(string $key, mixed $value): void
88
    {
89 9
        self::$cache[$this->cacheKey][$key] = $value;
90
    }
91
}
92