Conditions | 11 |
Paths | 27 |
Total Lines | 35 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
113 | public function validate($json) |
||
114 | { |
||
115 | $jsonAsArray = json_decode($json, true); |
||
116 | |||
117 | foreach ($jsonAsArray as $name => $property) { |
||
118 | if (!isset($this->properties()[$name])) { |
||
119 | throw new Exceptions\NotAllowedPropertyException(); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | foreach ($this->required as $requiredProperty) { |
||
124 | if (!isset($jsonAsArray[$requiredProperty])) { |
||
125 | throw new Exceptions\MissingPropertyException(); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | foreach ($jsonAsArray as $name => $property) { |
||
130 | $prop = $this->properties()[$name]; |
||
131 | if (gettype($jsonAsArray[$name]) != $prop['type']) { |
||
132 | if ($prop['type'] == Schema::PRIMITIVE_INTEGER && is_numeric($jsonAsArray[$name])) { |
||
133 | continue; |
||
134 | } |
||
135 | |||
136 | if ($prop['type'] == Schema::PRIMITIVE_ARRAY) { |
||
137 | if (!isset($prop['items'])) { |
||
138 | throw new Exceptions\UndefinedArrayTypeException(); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | throw new Exceptions\NotAllowedValueException(); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return json_decode($json); |
||
147 | } |
||
148 | |||
159 |