GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 5c516c...2319cc )
by Nicolaas
01:25
created

SecondHandProductGroup   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 6
dl 0
loc 118
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A i18n_singular_name() 0 4 1
A i18n_plural_name() 0 4 1
A getCMSFields() 0 11 1
B onBeforeWrite() 0 20 6
A hasOtherSecondHandProductGroupsOnThisLevel() 0 11 3
A BestRootParentPage() 0 12 2
A getBuyableClassName() 0 4 1
1
<?php
2
3
4
class SecondHandProductGroup extends ProductGroup
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...
5
{
6
7
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db 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
        'RootParent' => 'Boolean'
9
    );
10
11
    private static $allowed_children = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_children 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
        'SecondHandProductGroup',
13
        'SecondHandProduct'
14
    );
15
16
    private static $icon = 'ecommerce_second_hand_product/images/treeicons/SecondHandProductGroup';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $icon 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...
17
18
    /**
19
     * Standard SS variable.
20
     */
21
    private static $singular_name = 'Second Hand Product Holder';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
22
    public function i18n_singular_name()
23
    {
24
        return self::$singular_name;
25
    }
26
27
    /**
28
     * Standard SS variable.
29
     */
30
    private static $plural_name = 'Second Hand Product Holders';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
    public function i18n_plural_name()
32
    {
33
        return self::$plural_name;
34
    }
35
36
    /**
37
     * Standard SS variable.
38
     *
39
     * @var string
40
     */
41
    private static $description = 'A product category page specifically for second had products';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $description 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...
42
43
    public function getCMSFields() {
44
        $fields = parent::getCMSFields();
45
        $fields->addFieldToTab(
46
            'Root.SecondHand',
47
            CheckboxField::create(
48
                'RootParent',
49
                _t('SecondHandProductGroup.LANDING_PAGE', 'Landing Page')
50
            )
51
        );
52
        return $fields;
53
    }
54
55
    /**
56
     * Event handler called before writing to the database.
57
     */
58
    public function onBeforeWrite()
59
    {
60
        parent::onBeforeWrite();
61
        if($this->ParentID) {
62
            $parent = SiteTree::get()->byID($this->ParentID);
63
            if($parent) {
64
                if($parent instanceof SecondHandProductGroup) {
65
                    $this->RootParent = false;
66
                } else {
67
                    if(! $this->hasOtherSecondHandProductGroupsOnThisLevel()) {
68
                        $this->RootParent = true;
69
                    }
70
                }
71
            }
72
        } else {
73
            if( ! $this->hasOtherSecondHandProductGroupsOnThisLevel()) {
74
                $this->RootParent = true;
75
            }
76
        }
77
    }
78
79
    /**
80
     * Level is within SiteTree hierarchy
81
     * @return boolean
82
     */
83
    protected function hasOtherSecondHandProductGroupsOnThisLevel()
84
    {
85
        if(!$this->ParentID) {
86
            $this->ParentID = 0;
87
        }
88
        return SecondHandProductGroup::get()
89
            ->filter(array('ParentID' => $this->ParentID))
90
            ->exclude(array('ID' => $this->ID))
91
            ->count() > 0 ? true : false;
92
93
    }
94
95
    /**
96
     * @return SecondHandProductGroup
97
     */
98
    function BestRootParentPage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99
    {
100
        $obj = DataObject::get_one(
101
            'SecondHandProductGroup',
102
            array('RootParent' => 1)
103
        );
104
        if($obj) {
105
            return $obj;
106
        } else {
107
            return SecondHandProductGroup::get()->first();
108
        }
109
    }
110
111
    /**
112
     * Returns the class we are working with.
113
     *
114
     * @return string
115
     */
116
    protected function getBuyableClassName()
117
    {
118
        return 'SecondHandProduct';
119
    }
120
121
}
122
123
class SecondHandProductGroup_Controller extends ProductGroup_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

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...
124
{
125
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions 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...
126
        'SearchSecondHandProducts',
127
        'search'
128
    );
129
130
    public function init()
131
    {
132
        Config::inst()->update(
133
            'ProductGroup',
134
            'base_buyable_class',
135
            'SecondHandProduct'
136
        );
137
        parent::init();
138
        $this->showFullList = true;
139
    }
140
141
    public function SearchSecondHandProducts()
0 ignored issues
show
Coding Style introduced by
SearchSecondHandProducts uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
142
    {
143
        $fields = new FieldList(
144
            new TextField('searchterm', 'Keyword', isset($_GET['searchterm']) ? $_GET['searchterm'] : '')
145
        );
146
        $actions = new FieldList(
147
            new FormAction('doSearchSecondHandProducts', 'Search')
148
        );
149
        $validator = new RequiredFields('searchterm');
150
        $form = Form::create($this, 'SearchSecondHandProducts', $fields, $actions, $validator);
151
        $form->setFormMethod('GET');
152
        $form->disableSecurityToken();
153
154
        return $form;
155
    }
156
157
    public function doSearchSecondHandProducts($data, $form)
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...
158
    {
159
        $page = SecondHandProductGroup::get()->first();
160
        if ($page) {
161
            return $this->redirect($this->link('search').'?searchterm='.$data['searchterm']);
162
        }
163
    }
164
165
    public function search($request)
166
    {
167
        $term = Convert::raw2sql($request->param('searchterm'));
0 ignored issues
show
Unused Code introduced by
$term 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...
168
    }
169
170
    public function HasSearchFilterAndSort()
171
    {
172
        return true;
173
    }
174
}
175