Test Failed
Push — stable ( cb1f97...b2434c )
by Nuno
01:59
created

Container::getNamespace()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 0
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework;
4
5
use RuntimeException;
6
use Illuminate\Container\Container as BaseContainer;
7
use LaravelZero\Framework\Exceptions\NotImplementedException;
8
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
9
10
/**
11
 * This is the Laravel Zero Framework container class.
12
 *
13
 * @author Nuno Maduro <[email protected]>
14
 */
15
class Container extends BaseContainer implements LaravelApplication
16
{
17
    /**
18
     * A custom callback used to configure Monolog.
19
     *
20
     * @var callable|null
21
     */
22
    protected $monologConfigurator;
23
24
    /**
25
     * The application namespace.
26
     *
27
     * @var string
28
     */
29
    protected $namespace;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function version()
35
    {
36
        return config('app.version');
37
    }
38
39
    /**
40
     * Get the base path of the Laravel installation.
41
     *
42
     * @param  string $path
43
     *
44
     * @return string
45
     */
46
    public function basePath($path = '')
47
    {
48
        return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
49
    }
50
51
    /**
52
     * Get the path to the application configuration files.
53
     *
54
     * @param  string $path
55
     *
56
     * @return string
57
     */
58
    public function configPath($path = '')
59
    {
60
        return BASE_PATH.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
61
    }
62
63
    /**
64
     * Get the path to the database directory.
65
     *
66
     * @param  string $path
67
     *
68
     * @return string
69
     */
70
    public function databasePath($path = '')
71
    {
72
        return (BASE_PATH.DIRECTORY_SEPARATOR.'database').($path ? DIRECTORY_SEPARATOR.$path : $path);
73
    }
74
75
    /**
76
     * Get the path to the language files.
77
     *
78
     * @return string
79
     */
80
    public function langPath()
81
    {
82
        return $this->resourcePath('lang');
83
    }
84
85
    /**
86
     * Get the path to the resources directory.
87
     *
88
     * @param  string $path
89
     *
90
     * @return string
91
     */
92
    public function resourcePath($path = '')
93
    {
94
        return BASE_PATH.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path);
95
    }
96
97
    /**
98
     * Get the path to the storage directory.
99
     *
100
     * @return string
101
     */
102
    public function storagePath()
103
    {
104
        return BASE_PATH.DIRECTORY_SEPARATOR.'storage';
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function environment()
111
    {
112
        return config('app.production') ? 'production' : 'development';
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function runningInConsole()
119
    {
120
        return true;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getNamespace()
127
    {
128
        if (! is_null($this->namespace)) {
129
            return $this->namespace;
130
        }
131
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
132
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
133
            foreach ((array) $path as $pathChoice) {
134
                if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) {
135
                    return $this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace can also be of type integer. However, the property $namespace is declared as type string. 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...
136
                }
137
            }
138
        }
139
        throw new RuntimeException('Unable to detect application namespace.');
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function isDownForMaintenance()
146
    {
147
        return false;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function registerConfiguredProviders()
154
    {
155
        throw new NotImplementedException;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function register($provider, $options = [], $force = false)
162
    {
163
        throw new NotImplementedException;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function registerDeferredProvider($provider, $service = null)
170
    {
171
        throw new NotImplementedException;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function boot()
178
    {
179
        throw new NotImplementedException;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function booting($callback)
186
    {
187
        throw new NotImplementedException;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function booted($callback)
194
    {
195
        throw new NotImplementedException;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function getCachedServicesPath()
202
    {
203
        throw new NotImplementedException;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getCachedPackagesPath()
210
    {
211
        throw new NotImplementedException;
212
    }
213
214
    /**
215
     * Define a callback to be used to configure Monolog.
216
     *
217
     * @param  callable $callback
218
     *
219
     * @return $this
220
     */
221
    public function configureMonologUsing(callable $callback)
222
    {
223
        $this->monologConfigurator = $callback;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Determine if the application has a custom Monolog configurator.
230
     *
231
     * @return bool
232
     */
233
    public function hasMonologConfigurator()
234
    {
235
        return ! is_null($this->monologConfigurator);
236
    }
237
238
    /**
239
     * Get the custom Monolog configurator for the application.
240
     *
241
     * @return callable
242
     */
243
    public function getMonologConfigurator()
244
    {
245
        return $this->monologConfigurator;
246
    }
247
}
248