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

InheritanceReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 3
b 0
f 0
dl 0
loc 28
ccs 12
cts 15
cp 0.8
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInheritance() 0 14 4
A flatMapTraits() 0 8 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