Passed
Push — master ( dd9bf7...8e9a90 )
by Mr
02:04
created

InheritanceReader::flatMapTraits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Interop;
10
11
use ReflectionClass;
12
13
trait InheritanceReader
14
{
15
    /** @return ReflectionClass[] */
16 7
    protected static function getInheritance(ReflectionClass $classReflection, bool $includeTraits = true): array
17
    {
18 7
        $current = $classReflection;
19 7
        $classes = $includeTraits
20 7
            ? array_merge([$classReflection], static::flatMapTraits($classReflection))
21 7
            : [$classReflection];
22
23 7
        while ($current = $current->getParentClass()) {
24
            $classes = $includeTraits
25
                ? array_merge($classes, [$current], static::flatMapTraits($current))
26
                : array_merge($classes, [$classReflection]);
27
        }
28
29 7
        return $classes;
30
    }
31
32
    /** @return ReflectionClass[] */
33 7
    private static function flatMapTraits(ReflectionClass $classReflection): array
34
    {
35 7
        $traits = [];
36 7
        foreach ($classReflection->getTraits() as $trait) {
37 7
            $traits = array_merge($traits, [$trait], static::flatMapTraits($trait));
38
        }
39
40 7
        return $traits;
41
    }
42
}
43