Conditions | 70 |
Paths | 1820 |
Total Lines | 218 |
Code Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
301 | private function parseConstraint($constraint) |
||
302 | { |
||
303 | // strip off aliasing |
||
304 | if (preg_match('{^([^,\s]++) ++as ++([^,\s]++)$}', $constraint, $match)) { |
||
305 | $constraint = $match[1]; |
||
306 | } |
||
307 | |||
308 | // strip @stability flags, and keep it for later use |
||
309 | if (preg_match('{^([^,\s]*?)@(' . self::$stabilitiesRegex . ')$}i', $constraint, $match)) { |
||
310 | $constraint = '' !== $match[1] ? $match[1] : '*'; |
||
311 | if ($match[2] !== 'stable') { |
||
312 | $stabilityModifier = $match[2]; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | // get rid of #refs as those are used by composer only |
||
317 | if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$}i', $constraint, $match)) { |
||
318 | $constraint = $match[1]; |
||
319 | } |
||
320 | |||
321 | if (preg_match('{^(v)?[xX*](\.[xX*])*$}i', $constraint, $match)) { |
||
322 | if (!empty($match[1]) || !empty($match[2])) { |
||
323 | return array(new Constraint('>=', '0.0.0.0-dev')); |
||
324 | } |
||
325 | |||
326 | return array(new MatchAllConstraint()); |
||
327 | } |
||
328 | |||
329 | $versionRegex = 'v?(\d++)(?:\.(\d++))?(?:\.(\d++))?(?:\.(\d++))?(?:' . self::$modifierRegex . '|\.([xX*][.-]?dev))(?:\+[^\s]+)?'; |
||
330 | |||
331 | // Tilde Range |
||
332 | // |
||
333 | // Like wildcard constraints, unsuffixed tilde constraints say that they must be greater than the previous |
||
334 | // version, to ensure that unstable instances of the current version are allowed. However, if a stability |
||
335 | // suffix is added to the constraint, then a >= match on the current version is used instead. |
||
336 | if (preg_match('{^~>?' . $versionRegex . '$}i', $constraint, $matches)) { |
||
337 | if (strpos($constraint, '~>') === 0) { |
||
338 | throw new \UnexpectedValueException( |
||
339 | 'Could not parse version constraint ' . $constraint . ': ' . |
||
340 | 'Invalid operator "~>", you probably meant to use the "~" operator' |
||
341 | ); |
||
342 | } |
||
343 | |||
344 | // Work out which position in the version we are operating at |
||
345 | if (isset($matches[4]) && '' !== $matches[4] && null !== $matches[4]) { |
||
346 | $position = 4; |
||
347 | } elseif (isset($matches[3]) && '' !== $matches[3] && null !== $matches[3]) { |
||
348 | $position = 3; |
||
349 | } elseif (isset($matches[2]) && '' !== $matches[2] && null !== $matches[2]) { |
||
350 | $position = 2; |
||
351 | } else { |
||
352 | $position = 1; |
||
353 | } |
||
354 | |||
355 | // when matching 2.x-dev or 3.0.x-dev we have to shift the second or third number, despite no second/third number matching above |
||
356 | if (!empty($matches[8])) { |
||
357 | $position++; |
||
358 | } |
||
359 | |||
360 | // Calculate the stability suffix |
||
361 | $stabilitySuffix = ''; |
||
362 | if (empty($matches[5]) && empty($matches[7]) && empty($matches[8])) { |
||
363 | $stabilitySuffix .= '-dev'; |
||
364 | } |
||
365 | |||
366 | $lowVersion = $this->normalize(substr($constraint . $stabilitySuffix, 1)); |
||
367 | $lowerBound = new Constraint('>=', $lowVersion); |
||
368 | |||
369 | // For upper bound, we increment the position of one more significance, |
||
370 | // but highPosition = 0 would be illegal |
||
371 | $highPosition = max(1, $position - 1); |
||
372 | $highVersion = $this->manipulateVersionString($matches, $highPosition, 1) . '-dev'; |
||
373 | $upperBound = new Constraint('<', $highVersion); |
||
374 | |||
375 | return array( |
||
376 | $lowerBound, |
||
377 | $upperBound, |
||
378 | ); |
||
379 | } |
||
380 | |||
381 | // Caret Range |
||
382 | // |
||
383 | // Allows changes that do not modify the left-most non-zero digit in the [major, minor, patch] tuple. |
||
384 | // In other words, this allows patch and minor updates for versions 1.0.0 and above, patch updates for |
||
385 | // versions 0.X >=0.1.0, and no updates for versions 0.0.X |
||
386 | if (preg_match('{^\^' . $versionRegex . '($)}i', $constraint, $matches)) { |
||
387 | // Work out which position in the version we are operating at |
||
388 | if ('0' !== $matches[1] || '' === $matches[2] || null === $matches[2]) { |
||
389 | $position = 1; |
||
390 | } elseif ('0' !== $matches[2] || '' === $matches[3] || null === $matches[3]) { |
||
391 | $position = 2; |
||
392 | } else { |
||
393 | $position = 3; |
||
394 | } |
||
395 | |||
396 | // Calculate the stability suffix |
||
397 | $stabilitySuffix = ''; |
||
398 | if (empty($matches[5]) && empty($matches[7]) && empty($matches[8])) { |
||
399 | $stabilitySuffix .= '-dev'; |
||
400 | } |
||
401 | |||
402 | $lowVersion = $this->normalize(substr($constraint . $stabilitySuffix, 1)); |
||
403 | $lowerBound = new Constraint('>=', $lowVersion); |
||
404 | |||
405 | // For upper bound, we increment the position of one more significance, |
||
406 | // but highPosition = 0 would be illegal |
||
407 | $highVersion = $this->manipulateVersionString($matches, $position, 1) . '-dev'; |
||
408 | $upperBound = new Constraint('<', $highVersion); |
||
409 | |||
410 | return array( |
||
411 | $lowerBound, |
||
412 | $upperBound, |
||
413 | ); |
||
414 | } |
||
415 | |||
416 | // X Range |
||
417 | // |
||
418 | // Any of X, x, or * may be used to "stand in" for one of the numeric values in the [major, minor, patch] tuple. |
||
419 | // A partial version range is treated as an X-Range, so the special character is in fact optional. |
||
420 | if (preg_match('{^v?(\d++)(?:\.(\d++))?(?:\.(\d++))?(?:\.[xX*])++$}', $constraint, $matches)) { |
||
421 | if (isset($matches[3]) && '' !== $matches[3] && null !== $matches[3]) { |
||
422 | $position = 3; |
||
423 | } elseif (isset($matches[2]) && '' !== $matches[2] && null !== $matches[2]) { |
||
424 | $position = 2; |
||
425 | } else { |
||
426 | $position = 1; |
||
427 | } |
||
428 | |||
429 | $lowVersion = $this->manipulateVersionString($matches, $position) . '-dev'; |
||
430 | $highVersion = $this->manipulateVersionString($matches, $position, 1) . '-dev'; |
||
431 | |||
432 | if ($lowVersion === '0.0.0.0-dev') { |
||
433 | return array(new Constraint('<', $highVersion)); |
||
434 | } |
||
435 | |||
436 | return array( |
||
437 | new Constraint('>=', $lowVersion), |
||
438 | new Constraint('<', $highVersion), |
||
439 | ); |
||
440 | } |
||
441 | |||
442 | // Hyphen Range |
||
443 | // |
||
444 | // Specifies an inclusive set. If a partial version is provided as the first version in the inclusive range, |
||
445 | // then the missing pieces are replaced with zeroes. If a partial version is provided as the second version in |
||
446 | // the inclusive range, then all versions that start with the supplied parts of the tuple are accepted, but |
||
447 | // nothing that would be greater than the provided tuple parts. |
||
448 | if (preg_match('{^(?P<from>' . $versionRegex . ') +- +(?P<to>' . $versionRegex . ')($)}i', $constraint, $matches)) { |
||
449 | // Calculate the stability suffix |
||
450 | $lowStabilitySuffix = ''; |
||
451 | if (empty($matches[6]) && empty($matches[8]) && empty($matches[9])) { |
||
452 | $lowStabilitySuffix = '-dev'; |
||
453 | } |
||
454 | |||
455 | $lowVersion = $this->normalize($matches['from']); |
||
456 | $lowerBound = new Constraint('>=', $lowVersion . $lowStabilitySuffix); |
||
457 | |||
458 | $empty = function ($x) { |
||
459 | return ($x === 0 || $x === '0') ? false : empty($x); |
||
460 | }; |
||
461 | |||
462 | if ((!$empty($matches[12]) && !$empty($matches[13])) || !empty($matches[15]) || !empty($matches[17]) || !empty($matches[18])) { |
||
463 | $highVersion = $this->normalize($matches['to']); |
||
464 | $upperBound = new Constraint('<=', $highVersion); |
||
465 | } else { |
||
466 | $highMatch = array('', $matches[11], $matches[12], $matches[13], $matches[14]); |
||
467 | |||
468 | // validate to version |
||
469 | $this->normalize($matches['to']); |
||
470 | |||
471 | $highVersion = $this->manipulateVersionString($highMatch, $empty($matches[12]) ? 1 : 2, 1) . '-dev'; |
||
472 | $upperBound = new Constraint('<', $highVersion); |
||
473 | } |
||
474 | |||
475 | return array( |
||
476 | $lowerBound, |
||
477 | $upperBound, |
||
478 | ); |
||
479 | } |
||
480 | |||
481 | // Basic Comparators |
||
482 | if (preg_match('{^(<>|!=|>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) { |
||
483 | try { |
||
484 | try { |
||
485 | $version = $this->normalize($matches[2]); |
||
486 | } catch (\UnexpectedValueException $e) { |
||
487 | // recover from an invalid constraint like foobar-dev which should be dev-foobar |
||
488 | // except if the constraint uses a known operator, in which case it must be a parse error |
||
489 | if (substr($matches[2], -4) === '-dev' && preg_match('{^[0-9a-zA-Z-./]+$}', $matches[2])) { |
||
490 | $version = $this->normalize('dev-'.substr($matches[2], 0, -4)); |
||
491 | } else { |
||
492 | throw $e; |
||
493 | } |
||
494 | } |
||
495 | |||
496 | $op = $matches[1] ?: '='; |
||
497 | |||
498 | if ($op !== '==' && $op !== '=' && !empty($stabilityModifier) && self::parseStability($version) === 'stable') { |
||
499 | $version .= '-' . $stabilityModifier; |
||
500 | } elseif ('<' === $op || '>=' === $op) { |
||
501 | if (!preg_match('/-' . self::$modifierRegex . '$/', strtolower($matches[2]))) { |
||
502 | if (strpos($matches[2], 'dev-') !== 0) { |
||
503 | $version .= '-dev'; |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | |||
508 | return array(new Constraint($matches[1] ?: '=', $version)); |
||
509 | } catch (\Exception $e) { |
||
510 | } |
||
511 | } |
||
512 | |||
513 | $message = 'Could not parse version constraint ' . $constraint; |
||
514 | if (isset($e)) { |
||
515 | $message .= ': ' . $e->getMessage(); |
||
516 | } |
||
517 | |||
518 | throw new \UnexpectedValueException($message); |
||
519 | } |
||
584 |