Completed
Push — master ( 8fb470...71ab64 )
by Vitaly
05:03
created

Inject   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 7.38 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 67.74%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 16
c 6
b 0
f 3
lcom 1
cbo 2
dl 9
loc 122
ccs 21
cts 31
cp 0.6774
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A toPropertyMetadata() 0 11 1
A buildFullClassName() 9 9 3
A validate() 0 23 3
A checkInheritanceViolation() 0 14 3
A checkInterfaceWithoutClassName() 0 6 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
/**
5
 * Created by PhpStorm.
6
 * User: root
7
 * Date: 27.07.2016
8
 * Time: 1:55.
9
 */
10
namespace samsonframework\container\annotation;
11
12
use samsonframework\container\metadata\PropertyMetadata;
13
14
/**
15
 * Injection annotation class.
16
 *
17
 * @Annotation
18
 */
19
class Inject implements PropertyInterface
20
{
21
    /** @var string Injectable dependency */
22
    protected $dependency;
23
24
    /**
25
     * Inject constructor.
26
     *
27
     * @param array $valueOrValues
28
     */
29 16
    public function __construct(array $valueOrValues)
30
    {
31
        // Get first argument from annotation
32 16
        $this->dependency = $valueOrValues['value'] ?? null;
33
34
        // Convert empty dependency to null
35 16
        $this->dependency = $this->dependency !== '' ? $this->dependency : null;
36
37
        // Removed first namespace separator if present
38 16
        $this->dependency = is_string($this->dependency) ? ltrim($this->dependency, '\\') : $this->dependency;
39 16
    }
40
41
    /** {@inheritdoc} */
42 16
    public function toPropertyMetadata(PropertyMetadata $propertyMetadata)
43
    {
44
        // Get @Inject("value")
45 16
        $propertyMetadata->dependency = $this->dependency;
46
47 16
        $this->validate(
48 16
            $propertyMetadata->typeHint,
49 16
            $propertyMetadata->dependency,
50 16
            $propertyMetadata->classMetadata->nameSpace
51
        );
52 16
    }
53
54
    /**
55
     * Validate dependency.
56
     *
57
     * @param string $type
58
     * @param string $dependency
59
     * @param string $namespace
60
     */
61 16
    protected function validate(&$type, &$dependency, $namespace)
62
    {
63
        //$dependency = $this->buildFullClassName($dependency, $namespace);
64 16
        $type = $this->buildFullClassName($type, $namespace);
65
66
//        // Check for inheritance violation
67
//        if ($this->checkInheritanceViolation($type, $dependency)) {
68
//            throw new \InvalidArgumentException(
69
//                '@Inject dependency violates ' . $type . ' inheritance with ' . $dependency
70
//            );
71
//        }
72
//
73
//        if ($this->checkInterfaceWithoutClassName($type, $dependency)) {
74
//            throw new \InvalidArgumentException(
75
//                'Cannot @Inject interface, inherited class name should be specified
76
//                ');
77
//        }
78
79
        // Empty @Inject with type hint - use type hine as dependency
80 16
        if ($dependency === null && $type !== null) {
81 12
            $dependency = $type;
82
        }
83 16
    }
84
85
    /**
86
     * Build full class name.
87
     *
88
     * @param string $className Full or short class name
89
     * @param string $namespace Name space
90
     *
91
     * @return string Full class name
92
     */
93 16 View Code Duplication
    protected function buildFullClassName($className, $namespace)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        // Check if we need to append namespace to dependency
96 16
        if ($className !== null && strpos($className, '\\') === false) {
97 11
            return $namespace . '\\' . $className;
98
        }
99
100 5
        return $className;
101
    }
102
103
    /**
104
     * Check if @Inject violates inheritance.
105
     *
106
     * @param string $type       Property/Parameter type
107
     * @param string $dependency @Inject value
108
     *
109
     * @return bool True if @Inject violates inheritance
110
     */
111
    protected function checkInheritanceViolation($type, $dependency) : bool
112
    {
113
        // Check for inheritance violation
114
        if ($dependency !== null && $type !== null) {
115
            $inheritance = array_merge(
116
                [$dependency],
117
                class_parents($dependency),
118
                class_implements($dependency)
119
            );
120
            return !in_array($type, $inheritance, true);
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * Check if @Inject has no class name and type hint is interface.
128
     *
129
     * @param string $type       Property/Parameter type
130
     * @param string $dependency @Inject value
131
     *
132
     * @return bool True if @Inject has no class name and type hint is interface.
133
     */
134
    protected function checkInterfaceWithoutClassName($type, $dependency) : bool
135
    {
136
        return $type !== null
137
        && $dependency === null
138
        && (new \ReflectionClass($type))->isInterface();
139
    }
140
}
141