Completed
Push — master ( 01b1a5...81f493 )
by Michael
04:03
created

SmartobjectAdsense::generateTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 59

Duplication

Lines 69
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 1
eloc 59
c 5
b 1
f 0
nc 1
nop 0
dl 69
loc 69
rs 9.2083

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// 
3
// ------------------------------------------------------------------------ //
4
//               XOOPS - PHP Content Management System                      //
5
//                   Copyright (c) 2000-2016 XOOPS.org                           //
6
//                      <http://xoops.org/>                             //
7
// ------------------------------------------------------------------------ //
8
// This program is free software; you can redistribute it and/or modify     //
9
// it under the terms of the GNU General Public License as published by     //
10
// the Free Software Foundation; either version 2 of the License, or        //
11
// (at your option) any later version.                                      //
12
13
// You may not change or alter any portion of this comment or credits       //
14
// of supporting developers from this source code or any supporting         //
15
// source code which is considered copyrighted (c) material of the          //
16
// original comment or credit authors.                                      //
17
// This program is distributed in the hope that it will be useful,          //
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
// GNU General Public License for more details.                             //
21
22
// You should have received a copy of the GNU General Public License        //
23
// along with this program; if not, write to the Free Software              //
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
// ------------------------------------------------------------------------ //
26
// URL: http://xoops.org/                                               //
27
// Project: XOOPS Project                                               //
28
// -------------------------------------------------------------------------//
29
30
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
32
include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobject.php';
33
34
/**
35
 * Class SmartobjectAdsense
36
 */
