Completed
Push — master ( 77281f...d1e393 )
by Michael
04:26
created

ExtcalFormSpin   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 482
Duplicated Lines 2.49 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 12
loc 482
rs 8.2857
wmc 39
lcom 1
cbo 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 1
A getValue() 0 4 1
A setValue() 0 4 1
A getMin() 0 4 1
A setMin() 0 4 1
A getMax() 0 4 1
A setMax() 0 4 1
A getSmallIncrement() 0 4 1
A setSmallIncrement() 0 7 2
A getLargeIncrement() 0 4 1
A setLargeIncrement() 0 7 2
A getSize() 0 4 1
A setSize() 0 7 2
A getImgFolder() 0 7 1
A setImgFolder() 0 6 2
A getUnite() 0 4 1
A setUnite() 0 4 1
A getStyleText() 0 4 1
A setStyleText() 0 6 2
A getStyleBordure() 0 4 1
A setStyleBordure() 0 6 2
A getMinMaxVisible() 0 4 1
A setMinMaxVisible() 0 4 1
B render() 12 77 5
A getFolder() 0 6 1
A htmlAddAttribut() 0 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ExtcalFormSpin element  -  Spin button.
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
13
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 *
15
 * @since           2.0.0
16
 *
17
 * @author          Jean-Jacques DELALANDRE <[email protected]>
18
 *
19
 * @version         ExtcalFormSpin v 1.2
20
 */
21
defined('XOOPS_ROOT_PATH') or die('Restricted access');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
22
23
xoops_load('XoopsFormElement');
24
25
/**
26
 * A select field.
27
 *
28
 * @author      Jean-Jacques DELALANDRE <[email protected]>
29
 * @copyright   JJD http:xoops.kiolo.com
30
 */
31
32
/**
33
 * Class ExtcalFormSpin.
34
 */
