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 ( 2d5b01...397d83 )
by Aden
02:55
created

Flare::getShow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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
            'notifications' => true,
37
            'version' => true,
38
        ]
39
    ];
40
41
    /**
42
     * Flare Configuration
43
     * 
44
     * @var array
45
     */
46
    protected $config;
47
48
    /**
49
     * The Title of the Admin Panel
50
     * 
51
     * @var string
52
     */
53
    protected $adminTitle;
54
55
    /**
56
     * Safe Title of the Admin Panel
57
     * 
58
     * @var string
59
     */
60
    protected $safeAdminTitle;
61
62
    /**
63
     * Relative Base URL of Admin Panel
64
     * 
65
     * @var string
66
     */
67
    protected $relativeAdminUrl;
68
69
    /**
70
     * __construct.
71
     */
72
    public function __construct()
73
    {
74
        $this->setLoadedConfig();
75
    }
76
77
    /**
78
     * Returns a Flare configuration value(s).
79
     * 
80
     * @param string $key
81
     * 
82
     * @return mixed
83
     */
84
    public function config($key)
85
    {
86
        return $this->getConfig($key);
87
    }
88
89
    /**
90
     * Returns a Flare configuration value(s), falling back 
91
     * to the defined bare-minimum configuration defaults 
92
     * if, for whatever reason the config is undefined.
93
     * 
94
     * @param string $key
95
     * 
96
     * @return mixed
97
     */
98
    public function getConfig($key)
99
    {
100
        if (array_key_exists($key, $this->config)) {
101
            return $this->config[$key];
102
        }
103
104
        return config('flare.'.$key);
105
    }
106
107
    /**
108
     * Allow setting of the Flare config at runtime.
109
     *
110
     * @return void
111
     */
112
    public function setConfig()
113
    {
114
115
    }
116
117
    /**
118
     * Set the loaded config to the protected property.
119
     *
120
     * @return void
121
     */
122
    public function setLoadedConfig()
123
    {
124
        $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...
125
    }
126
127
    /**
128
     * @return string
129
     * 
130
     * @deprecated 0.9 Use getAdminTitle() instead.
131
     */
132
    public function adminTitle()
133
    {
134
        return $this->getAdminTitle();
135
    }
136
137
    /**
138
     * Returns the defined Admin Title.
139
     *
140
     * @return string
141
     */
142
    public function getAdminTitle()
143
    {
144
        return $this->adminTitle ? $this->adminTitle : \Flare::config('admin_title');
145
    }
146
147
    /**
148
     * Sets the Admin Title
149
     * 
150
     * @param mixed $title
151
     *
152
     * @return void
153
     */
154
    public function setAdminTitle($title = null)
155
    {
156
        $this->adminTitle = $title;
157
    }
158
159
    /**
160
     * @return string
161
     * 
162
     * @deprecated 0.9 Use getSafeAdminTitle() instead.
163
     */
164
    public function safeAdminTitle()
165
    {
166
        return $this->getSafeAdminTitle();
167
    }
168
169
    /**
170
     * Returns the defined Admin Title, converted
171
     * to a safer format (for <title> tags etc.).
172
     * 
173
     * @return string
174
     */
175
    public function getSafeAdminTitle()
176
    {
177
        return $this->safeAdminTitle ? $this->adminTitle : strip_tags(\Flare::config('admin_title'));
178
    }
179
180
    /**
181
     * Sets the Safe Admin Title which is used 
182
     * in <title> tags etc.
183
     *
184
     * @param mixed $title
185
     * 
186
     * @return void
187
     */
188
    public function setSafeAdminTitle($title = null)
189
    {
190
        $this->safeAdminTitle = $title;
191
    }
192
193
    /**
194
     * Returns URL to a path in the Admin Panel, using the 
195
     * Admin URL defined in the Flare Config.
196
     * 
197
     * @param string $path
198
     * 
199
     * @return string
200
     */
201
    public function adminUrl($path = '')
202
    {
203
        return url($this->getRelativeAdminUrl($path));
0 ignored issues
show
Unused Code introduced by
The call to Flare::getRelativeAdminUrl() has too many arguments starting with $path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
204
    }
205
206
    /**
207
     * Returns URL to a path in the Admin Panel, using the 
208
     * Admin URL defined in the Flare Config.
209
     * 
210
     * @param string $path
211
     * 
212
     * @return string
213
     */
