Completed
Push — master ( d97ad7...f9e7b8 )
by Mihail
03:48
created

Widget   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 15.91 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 4
dl 7
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C widget() 7 32 8
A display() 0 1 1
A init() 0 1 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ffcms\Core\Arch;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Interfaces\iWidget;
8
use Ffcms\Core\Traits\DynamicGlobal;
9
10
abstract class Widget implements iWidget
11
{
12
    use DynamicGlobal;
13
14
    /** @var string|null */
15
    public static $class;
16
17
    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...
18
    {
19
        if (self::$class === null) {
20
            self::$class = get_called_class();
21
        }
22
23
        // check if class exist
24
        if (!class_exists(self::$class)) {
25
            return 'Error: Widget is not founded: ' . App::$Security->strip_tags(self::$class);
26
        }
27
28
        // init class and pass properties
29
        $object = new self::$class;
30 View Code Duplication
        if (Obj::isArray($params) && count($params) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            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...
32
                if (property_exists($object, $property)) {
33
                    $object->$property = $value;
34
                }
35
            }
36
        }
37
38
        // prepare output
39
        $out = null;
40
        try {
41
            $object->init();
42
            $out = $object->display();
43
        } catch (\Exception $e) {
44
            throw $e;
45
        }
46
47
        return $out;
48
    }
49
50
    public function display() {}
51
    public function init() {}
52
53
}