Completed
Push — master ( d34cec...faecd8 )
by Augusto
02:22
created

Callback::getReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routes;
10
11
use ReflectionFunction;
12
use ReflectionMethod;
13
14
/** A callback-based route */
15
class Callback extends AbstractRoute
16
{
17
    /** @var callable The actual callback this route holds */
18
    protected $callback;
19
20
    /** @var array String argument parameters from the Request */
21
    public $arguments;
22
23
    /** @var ReflectionFunctionAbstract The reflection for the callback */
24
    protected $reflection;
25
26
    /**
27
     * @param string   $method    The HTTP method (GET, POST, etc)
28
     * @param string   $pattern   The URI pattern for this route
29
     * @param callable $callback  The callback this route holds
30
     * @param array    $arguments Additional arguments for this callback
31
     */
32 31
    public function __construct(
33
        $method,
34
        $pattern,
35
        $callback,
36
        array $arguments = array()
37
    ) {
38 31
        $this->callback = $callback;
39 31
        $this->arguments = $arguments;
40 31
        parent::__construct($method, $pattern);
41 31
    }
42
43
    /**
44
     * Returns an appropriate Reflection for any callable object
45
     *
46
     * @return ReflectionFunctionAbstract The returned reflection object
47
     */
48 10
    public function getCallbackReflection()
49
    {
50 10
        if (is_array($this->callback)) {
51
            return new ReflectionMethod($this->callback[0], $this->callback[1]);
52
        } else {
53 10
            return new ReflectionFunction($this->callback);
54
        }
55
    }
56
57
    /**
58
     * Gets the reflection for a specific method. For callables, the reflection
59
     * is always the same. This follows the AbstractRoute implementation
60
     *
61
     * @param string $method The irrelevant HTTP method for this implementation
62
     *
63
     * @return ReflectionFunctionAbstract The returned reflection object
64
     */
65 10
    public function getReflection($method)
66
    {
67 10
        if (empty($this->reflection)) {
68 10
            $this->reflection = $this->getCallbackReflection();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getCallbackReflection() of type object<ReflectionMethod> or object<ReflectionFunction> is incompatible with the declared type object<Respect\Rest\Rout...ectionFunctionAbstract> of property $reflection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69 10
        }
70
71 10
        return $this->reflection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->reflection; (ReflectionMethod|Reflect...lectionFunctionAbstract) is incompatible with the return type documented by Respect\Rest\Routes\Callback::getReflection of type Respect\Rest\Routes\ReflectionFunctionAbstract.

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...
72
    }
73
74
    /**
75
     * Runs the callback when this route is matched with params
76
     *
77
     * @param string $method The irrelevant HTTP method for this implementation
78
     * @param array  $params An array of params for this request
79
     *
80
     * @see Respect\Rest\Request::$params
81
     *
82
     * @return mixed Whatever the callback returns
83
     */
84 17
    public function runTarget($method, &$params)
85
    {
86 17
        return call_user_func_array(
87 17
            $this->callback,
88 17
            array_merge($params, $this->arguments)
89 17
        );
90
    }
91
}
92