| Conditions | 12 |
| Paths | 4 |
| Total Lines | 67 |
| Code Lines | 46 |
| 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 namespace FlatPlan; |
||
| 91 | public function setMetaData($metaData) |
||
| 92 | { |
||
| 93 | $metaObj = new \stdClass(); |
||
| 94 | $errors = array(); |
||
| 95 | if (is_array($metaData)) { |
||
| 96 | foreach ($metaData as $key => $value) { |
||
| 97 | if (isset($this->allowedMetaKeys[$key])) { |
||
| 98 | $type = gettype($value); |
||
| 99 | switch ($this->allowedMetaKeys[$key]) { |
||
| 100 | case 'uri': |
||
| 101 | if (filter_var($value, FILTER_VALIDATE_URL)) { |
||
| 102 | $metaObj->{$key} = $value; |
||
| 103 | } else { |
||
| 104 | $errors[] = array( |
||
| 105 | 'key' => $key, |
||
| 106 | 'message' => 'Not a valid URI' |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | break; |
||
| 110 | |||
| 111 | case 'datetime': |
||
| 112 | if ($value instanceof \DateTime) { |
||
| 113 | $metaObj->{$key} = $value; |
||
| 114 | } else { |
||
| 115 | $errors[] = array( |
||
| 116 | 'key' => $key, |
||
| 117 | 'message' => 'Not a valid DateTime object' |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'array': |
||
| 123 | if (is_array($value)) { |
||
| 124 | $metaObj->{$key} = $value; |
||
| 125 | } else { |
||
| 126 | $errors[] = array( |
||
| 127 | 'key' => $key, |
||
| 128 | 'message' => 'Not a valid Array' |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | break; |
||
| 132 | |||
| 133 | default: |
||
| 134 | if ($type === $this->allowedMetaKeys[$key]) { |
||
| 135 | $metaObj->{$key} = $value; |
||
| 136 | } else { |
||
| 137 | $errors[] = array( |
||
| 138 | 'key' => $key, |
||
| 139 | 'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | $errors[] = array( |
||
| 146 | 'key' => $key, |
||
| 147 | 'message' => 'Not a valid MetaData key' |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!empty($errors)) { |
||
| 154 | throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->metaData = $metaObj; |
||
| 158 | } |
||
| 215 |