Passed
Push — master ( 9d7fc3...820a38 )
by Mihail
10:49
created

FrontWidget   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 3 Features 1
Metric Value
c 3
b 3
f 1
dl 0
loc 45
rs 10
wmc 5
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A widget() 0 9 2
A enabled() 0 18 3
1
<?php
2
namespace Extend\Core\Arch;
3
4
use Ffcms\Core\App;
5
use Ffcms\Core\Arch\Widget as NativeWidget;
6
use Apps\ActiveRecord\App as AppRecord;
7
use Ffcms\Core\Helper\Type\Str;
8
9
/**
10
 * Class FrontWidget. Special controller type for front widgets.
11
 * @package Extend\Core\Arch
12
 */
13
class FrontWidget extends NativeWidget
14
{
15
    public static $name;
16
17
    /**
18
     * Display widget compiled data.
19
     * @param array|null $params
20
     * @return null|string
21
     * @throws \Exception
22
     */
23
    public static function widget(array $params = null)
24
    {
25
        if (!self::enabled()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::enabled() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
26
            return null;
27
        }
28
29
        // call parent method
30
        return parent::widget($params);
31
    }
32
33
    /**
34
     * Check if widget is enabled
35
     * @param string|null $name
36
     * @return bool
37
     */
38
    public static function enabled($name = null)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        // get widget class-namespace callback and single class name
41
        self::$class = get_called_class();
42
        self::$name = Str::lastIn(self::$class, '\\', true);
43
44
        $wData = AppRecord::getItem('widget', self::$name);
0 ignored issues
show
Security Bug introduced by
It seems like self::$name can also be of type false; however, Apps\ActiveRecord\App::getItem() does only seem to accept string|array, did you maybe forget to handle an error condition?
Loading history...
45
        // widget is not founded, deny run
46
        if ($wData === null) {
47
            if (App::$Debug !== null) {
48
                App::$Debug->addMessage(__('Widget with name %name%[%class%] is not found', ['name' => self::$name, 'class' => self::$class]));
49
            }
50
            return null;
51
        }
52
53
        // if widget is disabled - lets return nothing
54
        return !(bool)$wData->disabled;
55
    }
56
57
}