Completed
Push — master ( 6f597b...d31185 )
by Vitaly
04:17 queued 01:29
created

Inject::checkInterfaceWithoutClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
crap 3
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
    /** {@inheritdoc} */
23 1
    public function toMethodMetadata(MethodMetadata $metadata)
24
    {
25 1
        $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...
26 1
    }
27
28
    /** {@inheritdoc} */
29 5
    public function toPropertyMetadata(PropertyMetadata $propertyMetadata)
30
    {
31
        // Get @Inject("value")
32 5
        $propertyMetadata->injectable = array_shift($this->collection);
33
34
        // Check if we need to append namespace to injectable
35 5 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...
36 1
            $propertyMetadata->injectable = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->injectable;
37
        }
38
39
        // Check if we need to append namespace to type hint
40 5 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...
41 1
            $propertyMetadata->typeHint = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->typeHint;
42
        }
43
44
        // Check for inheritance violation
45 5
        if ($this->checkInheritanceViolation($propertyMetadata)) {
46 1
            throw new \InvalidArgumentException('@Inject dependency violates ' . $propertyMetadata->typeHint . ' inheritance');
47
        }
48
49 4
        if ($this->checkInterfaceWithoutClassName($propertyMetadata)) {
50 1
            throw new \InvalidArgumentException('Cannot @Inject interface, inherited class name should be specified');
51
        }
52 3
    }
53
54
    /**
55
     * Check if @Inject violates inheritance.
56
     *
57
     * @param PropertyMetadata $propertyMetadata
58
     *
59
     * @return bool True if @Inject violates inheritance
60
     */
61 5
    protected function checkInheritanceViolation(PropertyMetadata $propertyMetadata) : bool
62
    {
63
        // Check for inheritance violation
64 5
        if ($propertyMetadata->injectable !== null && $propertyMetadata->typeHint !== null) {
65 3
            $inheritance = array_merge([$propertyMetadata->injectable], class_parents($propertyMetadata->injectable));
66 3
            return !in_array($propertyMetadata->typeHint, $inheritance, true);
67
        }
68
69 2
        return false;
70
    }
71
72
    /**
73
     * Check if @Inject has no class name and type hint is interface.
74
     *
75
     * @param PropertyMetadata $propertyMetadata
76
     *
77
     * @return bool True if @Inject has no class name and type hint is interface.
78
     */
79 4
    protected function checkInterfaceWithoutClassName(PropertyMetadata $propertyMetadata) : bool
80
    {
81 4
        return $propertyMetadata->typeHint !== null
82 4
        && $propertyMetadata->injectable === null
83 4
        && (new \ReflectionClass($propertyMetadata->typeHint))->isInterface();
84
    }
85
}
86