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 ( 397d83...af0ff8 )
by Aden
03:32
created

Flare   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 389
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 1

Importance

Changes 9
Bugs 3 Features 2
Metric Value
wmc 37
c 9
b 3
f 2
lcom 4
cbo 1
dl 0
loc 389
rs 8.6

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A admin() 0 4 1
A config() 0 4 1
A getConfig() 0 8 2
A setConfig() 0 4 1
A setLoadedConfig() 0 4 1
A adminTitle() 0 4 1
A getAdminTitle() 0 4 2
A setAdminTitle() 0 4 1
A safeAdminTitle() 0 4 1
A getSafeAdminTitle() 0 4 2
A setSafeAdminTitle() 0 4 1
A adminUrl() 0 4 1
A relativeAdminUrl() 0 4 1
A getRelativeAdminUrl() 0 4 2
A setRelativeAdminUrl() 0 4 1
A docsUrl() 0 4 1
A show() 0 8 2
A getShow() 0 6 2
A version() 0 4 1
A availableAttributes() 0 14 2
A attributeTypeExists() 0 4 2
A renderAttribute() 0 14 3
A resolveAttributeClass() 0 10 4
1
<?php
2
3
namespace LaravelFlare\Flare;
4
5
use Illuminate\Routing\Router;
6
use LaravelFlare\Flare\Admin\AdminManager;
7
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute;
8
9
class Flare
10
{
11
    /**
12
     * The Flare version.
13
     *
14
     * @var string
15
     */
16
    const VERSION = '0.9.x-dev';
17
18
    /**
19
     * Array of expected configuration keys
20
     * with the absolute bare-minimum defaults.
21
     * 
22
     * @var array
23
     */
24
    protected $configurationKeys = [
25
        'admin_title' => 'Laravel Flare',
26
        'admin_url' => 'admin',
27
        'admin_theme' => 'red',
28
        'admin' => [],
29
        'attributes' => [],
30
        'models' => [],
31
        'modules' => [],
32
        'widgets' => [],
33
        'permissions' => \LaravelFlare\Flare\Permissions\Permissions::class,
34
        'policies' => [],
35
        'show' => [
36
            'github' => true,
37
            'login' => true,
38
            'notifications' => true,
39
            'version' => true,
40
        ]
41
    ];
42
43
    /**
44
     * Admin Manager
45
     * 
46
     * @var \LaravelFlare\Flare\Admin\AdminManager
47
     */
48
    protected $admin;
49
50
    /**
51
     * Flare Configuration
52
     * 
53
     * @var array
54
     */
55
    protected $config;
56
57
    /**
58
     * The Title of the Admin Panel
59
     * 
60
     * @var string
61
     */
62
    protected $adminTitle;
63
64
    /**
65
     * Safe Title of the Admin Panel
66
     * 
67
     * @var string
68
     */
69
    protected $safeAdminTitle;
70
71
    /**
72
     * Relative Base URL of Admin Panel
73
     * 
74
     * @var string
75
     */
76
    protected $relativeAdminUrl;
77
78
    /**
79
     * __construct.
80
     */
81
    public function __construct(AdminManager $adminManager)
82
    {
83
        $this->setLoadedConfig();
84
85
        $this->admin = $adminManager;
86
    }
87
88
    /**
89
     * Returns the instance of the Admin Manager
90
     * 
91
     * @return \LaravelFlare\Flare\Admin\AdminManager
92
     */
93
    public function admin()
94
    {
95
        return $this->admin;
96
    }
97
98
    /**
99
     * Returns a Flare configuration value(s).
100
     * 
101
     * @param string $key
102
     * 
103
     * @return mixed
104
     */
105
    public function config($key)
106
    {
107
        return $this->getConfig($key);
108
    }
109
110
    /**
111
     * Returns a Flare configuration value(s), falling back 
112
     * to the defined bare-minimum configuration defaults 
113
     * if, for whatever reason the config is undefined.
114
     * 
115
     * @param string $key
116
     * 
117
     * @return mixed
118
     */
119
    public function getConfig($key)
120
    {
121
        if (array_key_exists($key, $this->config)) {
122
            return $this->config[$key];
123
        }
124
125
        return config('flare.'.$key);
126
    }
127
128
    /**
129
     * Allow setting of the Flare config at runtime.
130
     *
131
     * @return void
132
     */
133
    public function setConfig()
134
    {
135
136
    }
137
138
    /**
139
     * Set the loaded config to the protected property.
140
     *
141
     * @return void
142
     */
143
    public function setLoadedConfig()
144
    {
145
        $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...
146
    }
147
148
    /**
149
     * @return string
150
     * 
151
     * @deprecated 0.9 Use getAdminTitle() instead.
152
     */
153
    public function adminTitle()
154
    {
155
        return $this->getAdminTitle();
156
    }
157
158
    /**
159
     * Returns the defined Admin Title.
160
     *
161
     * @return string
162
     */
163
    public function getAdminTitle()
164
    {
165
        return $this->adminTitle ? $this->adminTitle : \Flare::config('admin_title');
166
    }
167
168
    /**
169
     * Sets the Admin Title
170
     * 
171
     * @param mixed $title
172
     *
173
     * @return void
174
     */
175
    public function setAdminTitle($title = null)
176
    {
177
        $this->adminTitle = $title;
178
    }
179
180
    /**
181
     * @return string
182
     * 
183
     * @deprecated 0.9 Use getSafeAdminTitle() instead.
184
     */
185
    public function safeAdminTitle()
186
    {
187
        return $this->getSafeAdminTitle();
188
    }
189
190
    /**
191
     * Returns the defined Admin Title, converted
192
     * to a safer format (for <title> tags etc.).
193
     * 
194
     * @return string
195
     */
196
    public function getSafeAdminTitle()
197
    {
198
        return $this->safeAdminTitle ? $this->adminTitle : strip_tags(\Flare::config('admin_title'));
199
    }
200
201
    /**
202
     * Sets the Safe Admin Title which is used 
203
     * in <title> tags etc.
204
     *
205
     * @param mixed $title
206
     * 
207
     * @return void
208
     */
209
    public function setSafeAdminTitle($title = null)
210
    {
211
        $this->safeAdminTitle = $title;
212
    }
213
214
    /**
215
     * Returns URL to a path in the Admin Panel, using the 
216
     * Admin URL defined in the Flare Config.
217
     * 
218
     * @param string $path
219
     * 
220
     * @return string
221
     */
222
    public function adminUrl($path = '')
223
    {
224
        return url($this->relativeAdminUrl($path));
225
    }
226
227
    /**
228
     * Returns URL to a path in the Admin Panel, using the 
229
     * Admin URL defined in the Flare Config.
230
     * 
231
     * @param string $path
232
     * 
233
     * @return string
234
     */
235
    public function relativeAdminUrl($path = '')
236
    {
237
        return rtrim($this->getRelativeAdminUrl().'/'.$path, '/');
238
    }
239
240
    /**
241
     * Returns URL to a path in the Admin Panel, using the 
242
     * Admin URL defined in the Flare Config.
243
     * 
244
     * @return string
245
     */
246
    public function getRelativeAdminUrl()
247
    {
248
        return $this->relativeAdminUrl ? $this->relativeAdminUrl : \Flare::config('admin_url');
249
    }
250
251
    /**
252
     * Set the Flare Relative Admin URL.
253
     *
254
     * If the provided path is null the relative path provided
255
     * with the getRelativeAdminUrl() method will return the
256
     * configuration file default (or the Flare fallbacks).
257
     * 
258
     * @param mixed $path
259
     */
260
    public function setRelativeAdminUrl($path = null)
261
    {
262
        $this->relativeAdminUrl = $path;
263
    }
264
265
    /**
266
     * Returns URL to a path in the Flare Documentation.
267
     * This is COMING SOON!
268
     * 
269
     * @param string $path
270
     * 
271
     * @return string
272
     */
273
    public function docsUrl($path = '')
274
    {
275
        return url('#'.$path);
276
    }
277
278
    /**
279
     * Determines whether part of the Flare Admin Panel
280
     * should be displayed or not and returns true / false.
281
     * 
282
     * @param  string $key
283
     * 
284
     * @return boolean
285
     */
286
    public function show($key = false)
287
    {
288
        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...
289
            return false;
290
        }
291
292
        return $this->getShow($key);
293
    }
