ORMIterableResultDecorator::normalizeResult()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 4
nop 1
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 6.0493
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\Batch;
4
5
use Doctrine\ORM\Internal\Hydration\IterableResult;
6
7
/**
8
 * Fixes https://github.com/doctrine/orm/issues/2821.
9
 *
10
 * @internal
11
 *
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ORMIterableResultDecorator implements \IteratorAggregate
15
{
16
    private IterableResult $result;
17
18 54
    public function __construct(IterableResult $result)
19
    {
20 54
        $this->result = $result;
21 54
    }
22
23 54
    public function getIterator(): \Traversable
24
    {
25 54
        foreach ($this->result as $key => $value) {
26 50
            yield $key => self::normalizeResult($value);
27
        }
28 54
    }
29
30
    /**
31
     * @param mixed $result
32
     *
33
     * @return mixed
34
     */
35 50
    private static function normalizeResult($result)
36
    {
37 50
        if (!\is_array($result)) {
38
            return $result;
39
        }
40
41 50
        $firstKey = \array_key_first($result);
42
43 50
        if (null !== $firstKey && \is_object($result[$firstKey]) && $result === [$firstKey => $result[$firstKey]]) {
44 41
            return $result[$firstKey];
45
        }
46
47 9
        if (\count($result) > 1) {
48 5
            $result = [\array_merge(...$result)];
49
        }
50
51 9
        return $result[$firstKey];
52
    }
53
}
54