| Conditions | 9 |
| Paths | 41 |
| Total Lines | 52 |
| Code Lines | 32 |
| 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 |
||
| 57 | public function Upload($sport, &$file) { |
||
| 58 | $response = new PhpDraftResponse(); |
||
| 59 | |||
| 60 | $tempName = $file->getRealPath(); |
||
| 61 | $pro_players = array(); |
||
| 62 | |||
| 63 | if (($handle = fopen($tempName, 'r')) == FALSE) { |
||
| 64 | $response->success = false; |
||
| 65 | $response->errors[] = "Files permission issue: unable to open CSV on server."; |
||
| 66 | |||
| 67 | return $response; |
||
| 68 | } |
||
| 69 | |||
| 70 | if (SET_CSV_TIMEOUT) { |
||
| 71 | set_time_limit(0); |
||
| 72 | } |
||
| 73 | |||
| 74 | while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) { |
||
| 75 | if ($data[0] == "Player") { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $new_player = new ProPlayer(); |
||
| 80 | |||
| 81 | $new_player->league = $sport; |
||
| 82 | $name_column = explode(",", $data[0]); |
||
| 83 | |||
| 84 | if (count($name_column) == 2) { |
||
| 85 | $new_player->last_name = trim($name_column[0]); |
||
| 86 | $new_player->first_name = trim($name_column[1]); |
||
| 87 | } else { |
||
| 88 | $new_player->last_name = "Player"; |
||
| 89 | $new_player->first_name = "Unknown"; |
||
| 90 | } |
||
| 91 | |||
| 92 | $new_player->position = isset($data[1]) ? trim($data[1]) : ''; |
||
| 93 | $new_player->team = isset($data[2]) ? trim($data[2]) : ''; |
||
| 94 | |||
| 95 | $pro_players[] = $new_player; |
||
| 96 | } |
||
| 97 | |||
| 98 | fclose($handle); |
||
| 99 | |||
| 100 | try { |
||
| 101 | $this->app['phpdraft.ProPlayerRepository']->SaveProPlayers($sport, $pro_players); |
||
| 102 | $response->success = true; |
||
| 103 | } catch (Exception $e) { |
||
| 104 | $response->success = false; |
||
| 105 | $response->errors[] = "Error encountered when updating new players to database: " . $e->getMessage(); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $response; |
||
| 109 | } |
||
| 111 |