Conditions | 7 |
Paths | 18 |
Total Lines | 106 |
Lines | 24 |
Ratio | 22.64 % |
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 |
||
138 | protected function createStockists() |
||
139 | { |
||
140 | flush(); |
||
141 | ob_end_flush(); |
||
142 | DB::alteration_message("================================================ CREATING STOCKISTS ================================================"); |
||
143 | ob_start(); |
||
144 | $stockistsCompleted = array(); |
||
|
|||
145 | $rootPage = StockistSearchPage::get()->filter(array("ClassName" => "ShopfinderPage"))->first(); |
||
146 | if (!$rootPage) { |
||
147 | die("You must setup a stockist search page first"); |
||
148 | } |
||
149 | flush(); |
||
150 | ob_end_flush(); |
||
151 | DB::alteration_message("<h2>Found Root Page: ".$rootPage->Title." (".$rootPage->ID.")</h2>"); |
||
152 | ob_start(); |
||
153 | $rowCount = 0; |
||
154 | $continents = array(); |
||
155 | $countryCheckArray = array(); |
||
156 | $types = array(); |
||
157 | foreach ($this->csv as $row) { |
||
158 | $rowCount++; |
||
159 | print_r($row); |
||
160 | flush(); |
||
161 | ob_end_flush(); |
||
162 | DB::alteration_message("<h2>$rowCount: Creating stockist: ".$row["NAME"]."</h2>"); |
||
163 | ob_start(); |
||
164 | |||
165 | if (!isset($countryCheckArray[$row['COUNTRYCODE']])) { |
||
166 | $countryPage = StockistCountryPage::get() |
||
167 | ->filter(array("CountryCode" => $row['COUNTRYCODE'])) |
||
168 | ->first(); |
||
169 | $countryCheckArray[$row['COUNTRYCODE']] = $countryPage; |
||
170 | } else { |
||
171 | $countryPage = $countryCheckArray[$row['COUNTRYCODE']]; |
||
172 | } |
||
173 | |||
174 | View Code Duplication | if (!$countryPage) { |
|
175 | $countryPage = new StockistCountryPage(); |
||
176 | flush(); |
||
177 | ob_end_flush(); |
||
178 | DB::alteration_message(" --- Creating new country page page ".$row['COUNTRYCODE'], "created"); |
||
179 | ob_start(); |
||
180 | } else { |
||
181 | flush(); |
||
182 | ob_end_flush(); |
||
183 | DB::alteration_message(" --- Existing country page ".$row['COUNTRYCODE'], "changed"); |
||
184 | ob_start(); |
||
185 | } |
||
186 | |||
187 | $countryPage->Title = $row['COUNTRY']; |
||
188 | $countryPage->MetaTitle = $row['COUNTRY']; |
||
189 | $countryPage->MenuTitle = $row['COUNTRY']; |
||
190 | $countryPage->URLSegment = $row['COUNTRY']; |
||
191 | $countryPage->CountryCode = $row['COUNTRYCODE']; |
||
192 | $countryPage->ParentID = $rootPage->ID; |
||
193 | $countryPage->writeToStage("Stage"); |
||
194 | $countryPage->Publish('Stage', 'Live'); |
||
195 | |||
196 | //stockist page |
||
197 | $stockistPage = StockistPage::get()->filter(array("Title" => $row["NAME"]))->first(); |
||
198 | View Code Duplication | if (!$stockistPage) { |
|
199 | $stockistPage = new StockistPage(); |
||
200 | flush(); |
||
201 | ob_end_flush(); |
||
202 | DB::alteration_message(" --- Creating Stockist: ".$row["NAME"], "created"); |
||
203 | ob_start(); |
||
204 | } else { |
||
205 | flush(); |
||
206 | ob_end_flush(); |
||
207 | DB::alteration_message(" --- Updating Stockist: ".$row["NAME"], "changed"); |
||
208 | ob_start(); |
||
209 | } |
||
210 | $name = trim($row["NAME"]); |
||
211 | $stockistPage->ParentID = $countryPage->ID; |
||
212 | $stockistPage->Title = $name; |
||
213 | $stockistPage->MenuTitle = $name; |
||
214 | $stockistPage->URLSegment = $name; |
||
215 | $stockistPage->Address = trim($row["ADDRESS"]); |
||
216 | |||
217 | $stockistPage->Phone = trim($row["PHONE"]); |
||
218 | $stockistPage->City = trim($row["CITY"]); |
||
219 | $stockistPage->HasPhysicalStore = 1; |
||
220 | $stockistPage->writeToStage('Stage'); |
||
221 | $stockistPage->Publish('Stage', 'Live'); |
||
222 | |||
223 | //types |
||
224 | |||
225 | $type = "Retailer"; |
||
226 | |||
227 | flush(); |
||
228 | ob_end_flush(); |
||
229 | DB::alteration_message(" --- Adding type: ".$type, "changed"); |
||
230 | ob_start(); |
||
231 | |||
232 | if (!isset($types[$type])) { |
||
233 | $typeObject = StockistPage_Type::get()->filter(array("Type" => $type))->first(); |
||
234 | $types[$type] = $typeObject; |
||
235 | } else { |
||
236 | $typeObject = $types[$type]; |
||
237 | } |
||
238 | } |
||
239 | flush(); |
||
240 | ob_end_flush(); |
||
241 | DB::alteration_message("====================== END =========================="); |
||
242 | ob_start(); |
||
243 | } |
||
244 | |||
259 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.