294
295
    /**
296
     * Determines whether part of the Flare Admin Panel
297
     * should be displayed or not and returns true / false.
298
     *
299
     * Accessor for getShow().
300
     * 
301
     * @param  string $key
302
     * 
303
     * @return boolean
304
     */
305
    public function getShow($key = false)
306
    {
307
        if (array_key_exists($key, $showConfig = $this->getConfig('show'))) {
308
            return $showConfig[$key];
309
        }
310
    }
311
312
    /**
313
     * Returns the current Flare Version.
314
     * 
315
     * @return string
316
     */
317
    public function version()
318
    {
319
        return self::VERSION;
320
    }
321
322
    /**
323
     * Returns an array of all of the Available Attribute Types.
324
     * 
325
     * @return array
326
     */
327
    protected function availableAttributes()
328
    {
329
        $availableAttributes = [];
330
331
        foreach (\Flare::config('attributes') as $attributeFullClassname) {
332
            $availableAttributes = array_add(
333
                                            $availableAttributes,
334
                                            class_basename($attributeFullClassname),
335
                                            $attributeFullClassname
336
                                        );
337
        }
338
339
        return $availableAttributes;
340
    }
341
342
    /**
343
     * Determines if an AttributeType class exists or not.
344
     * 
345
     * @param string $type
346
     * 
347
     * @return bool
348
     */
349
    protected function attributeTypeExists($type)
350
    {
351
        return $this->resolveAttributeClass($type) ? true : false;
352
    }
353
354
    /**
355
     * Render Attribute.
356
     *
357
     * @param string $action
358
     * @param string $attribute
359
     * @param string $field
360
     * @param string $model
361
     * @param string $modelManager
362
     *
363
     * @return \Illuminate\Http\Response
364
     */
365
    public function renderAttribute($action, $attribute, $field, $model, $modelManager)
366
    {
367
        if (!isset($field['type'])) {
368
            throw new \Exception('Attribute Field Type cannot be empty or undefined.');
369
        }
370
371
        if ($this->attributeTypeExists($field['type'])) {
372
            $fieldType = $this->resolveAttributeClass($field['type']);
373
374
            return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
375
        }
376
377
        return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
378
    }
379
380
    /**
381
     * Resolves the Class of an Attribute and returns it as a string.
382
     * 
383
     * @param string $type
384
     * 
385
     * @return string
386
     */
387
    protected function resolveAttributeClass($type)
388
    {
389
        $fullClassname = array_key_exists(title_case($type).'Attribute', $this->availableAttributes()) ? $this->availableAttributes()[title_case($type).'Attribute'] : false;
390
391
        if (!$fullClassname || !class_exists($fullClassname)) {
392
            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...
393
        }
394
395
        return $fullClassname;
396
    }
397
}
398