37
class SmartobjectAdsense extends SmartObject
38
{
39
    /**
40
     * SmartobjectAdsense constructor.
41
     */
42
    public function __construct()
43
    {
44
        $this->quickInitVar('adsenseid', XOBJ_DTYPE_INT, true);
45
        $this->quickInitVar('description', XOBJ_DTYPE_TXTAREA, true, _CO_SOBJECT_ADSENSE_DESCRIPTION, _CO_SOBJECT_ADSENSE_DESCRIPTION_DSC);
46
        $this->quickInitVar('client_id', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_CLIENT_ID, _CO_SOBJECT_ADSENSE_CLIENT_ID_DSC);
47
        $this->quickInitVar('tag', XOBJ_DTYPE_TXTBOX, false, _CO_SOBJECT_ADSENSE_TAG, _CO_SOBJECT_ADSENSE_TAG_DSC);
48
        $this->quickInitVar('format', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_FORMAT, _CO_SOBJECT_ADSENSE_FORMAT_DSC);
49
        $this->quickInitVar('border_color', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_BORDER_COLOR, _CO_SOBJECT_ADSENSE_BORDER_COLOR_DSC);
50
        $this->quickInitVar('background_color', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_BACKGROUND_COLOR, _CO_SOBJECT_ADSENSE_BORDER_COLOR_DSC);
51
        $this->quickInitVar('link_color', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_LINK_COLOR, _CO_SOBJECT_ADSENSE_LINK_COLOR_DSC);
52
        $this->quickInitVar('url_color', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_URL_COLOR, _CO_SOBJECT_ADSENSE_URL_COLOR_DSC);
53
        $this->quickInitVar('text_color', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_ADSENSE_TEXT_COLOR, _CO_SOBJECT_ADSENSE_TEXT_COLOR_DSC);
54
        $this->quickInitVar('style', XOBJ_DTYPE_TXTAREA, false, _CO_SOBJECT_ADSENSE_STYLE, _CO_SOBJECT_ADSENSE_STYLE_DSC);
55
56
        $this->setControl('format', array(
57
            'handler' => 'adsense',
58
            'method'  => 'getFormats'
59
        ));
60
61
        $this->setControl('border_color', array(
62
            'name'      => 'text',
63
            'size'      => 6,
64
            'maxlength' => 6
65
        ));
66
67
        $this->setControl('background_color', array(
68
            'name'      => 'text',
69
            'size'      => 6,
70
            'maxlength' => 6
71
        ));
72
73
        $this->setControl('link_color', array(
74
            'name'      => 'text',
75
            'size'      => 6,
76
            'maxlength' => 6
77
        ));
78
79
        $this->setControl('url_color', array(
80
            'name'      => 'text',
81
            'size'      => 6,
82
            'maxlength' => 6
83
        ));
84
85
        $this->setControl('text_color', array(
86
            'name'      => 'text',
87
            'size'      => 6,
88
            'maxlength' => 6
89
        ));
90
    }
91
92
    /**
93
     * @param  string $key
94
     * @param  string $format
95
     * @return mixed
96
     */
97
    public function getVar($key, $format = 's')
98
    {
99
        if ($format === 's' && in_array($key, array())) {
100
            //            return call_user_func(array($this, $key));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
            return $this->{$key}();
102
        }
103
104
        return parent::getVar($key, $format);
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function render()
111
    {
112
        global $smartobjectAdsenseHandler;
113
        if ($this->getVar('style', 'n') !== '') {
114
            $ret = '<div style="' . $this->getVar('style', 'n') . '">';
115
        } else {
116
            $ret = '<div>';
117
        }
118
119
        $ret .= '<script type="text/javascript"><!--
120
google_ad_client = "' . $this->getVar('client_id', 'n') . '";
121
google_ad_width = ' . $smartobjectAdsenseHandler->adFormats[$this->getVar('format', 'n')]['width'] . ';
122
google_ad_height = ' . $smartobjectAdsenseHandler->adFormats[$this->getVar('format', 'n')]['height'] . ';
123
google_ad_format = "' . $this->getVar('format', 'n') . '";
124
google_ad_type = "text";
125
google_ad_channel ="";
126
google_color_border = "' . $this->getVar('border_color', 'n') . '";
127
google_color_bg = "' . $this->getVar('background_color', 'n') . '";
128
google_color_link = "' . $this->getVar('link_color', 'n') . '";
129
google_color_url = "' . $this->getVar('url_color', 'n') . '";
130
google_color_text = "' . $this->getVar('text_color', 'n') . '";
131
//--></script>
132
<script type="text/javascript"
133
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
134
</script>
135
</div>';
136
137
        return $ret;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getXoopsCode()
144
    {
145
        $ret = '[adsense]' . $this->getVar('tag', 'n') . '[/adsense]';
146
147
        return $ret;
148
    }
149
150
    /**
151
     * @param $var
152
     * @return bool
153
     */
154
    public function emptyString($var)
155
    {
156
        return (strlen($var) > 0);
157
    }
158
159
    /**
160
     * @return mixed|string
161
     */
162 View Code Duplication
    public function generateTag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
163
    {
164
        $title = rawurlencode(strtolower($this->getVar('description', 'e')));
165
        $title = xoops_substr($title, 0, 10, '');
166
        // Transformation des ponctuations
167
        $pattern = array(
168
            '/%09/', // Tab
169
            '/%20/', // Space
170
            '/%21/', // !
171
            '/%22/', // "
172
            '/%23/', // #
173
            '/%25/', // %
174
            '/%26/', // &
175
            '/%27/', // '
176
            '/%28/', // (
177
            '/%29/', // )
178
            '/%2C/', // ,
179
            '/%2F/', // /
180
            '/%3A/', // :
181
            '/%3B/', // ;
182
            '/%3C/', // <
183
            '/%3D/', // =
184
            '/%3E/', // >
185
            '/%3F/', // ?
186
            '/%40/', // @
187
            '/%5B/', // [
188
            '/%5C/', // \
189
            '/%5D/', // ]
190
            '/%5E/', // ^
191
            '/%7B/', // {
192
            '/%7C/', // |
193
            '/%7D/', // }
194
            '/%7E/', // ~
195
            "/\./" // .
196
        );
197
        $rep_pat = array('-', '-', '-', '-', '-', '-100', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-at-', '-', '-', '-', '-', '-', '-', '-', '-', '-');
198
        $title   = preg_replace($pattern, $rep_pat, $title);
199
200
        // Transformation des caract�res accentu�s
201
        $pattern = array(
202
            '/%B0/', // °
203
            '/%E8/', // è
204
            '/%E9/', // é
205
            '/%EA/', // ê
206
            '/%EB/', // ë
207
            '/%E7/', // ç
208
            '/%E0/', // à
209
            '/%E2/', // â
210
            '/%E4/', // ä
211
            '/%EE/', // î
212
            '/%EF/', // ï
213
            '/%F9/', // ù
214
            '/%FC/', // ü
215
            '/%FB/', // û
216
            '/%F4/', // ô
217
            '/%F6/', // ö
218
        );
219
        $rep_pat = array('-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o');
220
        $title   = preg_replace($pattern, $rep_pat, $title);
221
222
        $tableau = explode('-', $title); // Transforme la chaine de caract�res en tableau
223
        $tableau = array_filter($tableau, array($this, 'emptyString')); // Supprime les chaines vides du tableau
224
        $title   = implode('-', $tableau); // Transforme un tableau en chaine de caract�res s�par� par un tiret
225
226
        $title .= time();
227
        $title = md5($title);
228
229
        return $title;
230
    }
231
232
    /**
233
     * @return string
234
     */
235 View Code Duplication
    public function getCloneLink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
236
    {
237
        $ret = '<a href="' .
238
               SMARTOBJECT_URL .
239
               'admin/adsense.php?op=clone&adsenseid=' .
240
               $this->getVar('adsenseid') .
241
               '"><img src="' .
242
               SMARTOBJECT_IMAGES_ACTIONS_URL .
243
               'editcopy.png" alt="' .
244
               _CO_SOBJECT_ADSENSE_CLONE .
245
               '" title="' .
246
               _CO_SOBJECT_ADSENSE_CLONE .
247
               '" /></a>';
248
249
        return $ret;
250
    }
251
}
252
253
/**
254
 * Class SmartobjectAdsenseHandler
255
 */
256
class SmartobjectAdsenseHandler extends SmartPersistableObjectHandler
257
{
258
    public $adFormats;
259
    public $adFormatsList;
260
    public $objects = false;
261
262
    /**
263
     * SmartobjectAdsenseHandler constructor.
264
     * @param XoopsDatabase $db
265
     */
266
    public function __construct(XoopsDatabase $db)
267
    {
268
        parent::__construct($db, 'adsense', 'adsenseid', 'description', '', 'smartobject');
269
        $this->adFormats     = array();
270
        $this->adFormatsList = array();
271
272
        $this->adFormats['728x90_as']['caption'] = '728 X 90 Leaderboard';
273
        $this->adFormats['728x90_as']['width']   = 728;
274
        $this->adFormats['728x90_as']['height']  = 90;
275
        $this->adFormatsList['728x90_as']        = $this->adFormats['728x90_as']['caption'];
276
277
        $this->adFormats['468x60_as']['caption'] = '468 X 60 Banner';
278
        $this->adFormats['468x60_as']['width']   = 468;
279
        $this->adFormats['468x60_as']['height']  = 60;
280
        $this->adFormatsList['468x60_as']        = $this->adFormats['468x60_as']['caption'];
281
282
        $this->adFormats['234x60_as']['caption'] = '234 X 60 Half Banner';
283
        $this->adFormats['234x60_as']['width']   = 234;
284
        $this->adFormats['234x60_as']['height']  = 60;
285
        $this->adFormatsList['234x60_as']        = $this->adFormats['234x60_as']['caption'];
286
287
        $this->adFormats['120x600_as']['caption'] = '120 X 600 Skyscraper';
288
        $this->adFormats['120x600_as']['width']   = 120;
289
        $this->adFormats['120x600_as']['height']  = 600;
290
        $this->adFormatsList['120x600_as']        = $this->adFormats['120x600_as']['caption'];
291
292
        $this->adFormats['160x600_as']['caption'] = '160 X 600 Wide Skyscraper';
293
        $this->adFormats['160x600_as']['width']   = 160;
294
        $this->adFormats['160x600_as']['height']  = 600;
295
        $this->adFormatsList['160x600_as']        = $this->adFormats['160x600_as']['caption'];
296
297
        $this->adFormats['120x240_as']['caption'] = '120 X 240 Vertical Banner';
298
        $this->adFormats['120x240_as']['width']   = 120;
299
        $this->adFormats['120x240_as']['height']  = 240;
300
        $this->adFormatsList['120x240_as']        = $this->adFormats['120x240_as']['caption'];
301
302
        $this->adFormats['336x280_as']['caption'] = '336 X 280 Large Rectangle';
303
        $this->adFormats['336x280_as']['width']   = 136;
304
        $this->adFormats['336x280_as']['height']  = 280;
305
        $this->adFormatsList['336x280_as']        = $this->adFormats['336x280_as']['caption'];
306
307
        $this->adFormats['300x250_as']['caption'] = '300 X 250 Medium Rectangle';
308
        $this->adFormats['300x250_as']['width']   = 300;
309
        $this->adFormats['300x250_as']['height']  = 250;
310
        $this->adFormatsList['300x250_as']        = $this->adFormats['300x250_as']['caption'];
311
312
        $this->adFormats['250x250_as']['caption'] = '250 X 250 Square';
313
        $this->adFormats['250x250_as']['width']   = 250;
314
        $this->adFormats['250x250_as']['height']  = 250;
315
        $this->adFormatsList['250x250_as']        = $this->adFormats['250x250_as']['caption'];
316
317
        $this->adFormats['200x200_as']['caption'] = '200 X 200 Small Square';
318
        $this->adFormats['200x200_as']['width']   = 200;
319
        $this->adFormats['200x200_as']['height']  = 200;
320
        $this->adFormatsList['200x200_as']        = $this->adFormats['200x200_as']['caption'];
321
322
        $this->adFormats['180x150_as']['caption'] = '180 X 150 Small Rectangle';
323
        $this->adFormats['180x150_as']['width']   = 180;
324
        $this->adFormats['180x150_as']['height']  = 150;
325
        $this->adFormatsList['180x150_as']        = $this->adFormats['180x150_as']['caption'];
326
327
        $this->adFormats['125x125_as']['caption'] = '125 X 125 Button';
328
        $this->adFormats['125x125_as']['width']   = 125;
329
        $this->adFormats['125x125_as']['height']  = 125;
330
        $this->adFormatsList['125x125_as']        = $this->adFormats['125x125_as']['caption'];
331
    }
332
333
    /**
334
     * @return array
335
     */
336
    public function getFormats()
337
    {
338
        return $this->adFormatsList;
339
    }
340
341
    /**
342
     * @param $obj
343
     * @return bool
344
     */
345
    public function beforeSave($obj)
346
    {
347
        if ($obj->getVar('tag') === '') {
348
            $obj->setVar('tag', $title = $obj->generateTag());
349
        }
350
351
        return true;
352
    }
353
354
    /**
355
     * @return array|bool
356
     */
357
    public function getAdsensesByTag()
358
    {
359
        if (!$this->objects) {
360
            $adsensesObj = $this->getObjects(null, true);
361
            $ret         = array();
362
            foreach ($adsensesObj as $adsenseObj) {
363
                $ret[$adsenseObj->getVar('tag')] = $adsenseObj;
364
            }
365
            $this->objects = $ret;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ret of type array is incompatible with the declared type boolean of property $objects.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
366
        }
367
368
        return $this->objects;
369
    }
370
}
371