AppContextBase::initSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace evelikto\di;
4
5
/**
6
 * Implements a dynamic Dependency Injection container with autowiring support.
7
 *
8
 * Evelikto-DI is configurable via an arbitrary class instance in which every method is
9
 * considered to be a factory method and each constant is considered to be a config value.
10
 */
11
abstract class AppContextBase
12
{
13
    const DI_CONTAINER_KEY = 'appContext';
14
    const DI_SESSION_STORAGE_KEY = 'diSessionStorage';
15
    const DI_GLOBAL_STORAGE_KEY = 'diGlobalStorage';
16
17
    /** @var  object  config  Application config, which provides constants and factory methods */
18
    protected $config;
19
20
    /** @var \ReflectionClass Cache for efficient retrieval of factory methods */
21
    protected $configReflClass;
22
23
    /** @var string configCName Cache for efficient retrieval of constant values */
24
    protected $configClassName;
25
26
    /**
27
     * Creates new application context and registers itself.
28
     *
29
     * @param  object  $config  Configuration class instance.
30
     */
31
    public function __construct($config) {
32
        $this->config = $config;
33
        $this->configReflClass = new \ReflectionClass($config);
34
        $this->configClassName = get_class($config);
35
36
        $this->initSession();
37
        $this->initGlobal();
38
39
        $this->set(static::DI_CONTAINER_KEY, $this);
40
        $this->set(get_class($this), $this);
41
    }
42
43
    /**
44
     * Creates a new or fetches a previously resolved dependency.
45
     *
46
     * @param   string  $name  Name of the dependency to be resolved.
47
     * @return  mixed          The resolved dependency.
48
     */
49
    public function get(string $name) {
50
        $value = $this->read($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->read($name) targeting evelikto\di\AppContextBase::read() 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...
51
        if ($value !== null)
0 ignored issues
show
introduced by
The condition $value !== null is always false.
Loading history...
52
            return $value;
53
54
        $bean = $this->create($name);
55
        if ($bean === null)
56
            throw new UnresolvableDependency($name);
57
58
        return $this->store($bean);
59
    }
60
61
    /**
62
     * Stores manually created object.
63
     *
64
     * @param   string  $name  Name of the dependency.
65
     * @param   mixed   $name  Dependency to be stored.
66
     * @param   string  $name  Scope name. Defaults to the singleton (local) scope.
67
     * @return  mixed          Returns the stored object back.
68
     */
69
    public function set(string $name, $value, string $scope = null) {
70
        return $this->store(new Bean($name, $value, $scope));
71
    }
72
73
    /**
74
     * Resolves arguments for a factory method or a controller action.
75
     *
76
     * @param   \ReflectionParameter  $param  Parameter which has to be resolved.
77
     * @return  mixed                         Resolved dependency.
78
     * @throws  UnresolvableParameter         If the parameter cannot be resolved.
79
     */
80
    public function resolve(\ReflectionParameter $param) {
81
        $value = $this->resolveStoredByName($param);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->resolveStoredByName($param) targeting evelikto\di\AppContextBase::resolveStoredByName() 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...
82
        if ($value !== null)
0 ignored issues
show
introduced by
The condition $value !== null is always false.
Loading history...
83
            return $value;
84
85
        $bean = $this->resolveMethodByName($param) ?? $this->resolveByType($param);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolveMethodByName($param) targeting evelikto\di\AppContextBase::resolveMethodByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Bug introduced by
Are you sure the usage of $this->resolveByType($param) targeting evelikto\di\AppContextBase::resolveByType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
86
        if ($bean !== null)
0 ignored issues
show
introduced by
The condition $bean !== null is always false.
Loading history...
87
            return $this->store($bean);
88
89
        $value = $this->resolveByDefault($param);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->resolveByDefault($param) targeting evelikto\di\AppContextBase::resolveByDefault() 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...
90
        if ($value !== null)
0 ignored issues
show
introduced by
The condition $value !== null is always false.
Loading history...
91
            return $value;
92
93
        throw new UnresolvableParameter($param->getName());
94
    }
95
96
    /**
97
     * Reads previously saved dependency or just a constant from config class.
98
     *
99
     * @param   string  $name  Dependency which has to be read.
100
     * @return  mixed          Read dependency, constant or null.
101
     */
102
    protected function read(string $name) {
103
        return $this->fromLocal($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromLocal($name) targeting evelikto\di\AppContextBase::fromLocal() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
104
            ?? $this->fromConst($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromConst($name) targeting evelikto\di\AppContextBase::fromConst() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
105
            ?? $this->fromSession($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromSession($name) targeting evelikto\di\AppContextBase::fromSession() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
106
            ?? $this->fromGlobal($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromGlobal($name) targeting evelikto\di\AppContextBase::fromGlobal() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
107
            ?? null;
108
    }
109
110
    /**
111
     * Creates a new dependency in a number of ways.
112
     *
113
     * @param  string  $name  Name of the dependency to be created. Can be class or interface name.
114
     * @return Bean           Resolved dependency wrapped as a Bean class instance or null.
115
     */
116
    protected function create(string $name) {
117
        return $this->fromMethod($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromMethod($name) targeting evelikto\di\AppContextBase::fromMethod() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
118
            ?? $this->fromClass($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromClass($name) targeting evelikto\di\AppContextBase::fromClass() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
119
            ?? $this->fromInterfaceMethod($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromInterfaceMethod($name) targeting evelikto\di\AppContextBase::fromInterfaceMethod() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
120
            ?? $this->fromInterfaceAlias($name)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromInterfaceAlias($name) targeting evelikto\di\AppContextBase::fromInterfaceAlias() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
121
            ?? null;
122
    }
123
124
    /**
125
     * Saves a dependency, returns unwrapped value.
126
     *
127
     * @param  Bean  $bean  Wrapped dependency to be saved.
128
     * @return mixed        Unwrapped dependency value.
129
     */
130
    protected function store(Bean $bean) {
131
        return $this->toLocal($bean)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->toLocal($bean) targeting evelikto\di\AppContextBase::toLocal() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
132
            ?? $this->toSession($bean)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->toSession($bean) targeting evelikto\di\AppContextBase::toSession() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
133
            ?? $this->toGlobal($bean)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->toGlobal($bean) targeting evelikto\di\AppContextBase::toGlobal() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
134
            ?? $bean->value;
135
    }
136
137
    /**
138
     * Recursively resolves all method parameters
139
     *
140
     * @param  \ReflectionMethod  $method  Method to be autowired.
141
     * @returns                            Array of resolved parameters.
142
     */
143
    protected function autowire(\ReflectionMethod $method) {
144
        return array_map([$this, 'resolve'], $method->getParameters());
145
    }
146
147
    // Placeholders to disable functions not redefined by traits
148
149
    // ClassCreator
150
    protected function fromClass(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

150
    protected function fromClass(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
        return null;
152
    }
153
154
    // InterfaceAliasCreator
155
    protected function fromInterfaceAlias(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
    protected function fromInterfaceAlias(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
        return null;
157
    }
158
159
    // InterfaceMethodCreator
160
    protected function fromInterfaceMethod(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

160
    protected function fromInterfaceMethod(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
        return null;
162
    }
163
164
    // MethodCreator
165
    protected function fromMethod(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

165
    protected function fromMethod(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166
        return null;
167
    }
168
169
    // ConstReader
170
    protected function fromConst(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

170
    protected function fromConst(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
        return null;
172
    }
173
174
    // DefaultResolver
175
    protected function resolveByDefault(\ReflectionParameter $param) {
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

175
    protected function resolveByDefault(/** @scrutinizer ignore-unused */ \ReflectionParameter $param) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
        return null;
177
    }
178
179
    // NameResolver
180
    protected function resolveStoredByName(\ReflectionParameter $param) {
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

180
    protected function resolveStoredByName(/** @scrutinizer ignore-unused */ \ReflectionParameter $param) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
        return null;
182
    }
183
    protected function resolveMethodByName(\ReflectionParameter $param) {
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

183
    protected function resolveMethodByName(/** @scrutinizer ignore-unused */ \ReflectionParameter $param) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
        return null;
185
    }
186
187
    // TypeResolver
188
    protected function resolveByType(\ReflectionParameter $param) {
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

188
    protected function resolveByType(/** @scrutinizer ignore-unused */ \ReflectionParameter $param) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
        return null;
190
    }
191
192
    // LocalStorage
193
    protected function fromLocal(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    protected function fromLocal(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
        return null;
195
    }
196
    protected function toLocal(Bean $bean) {
0 ignored issues
show
Unused Code introduced by
The parameter $bean is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

196
    protected function toLocal(/** @scrutinizer ignore-unused */ Bean $bean) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
        return null;
198
    }
199
200
    // SessionStorage
201
    protected function initSession() {
202
    }
203
    protected function fromSession(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

203
    protected function fromSession(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
204
        return null;
205
    }
206
    protected function toSession(Bean $bean) {
0 ignored issues
show
Unused Code introduced by
The parameter $bean is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

206
    protected function toSession(/** @scrutinizer ignore-unused */ Bean $bean) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
        return null;
208
    }
209
210
    // GlobalStorage
211
    protected function initGlobal() {
212
    }
213
    protected function fromGlobal(string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

213
    protected function fromGlobal(/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
        return null;
215
    }
216
    protected function toGlobal(Bean $bean) {
0 ignored issues
show
Unused Code introduced by
The parameter $bean is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

216
    protected function toGlobal(/** @scrutinizer ignore-unused */ Bean $bean) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
        return null;
218
    }
219
}