/** * Resolves an alias into a Version. * * IMPROVE: this method has an NPath complexity of 400. The configured NPath complexity threshold is 200. * * @SuppressWarnings(PHPMD.NPathComplexity) * * @param string $alias * @param Collection $collection * * @return \Baleen\Migrations\Version\VersionInterface|null|string */ protected function doResolve($alias, Collection $collection) { if (!$collection instanceof Sortable) { return null; } // parse alias $matches = []; if (!preg_match(self::PATTERN, $alias, $matches)) { return null; } list(, $newAlias, $operator) = $matches; // resolve the new alias (this will allow to resolve e.g. HEAD-1) $absoluteVersion = $collection->get($newAlias); if (null === $absoluteVersion) { return null; } // calculate the offset $count = !isset($matches[3]) ? strlen($operator) : (int) $matches[3]; if (strlen($operator) > 1) { $operator = substr($operator, 0, 1); } $multiplier = $operator === '+' ? 1 : -1; $offset = $count * $multiplier; // find version by absolute position + offset $absolutePos = $collection->indexOf($absoluteVersion); return $collection->get($absolutePos + $offset); }