| Conditions | 23 |
| Paths | 12 |
| Total Lines | 86 |
| Code Lines | 67 |
| 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 |
||
| 81 | public function requireDefaultRecords() |
||
| 82 | { |
||
| 83 | parent::requireDefaultRecords(); |
||
| 84 | $bt = defined('DB::USE_ANSI_SQL') ? '"' : '`'; |
||
| 85 | |||
| 86 | $update = array(); |
||
| 87 | $siteConfig = DataObject::get_one('SiteConfig'); |
||
| 88 | $folder = Folder::findOrMake(self::get_folder_name()); |
||
| 89 | if ($siteConfig && $folder) { |
||
| 90 | $fullArray = self::get_images_to_replace(); |
||
| 91 | //copying .... |
||
| 92 | if ($fullArray) { |
||
| 93 | foreach ($fullArray as $key => $array) { |
||
| 94 | $className = $array["ClassName"]; |
||
| 95 | $fieldName = $array["FieldName"]."ID"; |
||
| 96 | if (class_exists($className)) { |
||
| 97 | $dataObject = singleton($className); |
||
| 98 | $dbFieldName = $array["DBFieldName"]; |
||
| 99 | $fileName = basename($array["CopyFromPath"]); |
||
| 100 | $fromLocationLong = Director::baseFolder().'/'.$array["CopyFromPath"]; |
||
| 101 | $toLocationShort = "assets/".self::get_folder_name()."/{$fileName}"; |
||
| 102 | $toLocationLong = Director::baseFolder().'/'.$toLocationShort; |
||
| 103 | $image = DataObject::get_one('Image', "Filename='$toLocationShort' AND ParentID = ".$folder->ID); |
||
| 104 | if (!$image) { |
||
| 105 | if (!file_exists($toLocationLong)) { |
||
| 106 | copy($fromLocationLong, $toLocationLong); |
||
| 107 | } |
||
| 108 | $image = new Image(); |
||
| 109 | $image->ParentID = $folder->ID; |
||
| 110 | $image->FileName = $toLocationShort; |
||
| 111 | $image->setName($fileName); |
||
| 112 | $image->write(); |
||
| 113 | } elseif (!$image && file_exists($toLocationLong)) { |
||
| 114 | debug::show("need to update files"); |
||
| 115 | } |
||
| 116 | if ($image && $image->ID) { |
||
| 117 | if (!$siteConfig->$dbFieldName) { |
||
| 118 | $siteConfig->$dbFieldName = $image->ID; |
||
| 119 | $update[]= "created placeholder image for $key"; |
||
| 120 | } |
||
| 121 | $updateSQL = " UPDATE {$bt}".$className."{$bt}"; |
||
| 122 | if (isset($_GET["removeplaceholderimages"])) { |
||
| 123 | $setSQL = " SET {$bt}".$fieldName."{$bt} = 0"; |
||
| 124 | $whereSQL = " WHERE {$bt}".$fieldName."{$bt} = ".$image->ID; |
||
| 125 | DB::alteration_message("removing ".$className.".".$fieldName." placeholder images", 'deleted'); |
||
| 126 | } else { |
||
| 127 | DB::alteration_message("adding ".$className.".".$fieldName." placeholder images", 'created'); |
||
| 128 | $setSQL = " SET {$bt}".$fieldName."{$bt} = ".$image->ID; |
||
| 129 | if (!isset($_GET["forceplaceholder"])) { |
||
| 130 | $whereSQL = " WHERE {$bt}".$fieldName."{$bt} IS NULL OR {$bt}".$fieldName."{$bt} = 0"; |
||
| 131 | } else { |
||
| 132 | $whereSQL = ''; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $sql = $updateSQL.$setSQL.$whereSQL; |
||
| 136 | DB::query($sql); |
||
| 137 | $versioningPresent = false; |
||
| 138 | $array = $dataObject->stat('extensions'); |
||
| 139 | if (is_array($array) && count($array)) { |
||
| 140 | if (in_array("Versioned('Stage', 'Live')", $array)) { |
||
| 141 | $versioningPresent = true; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | if ($dataObject->stat('versioning')) { |
||
| 145 | $versioningPresent = true; |
||
| 146 | } |
||
| 147 | if ($versioningPresent) { |
||
| 148 | $sql = str_replace("{$bt}$className{$bt}", "{$bt}{$className}_Live{$bt}", $sql); |
||
| 149 | DB::query($sql); |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | debug::show("could not create image!".print_r($array)); |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | debug::show("bad classname reference ".$className); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | if (count($update)) { |
||
| 160 | $siteConfig->write(); |
||
| 161 | DB::alteration_message($siteConfig->ClassName." created/updated: ".implode(" --- ", $update), 'created'); |
||
| 162 | } |
||
| 163 | } elseif (!$folder) { |
||
| 164 | debug::show("COULD NOT CREATE FOLDER: ".self::get_folder_name()); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 |
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.