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

SortableElementsTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 14
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A sort() 0 23 4
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
}