@{inheritdoc}

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 DeltaInterface|null
514690
protected function doResolve($alias, Collection $collection)
    {
        // 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 getPosition + offset
        $absolutePos = $collection->getPosition($absoluteVersion);
        return $collection->getByPosition($absolutePos + $offset);
    }
1003271
private function getIds(
        $collection = null,
        $systemCollections = true,
        $types = null,
        $search = null,
        $orderBy = null,
        $orderSort = null,
        $limit = null,
        $offset = null
    ) {
        $subQuery = $this->getIdsQuery(
            $collection,
            $systemCollections,
            $types,
            $search,
            $orderBy,
            $orderSort,
            $limit,
            $offset
        );

        return $subQuery->getScalarResult();
    }
1002142
public function resolveAlias(string $alias)
    {
        if (isset($this->aliases[$alias]))
            return $this->aliases[$alias];

        if (empty($this->parent_scope))
            return null;

        return $this->parent_scope->resolveAlias($alias);
    }
374028
public function initial($collection=null, $n=null) {
    list($collection, $n) = self::_wrapArgs(func_get_args(), 2);
    
    $collection = (array) self::_collection($collection);
    
    if(is_null($n)) $n = 1;
    $first_index = count($collection) - $n;
    $__ = new self;
    return self::_wrap($__->first($collection, $first_index));
  }
787428
public function run(DeltaInterface $target, OptionsInterface $options)
    {
        $current = 1;
        $collection = $this->getCollection();
        $context = CollectionContext::createWithProgress(max($collection->count(), 1), $current);
        $migrationRunner = $this->migrationRunner->withContext($context);

        $this->getPublisher()->publish(new CollectionBeforeEvent($target, $options, $collection));

        $modified = new Collection();
        $comparator = $collection->getComparator();

        // IMPROVE: add tests to see if rewind is necessary
        $collection->first(); // rewind
        foreach ($collection as $version) {
            $context->getProgress()->update($current);
            $result = $migrationRunner->run($version, $options);
            if ($result) {
                $modified->add($version);
            }
            if ($comparator->compare($version, $target) >= 0) {
                break;
            }
            $current += 1;
        }

        $event = new CollectionAfterEvent($target, $options, $modified);
        $this->getPublisher()->publish($event);

        return $event;
    }
1083077
public function ddl(Collection $collection)
    {
        if (!empty($this->options['autoddl'])) {
            $sql = $this->dialect->grammarDDL($collection, $this->options['autoddl']);

            $this->execute($sql);
        }
    }
580281
public function createRepositoryTransformer(string $alias) : DataTransformerInterface
    {
        if (!$this->collection->has($alias)) {
            throw new MissingFormDataTransformerException($alias);
        }

        $serviceId = $this->collection->get($alias);

        return $this->get($serviceId);
    }
572
public function count($alias = null)
    {
        if ($this->controller && $this->controller->hasMethod('count')) {
            return $this->controller->count($this, $alias);
        }
        throw $this->exception('The controller doesn\'t support count', 'NotImplemented')
            ->addMoreInfo('controller', $this->controller ? $this->controller->short_name : 'none');
    }
645688
public function getTableByAlias($alias)
    {
        return $this->hasTableAlias($alias) ? $this->getExpression($this->getHash($alias)) : null;
    }
617094
public function intersect($collection)
    {
        $intersection = new DefaultCollection();
        /** @var ModelInterface $localModel */
        foreach ($this as $localModel) {
            /** @var ModelInterface $otherModel */
            foreach ($collection as $otherModel) {
                if (($localModel->getProviderName() === $otherModel->getProviderName())
                    && ($localModel->getId() === $otherModel->getId())
                ) {
                    $intersection->push($localModel);
                }
            }
        }

        return $intersection;
    }
236320
public function findByAlias(string $alias): ?Tag
    {
        /** @var TagModel $adapter */
        $adapter = $this->framework->getAdapter(TagModel::class);

        $criteria = $this->getCriteria();
        $criteria['alias'] = $alias;

        if (null === ($model = $adapter->findOneByCriteria($criteria))) {
            return null;
        }

        return ModelCollection::createTagFromModel($model);
    }
645949
public function newCollectionUpdateStruct(?Collection $collection = null): CollectionUpdateStruct
    {
        $collectionUpdateStruct = new CollectionUpdateStruct();

        if ($collection !== null) {
            $collectionUpdateStruct->offset = $collection->getOffset();
            $collectionUpdateStruct->limit = $collection->getLimit() ?? 0;
        }

        return $collectionUpdateStruct;
    }
68062
public function query(Criteria $criteria=null, $alias=null)
    {
        $query = new Query($this->_connection);

        if($alias) {
            $query->from('`'.$this->_tableName."` AS `".$alias."`");
        } else {
            $query->from('`'.$this->_tableName.'`');
        }

        // add optional where clause
        if($criteria) $query->where($criteria->toSql());

        return $query;
    }
841032
public function renumberCollection(Collection $collection)
    {
        $i = 0;

        foreach ( $collection as $item ) {
            if ( $item instanceof NumericallySortableContract ) {
                $item->updateSortingOrder(++$i);
            }
        }

        return $this;
    }
983746
public function alias($alias, $binding)
    {
        if ($this->resolve($binding)) {
            $this->bindings[$alias] = $this->bindings[$binding];
            $this->cache[$alias] = $this->cache[$binding];
            $this->aliases[$alias] = $binding;
        }
    }
1214903
public function makeCollection(ArrayAccess $collection, array $meta = [], TransformerAbstract $transformer = null)
    {
        if ($this->isPaged($collection)) {
            $paginator = $collection;
            $collection = $collection->getIterator();
            $resource = $this->makeFractalCollection($collection, $this->getTransformer($transformer));
            $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
        } else {
            $resource = $this->makeFractalCollection($collection, $this->getTransformer($transformer));
        }

        $resource = $this->addFractalMeta($resource, $meta);

        $fractal = $this->getFractalFactory()->createData($resource)->toArray();

        return $fractal;
    }
94985
public function schemaCollection(SchemaCollection $collection = null)
    {
        deprecationWarning(
            'Connection::schemaCollection() is deprecated. ' .
            'Use Connection::setSchemaCollection()/getSchemaCollection() instead.'
        );
        if ($collection !== null) {
            $this->setSchemaCollection($collection);
        }

        return $this->getSchemaCollection();
    }