Conditions | 13 |
Paths | 8 |
Total Lines | 71 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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; |
||
101 | public function setMetaData($metaData) |
||
102 | { |
||
103 | $metaObj = $this->getMetaData(); |
||
104 | if (is_null($metaObj)) { |
||
105 | $metaObj = new \stdClass(); |
||
106 | } |
||
107 | |||
108 | $errors = array(); |
||
109 | if (is_array($metaData)) { |
||
110 | foreach ($metaData as $key => $value) { |
||
111 | if (isset($this->allowedMetaKeys[$key])) { |
||
112 | $type = gettype($value); |
||
113 | switch ($this->allowedMetaKeys[$key]) { |
||
114 | case 'uri': |
||
115 | if (substr($value, 0, 4) === 'http') { |
||
116 | $metaObj->{$key} = $value; |
||
117 | } else { |
||
118 | $errors[] = array( |
||
119 | 'key' => $key, |
||
120 | 'message' => 'Not a valid URI' |
||
121 | ); |
||
122 | } |
||
123 | break; |
||
124 | |||
125 | case 'datetime': |
||
126 | if ($value instanceof \DateTime) { |
||
127 | $metaObj->{$key} = $value->format('c'); |
||
128 | } else { |
||
129 | $errors[] = array( |
||
130 | 'key' => $key, |
||
131 | 'message' => 'Not a valid DateTime object' |
||
132 | ); |
||
133 | } |
||
134 | break; |
||
135 | |||
136 | case 'array': |
||
137 | if (is_array($value)) { |
||
138 | $metaObj->{$key} = $value; |
||
139 | } else { |
||
140 | $errors[] = array( |
||
141 | 'key' => $key, |
||
142 | 'message' => 'Not a valid Array' |
||
143 | ); |
||
144 | } |
||
145 | break; |
||
146 | |||
147 | default: |
||
148 | if ($type === $this->allowedMetaKeys[$key]) { |
||
149 | $metaObj->{$key} = $value; |
||
150 | } else { |
||
151 | $errors[] = array( |
||
152 | 'key' => $key, |
||
153 | 'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
||
154 | ); |
||
155 | } |
||
156 | break; |
||
157 | } |
||
158 | } else { |
||
159 | $errors[] = array( |
||
160 | 'key' => $key, |
||
161 | 'message' => 'Not a valid MetaData key' |
||
162 | ); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if (!empty($errors)) { |
||
168 | throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
||
|
|||
169 | } |
||
170 | |||
171 | $this->metaData = $metaObj; |
||
172 | } |
||
271 |