PageRaterStarField   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 20.59 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 21
loc 102
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B Field() 21 63 8
A getRequiredFields() 0 5 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
3
class PageRaterStarField extends FormField
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...
4
{
5
    private static $extra_form_selector = '';
0 ignored issues
show
Unused Code introduced by
The property $extra_form_selector is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
7
    private static $allow_comments = false;
0 ignored issues
show
Unused Code introduced by
The property $allow_comments is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
8
9
    private static $allow_name = false;
0 ignored issues
show
Unused Code introduced by
The property $allow_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    private static $allow_title = false;
0 ignored issues
show
Unused Code introduced by
The property $allow_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    protected $rating = 0;
14
15
    protected $starOptions = 5;
16
17
    protected $moreFieldsArray = array();
18
19
    /**
20
     * Returns an input field, class="start field" and type="hidden" with an optional maxlength
21
     */
22
    public function __construct($name, $title = null, $value = "", $starOptions = null, $form = null)
23
    {
24
        if ($starOptions) {
25
            $this->starOptions = $starOptions;
26
        } else {
27
            $this->starOptions = PageRating::get_number_of_stars();
28
        }
29
30
        parent::__construct($name, $title, $value, $form);
0 ignored issues
show
Unused Code introduced by
The call to FormField::__construct() has too many arguments starting with $form.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
31
    }
32
33
    protected $_field_value = null;
34
35
    public function Field($properties = array())
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...
36
    {
37
        if (! $this->_field_value) {
38
            Requirements::javascript(THIRDPARTY_DIR .'/jquery/jquery.js');
39
            Requirements::javascript(THIRDPARTY_DIR .'/jquery-form/jquery.form.js');
40
            Requirements::javascript('pagerater/javascript/jquery.rating.pack.js');
41
            Requirements::javascript('pagerater/javascript/PageRater.js');
42
            if ($this->Config()->get("extra_form_selector")) {
43
                Requirements::customScript("PageRater.set_extra_form_selector('".$this->Config()->get("extra_form_selector")."');");
44
            }
45
            Requirements::themedCSS('jquery.rating', "pagerater");
46
            $html = "";
47
48
            $name = $this->getName();
49
            $id = $this->id();
50
            for ($i = 1; $i < $this->starOptions + 1; $i++) {
51
                if ($i == $this->Value()) {
52
                    $html .= "<input name='$id' class='$id' type='radio' checked='checked' value='$i' />";
53
                } else {
54
                    $html .= "<input name='$id' class='$id' type='radio' value='$i' />";
55
                }
56
            }
57
            $html .= "<input name='$name' type='hidden' id='$id' />";
58
59 View Code Duplication
            if ($this->Config()->get("allow_name")) {
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...
60
                $fieldID = $id."_Name";
61
                $nameField = TextField::create($fieldID, _t("PageRaterStarField.NAME_LABEL", "Your Name"));
62
                $nameField->addExtraClass("additionalComments");
63
                $html .= $nameField->FieldHolder();
64
                $this->moreFieldsArray[] = $fieldID;
65
            }
66
67 View Code Duplication
            if ($this->Config()->get("allow_title")) {
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...
68
                $fieldID = $id."_Title";
69
                $titleField = TextField::create($fieldID, _t("PageRaterStarField.TITLE_LABEL", "Comment Header"));
70
                $titleField->addExtraClass("additionalComments");
71
                $html .= $titleField->FieldHolder();
72
                $this->moreFieldsArray[] = $fieldID;
73
            }
74
75 View Code Duplication
            if ($this->Config()->get("allow_comments")) {
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...
76
                $fieldID = $id."_Comment";
77
                $commentField = TextareaField::create($fieldID, _t("PageRaterStarField.COMMENTS_LABEL", "Any comments you may have"));
78
                $commentField->addExtraClass("additionalComments");
79
                $html .= $commentField->FieldHolder();
80
                $this->moreFieldsArray[] = $fieldID;
81
            }
82
            $moreFieldsAsString = implode("', '", $this->moreFieldsArray);
83
            Requirements::customScript('
84
            if(typeof PageRaterVariables === "undefined") {
85
                var PageRaterVariables = [];
86
            }
87
            PageRaterVariables.push(
88
                {
89
                    id: \''.$id.'\',
90
                    fields: [\''.$moreFieldsAsString.'\']
91
                }
92
            );
93
            ');
94
            $this->_field_value = $html;
95
        }
96
        return $this->_field_value;
97
    }
98
99
    public function getRequiredFields()
100
    {
101
        $this->Field();
102
        return $this->moreFieldsArray;
103
    }
104
}
105