Passed
Push — master ( 55fca5...85d75f )
by Kirill
04:44 queued 10s
created

Instantiator::getTraitConstructors()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Attributes package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Internal\Instantiator;
13
14
abstract class Instantiator implements InstantiatorInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private const CONSTRUCTOR_NAME = '__construct';
20
21
    /**
22
     * @param \ReflectionClass $class
23
     * @return \ReflectionMethod|null
24
     */
25
    protected function getConstructor(\ReflectionClass $class): ?\ReflectionMethod
26
    {
27
        if ($class->hasMethod(self::CONSTRUCTOR_NAME)) {
28
            return $class->getMethod(self::CONSTRUCTOR_NAME);
29
        }
30
31
        if ($constructor = $this->getTraitConstructors($class)) {
32
            return $constructor;
33
        }
34
35
        if ($parent = $class->getParentClass()) {
36
            return $this->getConstructor($parent);
37
        }
38
39
        return null;
40
    }
41
42
    /**
43
     * @param \ReflectionClass $class
44
     * @return \ReflectionMethod|null
45
     */
46
    private function getTraitConstructors(\ReflectionClass $class): ?\ReflectionMethod
47
    {
48
        foreach ($class->getTraits() as $trait) {
49
            if ($constructor = $this->getConstructor($trait)) {
50
                return $constructor;
51
            }
52
53
            if ($constructor = $this->getTraitConstructors($trait)) {
54
                return $constructor;
55
            }
56
        }
57
58
        return null;
59
    }
60
}