Completed
Push — master ( 527bb5...aca9c3 )
by César
05:15
created

RouteReflection::handleRequestCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Preetender\Routing;
4
use League\Container\Exception\ContainerException;
5
use Preetender\Routing\Response\ExecuteResponse;
6
7
/**
8
 * Trait RouteReflection
9
 * @package Preetender\Routing
10
 */
11
trait RouteReflection
12
{
13
    /** @var array */
14
    protected static $numberOfParameters = [];
15
16
    /** @var */
17
    protected static $reflectionObject;
18
19
    /** @var null  */
20
    protected static $resolveMethodName = null;
21
22
    /**
23
     * ...
24
     *
25
     * @return mixed
26
     */
27
    protected function handleRequestCallable()
28
    {
29
        static::createReflection(new \ReflectionFunction($this->getCallable()));
0 ignored issues
show
Bug introduced by
It seems like getCallable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
        $call = call_user_func_array($this->callable, static::resolveParameters());
0 ignored issues
show
Bug introduced by
The property callable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        return ExecuteResponse::factory($call, $this->request, $this->response);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    /**
35
     * ...
36
     *
37
     * @return mixed
38
     */
39
    protected function handleRequestController()
40
    {
41
        $attributes = RouteController::prepare($this->getCallable());
0 ignored issues
show
Bug introduced by
It seems like getCallable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
42
        static::createReflection(new \ReflectionClass($attributes['class']), $attributes['method']);
43
        $call = call_user_func_array([$attributes['class'], $attributes['method']], static::resolveParameters());
44
        return ExecuteResponse::factory($call, $this->request, $this->response);
45
    }
46
47
    /**
48
     * @param object $object
49
     * @param string|null $method
50
     */
51
    protected static function createReflection($object, string $method = null)
52
    {
53
        static::$reflectionObject = $object;
54
        static::$resolveMethodName = $method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $method can also be of type string. However, the property $resolveMethodName is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55
    }
56
57
    /**
58
     * @param int $position
59
     * @return mixed
60
     */
61
    protected static function getPointersAndReturnRequired(int $position)
62
    {
63
        return static::$parameters[ array_flip( static::$numberOfParameters)[$position] ];
64
    }
65
66
    /**
67
     * ...
68
     * @return array
69
     */
70
    protected static function resolveParameters()
71
    {
72
        $attributes = [];
73
        $parameters = static::getParametersObject();
74
        if(count($parameters) > 0) {
75
            foreach ($parameters as $parameter) {
76
                /** @var \ReflectionParameter $parameter */
77
                if(!$parameter->getClass()) {
78
                    static::$numberOfParameters[] = $parameter->getPosition();
79
                    $attributes[$parameter->getPosition()] = static::getPointersAndReturnRequired($parameter->getPosition());
80
                    continue;
81
                }
82
                $className = $parameter->getClass()->getName();
83
                if(static::checkClassRegisterContainer($className)) {
84
                    $attributes[$parameter->getPosition()] = Kernel::getContainer()->get($className);
85
                }
86
            }
87
        }
88
        return $attributes;
89
    }
90
91
    /**
92
     * Check whether injected class is registered in container.
93
     *
94
     * @param string $className
95
     * @return bool
96
     */
97
    protected static function checkClassRegisterContainer(string $className)
98
    {
99
        if(!Kernel::getContainer()->has($className)) {
100
            throw new ContainerException("Class [{$className}] not registered in the container.");
101
        }
102
        return true;
103
    }
104
105
    /**
106
     * Get all parameters to object
107
     *
108
     * @return mixed
109
     */
110
    protected static function getParametersObject()
111
    {
112
        if( static::$reflectionObject instanceof \ReflectionClass) {
113
            $method = static::$reflectionObject->getMethod( static::$resolveMethodName );
114
            return $method->getParameters();
115
        }
116
        if ( static::$reflectionObject instanceof \ReflectionFunction) {
117
            return static::$reflectionObject->getParameters();
118
        }
119
    }
120
}