Passed
Push — master ( f54d79...d46c99 )
by Dmitrijs
01:55
created

MaterializeWidgetTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 70
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPlugin() 0 10 2
A init() 0 6 2
A registerClientEvents() 0 3 2
1
<?php
2
/**
3
 * @link https://github.com/DMGPage/yii2-materialize
4
 * @copyright Copyright (c) 2018 Dmitrijs Reinmanis
5
 * @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE
6
 */
7
8
namespace dmgpage\yii2materialize\widgets;
9
10
use dmgpage\yii2materialize\assets\MaterializePluginAsset;
11
12
/**
13
 * MaterializeWidgetTrait is the trait, which provides basic for all Materialize widgets features.
14
 *
15
 * Note: class, which uses this trait must declare public field named `options` with the array default value:
16
 *
17
 * ```php
18
 * class MyWidget extends \yii\base\Widget
19
 * {
20
 *     use MaterializeWidgetTrait;
21
 *
22
 *     public $options = [];
23
 * }
24
 * ```
25
 *
26
 * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.
27
 *
28
 * @package widgets
29
 */
30
trait MaterializeWidgetTrait
31
{
32
    /**
33
     * @var array the options for the underlying Materialize JS plugin.
34
     * Please refer to the corresponding Materialize plugin Web page for possible options.
35
     * For example, [this page](https://materializecss.com/modals.html) shows
36
     * how to use the "Modal" plugin and the supported options.
37
     *
38
     * @see http://materializecss.com/
39
     */
40
    public $clientOptions = [];
41
42
    /**
43
     * @var array the event handlers for the underlying Materialize JS plugin.
44
     * Please refer to the corresponding Materialize plugin Web page for possible events.
45
     * For example, [this page](https://materializecss.com/modals.html) shows
46
     * how to use the "Modal" plugin and the supported events.
47
     *
48
     * @see http://materializecss.com/
49
     */
50
    public $clientEvents = [];
51
52
    /**
53
     * Initializes the widget.
54
     * This method will register the Materialize asset bundle. If you override this method,
55
     * make sure you call the parent implementation first.
56
     */
57
    public function init()
58
    {
59
        parent::init();
60
        
61
        if (!isset($this->options['id'])) {
62
            $this->options['id'] = $this->getId();
1 ignored issue
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            /** @scrutinizer ignore-call */ 
63
            $this->options['id'] = $this->getId();
Loading history...
63
        }
64
    }
65
66
    /**
67
     * Registers a specific Materialize plugin and the related events
68
     * @param string $name the name of the Materialize plugin
69
     *
70
     * @uses [[MaterializePluginAsset::register()]]
71
     * @uses [[registerClientEvents()]]
72
     */
73
    protected function registerPlugin($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    protected function registerPlugin(/** @scrutinizer ignore-unused */ $name)

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

Loading history...
74
    {
75
        $view = $this->getView();
76
        MaterializePluginAsset::register($view);
77
78
        if ($this->clientOptions !== false) {
0 ignored issues
show
introduced by
The condition $this->clientOptions !== false is always true.
Loading history...
79
            //...
80
        }
81
82
        $this->registerClientEvents();
83
    }
84
85
    /**
86
     * Registers JS event handlers that are listed in [[clientEvents]].
87
     */
88
    protected function registerClientEvents()
89
    {
90
        if (!empty($this->clientEvents)) {
91
            //...
92
        }
93
    }
94
95
    /**
96
     * @return \yii\web\View the view object that can be used to render views or view files.
97
     * @see \yii\base\Widget::getView()
98
     */
99
    abstract function getView();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
}
101