35
class ExtcalFormSpin extends XoopsFormElement
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
36
{
37
    /**
38
     * Value.
39
     *
40
     * @var int
41
     */
42
    protected $_value = 0;
43
44
    /**
45
     * Value minimum.
46
     *
47
     * @var int
48
     */
49
    protected $_min = 0;
50
51
    /**
52
     * Value maximum.
53
     *
54
     * @var int
55
     */
56
    protected $_max = 100;
57
58
    /**
59
     * Small increment.
60
     *
61
     * @var int
62
     */
63
    protected $_smallIncrement = 1;
64
65
    /**
66
     * Large increment.
67
     *
68
     * @var int
69
     */
70
    protected $_largeIncrement = 10;
71
72
    /**
73
     *  unite for information on value.
74
     *
75
     * @var string
76
     */
77
    protected $_unite = '';
78
79
    /**
80
     * Folder of arrow image.
81
     *
82
     * @var string
83
     */
84
    protected $_imgFolder = 'default';
85
86
    /**
87
     * size of input text in nb car.
88
     *
89
     * @var int
90
     */
91
    protected $_size = 2;
92
93
    /**
94
     *  minMaxVisible show buttons to go minimum and maximum.
95
     *
96
     * @var int
97
     */
98
    protected $_minMaxVisible = true;
99
100
    /**
101
     *  tyleBordure ;  style CSS of frame control.
102
     *
103
     * @var string
104
     */
105
    protected $_styleBordure = 'color: #FFFFFF; background-color: #CCCCCC; line-height: 100%;border-width:1px; border-style: solid; border-color: #000000; margin-top: 0; margin-bottom: 0; padding: 0';
106
107
    /**
108
     *  tyleText : style CSS of input text.
109
     *
110
     * @var string
111
     */
112
    protected $_styleText = 'color: #000000; text-align: right; margin-left: 1; margin-right: 2; padding-right: 8';
113
114
    /**
115
     * Allow loading of javascript.
116
     *
117
     * @var bool
118
     */
119
    protected $_loadJS = true;
120
121
    /*---------------------------------------------------------------*/
122
    /**
123
     * Constructor.
124
     *
125
     * @param string $caption        Caption
126
     * @param string $name           "name" attribute
127
     * @param int    $value          Pre-selected value.
128
     * @param int    $min            value
129
     * @param int    $max            value
130
     * @param int    $smallIncrement Increment when click on button
131
     * @param int    $largeIncrement Increment when click on button
132
     * @param int    $size           Number caractere of inputtext
133
     * @param string $unite          of the value
134
     * @param string $imgFolder      of image gif for button
135
     * @param string $styleText      style CSs of text
136
     * @param string $styleBordure   style CSs of frame
137
     * @param bool   $minMaxVisible  show min and mas buttons
138
     */
139
    public function __construct(
140
        $caption,
141
        $name,
142
        $value = 0,
143
        $min = 0,
144
        $max = 100,
145
        $smallIncrement = 1,
146
        $largeIncrement = 10,
147
        $size = 5,
148
        $unite = '',
149
        $imgFolder = 'default',
150
        $styleText = '',
151
        $styleBordure = '',
152
        $minMaxVisible = true
153
    ) {
154
        $this->setName($name);
155
        $this->setCaption($caption);
156
        $this->setValue($value);
157
        $this->setMin($min);
158
        $this->setMax($max);
159
        $this->setSmallIncrement($smallIncrement);
160
        $this->setLargeIncrement($largeIncrement);
161
        $this->setSize($size);
162
        $this->setUnite($unite);
163
        $this->setImgFolder($imgFolder);
164
        $this->setStyleText($styleText);
165
        $this->setStyleBordure($styleBordure);
166
        $this->setMinMaxVisible($minMaxVisible);
167
    }
168
169
    /*-----------------------------------------------------------------*/
170
    /**
171
     * Get the values.
172
     */
173
    public function getValue()
174
    {
175
        return $this->_value;
176
    }
177
178
    /**
179
     * Set the value.
180
     *
181
     * @param  $value int
182
     */
183
    public function setValue($value)
184
    {
185
        $this->_value = $value;
186
    }
187
188
    /*-----------------------------------------------------------------*/
189
    /**
190
     * Get the min value.
191
     */
192
    public function getMin()
193
    {
194
        return $this->_min;
195
    }
196
197
    /**
198
     * Set the min value.
199
     *
200
     * @param  $min int
201
     */
202
    public function setMin($min)
203
    {
204
        $this->_min = (int)$min;
205
    }
206
    /*-----------------------------------------------------------------*/
207
    /**
208
     * Get the max value - must be more great then min.
209
     */
210
    public function getMax()
211
    {
212
        return $this->_max;
213
    }
214
215
    /**
216
     * Set the max value - must be more great then min.
217
     *
218
     * @param  $max int
219
     */
220
    public function setMax($max)
221
    {
222
        $this->_max = (int)$max;
223
    }
224
225
    /*-----------------------------------------------------------------*/
226
    /**
227
     * Get the small increment when click a short time on up down nutton.
228
     */
229
    public function getSmallIncrement()
230
    {
231
        return $this->_smallIncrement;
232
    }
233
234
    /**
235
     * Set the small increment when click a short time on up down nutton
236
     * must be  " > 0 ".
237
     *
238
     * @param $smallIncrement
239
     *
240
     * @internal param int $value
241
     */
242
    public function setSmallIncrement($smallIncrement)
243
    {
244
        $this->_smallIncrement = (int)$smallIncrement;
245
        if ($this->_smallIncrement == 0) {
246
            $this->_smallIncrement = 1;
247
        }
248
    }
249
250
    /*-----------------------------------------------------------------*/
251
    /**
252
     * Get the large increment when click a long time on up down nutton.
253
     */
254
    public function getLargeIncrement()
255
    {
256
        return $this->_largeIncrement;
257
    }
258
259
    /**
260
     * Set the large increment when click a long time on up down nutton.
261
     *
262
     * @param  $largeIncrement int
263
     */
264
    public function setLargeIncrement($largeIncrement)
265
    {
266
        $this->_largeIncrement = (int)$largeIncrement;
267
        if ($this->_largeIncrement == 0) {
268
            $this->_largeIncrement = 10;
269
        }
270
    }
271
272
    /*-----------------------------------------------------------------*/
273
    /**
274
     * Get the size in nb car of the input text for the value.
275
     */
276
    public function getSize()
277
    {
278
        return $this->_size;
279
    }
280
281
    /**
282
     * Set the size in nb car of the input text for the value
283
     * must be 2 car min.
284
     *
285
     * @param  $size mixed
286
     */
287
    public function setSize($size)
288
    {
289
        $this->_size = $size;
290
        if ($this->_size == 0) {
291
            $this->_size = 2;
292
        }
293
    }
294
295
    /*-----------------------------------------------------------------*/
296
    /**
297
     * @return string
298
     */
299
    public function getImgFolder()
300
        /*
301
         * Get the shortname of the folder images
302
         */
303
    {
304
        return $this->_imgFolder;
305
    }
306
307
    /**
308
     * Set the shortname of the folder images.
309
     *
310
     * @param  $folder string
311
     */
312
    public function setImgFolder($folder)
313
    {
314
        if ($folder != '') {
315
            $this->_imgFolder = $folder;
316
        }
317
    }
318
    /*-----------------------------------------------------------------*/
319
    /**
320
     * Get the label of unites between value and buttons.
321
     */
322
    public function getUnite()
323
    {
324
        return $this->_unite;
325
    }
326
327
    /**
328
     * Set the label of unites between value and buttons.
329
     *
330
     * @param  $unite string
331
     */
332
    public function setUnite($unite)
333
    {
334
        $this->_unite = $unite;
335
    }
336
    /*-----------------------------------------------------------------*/
337
    /**
338
     * Get the style CSS of the text.
339
     */
340
    public function getStyleText()
341
    {
342
        return $this->_styleText;
343
    }
344
345
    /**
346
     * Set the style CSS of the text.
347
     *
348
     * @param  $style string
349
     */
350
    public function setStyleText($style)
351
    {
352
        if ($style != '') {
353
            $this->_styleText = $style;
354
        }
355
    }
356
    /*-----------------------------------------------------------------*/
357
    /**
358
     * Get the style CSS of the frame.
359
     */
360
    public function getStyleBordure()
361
    {
362
        return $this->_styleBordure;
363
    }
364
365
    /**
366
     * Set the style CSS of the frame.
367
     *
368
     * @param  $style string
369
     */
370
    public function setStyleBordure($style)
371
    {
372
        if ($style != '') {
373
            $this->_styleBordure = $style;
374
        }
375
    }
376
    /*-----------------------------------------------------------------*/
377
    /**
378
     * Get MinMaxVisible : show the button to go min and max value.
379
     */
380
    public function getMinMaxVisible()
381
    {
382
        return $this->_minMaxVisible;
383
    }
384
385
    /**
386
     * Set  MinMaxVisible : show the button to go min and max value.
387
     *
388
     * @param  $visible bool
389
     */
390
    public function setMinMaxVisible($visible)
391
    {
392
        $this->_minMaxVisible = $visible;
393
    }
394
    /**********************************************************************/
395
396
    /**
397
     * Prepare HTML for output.
398
     *
399
     * @return string HTML
400
     */
401
    public function render()
402
    {
403
        $sSpinFolder = $this->getFolder();
404
        $sFolderImg  = "{$sSpinFolder}/images/{$this->getImgFolder()}/";
405
406
        $prefixe  = $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
407
        $prefixe2 = 'spin' . $prefixe;
408
409
        $smallIncrement = $this->getSmallIncrement();
410
        $largeIncrement = $this->getLargeIncrement();
411
412
        /*----------------------------------------------*/
413
        $delai        = 200;
414
        $onMouseDown1 = "spinStart(\"{$prefixe}\", \"{$prefixe2}\",  {$smallIncrement},  {$largeIncrement}, {$delai}, \"{$sFolderImg}spinUp1.gif\");";
415
        $onMouseDown2 = "spinStart(\"{$prefixe}\", \"{$prefixe2}\", -{$smallIncrement}, -{$largeIncrement}, {$delai}, \"{$sFolderImg}spinDown1.gif\");";
416
417
        $onMouseUp = 'spinStop();';
418
        //----------------------------------------------------------------
419
        $styleBordure = $this->htmlAddAttribut('style', $this->getStyleBordure());
420
        $styleText    = $this->htmlAddAttribut('style', $this->getStyleText());
421
        $styleArrow   = 'style="display: table-cell;vertical-align: middle; text-align: center; line-height: 100%; font-size: 7 pt; margin-top: 0; margin-bottom: 0; padding: 0"';
422
        //----------------------------------------------------------------
423
        $t = array();
424
425
        if ($this->_loadJS) {
426
            $js  = $sSpinFolder . '/js/spin.js';
427
            $t[] = "<script src='{$js}' type='text/javascript'></script>";
428
        }
429
430
        $t[] = "<div STYLE='width:50px;'>";
431
        //$t[] = "<table border='0' width='8%' cellpadding='0' cellspacing='0'>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
432
        $t[] = "<table border='0' width='8%' cellpadding='0' cellspacing='0' {$styleBordure}>";
433
        $t[] = '  <tr>';
434
        //$t[] = "    <td width='60%'>{$Caption}</td>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
435
        $t[] = "    <td width='60%'>";
436
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_min' VALUE='{$this->getMin()}'>";
437
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_max' VALUE='{$this->getMax()}'>";
438
        $t[] = "      <INPUT TYPE='hidden' NAME='{$prefixe2}_smallIncrement' VALUE='{$this->_smallIncrement}'  style='text-align: right;'>";
439
        $t[] = "      <input type='text'  name='{$prefixe}' size='{$this->getSize()}' value='{$this->getValue()}' {$styleText}>";
440
        $t[] = '    </td>';
441
442
        $unite = $this->getUnite();
443
        if ($unite != '') {
444
            $t[] = "    <td style='display: table-cell;vertical-align: middle; '>&nbsp;{$unite}&nbsp;</td>";
445
        }
446
        //-------------------------------------------------------
447 View Code Duplication
        if ($this->getMinMaxVisible()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
448
            $onMouseDownMin = "spinSetValue(\"{$prefixe}\", \"{$prefixe2}\",  \"Min\", {$this->getMin()}, {$delai}, \"{$sFolderImg}spinMin1.gif\");";
449
            $t[]            = "    <td width='63%' align='center' {$styleArrow}>";
450
            $t[]            = "      <img border='0' name='{$prefixe2}_imgMin' src='{$sFolderImg}spinMin0.gif'   onmousedown='{$onMouseDownMin}'><br>";
451
            $t[]            = '    </td>';
452
        }
453
        //-------------------------------------------------------
454
        $t[] = "    <td width='63%' align='center' {$styleArrow}>";
455
456
        $t[] = "      <img border='0' name='{$prefixe2}_img0' src='{$sFolderImg}spinUp0.gif'   onmousedown='{$onMouseDown1}' onmouseup='{$onMouseUp}' onmouseout='{$onMouseUp}'><br>";
457
        $t[] = "      <img border='0' name='{$prefixe2}_img1' src='{$sFolderImg}spinDown0.gif' onmousedown='{$onMouseDown2}' onmouseup='{$onMouseUp}' onmouseout='{$onMouseUp}'>";
458
459
        $t[] = '    </td>';
460
461
        //-------------------------------------------------------
462 View Code Duplication
        if ($this->getMinMaxVisible()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
463
            $onMouseDownMax = "spinSetValue(\"{$prefixe}\", \"{$prefixe2}\",  \"Max\", {$this->getMax()}, {$delai}, \"{$sFolderImg}spinMax1.gif\");";
464
            $t[]            = "    <td width='63%' align='center' {$styleArrow}>";
465
            $t[]            = "      <img border='0' name='{$prefixe2}_imgMax' src='{$sFolderImg}spinMax0.gif'   onmousedown='{$onMouseDownMax}'><br>";
466
            $t[]            = '    </td>';
467
        }
468
        //-------------------------------------------------------
469
470
        $t[] = '  </tr>';
471
        $t[] = '</table>' . "\n";
472
        $t[] = '</div>';
473
        //-------------------------------------------
474
        $html = implode("\n", $t);
475
476
        return $html;
477
    }
478
479
    /**************************************************************************
480
     * calcul du dossier du composant
481
     *************************************************************************/
482
    public function getFolder()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
getFolder uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
483
    {
484
        $sSpinFolder = $GLOBALS['xoops']->url('modules/extcal/class/form/spin/');
485
486
        return $sSpinFolder;
487
    }
488
489
    /********************************************************************
490
     *
491
     ********************************************************************
492
     * @param        $attribut
493
     * @param        $value
494
     * @param string $default
495
     * @return string
496
     */
497
    public function htmlAddAttribut($attribut, $value, $default = '')
498
    {
499
        $r = '';
500
        if ($value == '') {
501
            $value = $default;
502
        }
503
504
        if ($value != '') {
505
            if (substr($value, 0, strlen($attribut)) != $attribut) {
506
                $r = "{$attribut}=\"{$value}\"";
507
            }
508
509
            return $r;
510
        }
511
512
        /*-----------------------------------------------*/
513
        /*---          fin de la classe               ---*/
514
        /*-----------------------------------------------*/
515
    }
516
}
517