Passed
Pull Request — master (#265)
by Alexander
05:00 queued 02:31
created

AttributesRulesProvider::parseRules()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 21
ccs 12
cts 14
cp 0.8571
rs 9.2222
cc 6
nc 10
nop 0
crap 6.105
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\RulesProvider;
6
7
use Generator;
8
use ReflectionAttribute;
9
use ReflectionClass;
10
use ReflectionException;
11
use ReflectionObject;
12
use ReflectionProperty;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\RulesProviderInterface;
15
16
final class AttributesRulesProvider implements RulesProviderInterface
17
{
18
    /**
19
     * @var Generator|null
20
     */
21
    private Generator|null $rules = null;
22
23 13
    public function __construct(
24
        /**
25
         * @param class-string|object $class
26
         */
27
        private string|object $source,
28
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC
29
    ) {
30
    }
31
32
    /**
33
     * @throws ReflectionException
34
     *
35
     * @return iterable
36
     */
37 13
    public function getRules(): iterable
38
    {
39 13
        if ($this->rules === null) {
40 13
            $this->rules = $this->parseRules();
41
        }
42
43 13
        yield from $this->rules;
44
    }
45
46
    /**
47
     * @throws ReflectionException
48
     *
49
     * @return Generator
50
     */
51 13
    private function parseRules(): iterable
52
    {
53 13
        $reflection = is_object($this->source)
54 5
            ? new ReflectionObject($this->source)
55 8
            : new ReflectionClass($this->source);
56
57 13
        $reflectionProperties = $reflection->getProperties();
58 13
        if ($reflectionProperties === []) {
59
            return [];
60
        }
61 13
        foreach ($reflectionProperties as $property) {
62 13
            if (!$this->isUseProperty($property)) {
63 7
                continue;
64
            }
65
66 13
            $attributes = $property->getAttributes(RuleInterface::class, ReflectionAttribute::IS_INSTANCEOF);
67 13
            if ($attributes === []) {
68
                continue;
69
            }
70
71 13
            yield $property->getName() => $this->createAttributes($attributes);
72
        }
73
    }
74
75
    /**
76
     * @param array<array-key, ReflectionAttribute<RuleInterface>> $attributes
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, Reflect...tribute<RuleInterface>> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, ReflectionAttribute<RuleInterface>>.
Loading history...
77
     *
78
     * @return iterable
79
     */
80 13
    private function createAttributes(array $attributes): iterable
81
    {
82 13
        foreach ($attributes as $attribute) {
83 13
            yield $attribute->newInstance();
84
        }
85
    }
86
87 13
    private function isUseProperty(ReflectionProperty $property): bool
88
    {
89 13
        return ($property->isPublic() && ($this->propertyVisibility & ReflectionProperty::IS_PUBLIC))
90 12
            || ($property->isPrivate() && ($this->propertyVisibility & ReflectionProperty::IS_PRIVATE))
91 13
            || ($property->isProtected() && ($this->propertyVisibility & ReflectionProperty::IS_PROTECTED));
92
    }
93
}
94