SiteTreeCheckboxSetField   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 13.6 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 17
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setSiteTreeParentID() 0 4 1
A setSiteTreeParentAndChildClassNames() 0 5 1
A setClassNamesForItems() 0 10 2
A addClassNameForItems() 0 4 1
A getSource() 0 10 2
B getAllChildrenForSiteTreeParent() 17 17 6

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 SiteTreeCheckboxSetField extends CheckboxSetField
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
6
    /**
7
     *
8
     * @var Int
9
     */
10
    protected $siteTreeParentID = 0;
11
12
    /**
13
     *
14
     * @var Array
15
     */
16
    protected $arrayOfAllowedIDs = array();
17
18
    /**
19
     *
20
     * @var array
21
     */
22
    protected $classNamesForItems = array("SiteTree");
23
24
    /**
25
     *
26
     * @var Array
27
     */
28
    protected $siteTreeParentAllChildren = array();
29
30
    /**
31
     * Creates a new dropdown field.
32
     *
33
     * @param string $name The field name
34
     * @param string $title The field title
35
     * @param array $source An map of the dropdown items
36
     * @param string|array $value You can pass an array of values or a single value like a drop down to be selected
37
     * @param int $size Optional size of the select element
0 ignored issues
show
Bug introduced by
There is no parameter named $size. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
     * @param form The parent form
0 ignored issues
show
Documentation introduced by
Should the type for parameter $form not be The|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     */
40
    public function __construct($name, $title = '', $source = array(), $value='', $form=null, $emptyString=null)
41
    {
42
        Requirements::css('sitetreeformfields/css/SiteTreeCheckboxSetField.css');
43
        parent::__construct($name, $title, $source, $value, $form, $emptyString);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 40 can also be of type array; however, DropdownField::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
    }
45
46
    /**
47
     *
48
     * @param Int $id
49
     */
50
    public function setSiteTreeParentID($id)
51
    {
52
        $this->siteTreeParentID = $id;
53
    }
54
55
    /**
56
     *
57
     * @param Int $id
58
     * @param Int | string $array
59
     */
60
    public function setSiteTreeParentAndChildClassNames($id, $array)
61
    {
62
        $this->siteTreeParentID = $id;
63
        $this->setClassNamesForItems($array);
64
    }
65
66
    /**
67
     *
68
     * @param Array | str $array
69
     */
70
    public function setClassNamesForItems($array)
71
    {
72
        unset($this->classNamesForItems);
73
        if (is_array($array)) {
74
            $this->classNamesForItems = $array;
75
        } else {
76
            $this->classNamesForItems = array($array);
77
        }
78
        $this->source = $this->getSource();
79
    }
80
81
    /**
82
     *
83
     * @param String $str
84
     */
85
    public function addClassNameForItems($str)
86
    {
87
        $this->classNamesForItems[] = $str;
88
    }
89
90
91
    /**
92
     *
93
     * @return Array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|ArrayAccess?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
94
     */
95
    public function getSource()
96
    {
97
        $source = parent::getSource();
98
        //debug::log("original source count ".implode($this->classNamesForItems)." ".$this->siteTreeParentID.": ".count($source));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
        if ($this->siteTreeParentID) {
100
            $arrayItems = array();
0 ignored issues
show
Unused Code introduced by
$arrayItems is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
            $source = $this->getAllChildrenForSiteTreeParent($this->siteTreeParentID);
102
        }
103
        return $source;
104
    }
105
106
    /**
107
     * @param Int
108
     * @return Array
109
     */
110 View Code Duplication
    private function getAllChildrenForSiteTreeParent($parentID)
0 ignored issues
show
Duplication introduced by
This method 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...
111
    {
112
        $children = SiteTree::get()->filter(array("ParentID" => $parentID));
113
        if ($children && $children->count()) {
114
            foreach ($children as $child) {
115
                foreach ($this->classNamesForItems as $matchingCLassName) {
116
                    //debug::log("has child");
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
                    if ($child instanceof $matchingCLassName) {
118
                        //debug::log("we now have ".count($this->siteTreeParentAllChildren)." children");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
                        $this->siteTreeParentAllChildren[$child->ID] = $child->MenuTitle;
120
                    }
121
                }
122
                $this->getAllChildrenForSiteTreeParent($child->ID);
123
            }
124
        }
125
        return $this->siteTreeParentAllChildren;
126
    }
127
}
128