Completed
Push — master ( c23c04...32905f )
by Nikita
01:15
created

TypeRegistry::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace LaraComponents\GraphQL\Helpers;
4
5
use InvalidArgumentException;
6
7
class TypeRegistry
8
{
9
    /**
10
     * The application instance.
11
     *
12
     * @var \Illuminate\Foundation\Application
13
     */
14
    protected $app;
15
16
    /**
17
     * The array of types.
18
     *
19
     * @var array
20
     */
21
    protected $types = [];
22
23
    /**
24
     * The array of resolved types.
25
     *
26
     * @var array
27
     */
28
    protected $instances = [];
29
30
    /**
31
     * Create a new instance.
32
     *
33
     * @param  \Illuminate\Foundation\Application  $app
34
     * @param  array  $types
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct($app, array $types = [])
38
    {
39
        $this->app = $app;
40
41
        foreach ($types as $name => $class) {
42
            $this->setType($class, is_numeric($name) ? null : $name);
43
        }
44
    }
45
46
    /**
47
     * Set type into registry
48
     *
49
     * @param string $class
50
     * @param string $name
51
     */
52
    public function setType($class, $name = null)
53
    {
54
        $name = $this->getName($class, $name);
55
        $this->types[$name] = $class;
56
    }
57
58
    /**
59
     * Resolve type from registry
60
     *
61
     * @param  string $type
62
     * @param  string $parameters
63
     * @return mixed
64
     * @throws \InvalidArgumentException
65
     */
66
    protected function resolveType($type, array $parameters)
67
    {
68
        if (! array_key_exists($type, $this->types)) {
69
            throw new InvalidArgumentException(sprintf('The type "%s" is not found.', $type));
70
        }
71
72
        if (count($parameters)) {
73
            return $this->make($type, $parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array, but the function expects a string.

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...
74
        }
75
76
        if (! array_key_exists($type, $this->instances)) {
77
            $this->instances[$type] = $this->make($type, $parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array, but the function expects a string.

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...
78
        }
79
80
        return $this->instances[$type];
81
    }
82
83
    /**
84
     * Make a new instance of type
85
     *
86
     * @param  string $type
87
     * @param  string $parameters
88
     * @return mixed
89
     */
90
    protected function make($type, $parameters)
91
    {
92
        $class = $this->types[$type];
93
94
        if (count($parameters)) {
95
            return new $class(...$parameters);
96
        }
97
98
        return new $class();
99
    }
100
101
    /**
102
     * Get name  from className
103
     * @param  string $class
104
     * @param  string $name
105
     * @return string
106
     */
107
    protected function getName($class, $name = null)
108
    {
109
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
110
            return $name;
111
        }
112
113
        if ($prevPos = strrpos($class, '\\')) {
114
            $class = substr($class, $prevPos + 1);
115
        }
116
117
        if (substr($class, -4) == 'Type') {
118
            $class = substr($class, 0, -4);
119
        }
120
121
        return lcfirst($class);
122
    }
123
124
    /**
125
     * Dynamically call the type instance.
126
     *
127
     * @param  string $type
128
     * @param  array $parameters
129
     * @return mixed
130
     */
131
    public function __call($type, $parameters)
132
    {
133
        return $this->resolveType($type, $parameters);
134
    }
135
}
136