Completed
Push — master ( 3a775b...45e4bb )
by Ben
03:48
created

AbstractWidget::addCfg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Benrowe\Laravel\Widgets;
4
5
use Arrilot\Widgets\AbstractWidget as BaseAbstractWidget;
6
7
/**
8
 *
9
 */
10
abstract class AbstractWidget extends BaseAbstractWidget
11
{
12
    /**
13
     * Constructor
14
     * Store the configuration & initialise the widget
15
     *
16
     * @param array $config
17
     */
18
    public function __construct($config = [])
19
    {
20
        foreach ($config as $key => $value) {
21
            $this->addCfg($key, $value);
22
        }
23
        $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...
24
    }
25
26
    /**
27
     * Custom initilisation for the widget
28
     *
29
     * @return boolean
30
     */
31
    protected function init()
32
    {
33
        return true;
34
    }
35
36
    /**
37
     * Retrieve the configuration value from the widget, based
38
     * on the supplied key
39
     *
40
     * @param  string $key
41
     * @param  mixed $default
42
     * @return mixed
43
     */
44
    public function cfg($key, $default = null)
45
    {
46
        if (array_key_exists($key, $this->config)) {
47
            return $this->config[$key];
48
        }
49
        if ($this->isConfigProperty($key)) {
50
            return $this->$key;
51
        }
52
        return $default;
53
    }
54
55
    /**
56
     * Add a configuration into the widget
57
     *
58
     * @param string $key
59
     * @param mixed $value
60
     * @return nil
0 ignored issues
show
Documentation introduced by
Should the return type not be nil|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62
    public function addCfg($key, $value)
63
    {
64
        if ($this->isConfigProperty($key)) {
65
            $this->$key = $value;
66
            return;
67
        }
68
69
    }
70
71
    /**
72
     * Determine if the public property exists, and is public
73
     *
74
     * @param  string  $propertyName
75
     * @return boolean
76
     */
77
    private function isConfigProperty($propertyName)
78
    {
79
        try {
80
            $reflect = new \ReflectionClass($this);
81
            $property = $reflect->getProperty($propertyName);
82
            return $property->isPublic() && !$property->isStatic();
83
        } catch (\ReflectionException $e) {
84
            return false;
85
        }
86
87
    }
88
}
89