| Conditions | 11 |
| Paths | 27 |
| Total Lines | 35 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 108 | public function validate($json) |
||
| 109 | { |
||
| 110 | $jsonAsArray = json_decode($json, true); |
||
| 111 | |||
| 112 | foreach ($jsonAsArray as $name => $property) { |
||
| 113 | if (!isset($this->properties()[$name])) { |
||
| 114 | throw new Exceptions\NotAllowedPropertyException(); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | foreach ($this->required as $requiredProperty) { |
||
| 119 | if (!isset($jsonAsArray[$requiredProperty])) { |
||
| 120 | throw new Exceptions\MissingPropertyException(); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | foreach ($jsonAsArray as $name => $property) { |
||
| 125 | $prop = $this->properties()[$name]; |
||
| 126 | if (gettype($jsonAsArray[$name]) != $prop['type']) { |
||
| 127 | if ($prop['type'] == Schema::PRIMITIVE_INTEGER && is_numeric($jsonAsArray[$name])) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($prop['type'] == Schema::PRIMITIVE_ARRAY) { |
||
| 132 | if (!isset($prop['items'])) { |
||
| 133 | throw new Exceptions\UndefinedArrayItemsTypeException(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | throw new Exceptions\NotAllowedValueException(); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return json_decode($json); |
||
| 142 | } |
||
| 143 | |||
| 154 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: