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.
Passed
Push — master ( cf9260...b52382 )
by
unknown
03:19
created

Module   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 461
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 461
rs 8.5599
c 0
b 0
f 0
wmc 48

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setSegments() 0 5 2
A getParentSegments() 0 7 2
A getProperties() 0 3 1
A __construct() 0 5 1
A getChecksum() 0 3 1
A getSegments() 0 7 2
A getCode() 0 3 1
A setParentSegments() 0 5 2
A getThemes() 0 12 3
A getDefaultControllerClassName() 0 3 1
A setProperties() 0 11 2
A getPresets() 0 3 1
A setPresets() 0 5 1
A getNamespace() 0 3 1
A setNamespace() 0 5 1
A getDir() 0 10 3
A getResourcesDir() 0 19 4
A hasTheme() 0 15 5
A getPublicDir() 0 3 1
A loadModel() 0 6 2
A getParameter() 0 3 1
A getType() 0 3 1
B getTheme() 0 23 7
A setType() 0 5 1
A getThemesDir() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Module often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Module, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Containers\Modules\DataStructures;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Spl\DataStructures\SplArrayObject;
19
use O2System\Spl\Info\SplDirectoryInfo;
20
21
/**
22
 * Class Module
23
 *
24
 * @package O2System\Framework\Containers\Modules\DataStructures
25
 */
26
class Module extends SplDirectoryInfo
27
{
28
    /**
29
     * Module::$type
30
     *
31
     * @var string
32
     */
33
    protected $type = 'MODULE';
34
35
    /**
36
     * Module::$namespace
37
     *
38
     * @var string
39
     */
40
    protected $namespace;
41
42
    /**
43
     * Module::$segments
44
     *
45
     * @var string
46
     */
47
    protected $segments;
48
49
    /**
50
     * Module::$parentSegments
51
     *
52
     * @var string
53
     */
54
    protected $parentSegments;
55
56
    /**
57
     * Module::$properties
58
     *
59
     * @var array
60
     */
61
    protected $properties = [];
62
63
    /**
64
     * Module::$presets
65
     *
66
     * @var array
67
     */
68
    protected $presets = [];
69
70
    // ------------------------------------------------------------------------
71
72
    /**
73
     * Module::__construct
74
     *
75
     * @param string $dir
76
     */
77
    public function __construct($dir)
78
    {
79
        parent::__construct($dir);
80
81
        $this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false);
0 ignored issues
show
Bug introduced by
The constant O2System\Framework\Conta...ataStructures\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
82
    }
83
84
    // ------------------------------------------------------------------------
85
86
    /**
87
     * Module::getSegments
88
     *
89
     * @param bool $returnArray
90
     *
91
     * @return array|string
92
     */
93
    public function getSegments($returnArray = true)
94
    {
95
        if ($returnArray) {
96
            return explode('/', $this->segments);
97
        }
98
99
        return $this->segments;
100
    }
101
102
    // ------------------------------------------------------------------------
103
104
    /**
105
     * Module::setSegments
106
     *
107
     * @param array|string $segments
108
     *
109
     * @return static
110
     */
111
    public function setSegments($segments)
112
    {
113
        $this->segments = is_array($segments) ? implode('/', $segments) : $segments;
114
115
        return $this;
116
    }
117
118
    // ------------------------------------------------------------------------
119
120
    /**
121
     * Module::getParentSegments
122
     *
123
     * @param bool $returnArray
124
     *
125
     * @return array|string
126
     */
127
    public function getParentSegments($returnArray = true)
128
    {
129
        if ($returnArray) {
130
            return explode('/', $this->parentSegments);
131
        }
132
133
        return $this->parentSegments;
134
    }
135
136
    // ------------------------------------------------------------------------
137
138
    /**
139
     * Module::setParentSegments
140
     *
141
     * @param array|string $parentSegments
142
     *
143
     * @return static
144
     */
145
    public function setParentSegments($parentSegments)
146
    {
147
        $this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments;
148
149
        return $this;
150
    }
151
152
    // ------------------------------------------------------------------------
153
154
    /**
155
     * Module::getCode
156
     *
157
     * @return string
158
     */
159
    public function getCode()
160
    {
161
        return strtoupper(substr(md5(spl_object_hash($this)), 2, 7));
162
    }
