| Conditions | 8 |
| Paths | 8 |
| Total Lines | 65 |
| 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 |
||
| 54 | public function saveFile() |
||
| 55 | { |
||
| 56 | if (!$this->fileName->get()) { |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | $path = 'Config/' . $this->fileName->get(); |
||
| 61 | $fileSystem = $this->fileSystem->getUserData(); |
||
| 62 | |||
| 63 | if (!$fileSystem->has($path)) { |
||
| 64 | $this->logger->warning("Can't find file '$path'! Won't save server settings!"); |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** @var \SimpleXMLElement */ |
||
| 69 | $oldXml = simplexml_load_string($fileSystem->read($path)); |
||
| 70 | if ($oldXml === false) { |
||
| 71 | $this->logger->error("Invalid xml in source file '$path'! Won't save server settings!"); |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $adapter = array("name" => "name", |
||
| 76 | "password" => "password", |
||
| 77 | "comment" => "comment", |
||
| 78 | "passwordForSpectator" => "password_spectator", |
||
| 79 | "hideServer" => "hide_server", |
||
| 80 | "nextMaxPlayers" => "max_players", |
||
| 81 | "nextMaxSpectatos" => "max_spectators", |
||
| 82 | "isP2PUpload" => "enable_p2p_upload", |
||
| 83 | "isP2PDownload" => "enable_p2p_download", |
||
| 84 | "nextLadderMode" => "ladder_mode", |
||
| 85 | "nextCallVoteTimeOut" => "callvote_timeout", |
||
| 86 | "callVoteRatio" => "callvote_ratio", |
||
| 87 | "allowMapDownload" => "allow_map_download", |
||
| 88 | "autoSaveReplays" => "autosave_replays", |
||
| 89 | "autoSaveValidationReplays" => "autosave_validation_replays", |
||
| 90 | "refereePassword" => "referee_password", |
||
| 91 | "refereeMode" => "referee_validation_mode", |
||
| 92 | "disableHorns" => "disable_horns", |
||
| 93 | "clientInputsMaxLatency" => "clientinputs_maxlatency", |
||
| 94 | "keepPlayerSlots" => "keep_player_slots", |
||
| 95 | ); |
||
| 96 | |||
| 97 | $new = $this->gameDataStorage->getServerOptions(); |
||
| 98 | foreach ($this->gameDataStorage->getServerOptions() as $key => $value) { |
||
|
|
|||
| 99 | // $search = $key; |
||
| 100 | if (array_key_exists($key, $adapter)) { |
||
| 101 | $search = $adapter[$key]; |
||
| 102 | } else { |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | $out = $new->{$key}; |
||
| 106 | if (is_bool($value)) { |
||
| 107 | $out = "False"; |
||
| 108 | if ($value) { |
||
| 109 | $out = "True"; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $oldXml->server_options->{$search}[0] = $out; |
||
| 113 | } |
||
| 114 | $this->logger->info('Saving server settings to : ' . $path); |
||
| 115 | $xml = $oldXml->asXML(); |
||
| 116 | |||
| 117 | $fileSystem->update($path, $xml); |
||
| 118 | } |
||
| 119 | |||
| 151 | } |