EcommerceVote::requireDefaultRecords()   C
last analyzed

Complexity

Conditions 13
Paths 18

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 5.6547
c 0
b 0
f 0
cc 13
eloc 38
nc 18
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class EcommerceVote extends DataObject
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
    public static $db = array(
6
        "SessionID" => "Varchar(64)"
7
    );
8
9
    public static $has_one = array(
10
        "Page" => "SiteTree"
11
    );
12
13
    protected static $array_of_classes_used = array();
14
    public static function set_array_of_classes_used($v)
15
    {
16
        self::$array_of_classes_used = $v;
17
    }
18
    public static function get_array_of_classes_used()
19
    {
20
        return self::$array_of_classes_used;
21
    }
22
23
    protected static $create_defaults = false;
24
    public static function set_create_defaults($v)
25
    {
26
        self::$create_defaults = $v;
27
    }
28
    public static function get_create_defaults()
29
    {
30
        return self::$create_defaults;
31
    }
32
33
    protected static $default_votes = 100;
34
    public static function set_default_votes($v)
35
    {
36
        self::$default_votes = $v;
37
    }
38
    public static function get_default_votes()
39
    {
40
        return self::$default_votes;
41
    }
42
43
    protected static $random_size_to_add_to_default = 10;
44
    public static function set_random_size_to_add_to_default($v)
45
    {
46
        self::$random_size_to_add_to_default = $v;
47
    }
48
    public static function get_random_size_to_add_to_default()
49
    {
50
        return self::$random_size_to_add_to_default;
51
    }
52
53
    public static $has_many = array();
54
55
    public static $many_many = array();
56
57
    public static $belongs_many_many = array();
58
59
    public static $many_many_extraFields = array();
60
61
    //database related settings
62
    public static $indexes = array(
63
        "SessionID" => true,
64
    );
65
66
    public function onBeforeWrite()
67
    {
68
        parent::onBeforeWrite();
69
        if (!$this->SessionID) {
0 ignored issues
show
Documentation introduced by
The property SessionID does not exist on object<EcommerceVote>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
            $this->SessionID = Session_ID();
0 ignored issues
show
Documentation introduced by
The property SessionID does not exist on object<EcommerceVote>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
        }
72
    }
73
74
    public function onAfterWrite()
75
    {
76
        parent::onAfterWrite();
77
        if (DataObject::get_one("EcommerceVote", "SessionID = '".Session_ID()."' AND PageID =".$this->PageID." AND ID <> ".$this->ID)) {
0 ignored issues
show
Documentation introduced by
The property PageID does not exist on object<EcommerceVote>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property ID does not exist on object<EcommerceVote>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
            $this->delete();
79
        }
80
    }
81
82
83
    public function requireDefaultRecords()
84
    {
85
        parent::requireDefaultRecords();
86
        $objects = DataObject::get("EcommerceVote", $filter = "", $sort = "Created DESC", $join = "", $limit = "0, 1000");
87
        $array = array();
88
        if ($objects) {
89
            foreach ($objects as $obj) {
90
                if (isset($array[$obj->PageID])) {
91
                    if ($array[$obj->PageID] == $obj->SessionID) {
92
                        $obj->delete();
93
                        DB::alteration_message("deleting double vote", "deleted");
94
                    }
95
                } else {
96
                    $array[$obj->PageID] = $obj->SessionID;
97
                }
98
            }
99
        }
100
        unset($array);
101
        $array = null;
0 ignored issues
show
Unused Code introduced by
$array 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...
102
        if (self::get_create_defaults()) {
103
            $array = self::get_array_of_classes_used();
104
            if (!is_array($array)|| !count($array)) {
105
                $array = array("SiteTree");
106
            }
107
            if (count($array)) {
108
                foreach ($array as $className) {
109
                    $pages = DataObject::get($className, "EcommerceVote.ID IS NULL", $sort = "", $join = "LEFT JOIN EcommerceVote on EcommerceVote.PageID = SiteTree.ID");
110
                    if ($pages) {
111
                        foreach ($pages as $page) {
112
                            $number = intval(self::get_default_votes() + rand(0, self::get_random_size_to_add_to_default()));
113
                            $i = 0;
114
                            while ($i < $number) {
115
                                $obj = new EcommerceVote();
116
                                $obj->PageID = $page->ID;
0 ignored issues
show
Documentation introduced by
The property PageID does not exist on object<EcommerceVote>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
                                $obj->SessionID = "default_votes_".$i;
0 ignored issues
show
Documentation introduced by
The property SessionID does not exist on object<EcommerceVote>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
                                $obj->write();
119
                                DB::alteration_message("creating vote $i for ".$page->Title, "created");
120
                                $i++;
121
                            }
122
                        }
123
                    } else {
124
                        DB::alteration_message("no pages for $className", "deleted");
125
                    }
126
                }
127
            } else {
128
                DB::alteration_message("classname array is empty", "deleted");
129
            }
130
        } else {
131
            DB::alteration_message("not creating defaults in EcommerceVote", "deleted");
132
        }
133
    }
134
135
    public static $searchable_fields = array("PageID");
136
137
    public static $field_labels = array("PageID" => "Page");
138
139
    public static $summary_fields = array("Page.Title");
140
141
    public static $singular_name = "Vote";
142
    public function i18n_single_name()
143
    {
144
        return _t("EcommerceVote.ECOMMERCEVOTE", "Vote");
145
    }
146
147
    public static $plural_name = "Votes";
148
    public function i18n_plural_name()
149
    {
150
        return _t("EcommerceVote.ECOMMERCEVOTES", "Votes");
151
    }
152
}
153
154
155
class EcommerceVote_ModelAdmin extends ModelAdmin
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...
156
{
157
    public static $managed_models = array("EcommerceVote");
158
    public static $url_segment = 'votes';
159
    public static $menu_title = 'Votes';
160
}
161