163
164
    // ------------------------------------------------------------------------
165
166
    /**
167
     * Module::getChecksum
168
     *
169
     * @return string
170
     */
171
    public function getChecksum()
172
    {
173
        return md5($this->getMTime());
174
    }
175
176
    // ------------------------------------------------------------------------
177
178
    /**
179
     * Module::getProperties
180
     *
181
     * @return \O2System\Spl\DataStructures\SplArrayObject
182
     */
183
    public function getProperties()
184
    {
185
        return new SplArrayObject($this->properties);
186
    }
187
188
    // ------------------------------------------------------------------------
189
190
    /**
191
     * Module::setProperties
192
     *
193
     * @param array $properties
194
     *
195
     * @return static
196
     */
197
    public function setProperties(array $properties)
198
    {
199
        if (isset($properties[ 'presets' ])) {
200
            $this->setPresets($properties[ 'presets' ]);
201
202
            unset($properties[ 'presets' ]);
203
        }
204
205
        $this->properties = $properties;
206
207
        return $this;
208
    }
209
210
    // ------------------------------------------------------------------------
211
212
    /**
213
     * Module::getPresets
214
     *
215
     * @return \O2System\Spl\DataStructures\SplArrayObject
216
     */
217
    public function getPresets()
218
    {
219
        return new SplArrayObject($this->presets);
220
    }
221
222
    // ------------------------------------------------------------------------
223
224
    /**
225
     * Module::setPresets
226
     *
227
     * @param array $presets
228
     *
229
     * @return static
230
     */
231
    public function setPresets(array $presets)
232
    {
233
        $this->presets = $presets;
234
235
        return $this;
236
    }
237
238
    // ------------------------------------------------------------------------
239
240
    /**
241
     * Module::getDefaultControllerClassName
242
     *
243
     * @return string
244
     */
245
    public function getDefaultControllerClassName()
246
    {
247
        return $this->getNamespace() . 'Controllers\\' . $this->getDirName();
248
    }
249
250
    // ------------------------------------------------------------------------
251
252
    /**
253
     * Module::getNamespace
254
     *
255
     * @return string
256
     */
257
    public function getNamespace()
258
    {
259
        return $this->namespace;
260
    }
261
262
    // ------------------------------------------------------------------------
263
264
    /**
265
     * Module::setNamespace
266
     *
267
     * @param string $namespace
268
     *
269
     * @return static
270
     */
271
    public function setNamespace($namespace)
272
    {
273
        $this->namespace = trim($namespace, '\\') . '\\';
274
275
        return $this;
276
    }
277
278
    // ------------------------------------------------------------------------
279
280
    /**
281
     * Module::getThemes
282
     *
283
     * @return array
284
     */
285
    public function getThemes()
286
    {
287
        $directory = new SplDirectoryInfo($this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR);
288
289
        $themes = [];
290
        foreach ($directory->getTree() as $themeName => $themeTree) {
291
            if (($theme = $this->getTheme($themeName)) instanceof Module\Theme) {
292
                $themes[ $themeName ] = $theme;
293
            }
294
        }
295
296
        return $themes;
297
    }
298
299
    // ------------------------------------------------------------------------
300
301
    /**
302
     * Module::getResourcesDir
303
     *
304
     * @param string|null $subDir
305
     *
306
     * @return bool|string
307
     */
308
    public function getResourcesDir($subDir = null)
309
    {
310
        $dirResources = PATH_RESOURCES;
311
312
        $dirPath = str_replace(PATH_APP, '', $this->getRealPath());
313
        $dirPathParts = explode(DIRECTORY_SEPARATOR, $dirPath);
314
315
        if(count($dirPathParts)) {
316
            $dirPathParts = array_map('dash', $dirPathParts);
317
            $dirResources.= implode(DIRECTORY_SEPARATOR, $dirPathParts);
318
        }
319
320
        if (is_null($subDir)) {
321
            return $dirResources;
322
        } elseif (is_dir($dirResources . $subDir . DIRECTORY_SEPARATOR)) {
323
            return $dirResources . $subDir . DIRECTORY_SEPARATOR;
324
        }
325
326
        return false;
327
    }
328
329
    // ------------------------------------------------------------------------
