Completed
Push — master ( aef52d...15366c )
by devosc
05:36 queued 03:35
created

Builder::reflectionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Resolver;
7
8
use Mvc5\Exception;
9
10
class Builder
11
    extends \ReflectionClass
12
{
13
    /**
14
     * @var array|self[]
15
     */
16
    protected static $class = [];
17
18
    /**
19
     * @var \ReflectionMethod
20
     */
21
    protected $constructor;
22
23
    /**
24
     * @var \ReflectionParameter[]
25
     */
26
    protected $params;
27
28
    /**
29
     * @param $name
30
     * @param array $args
31
     * @param callable $callback
32
     * @return object
33
     */
34
    static function create($name, array $args, callable $callback)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
        $class = static::reflectionClass($name);
37
38
        if (!$class->constructor()) {
39
            return $class->newInstanceWithoutConstructor();
40
        }
41
42
        if ($args && !is_string(key($args))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            return new $name(...$args);
44
        }
45
46
        $matched = [];
47
        $params  = $class->params();
48
49
        foreach($params as $param) {
50
            if (isset($args[$param->name])) {
51
                $matched[] = $args[$param->name];
52
                continue;
53
            }
54
55
            if ($param->isOptional()) {
56
                $param->isDefaultValueAvailable() &&
57
                    $matched[] = $param->getDefaultValue();
58
                continue;
59
            }
60
61
            if (null !== ($hint = $param->getClass()) && null !== $match = $callback($hint->name)) {
62
                $matched[] = $match;
63
                continue;
64
            }
65
66
            if (null !== $match = $callback($param->name)) {
67
                $matched[] = $match;
68
                continue;
69
            }
70
71
            Exception::runtime('Missing required parameter $' . $param->name . ' for ' . $name);
72
        }
73
74
        return new $name(...($params ? $matched : array_values($args)));
75
    }
76
77
    /**
78
     * @return bool|\ReflectionMethod
79
     */
80
    protected function constructor()
81
    {
82
        return $this->constructor ?? $this->constructor = (parent::getConstructor() ?: false);
0 ignored issues
show
Documentation Bug introduced by
It seems like parent::getConstructor() ?: false can also be of type false. However, the property $constructor is declared as type object<ReflectionMethod>. 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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getConstructor() instead of constructor()). Are you sure this is correct? If so, you might want to change this to $this->getConstructor().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
83
    }
84
85
    /**
86
     * @param \ReflectionMethod|null $method
87
     * @return array|\ReflectionParameter[]
88
     */
89
    protected function constructorParams($method)
90
    {
91
        return $method ? $method->getParameters() : [];
92
    }
93
94
    /**
95
     * @return \ReflectionParameter[]
96
     */
97
    protected function params()
98
    {
99
        return $this->params ?? $this->params = $this->constructorParams($this->constructor());
0 ignored issues
show
Documentation introduced by
$this->constructor() is of type object|false, but the function expects a object<ReflectionMethod>|null.

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...
100
    }
101
102
    /**
103
     * @param $name
104
     * @return self
105
     */
106
    static function reflectionClass($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
107
    {
108
        return static::$class[$name] ?? (static::$class[$name] = new self($name));
109
    }
110
}
111