Passed
Pull Request — 1.x (#35)
by Marko
05:54
created

SortableElementsTrait::sort()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
4
namespace KunicMarko\SonataAnnotationBundle\Grouping;
5
6
7
/**
8
 * @internal
9
 *
10
 * @author Marko Kunic <[email protected]>
11
 */
12
trait SortableElementsTrait
13
{
14
    private function sort(SortableElement ...$elements): array
15
    {
16
        $elementsWithPosition = [];
17
        $elementsWithoutPosition = [];
18
19
        foreach ($elements as $element) {
20
            if ($element->getPosition() === null) {
21
                $elementsWithoutPosition[] = $element;
22
                continue;
23
            }
24
25
            if (isset($elementsWithPosition[$element->getPosition()])) {
26
                throw new \InvalidArgumentException(sprintf(
27
                    'Element "%s" has same position as Element "%s".',
28
                    $element->getName(),
29
                    $elementsWithPosition[$element->getPosition()]->getName()
30
                ));
31
            }
32
33
            $elementsWithPosition[$element->getPosition()] = $element;
34
        }
35
36
        return \array_merge($elementsWithPosition, $elementsWithoutPosition);
37
    }
38
}