Passed
Push — feature/issue-165 ( 2b7e50...101bca )
by Mikaël
14:10
created

Rules::getArrayRule()   A

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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
6
use WsdlToPhp\PackageGenerator\Model\Method;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Model\Struct;
10
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
11
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
12
13
class Rules
14
{
15
    /**
16
     * @var StructAttribute
17
     */
18
    protected $attribute;
19
    /**
20
     * @var AbstractModelFile
21
     */
22
    protected $file;
23
    /**
24
     * @var PhpMethod
25
     */
26
    protected $method;
27
    /**
28
     * @var MethodContainer
29
     */
30 780
    protected $methods;
31
    /**
32 780
     * @param AbstractModelFile $file
33 780
     * @param PhpMethod $method
34
     * @param StructAttribute $attribute
35
     */
36
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods)
37
    {
38 228
        $this->file = $file;
39
        $this->method = $method;
40 228
        $this->attribute = $attribute;
41 114
        $this->methods = $methods;
42 228
    }
43 102
    /**
44 225
     * @param string $parameterName
45 108
     * @param bool $itemType
46 204
     */
47 150
    public function applyRules($parameterName, $itemType = false)
48 75
    {
49 228
        if ($this->attribute->isArray() && !$itemType) {
50 228
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
51
        } elseif ($this->attribute->isList() && !$itemType) {
52
            $this->getListRule()->applyRule($parameterName, null, $itemType);
53
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) {
54
            $this->getEnumerationRule()->applyRule($parameterName, null);
55
        } elseif ($itemType) {
56
            $this->getItemTypeRule()->applyRule($parameterName, $itemType);
57
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) {
58 6
            $rule->applyRule($parameterName, null, $itemType);
59
        }
60 6
        $this->applyRulesFromModel($this->attribute, $parameterName, $itemType);
61 6
    }
62 6
    /**
63 6
     * This method is called when an attribute has a union meta which means the attribute is of several types.
64 3
     * In this case, the types are currently only of type string (normally) so we add the rules according to each type
65 3
     * @param string $parameterName
66 6
     * @param bool $itemType
67
     * @param string[] $unionTypes
68
     */
69
    protected function applyUnionRules($parameterName, $itemType, array $unionTypes)
70
    {
71
        foreach ($unionTypes as $type) {
72
            $struct = $this->getAttribute()->getGenerator()->getStructByName($type);
73 228
            if ($struct instanceof Struct) {
74
                $this->applyRulesFromModel($struct, $parameterName, $itemType);
75 228
            }
76 192
        }
77 192
    }
78 48
    /**
79 192
     * Generic method to apply rules from current model
80 68
     * @param AbstractModel $model
81 3
     * @param string $parameterName
82 114
     * @param bool $itemType
83 228
     */
84
    protected function applyRulesFromModel(AbstractModel $model, $parameterName, $itemType = false)
85
    {
86
        foreach ($model->getMeta() as $metaName => $metaValue) {
87
            $rule = $this->getRule($metaName);
88 228
            if ($rule instanceof AbstractRule) {
89
                $rule->applyRule($parameterName, $metaValue, $itemType);
90 228
            } elseif ($metaName === 'union' && is_array($metaValue) && count($metaValue) > 0) {
91 228
                $this->getUnionTypeRule()->applyRule($parameterName, $metaValue, $itemType);
92 228
            }
93 198
        }
94
    }
95 114
    /**
96 228
     * @param string $metaName
97
     * @return AbstractRule|null
98
     */
99
    protected function getRule($metaName)
100
    {
101 114
        if (is_string($metaName)) {
0 ignored issues
show
introduced by
The condition is_string($metaName) is always true.
Loading history...
102
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($metaName));
103 114
            if (class_exists($className)) {
104
                return new $className($this);
105
            }
106
        }
107
        return null;
108 102
    }
109
    /**
110 102
     * @return ArrayRule
111
     */
112
    public function getArrayRule()
113
    {
114
        return $this->getRule('array');
115 108
    }
116
    /**
117 108
     * @return EnumerationRule
118
     */
119
    public function getEnumerationRule()
120
    {
121
        return $this->getRule('enumeration');
122 276
    }
123
    /**
124 276
     * @return ItemTypeRule
125
     */
126
    public function getItemTypeRule()
127
    {
128
        return $this->getRule('itemType');
129
    }
130 780
    /**
131
     * @return UnionRule
0 ignored issues
show
Bug introduced by
The type WsdlToPhp\PackageGenerat...le\Validation\UnionRule 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...
132 780
     */
133 780
    public function getUnionTypeRule()
134
    {
135
        return $this->getRule('union');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRule('union') also could return the type WsdlToPhp\PackageGenerat...Validation\AbstractRule which is incompatible with the documented return type WsdlToPhp\PackageGenerat...le\Validation\UnionRule.
Loading history...
136
    }
137
    /**
138 276
     * @return ListRule
139
     */
140 276
    public function getListRule()
141
    {
142
        return $this->getRule('list');
143
    }
144
    /**
145
     * @return StructAttribute
146 780
     */
147
    public function getAttribute()
148 780
    {
149 780
        return $this->attribute;
150
    }
151
    /**
152
     * @param StructAttribute $attribute
153
     * @return Rules
154 750
     */
155
    public function setAttribute(StructAttribute $attribute)
156 750
    {
157
        $this->attribute = $attribute;
158
        return $this;
159
    }
160
    /**
161
     * @return AbstractModelFile
162 780
     */
163
    public function getFile()
164 780
    {
165 780
        return $this->file;
166
    }
167
    /**
168
     * @param AbstractModelFile $file
169
     * @return Rules
170
     */
171
    public function setFile(AbstractModelFile $file)
172
    {
173
        $this->file = $file;
174
        return $this;
175
    }
176
    /**
177
     * @return PhpMethod
178
     */
179
    public function getMethod()
180
    {
181
        return $this->method;
182
    }
183
    /**
184
     * @param PhpMethod $method
185
     * @return Rules
186
     */
187
    public function setMethod(PhpMethod $method)
188
    {
189
        $this->method = $method;
190
        return $this;
191
    }
192
    /**
193
     * @return MethodContainer
194
     */
195
    public function getMethods()
196
    {
197
        return $this->methods;
198
    }
199
}
200