Completed
Branch 2.x (dc1b30)
by Akihito
02:25
created

InjectionPoint::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Doctrine\Common\Annotations\Reader;
10
11
final class InjectionPoint implements InjectionPointInterface
12
{
13
    /**
14
     * @var \ReflectionParameter
15
     */
16
    private $parameter;
17
18
    /**
19
     * @var Reader
20
     */
21
    private $reader;
22
23 34
    public function __construct(\ReflectionParameter $parameter, Reader $reader)
24
    {
25 34
        $this->parameter = $parameter;
26 34
        $this->reader = $reader;
27 34
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function getParameter()
33
    {
34 2
        return $this->parameter;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3
    public function getMethod()
41
    {
42 3
        return $this->parameter->getDeclaringFunction();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->parameter->getDeclaringFunction(); (ReflectionFunction) is incompatible with the return type declared by the interface Ray\Di\InjectionPointInterface::getMethod of type ReflectionMethod.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function getClass()
49
    {
50 1
        return $this->parameter->getDeclaringClass();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function getQualifiers()
57
    {
58 2
        $qualifiers = [];
59 2
        $annotations = $this->reader->getMethodAnnotations($this->getMethod());
0 ignored issues
show
Documentation introduced by
$this->getMethod() is of type object<ReflectionFunction>, but the function expects a object<ReflectionMethod>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60 2
        foreach ($annotations as $annotation) {
61 2
            $qualifier = $this->reader->getClassAnnotation(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $qualifier is correct as $this->reader->getClassA...ay\\Di\\Di\\Qualifier') (which targets Doctrine\Common\Annotati...r::getClassAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62 2
                new \ReflectionClass($annotation),
63
                'Ray\Di\Di\Qualifier'
64 2
            );
65 2
            if ($qualifier) {
66 2
                $qualifiers[] = $annotation;
67 2
            }
68 2
        }
69
70 2
        return $qualifiers;
71
    }
72
}
73