Conditions | 10 |
Paths | 4 |
Total Lines | 56 |
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 | default: |
||
123 | if ($type === $this->allowedMetaKeys[$key]) { |
||
124 | $metaObj->{$key} = $value; |
||
125 | } else { |
||
126 | $errors[] = array( |
||
127 | 'key' => $key, |
||
128 | 'message' => 'Expected ' . $this->allowedMetaKeys[$key] . '; received ' . $type |
||
129 | ); |
||
130 | } |
||
131 | break; |
||
132 | } |
||
133 | } else { |
||
134 | $errors[] = array( |
||
135 | 'key' => $key, |
||
136 | 'message' => 'Not a valid MetaData key' |
||
137 | ); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | if (!empty($errors)) { |
||
143 | throw new \ErrorException('Invalid MetaData: ' . print_r($errors, true)); |
||
144 | } |
||
145 | |||
146 | $this->metaData = $metaObj; |
||
147 | } |
||
206 |