GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( af0ff8...730a99 )
by Aden
03:03
created

Flare::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace LaravelFlare\Flare;
4
5
use Illuminate\Routing\Router;
6
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute;
7
8
class Flare
9
{
10
    /**
11
     * The Flare version.
12
     *
13
     * @var string
14
     */
15
    const VERSION = '0.9.x-dev';
16
17
    /**
18
     * Array of expected configuration keys
19
     * with the absolute bare-minimum defaults.
20
     * 
21
     * @var array
22
     */
23
    protected $configurationKeys = [
24
        'admin_title' => 'Laravel Flare',
25
        'admin_url' => 'admin',
26
        'admin_theme' => 'red',
27
        'admin' => [],
28
        'attributes' => [],
29
        'models' => [],
30
        'modules' => [],
31
        'widgets' => [],
32
        'permissions' => \LaravelFlare\Flare\Permissions\Permissions::class,
33
        'policies' => [],
34
        'show' => [
35
            'github' => true,
36
            'login' => true,
37
            'notifications' => true,
38
            'version' => true,
39
        ]
40
    ];
41
42
    /**
43
     * Array of Helper Methods.
44
     *
45
     * @var array
46
     */
47
    protected $helpers = [
48
        'admin' => \LaravelFlare\Flare\Admin\AdminManager::class,
49
    ];
50
51
    /**
52
     * Application Instance
53
     * 
54
     * @var \Illuminate\Foundation\Application
55
     */
56
    protected $app;
57
58
    /**
59
     * Flare Configuration
60
     * 
61
     * @var array
62
     */
63
    protected $config;
64
65
    /**
66
     * The Title of the Admin Panel
67
     * 
68
     * @var string
69
     */
70
    protected $adminTitle;
71
72
    /**
73
     * Safe Title of the Admin Panel
74
     * 
75
     * @var string
76
     */
77
    protected $safeAdminTitle;
78
79
    /**
80
     * Relative Base URL of Admin Panel
81
     * 
82
     * @var string
83
     */
84
    protected $relativeAdminUrl;
85
86
    /**
87
     * __construct.
88
     *
89
     * @param \Illuminate\Foundation\Application $app
90
     */
91
    public function __construct($app)
92
    {
93
        $this->app = $app;
94
95
        $this->setLoadedConfig();
96
    }
97
98
    /**
99
     * Returns the Application Instance.
100
     * 
101
     * @return mixed
102
     */
103
    public function app()
104
    {
105
        return $this->app;
106
    }
107
108
    /**
109
     * Returns a Flare configuration value(s).
110
     * 
111
     * @param string $key
112
     * 
113
     * @return mixed
114
     */
115
    public function config($key)
116
    {
117
        return $this->getConfig($key);
118
    }
119
120
    /**
121
     * Returns a Flare configuration value(s), falling back 
122
     * to the defined bare-minimum configuration defaults 
123
     * if, for whatever reason the config is undefined.
124
     * 
125
     * @param string $key
126
     * 
127
     * @return mixed
128
     */
129
    public function getConfig($key)
130
    {
131
        if (array_key_exists($key, $this->config)) {
132
            return $this->config[$key];
133
        }
134
135
        return config('flare.'.$key);
136
    }
137
138
    /**
139
     * Allow setting of the Flare config at runtime.
140
     *
141
     * @return void
142
     */
143
    public function setConfig()
144
    {
145
146
    }
147
148
    /**
149
     * Set the loaded config to the protected property.
150
     *
151
     * @return void
152
     */
153
    public function setLoadedConfig()
154
    {
155
        $this->config = config('flare.config');   
0 ignored issues
show
Documentation Bug introduced by
It seems like config('flare.config') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
156
    }
157
158
    /**
159
     * @return string
160
     * 
161
     * @deprecated 0.9 Use getAdminTitle() instead.
162
     */
163
    public function adminTitle()
164
    {
165
        return $this->getAdminTitle();
166
    }
167
168
    /**
169
     * Returns the defined Admin Title.
170
     *
171
     * @return string
172
     */
173
    public function getAdminTitle()
174
    {
175
        return $this->adminTitle ? $this->adminTitle : \Flare::config('admin_title');
176
    }
177
178
    /**
179
     * Sets the Admin Title
180
     * 
181
     * @param mixed $title
182
     *
183
     * @return void
184
     */
185
    public function setAdminTitle($title = null)
186
    {
187
        $this->adminTitle = $title;
188
    }
189
190
    /**
191
     * @return string
192
     * 
193
     * @deprecated 0.9 Use getSafeAdminTitle() instead.
194
     */
195
    public function safeAdminTitle()
196
    {
197
        return $this->getSafeAdminTitle();
198
    }
199
200
    /**
201
     * Returns the defined Admin Title, converted
202
     * to a safer format (for <title> tags etc.).
203
     * 
204
     * @return string
205
     */
206
    public function getSafeAdminTitle()
207
    {
208
        return $this->safeAdminTitle ? $this->adminTitle : strip_tags(\Flare::config('admin_title'));
209
    }
210
211
    /**
212
     * Sets the Safe Admin Title which is used 
213
     * in <title> tags etc.
214
     *
215
     * @param mixed $title
216
     * 
217
     * @return void
218
     */
219
    public function setSafeAdminTitle($title = null)
220
    {
221
        $this->safeAdminTitle = $title;
222
    }
223
224
    /**
225
     * Returns URL to a path in the Admin Panel, using the 
226
     * Admin URL defined in the Flare Config.
227
     * 
228
     * @param string $path
229
     * 
230
     * @return string
231
     */
232
    public function adminUrl($path = '')
233
    {
234
        return url($this->relativeAdminUrl($path));
235
    }
236
237
    /**
238
     * Returns URL to a path in the Admin Panel, using the 
239
     * Admin URL defined in the Flare Config.
240
     * 
241
     * @param string $path
242
     * 
243
     * @return string
244
     */
245
    public function relativeAdminUrl($path = '')
246
    {
247
        return rtrim($this->getRelativeAdminUrl().'/'.$path, '/');
248
    }
249
250
    /**
251
     * Returns URL to a path in the Admin Panel, using the 
252
     * Admin URL defined in the Flare Config.
253
     * 
254
     * @return string
255
     */
256
    public function getRelativeAdminUrl()
257
    {
258
        return $this->relativeAdminUrl ? $this->relativeAdminUrl : \Flare::config('admin_url');
259
    }
260
261
    /**
262
     * Set the Flare Relative Admin URL.
263
     *
264
     * If the provided path is null the relative path provided
265
     * with the getRelativeAdminUrl() method will return the
266
     * configuration file default (or the Flare fallbacks).
267
     * 
268
     * @param mixed $path
269
     */
270
    public function setRelativeAdminUrl($path = null)
271
    {
272
        $this->relativeAdminUrl = $path;
273
    }
274
275
    /**
276
     * Returns URL to a path in the Flare Documentation.
277
     * This is COMING SOON!
278
     * 
279
     * @param string $path
280
     * 
281
     * @return string
282
     */
283
    public function docsUrl($path = '')
284
    {
285
        return url('#'.$path);
286
    }
287
288
    /**
289
     * Determines whether part of the Flare Admin Panel
290
     * should be displayed or not and returns true / false.
291
     * 
292
     * @param  string $key
293
     * 
294
     * @return boolean
295
     */
296
    public function show($key = false)
297
    {
298
        if (!$key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false 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...
299
            return false;
300
        }
301
302
        return $this->getShow($key);
303
    }
304
305
    /**
306
     * Determines whether part of the Flare Admin Panel
307
     * should be displayed or not and returns true / false.
308
     *
309
     * Accessor for getShow().
310
     * 
311
     * @param  string $key
312
     * 
313
     * @return boolean
314
     */
315
    public function getShow($key = false)
316
    {
317
        if (array_key_exists($key, $showConfig = $this->getConfig('show'))) {
318
            return $showConfig[$key];
319
        }
320
    }
321
322
    /**
323
     * Returns the current Flare Version.
324
     * 
325
     * @return string
326
     */
327
    public function version()
328
    {
329
        return self::VERSION;
330
    }
331
332
    /**
333
     * Returns an array of all of the Available Attribute Types.
334
     * 
335
     * @return array
336
     */
337
    protected function availableAttributes()
338
    {
339
        $availableAttributes = [];
340
341
        foreach (\Flare::config('attributes') as $attributeFullClassname) {
342
            $availableAttributes = array_add(
343
                                            $availableAttributes,
344
                                            class_basename($attributeFullClassname),
345
                                            $attributeFullClassname
346
                                        );
347
        }
348
349
        return $availableAttributes;
350
    }
351
352
    /**
353
     * Determines if an AttributeType class exists or not.
354
     * 
355
     * @param string $type
356
     * 
357
     * @return bool
358
     */
359
    protected function attributeTypeExists($type)
360
    {
361
        return $this->resolveAttributeClass($type) ? true : false;
362
    }
363
364
    /**
365
     * Render Attribute.
366
     *
367
     * @param string $action
368
     * @param string $attribute
369
     * @param string $field
370
     * @param string $model
371
     * @param string $modelManager
372
     *
373
     * @return \Illuminate\Http\Response
374
     */
375
    public function renderAttribute($action, $attribute, $field, $model, $modelManager)
376
    {
377
        if (!isset($field['type'])) {
378
            throw new \Exception('Attribute Field Type cannot be empty or undefined.');
379
        }
380
381
        if ($this->attributeTypeExists($field['type'])) {
382
            $fieldType = $this->resolveAttributeClass($field['type']);
383
384
            return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
385
        }
386
387
        return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
388
    }
389
390
    /**
391
     * Resolves the Class of an Attribute and returns it as a string.
392
     * 
393
     * @param string $type
394
     * 
395
     * @return string
396
     */
397
    protected function resolveAttributeClass($type)
398
    {
399
        $fullClassname = array_key_exists(title_case($type).'Attribute', $this->availableAttributes()) ? $this->availableAttributes()[title_case($type).'Attribute'] : false;
400
401
        if (!$fullClassname || !class_exists($fullClassname)) {
402
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by LaravelFlare\Flare\Flare::resolveAttributeClass of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
403
        }
404
405
        return $fullClassname;
406
    }
407
408
    /**
409
     * Call a Helper Method.
410
     * 
411
     * @param string $method
412
     * @param mixed  $parameters
413
     * 
414
     * @return mixed
415
     */
416
    protected function callHelperMethod($method, $parameters)
417
    {
418
        return $this->app->make($this->helpers[$method], $parameters);
419
    }
420
421
    /**
422
     * Provide access to Helper Methods.
423
     *
424
     * This provides an extensible way of adding helper classes
425
     * which are registerable and available to adccess through
426
     * the Flare Facade.
427
     *
428
     * @param string $method
429
     * @param array  $parameters
430
     * 
431
     * @return mixed
432
     */
433
    public function __call($method, $parameters)
434
    {
435
        if (array_key_exists($method, $this->helpers)) {
436
            return $this->callHelperMethod($method, $parameters);
437
        }
438
439
        return call_user_func_array([$this, $method], $parameters);
440
    }
441
}
442