Dropdown2AutocompleteField   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 98.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 59
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A turnOnAutocomplete() 5 5 1
A turnOffAutocomplete() 5 5 1
B Field() 27 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
2
3
/**
4
 * just like a Dropdown Field but now with an autocomplete
5
 * system baked into it.
6
 *
7
 *
8
 */
9 View Code Duplication
class Dropdown2AutocompleteField extends DropdownField
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...
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...
10
{
11
12
    /**
13
     * @var boolean
14
     */
15
    private $autocomplete = true;
16
17
    /**
18
     *
19
     * @return $this
20
     */
21
    public function turnOnAutocomplete()
22
    {
23
        $this->autocomplete = true;
24
        return $this;
25
    }
26
27
    /**
28
     *
29
     * @return $this
30
     */
31
    public function turnOffAutocomplete()
32
    {
33
        $this->autocomplete = false;
34
        return $this;
35
    }
36
37
    /**
38
     * @param array $parameters
39
     * @return string
40
     */
41
    public function Field($parameters = array())
42
    {
43
        if ($this->autocomplete) {
44
            $this->addExtraClass("chosenAutocompleteField");
45
            $field = parent::Field($parameters);
46
            Requirements::css("dropdown2autocomplete/javascript/chosen/chosen.min.css");
47
            Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
48
            Requirements::javascript("dropdown2autocomplete/javascript/chosen/chosen.jquery.min.js");
49
50
            Requirements::customScript(
51
                '
52
                    jQuery("#'.$this->ID().'").chosen('.$this->Config()->get("js_settings").');
53
                    jQuery("body").on(
54
                        "focus",
55
                        ".chosenAutocompleteField:visible",
56
                        function(){
57
                            jQuery(this).chosen('.$this->Config()->get("js_settings").');
58
                        }
59
                    );
60
                ',
61
                $this->ID()."_chosen_setup"
62
            );
63
        } else {
64
            $field = parent::Field($parameters);
65
        }
66
        return $field;
67
    }
68
}
69