SmartFormCheckElement   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 32.76 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 19
loc 58
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 17 6
A renderValidationJS() 19 27 2

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 namespace XoopsModules\Smartobject\Form\Elements;
2
3
use XoopsModules\Smartobject;
4
5
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
6
7
/**
8
 * Class SmartFormCheckElement
9
 */
10
class SmartFormCheckElement extends \XoopsFormCheckBox
11
{
12
    /**
13
     *
14
     * /**
15
     * prepare HTML for output
16
     *
17
     * @return string
18
     */
19
    public function render()
20
    {
21
        $ret = '';
22
        if (count($this->getOptions()) > 1 && '[]' !== substr($this->getName(), -2, 2)) {
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
23
            $newname = $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...
24
            $this->setName($newname);
25
        }
26
        foreach ($this->getOptions() as $value => $name) {
27
            $ret .= "<input type='checkbox' name='" . $this->getName() . "' value='" . $value . "'";
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28
            if (count($this->getValue()) > 0 && in_array($value, $this->getValue())) {
29
                $ret .= ' checked';
30
            }
31
            $ret .= $this->getExtra() . '>' . $name . '<br>';
32
        }
33
34
        return $ret;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function renderValidationJS()
41
    {
42
        $js .= 'var hasSelections = false;';
0 ignored issues
show
Bug introduced by
The variable $js does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
        //sometimes, there is an implicit '[]', sometimes not
44
        $eltname = $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...
45 View Code Duplication
        if (false === strpos($eltname, '[')) {
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...
46
            $js .= "for (var i = 0; i < myform['{$eltname}[]'].length; i++) {
47
                if (myform['{$eltname}[]'][i].checked) {
48
                    hasSelections = true;
49
                }
50
51
            }
52
            if (hasSelections === false) {
53
                window.alert(\"{$eltmsg}\"); myform['{$eltname}[]'][0].focus(); return false; }\n";
0 ignored issues
show
Bug introduced by
The variable $eltmsg does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
        } else {
55
            $js .= "for (var i = 0; i < myform['" . $eltname . "'].length; i++) {
56
                if (myform['{$eltname}'][i].checked) {
57
                    hasSelections = true;
58
                }
59
60
            }
61
            if (hasSelections === false) {
62
                window.alert(\"{$eltmsg}\"); myform['{$eltname}'][0].focus(); return false; }\n";
63
        }
64
65
        return $js;
66
    }
67
}
68