Passed
Branch master (2952f7)
by Mikaël
03:29
created

Rules   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 49
dl 0
loc 136
ccs 66
cts 66
cp 1
rs 9.92
c 5
b 0
f 0
wmc 31

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B applyRules() 0 16 10
A applyRulesFromAttribute() 0 8 3
A getListRule() 0 3 1
A getItemTypeRule() 0 3 1
A getFile() 0 3 1
A getArrayRule() 0 3 1
A getAttribute() 0 3 1
A getMethods() 0 3 1
A setAttribute() 0 5 1
A getRule() 0 5 2
A getMethod() 0 3 1
A hasRuleBeenAppliedToAttribute() 0 3 1
A getAppliedRuleToAttributeKey() 0 6 1
A ruleHasBeenAppliedToAttribute() 0 3 1
A getGenerator() 0 3 1
A getXmlRule() 0 3 1
A setMethod() 0 5 1
A getEnumerationRule() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Generator\Generator;
10
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
11
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
12
13
final class Rules
14
{
15
    private StructAttribute $attribute;
16
17
    private AbstractModelFile $file;
18
19
    private PhpMethod $method;
20
21
    private MethodContainer $methods;
22
23
    private static array $rulesAppliedToAttribute = [];
24
25 158
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods)
26
    {
27 158
        $this->file = $file;
28 158
        $this->method = $method;
29 158
        $this->attribute = $attribute;
30 158
        $this->methods = $methods;
31 158
    }
32
33 112
    public function applyRules(string $parameterName, bool $itemType = false): void
34
    {
35 112
        if ($this->attribute->isArray() && !$itemType) {
36 52
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
37 102
        } elseif ($this->attribute->isXml() && !$itemType) {
38 4
            $this->getXmlRule()->applyRule($parameterName, null, $itemType);
39 102
        } elseif ($this->attribute->isList() && !$itemType) {
40 6
            $this->getListRule()->applyRule($parameterName, null, $itemType);
41 102
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) {
42 48
            $this->getEnumerationRule()->applyRule($parameterName, null);
43 102
        } elseif ($itemType) {
44 36
            $this->getItemTypeRule()->applyRule($parameterName, null);
45 96
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) {
46 82
            $rule->applyRule($parameterName, null, $itemType);
47
        }
48 112
        $this->applyRulesFromAttribute($parameterName, $itemType);
49 112
    }
50
51 112
    public function getRule(string $name): ?AbstractRule
52
    {
53 112
        $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name));
54
55 112
        return class_exists($className) ? new $className($this) : null;
56
    }
57
58 52
    public function getArrayRule(): ArrayRule
59
    {
60 52
        return $this->getRule('array');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('array') returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerat...le\Validation\ArrayRule.
Loading history...
61
    }
62
63 4
    public function getXmlRule(): XmlRule
64
    {
65 4
        return $this->getRule('xml');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('xml') returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerator\File\Validation\XmlRule.
Loading history...
66
    }
67
68 60
    public function getEnumerationRule(): EnumerationRule
69
    {
70 60
        return $this->getRule('enumeration');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('enumeration') returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerat...idation\EnumerationRule.
Loading history...
71
    }
72
73 50
    public function getItemTypeRule(): ItemTypeRule
74
    {
75 50
        return $this->getRule('itemType');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('itemType') returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerat...Validation\ItemTypeRule.
Loading history...
76
    }
77
78 6
    public function getListRule(): ListRule
79
    {
80 6
        return $this->getRule('list');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('list') returns the type null which is incompatible with the type-hinted return WsdlToPhp\PackageGenerat...ile\Validation\ListRule.
Loading history...
81
    }
82
83 146
    public function getAttribute(): StructAttribute
84
    {
85 146
        return $this->attribute;
86
    }
87
88 4
    public function setAttribute(StructAttribute $attribute): self
89
    {
90 4
        $this->attribute = $attribute;
91
92 4
        return $this;
93
    }
94
95 112
    public function getFile(): AbstractModelFile
96
    {
97 112
        return $this->file;
98
    }
99
100 146
    public function getMethod(): PhpMethod
101
    {
102 146
        return $this->method;
103
    }
104
105 54
    public function setMethod(PhpMethod $method): self
106
    {
107 54
        $this->method = $method;
108
109 54
        return $this;
110
    }
111
112 66
    public function getMethods(): MethodContainer
113
    {
114 66
        return $this->methods;
115
    }
116
117 4
    public function getGenerator(): Generator
118
    {
119 4
        return $this->file->getGenerator();
120
    }
121
122 146
    public static function ruleHasBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): void
123
    {
124 146
        self::$rulesAppliedToAttribute[self::getAppliedRuleToAttributeKey($rule, $value, $attribute)] = true;
125 146
    }
126
127 4
    public static function hasRuleBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): bool
128
    {
129 4
        return array_key_exists(self::getAppliedRuleToAttributeKey($rule, $value, $attribute), self::$rulesAppliedToAttribute);
130
    }
131
132 112
    private function applyRulesFromAttribute(string $parameterName, bool $itemType = false): void
133
    {
134 112
        foreach ($this->attribute->getMeta() as $metaName => $metaValue) {
135 100
            if (!($rule = $this->getRule($metaName)) instanceof AbstractRule) {
136 70
                continue;
137
            }
138
139 94
            $rule->applyRule($parameterName, $metaValue, $itemType);
140
        }
141 112
    }
142
143 146
    private static function getAppliedRuleToAttributeKey(AbstractRule $rule, $value, StructAttribute $attribute): string
144
    {
145 146
        return implode('_', [
146 146
            $rule->validationRuleComment($value),
147 146
            $attribute->getOwner()->getName(),
148 146
            $attribute->getName(),
149
        ]);
150
    }
151
}
152