Passed
Push — 2.x ( bef6c0...34f135 )
by Mikaël
03:54
created

Rules::getEnumerationRule()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Generator\Generator;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
10
11
class Rules
12
{
13
14
    /**
15
     * @var StructAttribute
16
     */
17
    protected $attribute;
18
19
    /**
20
     * @var AbstractModelFile
21
     */
22
    protected $file;
23
24
    /**
25
     * @var PhpMethod
26
     */
27
    protected $method;
28
29
    /**
30 780
     * @var MethodContainer
31
     */
32 780
    protected $methods;
33 780
    /**
34
     * @param AbstractModelFile $file
35
     * @param PhpMethod $method
36
     * @param StructAttribute $attribute
37
     */
38 228
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods)
39
    {
40 228
        $this->file = $file;
41 228
        $this->method = $method;
42 114
        $this->attribute = $attribute;
43 228
        $this->methods = $methods;
44 102
    }
45 225
46 108
    /**
47 204
     * @param string $parameterName
48 150
     * @param bool $itemType
49 75
     */
50 228
    public function applyRules($parameterName, $itemType = false)
51
    {
52
        $this->applyRulesFromModel($this->attribute, $parameterName, $itemType);
0 ignored issues
show
Bug introduced by
The method applyRulesFromModel() does not exist on WsdlToPhp\PackageGenerator\File\Validation\Rules. Did you maybe mean applyRules()? ( Ignorable by Annotation )

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

52
        $this->/** @scrutinizer ignore-call */ 
53
               applyRulesFromModel($this->attribute, $parameterName, $itemType);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        if ($this->attribute->isArray() && !$itemType) {
54
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
55
        } elseif ($this->attribute->isList() && !$itemType) {
56
            $this->getListRule()->applyRule($parameterName, null, $itemType);
57
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) {
58 6
            $this->getEnumerationRule()->applyRule($parameterName, null);
59
        } elseif ($itemType) {
60 6
            $this->getItemTypeRule()->applyRule($parameterName, $itemType);
61 6
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) {
62 6
            $rule->applyRule($parameterName, null, $itemType);
63 6
        }
64 3
        $this->applyRulesFromAttribute($parameterName, $itemType);
65 3
    }
66 6
67
    /**
68
     * Generic method to apply rules from current model
69
     * @param string $parameterName
70
     * @param bool $itemType
71
     */
72
    protected function applyRulesFromAttribute($parameterName, $itemType = false)
73 228
    {
74
        foreach ($this->attribute->getMeta() as $metaName => $metaValue) {
75 228
            $rule = $this->getRule($metaName);
76 192
            if ($rule instanceof AbstractRule) {
77 192
                $rule->applyRule($parameterName, $metaValue, $itemType);
78 48
            }
79 192
        }
80 68
    }
81 3
82 114
    /**
83 228
     * @param string $name
84
     * @return AbstractRule|null
85
     */
86
    protected function getRule($name)
87
    {
88 228
        if (is_string($name)) {
0 ignored issues
show
introduced by
The condition is_string($name) is always true.
Loading history...
89
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name));
90 228
            if (class_exists($className)) {
91 228
                return new $className($this);
92 228
            }
93 198
        }
94
        return null;
95 114
    }
96 228
97
    /**
98
     * @return ArrayRule
99
     */
100
    public function getArrayRule()
101 114
    {
102
        return $this->getRule('array');
103 114
    }
104
105
    /**
106
     * @return EnumerationRule
107
     */
108 102
    public function getEnumerationRule()
109
    {
110 102
        return $this->getRule('enumeration');
111
    }
112
113
    /**
114
     * @return ItemTypeRule
115 108
     */
116
    public function getItemTypeRule()
117 108
    {
118
        return $this->getRule('itemType');
119
    }
120
121
    /**
122 276
     * @return ListRule
123
     */
124 276
    public function getListRule()
125
    {
126
        return $this->getRule('list');
127
    }
128
    /**
129
     * @return StructAttribute
130 780
     */
131
    public function getAttribute()
132 780
    {
133 780
        return $this->attribute;
134
    }
135
136
    /**
137
     * @param StructAttribute $attribute
138 276
     * @return Rules
139
     */
140 276
    public function setAttribute(StructAttribute $attribute)
141
    {
142
        $this->attribute = $attribute;
143
        return $this;
144
    }
145
146 780
    /**
147
     * @return AbstractModelFile
148 780
     */
149 780
    public function getFile()
150
    {
151
        return $this->file;
152
    }
153
154 750
    /**
155
     * @return PhpMethod
156 750
     */
157
    public function getMethod()
158
    {
159
        return $this->method;
160
    }
161
162 780
    /**
163
     * @param PhpMethod $method
164 780
     * @return Rules
165 780
     */
166
    public function setMethod(PhpMethod $method)
167
    {
168
        $this->method = $method;
169
        return $this;
170
    }
171
    /**
172
     * @return MethodContainer
173
     */
174
    public function getMethods()
175
    {
176
        return $this->methods;
177
    }
178
    /**
179
     * @return Generator
180
     */
181
    public function getGenerator()
182
    {
183
        return $this->file->getGenerator();
184
    }
185
}
186