Conditions | 40 |
Paths | 2304 |
Total Lines | 159 |
Code Lines | 113 |
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 |
||
52 | public function run($request) |
||
53 | { |
||
54 | if (isset($_GET["forreal"])) { |
||
55 | $this->forReal = $_GET["forreal"]; |
||
56 | } |
||
57 | if (isset($_GET["summaryonly"])) { |
||
58 | $this->summaryOnly = $_GET["summaryonly"]; |
||
59 | DB::alteration_message("Prefer <a href=\"".$this->linkWithGetParameter("all", 1)."\">all details</a>?<hr />", "repaired"); |
||
60 | } |
||
61 | if (isset($_GET["doone"])) { |
||
62 | $this->forReal = 1; |
||
63 | $this->doOne = urldecode($_GET["doone"]); |
||
64 | } |
||
65 | if (isset($_GET["cleanupfolder"])) { |
||
66 | $this->cleanupFolder = intval($_GET["cleanupfolder"]); |
||
67 | } |
||
68 | |||
69 | //work out the folders to ignore... |
||
70 | foreach ($this->Config()->get("folders_to_ignore") as $folderToIgnoreName) { |
||
71 | $folderToIgnore = Folder::find_or_make($folderToIgnoreName); |
||
72 | $this->addListOfIgnoreFoldersArray($folderToIgnore); |
||
73 | } |
||
74 | if (count($this->listOfIgnoreFoldersArray)) { |
||
75 | DB::alteration_message("Files in the following Folders will be ignored: <br /> - ".implode("<br /> - ", $this->listOfIgnoreFoldersArray)."<hr />", "repaired"); |
||
76 | } |
||
77 | if (!$this->cleanupFolder) { |
||
78 | if (!$this->forReal) { |
||
79 | DB::alteration_message("Apply <a href=\"".$this->linkWithGetParameter("forreal", 1)."\">all suggested changes</a>? CAREFUL!<hr />", "deleted"); |
||
80 | } |
||
81 | if (!$this->summaryOnly) { |
||
82 | DB::alteration_message("Prefer a <a href=\"".$this->linkWithGetParameter("summaryonly", 1)."\">summary only</a>?<hr />", "repaired"); |
||
83 | } |
||
84 | $checks = MetaTagCMSControlFileUse::get()-where("\"ConnectionType\" IN ('HAS_ONE') AND \"IsLiveVersion\" = 0 AND \"DataObjectClassName\" <> 'File'"); |
||
85 | if ($checks && $checks->count()) { |
||
86 | foreach ($checks as $check) { |
||
87 | $folderName = $check->DataObjectClassName."_".$check->DataObjectFieldName; |
||
88 | if (!$this->doOne || $this->doOne == $folderName) { |
||
89 | $objectName = $check->DataObjectClassName; |
||
90 | $fieldName = $check->DataObjectFieldName."ID"; |
||
91 | $fileClassName = $check->FileClassName; |
||
92 | $folder = null; |
||
93 | DB::alteration_message( |
||
94 | "<hr /><h3>All files attached to $objectName . $fieldName <a href=\"".$this->linkWithGetParameter("doone", $folderName)."\">can be moved to</a> <span style=\"color: green;\">$folderName</span></h3>" |
||
95 | ); |
||
96 | if ($this->summaryOnly) { |
||
97 | //do nothing |
||
98 | } else { |
||
99 | $objects = null; |
||
100 | if ($check->FileIsFile) { |
||
101 | $objects = $objectName::get()->where("\"".$fieldName."\" > 0"); |
||
102 | } elseif ($check->DataObjectIsFile) { |
||
103 | $objects = $objectName::get()->where("\"".$fieldName."\" > 0"); |
||
104 | } |
||
105 | if ($objects && $objects->count()) { |
||
106 | foreach ($objects as $object) { |
||
107 | if ($object instanceof File) { |
||
108 | $file = $object;//do nothing |
||
109 | } else { |
||
110 | $file = File::get()->byID($object->$fieldName); |
||
111 | } |
||
112 | if ($file) { |
||
113 | if ($file instanceof Folder) { |
||
114 | //do nothing |
||
115 | } else { |
||
116 | if (!$folder) { |
||
117 | $folder = Folder::find_or_make($folderName); |
||
118 | } |
||
119 | if ($file->ParentID == $folder->ID) { |
||
120 | DB::alteration_message( |
||
121 | "OK ... ". $file->FileName, |
||
122 | "created" |
||
123 | ); |
||
124 | } else { |
||
125 | if (isset($this->listOfIgnoreFoldersArray[$file->ParentID])) { |
||
126 | DB::alteration_message( |
||
127 | "NOT MOVING (folder to be ignored): <br />/".$file->FileName." to <br />/assets/".$folderName."/".$file->Name."", |
||
128 | "repaired" |
||
129 | ); |
||
130 | } else { |
||
131 | DB::alteration_message( |
||
132 | "MOVING: <br />/".$file->FileName." to <br />/assets/".$folderName."/".$file->Name."", |
||
133 | "created" |
||
134 | ); |
||
135 | if ($this->forReal) { |
||
136 | if ($file->exists()) { |
||
137 | if (file_exists($file->getFullPath())) { |
||
138 | $file->ParentID = $folder->ID; |
||
139 | $file->write(); |
||
140 | } else { |
||
141 | DB::alteration_message( |
||
142 | "ERROR: phyiscal file could not be found: ".$file->getFullPath()." ", |
||
143 | "deleted" |
||
144 | ); |
||
145 | } |
||
146 | } else { |
||
147 | DB::alteration_message( |
||
148 | "ERROR: file not found in database: /".$file->FileName." ", |
||
149 | "deleted" |
||
150 | ); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } else { |
||
157 | DB::alteration_message( |
||
158 | "Could not find file referenced by ".$object->getTitle()." (".$object->class.", ".$object->ID.")", |
||
159 | "deleted" |
||
160 | ); |
||
161 | } |
||
162 | } |
||
163 | } else { |
||
164 | DB::alteration_message("No objects in $objectName $fieldName.", "deleted"); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | } else { |
||
170 | DB::alteration_message("Could not find any checks, please run /dev/build/", "deleted"); |
||
171 | } |
||
172 | } else { |
||
173 | DB::alteration_message("We are now showing folders only; <a href=\"".$this->linkWithGetParameter("all", 1)."\">view all</a><hr />", "restored"); |
||
174 | } |
||
175 | DB::alteration_message("---------------------------------------"); |
||
176 | DB::alteration_message("---------------------------------------"); |
||
177 | DB::alteration_message("CLEANING FOLDERS"); |
||
178 | DB::alteration_message("---------------------------------------"); |
||
179 | DB::alteration_message("---------------------------------------"); |
||
180 | $folders = Folder::get(); |
||
181 | $hasEmptyFolders = false; |
||
182 | if ($folders && $folders->count()) { |
||
183 | foreach ($folders as $folder) { |
||
184 | if (!MetaTagCMSControlFileUse::file_usage_count($folder, true)) { |
||
185 | $hasEmptyFolders = true; |
||
186 | if (file_exists($folder->getFullPath())) { |
||
187 | if (($this->cleanupFolder != $folder->ID) && ($this->cleanupFolder != -1)) { |
||
188 | DB::alteration_message("found an empty folder: <strong>".$folder->FileName."</strong>; <a href=\"".$this->linkWithGetParameter("cleanupfolder", $folder->ID)."\">delete now</a>?", "restored"); |
||
189 | } |
||
190 | if (($this->cleanupFolder == $folder->ID) || $this->cleanupFolder == -1) { |
||
191 | DB::alteration_message(" |
||
192 | Deleting empty folder: <strong>".$folder->FileName."</strong>", |
||
193 | "deleted" |
||
194 | ); |
||
195 | $folder->delete(); |
||
196 | } |
||
197 | } else { |
||
198 | DB::alteration_message("Could not find this phyiscal folder - it is empty can be deleted: ".$folder->getFullPath(), "deleted"); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } else { |
||
203 | DB::alteration_message("Could not find any folders. There might be something wrong!", "deleted"); |
||
204 | } |
||
205 | if (!$hasEmptyFolders) { |
||
206 | DB::alteration_message("There are no empty folders!", "created"); |
||
207 | } else { |
||
208 | DB::alteration_message("Delete <a href=\"".$this->linkWithGetParameter("cleanupfolder", -1)."\">all empty folders</a>?", "deleted"); |
||
209 | } |
||
210 | } |
||
211 | |||
228 |
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.