Completed
Push — master ( 92472c...fe56a0 )
by Vitaly
02:18
created

Inject::toMethodMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 27.07.2016
6
 * Time: 1:55.
7
 */
8
namespace samsonframework\container\annotation;
9
10
use samsonframework\container\metadata\MethodMetadata;
11
use samsonframework\container\metadata\PropertyMetadata;
12
13
/**
14
 * Injection annotation class.
15
 *
16
 * @Annotation
17
 */
18
class Inject extends CollectionValue implements MethodInterface, PropertyInterface
19
{
20
    /** {@inheritdoc} */
21 1
    public function toMethodMetadata(MethodMetadata $metadata)
22
    {
23 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...
24 1
    }
25
26
    /** {@inheritdoc} */
27 4
    public function toPropertyMetadata(PropertyMetadata $propertyMetadata)
28
    {
29
        // Get @Inject("value")
30 4
        $propertyMetadata->injectable = array_shift($this->collection);
31
32
        // Check if we need to append namespace to injectable
33 4 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...
34 1
            $propertyMetadata->injectable = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->injectable;
35 1
        }
36
37
        // Check if we need to append namespace to type hint
38 4 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...
39 1
            $propertyMetadata->typeHint = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->typeHint;
40 1
        }
41
42
        // Check for inheritance violation
43 4
        if ($propertyMetadata->injectable !== null && $propertyMetadata->typeHint !== null) {
44 3
            $inheritance = array_merge([$propertyMetadata->injectable], class_parents($propertyMetadata->injectable));
45 3
            if (!in_array($propertyMetadata->typeHint, $inheritance, true)) {
46 1
                throw new \InvalidArgumentException('@Inject dependency violates ' . $propertyMetadata->typeHint . ' inheritance');
47
            }
48 2
        }
49 3
    }
50
}
51