Conditions | 12 |
Paths | 66 |
Total Lines | 84 |
Code Lines | 47 |
Lines | 40 |
Ratio | 47.62 % |
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 |
||
131 | public static function create_city_and_parents($CityID) |
||
132 | { |
||
133 | $cityPage = null; |
||
134 | //check if the city exists at all |
||
135 | $sql = ' |
||
136 | SELECT cities.RegionID, regions.CountryID, countries.ContinentID From cities, regions, countries, continents |
||
137 | WHERE |
||
138 | cities.RegionID = regions.RegionID AND |
||
139 | regions.CountryID = countries.CountryID AND |
||
140 | countries.ContinentID = continents.ContinentID AND |
||
141 | cities.CityID = '.$CityID.' |
||
142 | LIMIT 1;'; |
||
143 | $result = DB::query($sql); |
||
144 | |||
145 | foreach ($result as $row) { |
||
146 | break; |
||
147 | } |
||
148 | $abstractHelpPage = new BrowseAbstractPage(); |
||
149 | if ($row) { |
||
150 | //1 check if world exists |
||
151 | if ($worldPage = DataObject::get_one("BrowseWorldPage")) { |
||
152 | //do nothing |
||
153 | } else { |
||
154 | $worldPage = new BrowseWorldPage(); |
||
155 | $name = "Find"; |
||
156 | $worldPage->Title = $name; |
||
157 | $worldPage->MetaTitle = $name; |
||
158 | $worldPage->MenuTitle = $name; |
||
159 | $worldPage->writeToStage('Stage'); |
||
160 | $worldPage->publish('Stage', 'Live'); |
||
161 | $worldPage->flushCache(); |
||
162 | } |
||
163 | |||
164 | //2 check if continent exists |
||
165 | $ContinentID = $row["ContinentID"]; |
||
166 | View Code Duplication | if ($continentPage = DataObject::get_one("BrowseContinentsPage", 'HiddenDataID = '.$ContinentID)) { |
|
167 | //debug::show("continent exists"); |
||
168 | } else { |
||
169 | //create continent |
||
170 | $continents = $abstractHelpPage->getDataFromTable("continents", "ContinentID = ".$ContinentID, null); |
||
171 | foreach ($continents as $continentData) { |
||
172 | $continentPage = new BrowseContinentsPage(); |
||
173 | $continentPage->CreateContinent($continentData, $worldPage); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | //3 check if country exists |
||
178 | $CountryID = $row["CountryID"]; |
||
179 | View Code Duplication | if ($countryPage = DataObject::get_one("BrowseCountriesPage", 'HiddenDataID = '.$CountryID)) { |
|
180 | //debug::show("country exists"); |
||
181 | } else { |
||
182 | //create Country |
||
183 | $countries = $abstractHelpPage->getDataFromTable("countries", "CountryID = ".$CountryID, null); |
||
184 | foreach ($countries as $countryData) { |
||
185 | $countryPage = new BrowseCountriesPage(); |
||
186 | $countryPage->CreateCountry($countryData, $continentPage); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | //4 check if region exists |
||
191 | $RegionID = $row["RegionID"]; |
||
192 | View Code Duplication | if ($regionPage = DataObject::get_one("BrowseRegionsPage", 'HiddenDataID = '.$RegionID)) { |
|
193 | //debug::show("region exists"); |
||
194 | } else { |
||
195 | //create region |
||
196 | $regions = $abstractHelpPage->getDataFromTable("regions", "RegionID = ".$RegionID, null); |
||
197 | foreach ($regions as $regionData) { |
||
198 | $regionPage = new BrowseRegionsPage(); |
||
199 | $regionPage->CreateRegion($regionData, $countryPage); |
||
200 | } |
||
201 | } |
||
202 | View Code Duplication | if ($cityPage = DataObject::get_one("BrowseCitiesPage", 'HiddenDataID = '.$CityID)) { |
|
203 | //debug::show("city exists"); |
||
204 | } else { |
||
205 | //create region |
||
206 | $cities = $abstractHelpPage->getDataFromTable("cities", "CityID = ".$CityID, null); |
||
207 | foreach ($cities as $city) { |
||
208 | $cityPage = new BrowseCitiesPage(); |
||
209 | $cityPage->CreateCity($city, $regionPage); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | return $cityPage; |
||
214 | } |
||
215 | |||
288 |
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.