Completed
Push — master ( 84efb4...d8405a )
by Ben
01:54
created

AbstractWidget::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Benrowe\Laravel\Widgets;
4
5
use Arrilot\Widgets\AbstractWidget as BaseAbstractWidget;
6
use Arrilot\Widgets\WidgetId;
7
8
/**
9
 *
10
 */
11
abstract class AbstractWidget extends BaseAbstractWidget
12
{
13
    const ID_PREFIX = 'widget-';
14
15
    /**
16
     * @var string
17
     */
18
    private $id;
19
20
    /**
21
     * Constructor
22
     * Store the configuration & initialise the widget
23
     *
24
     * @param array $config
25
     */
26
    public function __construct($config = [])
27
    {
28
        foreach ($config as $key => $value) {
29
            $this->addCfg($key, $value);
30
        }
31
        $this->init();
0 ignored issues
show
Unused Code introduced by
The call to the method Benrowe\Laravel\Widgets\AbstractWidget::init() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
32
    }
33
34
    /**
35
     * Custom initilisation for the widget
36
     *
37
     * @return boolean
38
     */
39
    protected function init()
40
    {
41
        return true;
42
    }
43
44
    /**
45
     * Retrieve the configuration value from the widget, based
46
     * on the supplied key
47
     *
48
     * @param  string $key
49
     * @param  mixed $default
50
     * @return mixed
51
     */
52
    public function cfg($key, $default = null)
53
    {
54
        if (array_key_exists($key, $this->config)) {
55
            return $this->config[$key];
56
        }
57
        if ($this->isConfigProperty($key)) {
58
            return $this->$key;
59
        }
60
        return $default;
61
    }
62
63
    /**
64
     * Add a configuration into the widget
65
     *
66
     * @param string $key
67
     * @param mixed $value
68
     * @return nil|null
69
     */
70
    public function addCfg($key, $value)
71
    {
72
        if ($this->isConfigProperty($key)) {
73
            $this->$key = $value;
74
            return;
75
        }
76
        $this->config[$key] = $value;
77
    }
78
79
    /**
80
     * Retrieve the widget id
81
     *
82
     * @param  boolean $autoGenerate automatically generate the id if none is
83
     *                               already set
84
     * @return string
85
     */
86
    public function getId($autoGenerate = true)
87
    {
88
        if (!$this->id && $autoGenerate) {
89
            $this->id = self::ID_PREFIX . WidgetId::get();
90
        }
91
        return $this->id;
92
    }
93
94
    /**
95
     * Set the widget identifier
96
     *
97
     * @param string $id
98
     */
99
    public function setId($id)
100
    {
101
        $this->id = $id;
102
    }
103
104
    /**
105
     * Determine if the public property exists, and is public
106
     *
107
     * @param  string  $propertyName
108
     * @return boolean
109
     */
110
    private function isConfigProperty($propertyName)
111
    {
112
        try {
113
            $reflect = new \ReflectionClass($this);
114
            $property = $reflect->getProperty($propertyName);
115
            return $property->isPublic() && !$property->isStatic();
116
        } catch (\ReflectionException $e) {
117
            return false;
118
        }
119
120
    }
121
}
122