| Conditions | 13 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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; |
||
| 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; |
||
| 117 | $obj->SessionID = "default_votes_".$i; |
||
| 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 | |||
| 161 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.