214
    public function relativeAdminUrl($path = '')
215
    {
216
        return rtrim($this->getRelativeAdminUrl().'/'.$path, '/');
217
    }
218
219
    /**
220
     * Returns URL to a path in the Admin Panel, using the 
221
     * Admin URL defined in the Flare Config.
222
     * 
223
     * @return string
224
     */
225
    public function getRelativeAdminUrl()
226
    {
227
        return $this->relativeAdminUrl ? $this->relativeAdminUrl : \Flare::config('admin_url');
228
    }
229
230
    /**
231
     * Set the Flare Relative Admin URL.
232
     *
233
     * If the provided path is null the relative path provided
234
     * with the getRelativeAdminUrl() method will return the
235
     * configuration file default (or the Flare fallbacks).
236
     * 
237
     * @param mixed $path
238
     */
239
    public function setRelativeAdminUrl($path = null)
240
    {
241
        $this->relativeAdminUrl = $path;
242
    }
243
244
    /**
245
     * Returns URL to a path in the Flare Documentation.
246
     * This is COMING SOON!
247
     * 
248
     * @param string $path
249
     * 
250
     * @return string
251
     */
252
    public function docsUrl($path = '')
253
    {
254
        return url('#'.$path);
255
    }
256
257
    /**
258
     * Determines whether part of the Flare Admin Panel
259
     * should be displayed or not and returns true / false.
260
     * 
261
     * @param  string $key
262
     * 
263
     * @return boolean
264
     */
265
    public function show($key = false)
266
    {
267
        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...
268
            return false;
269
        }
270
271
        return $this->getShow($key);
272
    }
273
274
    /**
275
     * Determines whether part of the Flare Admin Panel
276
     * should be displayed or not and returns true / false.
277
     *
278
     * Accessor for getShow().
279
     * 
280
     * @param  string $key
281
     * 
282
     * @return boolean
283
     */
284
    public function getShow($key = false)
285
    {
286
        if (array_key_exists($key, $showConfig = $this->getConfig('show'))) {
287
            return $showConfig[$key];
288
        }
289
    }
290
291
    /**
292
     * Returns the current Flare Version.
293
     * 
294
     * @return string
295
     */
296
    public function version()
297
    {
298
        return self::VERSION;
299
    }
300
301
    /**
302
     * Returns an array of all of the Available Attribute Types.
303
     * 
304
     * @return array
305
     */
306
    protected function availableAttributes()
307
    {
308
        $availableAttributes = [];
309
310
        foreach (\Flare::config('attributes') as $attributeFullClassname) {
311
            $availableAttributes = array_add(
312
                                            $availableAttributes,
313
                                            class_basename($attributeFullClassname),
314
                                            $attributeFullClassname
315
                                        );
316
        }
317
318
        return $availableAttributes;
319
    }
320
321
    /**
322
     * Determines if an AttributeType class exists or not.
323
     * 
324
     * @param string $type
325
     * 
326
     * @return bool
327
     */
328
    protected function attributeTypeExists($type)
329
    {
330
        return $this->resolveAttributeClass($type) ? true : false;
331
    }
332
333
    /**
334
     * Render Attribute.
335
     *
336
     * @param string $action
337
     * @param string $attribute
338
     * @param string $field
339
     * @param string $model
340
     * @param string $modelManager
341
     *
342
     * @return \Illuminate\Http\Response
343
     */
344
    public function renderAttribute($action, $attribute, $field, $model, $modelManager)
345
    {
346
        if (!isset($field['type'])) {
347
            throw new \Exception('Attribute Field Type cannot be empty or undefined.');
348
        }
349
350
        if ($this->attributeTypeExists($field['type'])) {
351
            $fieldType = $this->resolveAttributeClass($field['type']);
352
353
            return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
354
        }
355
356
        return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
357
    }
358
359
    /**
360
     * Resolves the Class of an Attribute and returns it as a string.
361
     * 
362
     * @param string $type
363
     * 
364
     * @return string
365
     */
366
    protected function resolveAttributeClass($type)
367
    {
368
        $fullClassname = array_key_exists(title_case($type).'Attribute', $this->availableAttributes()) ? $this->availableAttributes()[title_case($type).'Attribute'] : false;
369
370
        if (!$fullClassname || !class_exists($fullClassname)) {
371
            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...
372
        }
373
374
        return $fullClassname;
375
    }
376
}
377