These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace dominus77\sociallikesnext; |
||
4 | |||
5 | use yii\web\View; |
||
6 | use yii\web\JsExpression; |
||
7 | use yii\helpers\Json; |
||
8 | use yii\helpers\Html; |
||
9 | use dominus77\sociallikesnext\assets\SocialLikesAsset; |
||
10 | |||
11 | /** |
||
12 | * Class SocialLikes |
||
13 | * @package dominus77\sociallikesnext |
||
14 | */ |
||
15 | class SocialLikes extends \yii\base\Widget |
||
16 | { |
||
17 | const THEME_FLAT = 'flat'; |
||
18 | const THEME_LIGHT = 'light'; |
||
19 | const THEME_BIRMAN = 'birman'; |
||
20 | |||
21 | /** |
||
22 | * Theme buttons |
||
23 | * @var string flat, light, birman |
||
24 | */ |
||
25 | public $theme = 'flat'; |
||
26 | |||
27 | /** |
||
28 | * Display name of social networks, default true |
||
29 | * @var bool |
||
30 | */ |
||
31 | public $title = true; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | public $items = []; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | public $containerOptions = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | public $clientOptions = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | public $clientButtons = []; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | public $clientCss = ''; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | public $id; |
||
62 | |||
63 | /** |
||
64 | * Initializes the widget |
||
65 | */ |
||
66 | 2 | public function init() |
|
67 | { |
||
68 | 2 | parent::init(); |
|
69 | 2 | $this->id = $this->id ? $this->id : $this->getId(); |
|
70 | 2 | $this->registerAssets(); |
|
71 | $containerOptions = [ |
||
72 | 2 | 'id' => $this->id, |
|
73 | ]; |
||
74 | 2 | $this->containerOptions = array_merge($this->containerOptions, $containerOptions); |
|
75 | 2 | $this->containerOptions['class'] = (isset($this->containerOptions['class']) && !empty($this->containerOptions['class'])) ? |
|
76 | 1 | $this->theme . ' social-likes ' . $this->containerOptions['class'] : |
|
77 | 1 | $this->theme . ' social-likes'; |
|
78 | 2 | } |
|
79 | |||
80 | /** |
||
81 | * Run widget |
||
82 | * @return string|void |
||
83 | */ |
||
84 | 1 | public function run() |
|
85 | { |
||
86 | 1 | echo Html::beginTag('div', $this->containerOptions) . PHP_EOL; |
|
87 | 1 | $this->renderItems(); |
|
88 | 1 | echo Html::endTag('div') . PHP_EOL; |
|
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * Render items |
||
93 | */ |
||
94 | 1 | protected function renderItems() |
|
95 | { |
||
96 | 1 | foreach ($this->items as $key => $options) { |
|
97 | 1 | $title = $this->title; |
|
98 | 1 | if (isset($options['title'])) |
|
99 | 1 | $this->title = $options['title']; |
|
100 | 1 | if (isset($options['serviceOptions'])) |
|
101 | 1 | echo Html::tag('div', $this->title ? $key : '', $options['serviceOptions']) . PHP_EOL; |
|
102 | 1 | $this->title = $title; |
|
103 | } |
||
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * Register client assets |
||
108 | */ |
||
109 | 2 | protected function registerAssets() |
|
110 | { |
||
111 | 2 | $view = $this->getView(); |
|
112 | 2 | SocialLikesAsset::register($view); |
|
113 | 2 | $this->registerClientCss($view); |
|
114 | 2 | $this->registerClientButton($view); |
|
115 | 2 | $this->registerClientOptions($view); |
|
116 | 2 | } |
|
117 | |||
118 | /** |
||
119 | * @param null|\yii\web\View $view |
||
120 | */ |
||
121 | 2 | protected function registerClientCss($view = null) |
|
122 | { |
||
123 | 2 | if (!empty($this->clientCss)) { |
|
124 | 1 | $view->registerCss($this->clientCss); |
|
0 ignored issues
–
show
|
|||
125 | } |
||
126 | 2 | } |
|
127 | |||
128 | /** |
||
129 | * @param null|\yii\web\View $view |
||
130 | */ |
||
131 | 2 | protected function registerClientButton($view = null) |
|
132 | { |
||
133 | 2 | if (!empty($this->clientButtons)) { |
|
134 | 1 | $clientButtons = Json::encode($this->clientButtons); |
|
135 | 1 | $script = new JsExpression(" |
|
136 | 1 | var socialLikesButtons = {$clientButtons} |
|
137 | "); |
||
138 | 1 | $view->registerJs($script, View::POS_BEGIN); |
|
0 ignored issues
–
show
It seems like
$view is not always an object, but can also be of type null . Maybe add an additional type check?
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: function someFunction(A $objectMaybe = null)
{
if ($objectMaybe instanceof A) {
$objectMaybe->doSomething();
}
}
![]() |
|||
139 | } |
||
140 | 2 | } |
|
141 | |||
142 | /** |
||
143 | * @param null|\yii\web\View $view |
||
144 | */ |
||
145 | 2 | protected function registerClientOptions($view = null) |
|
146 | { |
||
147 | 2 | if (!empty($this->clientOptions)) { |
|
148 | 1 | $options = Json::encode($this->clientOptions); |
|
149 | 1 | $script = new JsExpression(" |
|
150 | 1 | var container = document.getElementById('{$this->id}'); |
|
151 | var socialLikes = SocialLikesNext.default; |
||
152 | 1 | socialLikes(container, {$options}); |
|
153 | "); |
||
154 | 1 | $view->registerJs($script); |
|
0 ignored issues
–
show
It seems like
$view is not always an object, but can also be of type null . Maybe add an additional type check?
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: function someFunction(A $objectMaybe = null)
{
if ($objectMaybe instanceof A) {
$objectMaybe->doSomething();
}
}
![]() |
|||
155 | } |
||
156 | 2 | } |
|
157 | } |
||
158 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: