Completed
Push — feature/issue-84 ( 44128e...1e0a90 )
by Mikaël
34:57
created

Rules   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
lcom 1
cbo 5
dl 0
loc 157
ccs 48
cts 48
cp 1
rs 10
c 1
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B applyRules() 0 13 6
A applyUnionRules() 0 9 3
B applyRulesFromModel() 0 11 5
A getRule() 0 10 3
A getArrayRule() 0 4 1
A getEnumerationRule() 0 4 1
A getItemTypeRule() 0 4 1
A getAttribute() 0 4 1
A setAttribute() 0 5 1
A getFile() 0 4 1
A setFile() 0 5 1
A getMethod() 0 4 1
A setMethod() 0 5 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
8
use WsdlToPhp\PackageGenerator\Model\Struct;
9
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
10
11
class Rules
12
{
13
    /**
14
     * @var StructAttribute
15
     */
16
    private $attribute;
17
    /**
18
     * @var AbstractModelFile
19
     */
20
    private $file;
21
    /**
22
     * @var PhpMethod
23
     */
24
    private $method;
25
    /**
26
     * @param AbstractModelFile $file
27
     * @param PhpMethod $method
28 104
     * @param StructAttribute $attribute
29
     */
30 104
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute)
31 104
    {
32
        $this->setFile($file)->setMethod($method)->setAttribute($attribute);
33
    }
34
    /**
35
     * @param string $parameterName
36 104
     * @param bool $itemType
37
     */
38 104
    public function applyRules($parameterName, $itemType = false)
39 104
    {
40 104
        $this->applyRulesFromModel($this->attribute, $parameterName, $itemType);
0 ignored issues
show
Documentation introduced by
$itemType is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41 16
        if ($this->getAttribute()->isArray() && !$itemType) {
42 16
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
43 104
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->getAttribute())) {
44 104
            $this->getEnumerationRule()->applyRule($parameterName, null, $itemType);
45 64
        } elseif ($itemType) {
46 104
            $this->getItemTypeRule()->applyRule($parameterName, null, $itemType);
47 52
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute()))) instanceof AbstractRule) {
48 104
            $rule->applyRule($parameterName, null, $itemType);
49 60
        }
50 100
    }
51 76
    /**
52 76
     * This method is called when an attribute has a union meta which means the attribute is of several types.
53 104
     * In this case, the types are currently only of type string (normally) so we add the rules according to each type
54
     * @param string $parameterName
55
     * @param string $itemType
56
     * @param array $unionTypes
57
     */
58 104
    protected function applyUnionRules($parameterName, $itemType, array $unionTypes)
59
    {
60 104
        foreach ($unionTypes as $type) {
61 104
            $struct = $this->getAttribute()->getGenerator()->getStruct($type);
62 104
            if ($struct instanceof Struct) {
63 104
                $this->applyRulesFromModel($struct, $parameterName, $itemType);
64
            }
65 104
        }
66 104
    }
67
    /**
68
     * Generic method to apply rules from current model
69
     * @param AbstractModel $model
70
     * @param string $parameterName
71 64
     * @param string $itemType
72
     */
73 64
    protected function applyRulesFromModel(AbstractModel $model, $parameterName, $itemType = false)
74
    {
75
        foreach ($model->getMeta() as $metaName => $metaValue) {
76
            $rule = $this->getRule($metaName);
77
            if ($rule instanceof AbstractRule) {
78 52
                $rule->applyRule($parameterName, $metaValue, $itemType);
0 ignored issues
show
Bug introduced by
It seems like $itemType defined by parameter $itemType on line 73 can also be of type string; however, WsdlToPhp\PackageGenerat...stractRule::applyRule() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
            } elseif ($metaName === 'union' && count($metaValue) > 0) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $metaName (integer) and 'union' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
80 52
                $this->applyUnionRules($parameterName, $itemType, $metaValue);
0 ignored issues
show
Bug introduced by
It seems like $itemType defined by parameter $itemType on line 73 can also be of type false; however, WsdlToPhp\PackageGenerat...ules::applyUnionRules() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation introduced by
$metaValue is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
            }
82
        }
83
    }
84
    /**
85 60
     * @param string $metaName
86
     * @return AbstractRule
87 60
     */
88
    protected function getRule($metaName)
89
    {
90
        if (is_string($metaName)) {
91
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($metaName));
92 104
            if (class_exists($className)) {
93
                return new $className($this);
94 104
            }
95
        }
96
        return null;
97
    }
98
    /**
99
     * @return ArrayRule
100 104
     */
101
    public function getArrayRule()
102 104
    {
103 104
        return $this->getRule('array');
104
    }
105
    /**
106
     * @return EnumerationRule
107
     */
108 104
    public function getEnumerationRule()
109
    {
110 104
        return $this->getRule('enumeration');
111
    }
112
    /**
113
     * @return ItemTypeRule
114
     */
115
    public function getItemTypeRule()
116 104
    {
117
        return $this->getRule('itemType');
118 104
    }
119 104
    /**
120
     * @return StructAttribute
121
     */
122
    public function getAttribute()
123
    {
124 104
        return $this->attribute;
125
    }
126 104
    /**
127
     * @param StructAttribute $attribute
128
     * @return Rules
129
     */
130
    public function setAttribute(StructAttribute $attribute)
131
    {
132 104
        $this->attribute = $attribute;
133
        return $this;
134 104
    }
135 104
    /**
136
     * @return AbstractModelFile
137
     */
138
    public function getFile()
139
    {
140
        return $this->file;
141
    }
142
    /**
143
     * @param AbstractModelFile $file
144
     * @return Rules
145
     */
146
    public function setFile(AbstractModelFile $file)
147
    {
148
        $this->file = $file;
149
        return $this;
150
    }
151
    /**
152
     * @return PhpMethod
153
     */
154
    public function getMethod()
155
    {
156
        return $this->method;
157
    }
158
    /**
159
     * @param PhpMethod $method
160
     * @return Rules
161
     */
162
    public function setMethod(PhpMethod $method)
163
    {
164
        $this->method = $method;
165
        return $this;
166
    }
167
}
168