Conditions | 21 |
Paths | 345 |
Total Lines | 55 |
Code Lines | 42 |
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 |
||
173 | public function add($crash,$new = false) { |
||
174 | global $globalTransaction, $globalDebug; |
||
175 | require_once('class.Connection.php'); |
||
176 | require_once('class.Image.php'); |
||
177 | $Connection = new Connection(); |
||
178 | $Image = new Image(); |
||
179 | |||
180 | if (empty($crash)) return false; |
||
181 | if (!$new) { |
||
182 | $query_delete = 'DELETE FROM accidents WHERE source = :source'; |
||
183 | $sthd = $Connection->db->prepare($query_delete); |
||
184 | $sthd->execute(array(':source' => $crash[0]['source'])); |
||
185 | } |
||
186 | if ($globalTransaction) $Connection->db->beginTransaction(); |
||
1 ignored issue
–
show
|
|||
187 | $initial_array = array('ident' => null,'type' => 'accident','url' => null,'registration' => null, 'date' => null, 'place' => null,'country' => null, 'latitude' => null, 'longitude' => null, 'fatalities' => null, 'title' => '','source' => '','aircraft_manufacturer' => null,'aircraft_name' => null); |
||
188 | $query_check = 'SELECT COUNT(*) as nb FROM accidents WHERE registration = :registration AND date = :date AND type = :type AND source = :source'; |
||
189 | $sth_check = $Connection->db->prepare($query_check); |
||
190 | $query = 'INSERT INTO accidents (aircraft_manufacturer,aircraft_name,ident,registration,date,url,country,place,title,fatalities,latitude,longitude,type,source) VALUES (:aircraft_manufacturer,:aircraft_name,:ident,:registration,:date,:url,:country,:place,:title,:fatalities,:latitude,:longitude,:type,:source)'; |
||
191 | $sth = $Connection->db->prepare($query); |
||
192 | $j = 0; |
||
193 | try { |
||
194 | foreach ($crash as $cr) { |
||
195 | //print_r($cr); |
||
196 | $cr = $cr + $initial_array; |
||
197 | $cr = array_map(function($value) { |
||
198 | return $value === "" ? NULL : $value; |
||
199 | }, $cr); |
||
200 | if ($cr['date'] != '' && $cr['registration'] != null && $cr['registration'] != '' && $cr['registration'] != '?' && $cr['registration'] != '-' && $cr['date'] < time() && !preg_match('/\s/',$cr['registration'])) { |
||
201 | $query_check_values = array(':registration' => $cr['registration'],':date' => date('Y-m-d',$cr['date']),':type' => $cr['type'],':source' => $cr['source']); |
||
202 | $sth_check->execute($query_check_values); |
||
203 | $result_check = $sth_check->fetch(PDO::FETCH_ASSOC); |
||
204 | if ($result_check['nb'] == 0) { |
||
205 | $query_values = array(':registration' => trim($cr['registration']),':date' => date('Y-m-d',$cr['date']),':url' => $cr['url'],':country' => $cr['country'],':place' => $cr['place'],':title' => $cr['title'],':fatalities' => $cr['fatalities'],':latitude' => $cr['latitude'],':longitude' => $cr['longitude'],':type' => $cr['type'],':source' => $cr['source'],':ident' => $cr['ident'],':aircraft_manufacturer' => $cr['aircraft_manufacturer'],':aircraft_name' => $cr['aircraft_name']); |
||
206 | $sth->execute($query_values); |
||
207 | if ($cr['date'] > time()-(30*86400)) { |
||
208 | if (empty($Image->getSpotterImage($cr['registration']))) { |
||
209 | //if ($globalDebug) echo 'Get image...'."\n"; |
||
210 | $Image->addSpotterImage($cr['registration']); |
||
211 | } |
||
212 | // elseif ($globalDebug) echo 'Image already in DB'."\n"; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | if ($globalTransaction && $j % 90 == 0) { |
||
217 | $Connection->db->commit(); |
||
1 ignored issue
–
show
|
|||
218 | $Connection->db->beginTransaction(); |
||
1 ignored issue
–
show
|
|||
219 | } |
||
220 | } |
||
221 | if ($globalTransaction) $Connection->db->commit(); |
||
1 ignored issue
–
show
|
|||
222 | } catch(PDOException $e) { |
||
223 | if ($globalTransaction) $Connection->db->rollBack(); |
||
1 ignored issue
–
show
|
|||
224 | echo $e->getMessage(); |
||
225 | } |
||
226 | $sth_check->closeCursor(); |
||
227 | } |
||
228 | |||
261 | ?> |
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.