Completed
Push — master ( 3bca0f...4b95a7 )
by Tomáš
05:03 queued 02:28
created

DefinitionAnalyzer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 87
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B shouldDefinitionBeAutowired() 0 22 6
A areAllConstructorArgumentsRequired() 0 13 2
B haveMissingArgumentsTypehints() 0 27 6
A hasConstructorArguments() 0 9 2
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 5
    public function __construct(DefinitionValidator $definitionValidator)
22
    {
23 5
        $this->definitionValidator = $definitionValidator;
24 5
    }
25
26 5
    public function shouldDefinitionBeAutowired(Definition $definition) : bool
27
    {
28 5
        if (!$this->definitionValidator->validate($definition)) {
29 1
            return false;
30
        }
31
32 5
        $classReflection = new ReflectionClass($definition->getClass());
33 5
        if (!$classReflection->hasMethod('__construct') || !$this->hasConstructorArguments($classReflection)) {
34 2
            return false;
35
        }
36
37 4
        $constructorReflection = $classReflection->getConstructor();
38 4
        if ($this->areAllConstructorArgumentsRequired($definition, $constructorReflection)) {
39 2
            return false;
40
        }
41
42 3
        if (!$this->haveMissingArgumentsTypehints($definition, $constructorReflection)) {
43 1
            return false;
44
        }
45
46 3
        return true;
47
    }
48
49 5
    private function hasConstructorArguments(ReflectionClass $classReflection) : bool
50
    {
51 5
        $constructorMethodReflection = $classReflection->getConstructor();
52 5
        if ($constructorMethodReflection->getNumberOfParameters()) {
53 4
            return true;
54
        }
55
56 1
        return false;
57
    }
58
59 4
    private function areAllConstructorArgumentsRequired(
60
        Definition $definition,
61
        ReflectionMethod $constructorReflection
62
    ) : bool {
63 4
        $constructorArgumentsCount = count($definition->getArguments());
64 4
        $constructorRequiredArgumentsCount = $constructorReflection->getNumberOfRequiredParameters();
65
66 4
        if ($constructorArgumentsCount === $constructorRequiredArgumentsCount) {
67 2
            return true;
68
        }
69
70 3
        return false;
71
    }
72
73 3
    private function haveMissingArgumentsTypehints(
74
        Definition $definition,
75
        ReflectionMethod $constructorReflection
76
    ) : bool {
77 3
        $arguments = $definition->getArguments();
78 3
        if (!count($arguments)) {
79 2
            return true;
80
        }
81
82 2
        $i = 0;
83 2
        foreach ($constructorReflection->getParameters() as $parameterReflection) {
84 2
            if (!isset($arguments[$i])) {
85 2
                if ($parameterReflection->isDefaultValueAvailable()) {
86 1
                    ++$i;
87 1
                    continue;
88
                }
89
90 1
                if (null !== $parameterReflection->getType()) {
91 1
                    return true;
92
                }
93
            }
94
95 2
            ++$i;
96
        }
97
98 1
        return false;
99
    }
100
}
101