sfWidgetFormSelect2PropelChoice   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 1
dl 94
loc 94
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 7 7 1
A getChoices() 10 10 4
B render() 28 28 2
A getStylesheets() 4 4 1
A getJavascripts() 4 4 1

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
require_once(dirname(__FILE__) . '/../select2/Select2.class.php');
3
4
/**
5
 * This widget is designed to generate more user friendly autocomplete widgets.
6
 *
7
 * @package     symfony
8
 * @subpackage  widget
9
 * @link        https://github.com/19Gerhard85/sfSelect2WidgetsPlugin
10
 * @author      Ing. Gerhard Schranz <[email protected]>
11
 * @version     0.1 2013-03-11
12
 */
13 View Code Duplication
class sfWidgetFormSelect2PropelChoice extends sfWidgetFormPropelChoice
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * Configures the current widget.
17
     *
18
     * Available options:
19
     *
20
     *  * url:            The URL to call to get the choices to use (required)
21
     *  * config:         A JavaScript array that configures the JQuery autocompleter widget
22
     *  * value_callback: A callback that converts the value before it is displayed
23
     *
24
     * @param array $options     An array of options
25
     * @param array $attributes  An array of default HTML attributes
26
     *
27
     * @see sfWidgetForm
28
     */
29
    protected function configure($options = array(), $attributes = array())
30
    {
31
        $this->addOption('culture', sfContext::getInstance()->getUser()->getCulture());
32
        $this->addOption('width', sfConfig::get('sf_sfSelect2Widgets_width'));
33
34
        parent::configure($options, $attributes);
35
    }
36
37
    public function getChoices()
38
    {
39
        $choices = parent::getChoices();
40
41
        if (count($choices) > 0 && isset($choices['']) && $choices[''] == '') {
42
            $choices[''] = '&nbsp;';
43
        }
44
45
        return $choices;
46
    }
47
48
    /**
49
     * @param  string $name        The element name
50
     * @param  string $value       The date displayed in this widget
51
     * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
52
     * @param  array  $errors      An array of errors for the field
53
     *
54
     * @return string An HTML tag string
55
     *
56
     * @see sfWidgetForm
57
     */
58
    public function render($name, $value = null, $attributes = array(), $errors = array())
59
    {
60
        $id = $this->generateId($name);
61
62
        $return = parent::render($name, $value, $attributes, $errors);
63
64
        $return .= sprintf(<<<EOF
65
<script type="text/javascript">
66
function formatResult(item)
67
{
68
    return item.text;
69
}
70
71
jQuery("#%s").select2(
72
{
73
    width:              '%s',
74
    allowClear:         %s
75
});
76
</script>
77
EOF
78
            ,
79
            $id,
80
            $this->getOption('width'),
81
            $this->getOption('add_empty') == true ? 'true' : 'false'
82
        );
83
84
        return $return;
85
    }
86
87
    /**
88
     * Gets the stylesheet paths associated with the widget.
89
     *
90
     * @return array An array of stylesheet paths
91
     */
92
    public function getStylesheets()
93
    {
94
        return Select2::addStylesheets();
95
    }
96
97
    /**
98
     * Gets the JavaScript paths associated with the widget.
99
     *
100
     * @return array An array of JavaScript paths
101
     */
102
    public function getJavascripts()
103
    {
104
        return Select2::addJavascripts($this->getOption('culture'));
105
    }
106
}
107