Completed
Pull Request — master (#2)
by Kevin
01:30
created

IterableQueryResultNormalizer::normalize()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 9.0444
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6.0493
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine;
4
5
/**
6
 * Fixes https://github.com/doctrine/orm/issues/2821.
7
 *
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class IterableQueryResultNormalizer
11
{
12
    /**
13
     * @param mixed $result
14
     *
15
     * @return mixed
16
     */
17 19
    public static function normalize($result)
18
    {
19 19
        if (!\is_array($result)) {
20
            return $result;
21
        }
22
23 19
        $firstKey = \array_key_first($result);
24
25 19
        if (null !== $firstKey && \is_object($result[$firstKey]) && $result === [$firstKey => $result[$firstKey]]) {
26 10
            return $result[$firstKey];
27
        }
28
29 9
        if (\count($result) > 1) {
30 5
            $result = [\array_merge(...$result)];
31
        }
32
33 9
        return $result[$firstKey];
34
    }
35
}
36