Conditions | 57 |
Paths | > 20000 |
Total Lines | 189 |
Code Lines | 128 |
Lines | 28 |
Ratio | 14.81 % |
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 |
||
104 | protected function validate() |
||
105 | { |
||
106 | if (!Config::get('core.use_translation')) { |
||
107 | throw new ConfigurationException('The datetime validator can only be used with use_translation on'); |
||
108 | } |
||
109 | $tm = $this->getContext()->getTranslationManager(); |
||
110 | $cal = null; |
||
111 | |||
112 | $check = $this->getParameter('check', true); |
||
113 | $locale = $this->hasParameter('locale') ? $tm->getLocale($this->getParameter('locale')) : $tm->getCurrentLocale(); |
||
114 | |||
115 | if ($this->hasMultipleArguments() && !$this->getParameter('arguments_format')) { |
||
116 | $cal = $tm->createCalendar(); |
||
117 | $cal->clear(); |
||
118 | $cal->setLenient(!$check); |
||
119 | foreach ($this->getArguments() as $calField => $field) { |
||
120 | $param = $this->getData($field); |
||
121 | if (defined($calField)) { |
||
122 | $calField = constant($calField); |
||
123 | } elseif (!is_numeric($calField)) { |
||
124 | throw new ValidatorException('Unknown argument name "' . $calField . '" for argument "' . $field . '" supplied. This needs to be one of the constants defined in AgaviDateDefinitions.'); |
||
125 | } |
||
126 | if (!is_scalar($param)) { |
||
127 | // everything which is non scalar is ignored, since it couldn't be handled anyways |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | if ($calField == DateDefinitions::MONTH) { |
||
132 | $param -= 1; |
||
133 | } |
||
134 | |||
135 | $cal->set($calField, (float) $param); |
||
136 | } |
||
137 | |||
138 | try { |
||
139 | $cal->getTime(); |
||
140 | } catch (AgaviException $e) { |
||
141 | $this->throwError('check'); |
||
142 | return false; |
||
143 | } |
||
144 | } else { |
||
145 | if ($argFormat = $this->getParameter('arguments_format')) { |
||
146 | $values = array(); |
||
147 | foreach ($this->getArguments() as $field) { |
||
148 | $values[] = $this->getData($field); |
||
149 | } |
||
150 | $param = vsprintf($argFormat, $values); |
||
151 | } else { |
||
152 | $param = $this->getData($this->getArgument()); |
||
153 | if (!is_scalar($param)) { |
||
154 | $this->throwError(); |
||
155 | return false; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $matchedFormat = false; |
||
160 | foreach ((array)$this->getParameter('formats', array()) as $key => $item) { |
||
161 | if (!is_array($item)) { |
||
162 | $item = array((is_int($key) ? 'format' : $key) => $item); |
||
163 | } |
||
164 | |||
165 | $itemLocale = empty($item['locale']) ? $locale : $tm->getLocale($item['locale']); |
||
166 | $type = empty($item['type']) ? 'format' : $item['type']; |
||
167 | |||
168 | if ($type == 'format') { |
||
169 | $formatString = $item['format']; |
||
170 | View Code Duplication | } elseif ($type == 'time' || $type == 'date' || $type == 'datetime') { |
|
|
|||
171 | $format = isset($item['format']) ? $item['format'] : null; |
||
172 | $formatString = DateFormatter::resolveFormat($format, $itemLocale, $type); |
||
173 | } elseif ($type == 'translation_domain') { |
||
174 | $td = $item['translation_domain']; |
||
175 | $formatString = $tm->_($item['format'], $td, $itemLocale); |
||
176 | } elseif ($type == 'unix') { |
||
177 | $matchedFormat = ($param === (string)(int)$param); |
||
178 | $cal = $tm->createCalendar($itemLocale); |
||
179 | $cal->setUnixTimestamp($param); |
||
180 | View Code Duplication | if ($matchedFormat) { |
|
181 | try { |
||
182 | if ($cal->getUnixTimestamp() !== (int)$param) { |
||
183 | $this->throwError('check'); |
||
184 | return false; |
||
185 | } |
||
186 | } catch (AgaviException $e) { |
||
187 | $matchedFormat = false; |
||
188 | } |
||
189 | } |
||
190 | } elseif ($type == 'unix_milliseconds') { |
||
191 | $matchedFormat = is_numeric($param); |
||
192 | $cal = $tm->createCalendar($itemLocale); |
||
193 | $cal->setTime($param); |
||
194 | View Code Duplication | if ($matchedFormat) { |
|
195 | try { |
||
196 | if ($cal->getTime() !== (float)$param) { |
||
197 | $this->throwError('check'); |
||
198 | return false; |
||
199 | } |
||
200 | } catch (AgaviException $e) { |
||
201 | $matchedFormat = false; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if (!$cal) { |
||
207 | try { |
||
208 | $format = new DateFormat($formatString); |
||
209 | $cal = $format->parse($param, $itemLocale, $check); |
||
210 | |||
211 | // no exception got thrown so the parsing was successful |
||
212 | $matchedFormat = true; |
||
213 | break; |
||
214 | } catch (AgaviException $e) { |
||
215 | // nop |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | if (!$matchedFormat) { |
||
221 | $this->throwError('format'); |
||
222 | return false; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $cal->setLenient(true); |
||
227 | $value = $cal; |
||
228 | |||
229 | if ($cast = $this->getParameter('cast_to')) { |
||
230 | // an array means the user wants it custom formatted |
||
231 | if (is_array($cast)) { |
||
232 | $type = empty($cast['type']) ? 'format' : $cast['type']; |
||
233 | if ($type == 'format') { |
||
234 | $formatString = $cast['format']; |
||
235 | View Code Duplication | } elseif ($type == 'time' || $type == 'date' || $type == 'datetime') { |
|
236 | $format = isset($cast['format']) ? $cast['format'] : null; |
||
237 | $formatString = DateFormatter::resolveFormat($format, $locale, $type); |
||
238 | } |
||
239 | |||
240 | $format = new DateFormat($formatString); |
||
241 | $value = $format->format($cal, $cal->getType(), $locale); |
||
242 | } else { |
||
243 | $cast = strtolower($cast); |
||
244 | if ($cast == 'unix') { |
||
245 | $value = $cal->getUnixTimestamp(); |
||
246 | } elseif ($cast == 'string') { |
||
247 | $value = $tm->_d($cal); |
||
248 | } elseif ($cast == 'datetime') { |
||
249 | $value = $cal->getNativeDateTime(); |
||
250 | } else { |
||
251 | $value = $cal; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | $defaultParseFormat = new DateFormat('yyyy-MM-dd HH:mm:ss.S'); |
||
257 | |||
258 | if ($this->hasParameter('min')) { |
||
259 | $min = $this->getMinOrMaxValue('min', $defaultParseFormat, $locale); |
||
260 | |||
261 | $isAfterEqual = $cal->after($min) || $cal->equals($min); |
||
262 | if (!$isAfterEqual) { |
||
263 | $this->throwError('min'); |
||
264 | return false; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | if ($this->hasParameter('max')) { |
||
269 | $max = $this->getMinOrMaxValue('max', $defaultParseFormat, $locale); |
||
270 | |||
271 | $isBefore = $cal->before($max); |
||
272 | if (!$isBefore) { |
||
273 | $this->throwError('max'); |
||
274 | return false; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | if ($this->hasParameter('export')) { |
||
279 | $export = $this->getParameter('export'); |
||
280 | if (is_string($export)) { |
||
281 | $this->export($value); |
||
282 | } elseif (is_array($export)) { |
||
283 | foreach ($export as $calField => $field) { |
||
284 | if (defined($calField)) { |
||
285 | $this->export($cal->get(constant($calField)), $field); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | |||
291 | return true; |
||
292 | } |
||
293 | |||
344 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.