330
331
    /**
332
     * Module::getTheme
333
     *
334
     * @param string $theme
335
     * @param bool   $failover
336
     *
337
     * @return bool|Module\Theme
338
     */
339
    public function getTheme($theme, $failover = true)
340
    {
341
        $theme = dash($theme);
342
343
        if ($failover === false) {
344
            if (is_dir($themePath = $this->getResourcesDir('themes') . $theme . DIRECTORY_SEPARATOR)) {
0 ignored issues
show
Bug introduced by
Are you sure $this->getResourcesDir('themes') of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

344
            if (is_dir($themePath = /** @scrutinizer ignore-type */ $this->getResourcesDir('themes') . $theme . DIRECTORY_SEPARATOR)) {
Loading history...
345
                $themeObject = new Module\Theme($themePath);
346
347
                if ($themeObject->isValid()) {
348
                    return $themeObject;
349
                }
350
            }
351
        } else {
352
            foreach (modules() as $module) {
353
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
354
                    continue;
355
                } elseif ($themeObject = $module->getTheme($theme, false)) {
356
                    return $themeObject;
357
                }
358
            }
359
        }
360
361
        return false;
362
    }
363
364
    // ------------------------------------------------------------------------
365
366
    /**
367
     * Module::getPublicDir
368
     *
369
     * @return string
370
     */
371
    public function getPublicDir()
372
    {
373
        return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath()));
374
    }
375
376
    // ------------------------------------------------------------------------
377
378
    /**
379
     * Module::getType
380
     *
381
     * @return string
382
     */
383
    public function getType()
384
    {
385
        return $this->type;
386
    }
387
388
    // ------------------------------------------------------------------------
389
390
    /**
391
     * Module::setType
392
     *
393
     * @param string $type
394
     *
395
     * @return static
396
     */
397
    public function setType($type)
398
    {
399
        $this->type = strtoupper($type);
400
401
        return $this;
402
    }
403
404
    // ------------------------------------------------------------------------
405
406
    /**
407
     * Module::getDir
408
     *
409
     * @param string $subDir
410
     * @param bool   $psrDir
411
     *
412
     * @return bool|string
413
     */
414
    public function getDir($subDir, $psrDir = false)
415
    {
416
        $subDir = $psrDir === true ? prepare_class_name($subDir) : $subDir;
417
        $subDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $subDir);
418
419
        if (is_dir($dirPath = $this->getRealPath() . $subDir)) {
420
            return $dirPath . DIRECTORY_SEPARATOR;
421
        }
422
423
        return false;
424
    }
425
426
    // ------------------------------------------------------------------------
427
428
    /**
429
     * Module::hasTheme
430
     *
431
     * @param string $theme
432
     *
433
     * @return bool
434
     */
435
    public function hasTheme($theme)
436
    {
437
        if (is_dir($this->getThemesDir() . $theme)) {
438
            return true;
439
        } else {
440
            foreach (modules() as $module) {
441
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
442
                    continue;
443
                } elseif (is_dir($module->getResourcesDir('themes') . $theme)) {
444
                    return true;
445
                }
446
            }
447
        }
448
449
        return false;
450
    }
451
452
    // ------------------------------------------------------------------------
453
454
    /**
455
     * Module::getThemesDir
456
     *
457
     * @return string
458
     */
459
    public function getThemesDir()
460
    {
461
        return $this->getResourcesDir('themes');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getResourcesDir('themes') could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
462
    }
463
464
    // ------------------------------------------------------------------------
465
466
    /**
467
     * Module::getParameter
468
     *
469
     * @return string
470
     */
471
    public function getParameter()
472
    {
473
        return snakecase($this->getDirName());
474
    }
475
476
    // ------------------------------------------------------------------------
477
478
    /**
479
     * Module::loadModel
480
     */
481
    public function loadModel()
482
    {
483
        $modelClassName = $this->namespace . 'Models\Base';
484
485
        if (class_exists($modelClassName)) {
486
            models()->load($modelClassName, strtolower($this->type));
0 ignored issues
show
Bug introduced by
The method load() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

486
            models()->/** @scrutinizer ignore-call */ load($modelClassName, strtolower($this->type));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method load() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

486
            models()->/** @scrutinizer ignore-call */ load($modelClassName, strtolower($this->type));
Loading history...
487
        }
488
    }
489
}