Completed
Push — master ( 2bf87e...513950 )
by Mihail
04:33
created

Widget::widget()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 30
rs 6.7272
cc 7
eloc 16
nc 7
nop 1
1
<?php
2
3
namespace Ffcms\Core\Arch;
4
5
use Apps\ActiveRecord\App as AppRecord;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Helper\Type\Obj;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Interfaces\iWidget;
10
use Ffcms\Core\Traits\DynamicGlobal;
11
12
/**
13
 * Class Widget. Provide constructor to work with widget-type based extensions for ffcms.
14
 * @package Ffcms\Core\Arch
15
 */
16
class Widget implements iWidget
17
{
18
    use DynamicGlobal;
19
20
    /** @var string|null */
21
    public static $class;
22
23
    public static function widget(array $params = null)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
24
    {
25
        self::$class = get_called_class();
26
27
        // check if class exist
28
        if (!class_exists(self::$class)) {
29
            return 'Error: Widget is not founded: ' . App::$Security->strip_tags(self::$class);
30
        }
31
32
        // init class and pass properties
33
        $object = new self::$class;
34
        if (Obj::isArray($params) && count($params) > 0) {
35
            foreach ($params as $property => $value) {
0 ignored issues
show
Bug introduced by
The expression $params of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
36
                if (property_exists($object, $property)) {
37
                    $object->$property = $value;
38
                }
39
            }
40
        }
41
42
        // prepare output
43
        $out = null;
44
        try {
45
            $object->init();
46
            $out = $object->display();
47
        } catch (\Exception $e) {
48
            throw $e;
49
        }
50
51
        return $out;
52
    }
53
54
    /**
55
     * Get widget configs from admin part as array $cfg=>$value
56
     * @return array|null|string
57
     */
58
    public function getConfigs()
59
    {
60
        $realName = Str::lastIn(self::$class, '\\', true);
61
        return AppRecord::getConfigs('widget', $realName);
62
    }
63
64
    public function display() {}
65
    public function init() {}
66
67
    /**
68
     * Check if widget is enabled. For native widget is always enabled
69
     * @param string $name
70
     * @return bool
71
     */
72
    public static function enabled($name)
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...
73
    {
74
        return true;
75
    }
76
}