Completed
Push — master ( 44d82c...71735f )
by Vitaly
02:38
created

Inject::checkInheritanceViolation()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 12
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\MethodMetadata;
13
use samsonframework\container\metadata\PropertyMetadata;
14
15
/**
16
 * Injection annotation class.
17
 *
18
 * @Annotation
19
 */
20
class Inject extends CollectionValue implements MethodInterface, PropertyInterface
21
{
22
    /** @var string Injectable dependency */
23 1
    protected $dependency;
24
25 1
    /**
26 1
     * Inject constructor.
27
     *
28
     * @param array $scopeOrScopes
29 5
     */
30
    public function __construct(array $scopeOrScopes)
31
    {
32 5
        parent::__construct($scopeOrScopes);
33
34
        // Get first argument from annotation
35 5
        $this->dependency = $this->collection[0] ?? null;
36 1
37
        // Removed first namespace separator if present
38
        $this->dependency = is_string($this->dependency) ? ltrim($this->dependency, '\\') : $this->dependency;
39
    }
40 5
41 1
    /** {@inheritdoc} */
42
    public function toMethodMetadata(MethodMetadata $metadata)
43
    {
44
        $metadata->dependencies = $this->collection;
0 ignored issues
show
Bug introduced by
The property dependencies does not seem to exist in samsonframework\container\metadata\MethodMetadata.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
45 5
    }
46 1
47
    /** {@inheritdoc} */
48
    public function toPropertyMetadata(PropertyMetadata $propertyMetadata)
49 4
    {
50 1
        // Get @Inject("value")
51
        $propertyMetadata->injectable = $this->dependency;
52 3
53
        // Check if we need to append namespace to injectable
54 View Code Duplication
        if ($propertyMetadata->injectable !== null && strpos($propertyMetadata->injectable, '\\') === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            $propertyMetadata->injectable = $propertyMetadata->classMetadata->nameSpace
56
                . '\\' . $propertyMetadata->injectable;
57
        }
58
59
        // Check if we need to append namespace to type hint
60 View Code Duplication
        if ($propertyMetadata->typeHint !== null && strpos($propertyMetadata->typeHint, '\\') === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61 5
            $propertyMetadata->typeHint = $propertyMetadata->classMetadata->nameSpace
62
                . '\\' . $propertyMetadata->typeHint;
63
        }
64 5
65 3
        // Check for inheritance violation
66 3
        if ($this->checkInheritanceViolation($propertyMetadata)) {
67
            throw new \InvalidArgumentException(
68
                '@Inject dependency violates ' . $propertyMetadata->typeHint . ' inheritance with ' . $propertyMetadata->injectable
69 2
            );
70
        }
71
72
        if ($this->checkInterfaceWithoutClassName($propertyMetadata)) {
73
            throw new \InvalidArgumentException(
74
                'Cannot @Inject interface, inherited class name should be specified
75
                ');
76
        }
77
78
        // Empty @Inject with type hint - use type hine as injectable
79 4
        if ($propertyMetadata->injectable === null && $propertyMetadata->typeHint !== null) {
80
            $propertyMetadata->injectable = $propertyMetadata->typeHint;
81 4
        }
82 4
    }
83 4
84
    /**
85
     * Check if @Inject violates inheritance.
86
     *
87
     * @param PropertyMetadata $propertyMetadata
88
     *
89
     * @return bool True if @Inject violates inheritance
90
     */
91
    protected function checkInheritanceViolation(PropertyMetadata $propertyMetadata) : bool
92
    {
93
        // Check for inheritance violation
94
        if ($propertyMetadata->injectable !== null && $propertyMetadata->typeHint !== null) {
95
            $inheritance = array_merge(
96
                [$propertyMetadata->injectable],
97
                class_parents($propertyMetadata->injectable),
98
                class_implements($propertyMetadata->injectable)
99
            );
100
            return !in_array($propertyMetadata->typeHint, $inheritance, true);
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Check if @Inject has no class name and type hint is interface.
108
     *
109
     * @param PropertyMetadata $propertyMetadata
110
     *
111
     * @return bool True if @Inject has no class name and type hint is interface.
112
     */
113
    protected function checkInterfaceWithoutClassName(PropertyMetadata $propertyMetadata) : bool
114
    {
115
        return $propertyMetadata->typeHint !== null
116
        && $propertyMetadata->injectable === null
117
        && (new \ReflectionClass($propertyMetadata->typeHint))->isInterface();
118
    }
119
}
120