Completed
Pull Request — master (#6)
by Tomáš
06:23 queued 04:21
created

DefinitionAnalyzer::shouldDefinitionBeAutowired()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 8.439
cc 6
eloc 14
nc 6
nop 1
crap 6.0131
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\DefaultAutowire\DependencyInjection\Definition;
9
10
use ReflectionClass;
11
use ReflectionMethod;
12
use Symfony\Component\DependencyInjection\Definition;
13
14
final class DefinitionAnalyzer
15
{
16
    /**
17
     * @var DefinitionValidator
18
     */
19
    private $definitionValidator;
20
21 2
    public function __construct(DefinitionValidator $definitionValidator)
22
    {
23 2
        $this->definitionValidator = $definitionValidator;
24 2
    }
25
26 2
    public function shouldDefinitionBeAutowired(Definition $definition) : bool
27
    {
28 2
        if (!$this->definitionValidator->validate($definition)) {
29 1
            return false;
30
        }
31
32 2
        $classReflection = new ReflectionClass($definition->getClass());
33 2
        if (!$classReflection->hasMethod('__construct')) {
34 1
            return false;
35
        }
36
37 2
        if (!$this->hasConstructorArguments($classReflection)) {
38
            return false;
39
        }
40
41 2
        if ($this->areAllConstructorArgumentsRequired($definition, $classReflection)) {
42 1
            return false;
43
        }
44
45 2
        $constructorReflection = $classReflection->getConstructor();
46 2
        if (!$this->haveMissingArgumentsTypehints($definition, $constructorReflection)) {
47 1
            return false;
48
        }
49
50 2
        return true;
51
    }
52
53 2
    private function areAllConstructorArgumentsRequired(Definition $definition, ReflectionClass $classReflection) : bool
54
    {
55 2
        $constructorMethodReflection = $classReflection->getConstructor();
56
57 2
        $constructorArgumentsCount = count($definition->getArguments());
58 2
        $constructorRequiredArgumentsCount = $constructorMethodReflection->getNumberOfRequiredParameters();
59
60 2
        if ($constructorArgumentsCount === $constructorRequiredArgumentsCount) {
61 1
            return true;
62
        }
63
64 2
        return false;
65
    }
66
67 2
    private function hasConstructorArguments(ReflectionClass $classReflection) : bool
68
    {
69 2
        $constructorMethodReflection = $classReflection->getConstructor();
70 2
        if ($constructorMethodReflection->getNumberOfParameters()) {
71 2
            return true;
72
        }
73
74
        return false;
75
    }
76
77 2
    private function haveMissingArgumentsTypehints(
78
        Definition $definition,
79
        ReflectionMethod $constructorReflection
80
    ) : bool {
81 2
        $arguments = $definition->getArguments();
82 2
        if (!count($arguments)) {
83 2
            return true;
84
        }
85
86 1
        $i = 0;
87 1
        foreach ($constructorReflection->getParameters() as $parameterReflection) {
88 1
            if (!isset($arguments[$i])) {
89 1
                ++$i;
90 1
                continue;
91
            }
92
93 1
            if ($arguments[$i] === '' && !$parameterReflection->getType()->allowsNull()) {
94
                return true;
95
            }
96
97 1
            ++$i;
98
        }
99
100 1
        return false;
101
    }
102
}
103