Completed
Push — master ( 9200bf...e0f8ea )
by Richard
15s
created

Select   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 98.15%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
lcom 2
cbo 1
dl 21
loc 147
ccs 53
cts 54
cp 0.9815
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isMultiple() 0 4 1
A getSize() 0 4 1
A addOptionGroup() 0 4 1
A __construct() 0 15 3
A renderOption() 0 10 2
B render() 0 27 6
A renderValidationJS() 21 21 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
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * Select - a select element
16
 *
17
 * @category  Xoops\Form\Select
18
 * @package   Xoops\Form
19
 * @author    Kazumi Ono <[email protected]>
20
 * @author    Taiwen Jiang <[email protected]>
21
 * @copyright 2001-2015 XOOPS Project (http://xoops.org)
22
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @link      http://xoops.org
24
 */
25
class Select extends OptionElement
26
{
27
    /**
28
     * Pre-selected values
29
     *
30
     * @var array
31
     */
32
    protected $value = array();
33
34
    /**
35
     * Constructor
36
     *
37
     * @param string|array $caption  Caption or array of all attributes
38
     * @param string       $name     name" attribute
39
     * @param mixed        $value    Pre-selected value (or array of them).
40
     * @param integer      $size     Number or rows. "1" makes a drop-down-list
41
     * @param boolean      $multiple Allow multiple selections?
42
     */
43 12
    public function __construct($caption, $name = null, $value = null, $size = 1, $multiple = false)
44
    {
45 12
        if (is_array($caption)) {
46 2
            parent::__construct($caption);
47 2
            $this->setIfNotSet('size', 1);
48
        } else {
49 11
            $this->setWithDefaults('caption', $caption, '');
50 11
            $this->setWithDefaults('name', $name, 'name_error');
51 11
            $this->set('value', $value);
52 11
            $this->setWithDefaults('size', $size, 1);
53 11
            if ($multiple) {
54
                $this->set('multiple');
55
            }
56
        }
57 12
    }
58
59
    /**
60
     * Are multiple selections allowed?
61
     *
62
     * @return bool
63
     */
64 1
    public function isMultiple()
65
    {
66 1
        return $this->has('multiple');
67
    }
68
69
    /**
70
     * Get the size
71
     *
72
     * @return int
73
     */
74 1
    public function getSize()
75
    {
76 1
        return (int) $this->get('size');
77
    }
78
79
     /**
80
     * Add multiple optgroup
81
     *
82
     * @param string $name     name attribute
83
     * @param array  $optgroup Associative array of value->name pairs
84
     *
85
     * @return void
86
     */
87 1
    public function addOptionGroup($name, $optgroup)
88
    {
89 1
        $this->setArrayItem('option', $name, $optgroup);
90 1
    }
91
92
    /**
93
     * render a single option
94
     *
95
     * @param string   $optionValue   option element value
96
     * @param string   $optionDisplay displayed text
97
     * @param string[] $selected      selected option values
98
     *
99
     * @return string
100
     */
101 2
    protected function renderOption($optionValue, $optionDisplay, $selected)
102
    {
103 2
        $rendered = '<option value="' . htmlspecialchars($optionValue, ENT_QUOTES) . '"';
104 2
        if (in_array($optionValue, $selected)) {
105 1
            $rendered .= ' selected="selected"';
106
        }
107 2
        $rendered .= '>' . $optionDisplay . '</option>' . "\n";
108
109 2
        return $rendered;
110
    }
111
112
    /**
113
     * Prepare HTML for output
114
     *
115
     * @return string HTML
116
     */
117 16
    public function render()
118
    {
119 16
        $selected = (array) $this->getValue();
120
121 16
        $ele_options = $this->getOptions();
122
123 16
        $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
124 16
        $this->themeDecorateElement();
125 16
        $attributes = $this->renderAttributeString();
126 16
        $rendered = '<select ' . $attributes . $extra .' >' . "\n";
127
128 16
        if (empty($ele_optgroup)) {
0 ignored issues
show
Bug introduced by
The variable $ele_optgroup seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
129 16
            foreach ($ele_options as $value => $display) {
130 16
                if (is_array($display)) {
131 1
                    $rendered .= '<optgroup label="' . $value . '">' . "\n";
132 1
                    foreach ($display as $optvalue => $optdisplay) {
133 1
                        $rendered .= $this->renderOption($optvalue, $optdisplay, $selected);
134
                    }
135
                } else {
136 16
                    $rendered .= $this->renderOption($value, $display, $selected);
137
                }
138
            }
139
        }
140 16
        $rendered .= '</select>' . "\n";
141
142 16
        return $rendered;
143
    }
144
145
    /**
146
     * Render custom javascript validation code
147
     *
148
     * @return string
149
     */
150 2 View Code Duplication
    public function renderValidationJS()
151
    {
152
        // render custom validation code if any
153 2
        if (!empty($this->customValidationCode)) {
154
            return implode("\n", $this->customValidationCode);
155
            // generate validation code if required
156 2
        } elseif ($this->isRequired()) {
157 1
            $eltname = $this->getName();
158 1
            $eltcaption = $this->getCaption();
159 1
            $eltmsg = empty($eltcaption)
160
                ? sprintf(\XoopsLocale::F_ENTER, $eltname)
161 1
                : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
162 1
            $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
163 1
            return "\nvar hasSelected = false; var selectBox = myform.{$eltname};"
164 1
                . "for (i = 0; i < selectBox.options.length; i++ ) { "
165 1
                . "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') "
166 1
                . "{ hasSelected = true; break; } }" . "if (!hasSelected) "
167 1
                . "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
168
        }
169 2
        return '';
170
    }
171
}
172