| Conditions | 20 |
| Paths | 36 |
| Total Lines | 153 |
| Code Lines | 112 |
| 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 |
||
| 74 | function updateByNsg($config) |
||
| 75 | { |
||
| 76 | $filename = getTempNam(); |
||
| 77 | if (!file_get_contents_curl('https://nsgreg.nga.mil/NSGDOC/files/doc/Document/GENC%20Standard%20Index%20XML%20Ed2.0.zip', $filename, 'https://nsgreg.nga.mil/doc/view?i=2382')) { |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | $zip = new Zip_Manager(); |
||
| 82 | $zip->open($filename); |
||
| 83 | $zip->filteredExtractTo('./', array( |
||
| 84 | '/^GENC Standard Index Ed2.0.xml$/' |
||
| 85 | )); |
||
| 86 | $zip->close(); |
||
| 87 | unlink($filename); |
||
| 88 | $filename = realpath("./GENC Standard Index Ed2.0.xml"); |
||
| 89 | $dom = new DOMDocument(); |
||
| 90 | if (!$dom->load($filename)) { |
||
| 91 | return false; |
||
| 92 | } |
||
| 93 | $dom->documentElement->removeAttributeNS('http://api.nsgreg.nga.mil/schema/genc/2.0', 'genc'); |
||
| 94 | $xpath = new DOMXPath($dom); |
||
| 95 | // $nodes = $xpath->query('//GENCStandardBaselineIndex/GeopoliticalEntity[encoding/char3Code][encoding/char2Code][encoding/numericCode][name]'); |
||
| 96 | $nodes = $xpath->query('//GENCStandardBaselineIndex/GeopoliticalEntity'); |
||
| 97 | if (!$nodes || !$nodes->length) { |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | $pdo = getPDOConnection($config); |
||
| 101 | |||
| 102 | $st1 = $pdo->prepare('select id from country where iso3 = ?'); |
||
| 103 | $st2 = $pdo->prepare('update country set name = ? where id = ?'); |
||
| 104 | $st3 = $pdo->prepare('insert into country (id, name, iso2, iso3, iso_num, reviewed) values (nextval(\'country_id_seq\'), ?,?,?,?, 0)'); |
||
| 105 | |||
| 106 | $pdo->beginTransaction(); |
||
| 107 | foreach ($nodes as $node) { |
||
| 108 | $char3Code = $node->getElementsByTagName('char3Code')->item(0)->nodeValue; |
||
| 109 | if (!$st1->execute(array( |
||
| 110 | $char3Code |
||
| 111 | ))) { |
||
| 112 | print_r($char3Code); |
||
| 113 | print_r($pdo->errorInfo()); |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | if ($r = $st1->fetchAll()) { |
||
| 117 | if (!$st2->execute(array( |
||
| 118 | $node->getElementsByTagName('name') |
||
| 119 | ->item(0)->nodeValue, |
||
| 120 | $r[0]['id'] |
||
| 121 | ))) { |
||
| 122 | print_r($char3Code); |
||
| 123 | print_r($pdo->errorInfo()); |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | if (!$st3->execute(array( |
||
| 128 | $node->getElementsByTagName('name') |
||
| 129 | ->item(0)->nodeValue, |
||
| 130 | $node->getElementsByTagName('char2Code') |
||
| 131 | ->item(0)->nodeValue, |
||
| 132 | $char3Code, |
||
| 133 | $node->getElementsByTagName('numericCode') |
||
| 134 | ->item(0)->nodeValue |
||
| 135 | ))) { |
||
| 136 | print_r($char3Code); |
||
| 137 | print_r($pdo->errorInfo()); |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | /* |
||
| 142 | * echo $node->getElementsByTagName('char3Code')->item(0)->nodeValue; echo $node->getElementsByTagName('char2Code')->item(0)->nodeValue; echo $node->getElementsByTagName('numericCode')->item(0)->nodeValue; echo $node->getElementsByTagName('name')->item(0)->nodeValue; |
||
| 143 | */ |
||
| 144 | } |
||
| 145 | |||
| 146 | $st1->closeCursor(); |
||
| 147 | $st2->closeCursor(); |
||
| 148 | $st3->closeCursor(); |
||
| 149 | |||
| 150 | $st1 = $pdo->prepare('select id id from state a1 where a1.iso6 = ?'); |
||
| 151 | $st2 = $pdo->prepare('select a1.id id from state a1 inner join country a2 on a1.country_id = a2.id where a2.iso3 = ? and a1.name = ?'); |
||
| 152 | $st3 = $pdo->prepare('update state set iso6 = ?, name = ?, country_id = (select id from country where iso3 = ?) where id = ?'); |
||
| 153 | $st4 = $pdo->prepare('insert into state (id, country_id, iso6, name, reviewed) values (nextval(\'state_id_seq\'), (select id from country where iso3 = ?), ?, ?, 0)'); |
||
| 154 | $xpath = new DOMXPath($dom); |
||
| 155 | // $nodes = $xpath->query('//GENCStandardBaselineIndex/AdministrativeSubdivision[country][encoding/char6Code][name]'); |
||
| 156 | $nodes = $xpath->query('//GENCStandardBaselineIndex/AdministrativeSubdivision'); |
||
| 157 | if (!$nodes || !$nodes->length) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | foreach ($nodes as $node) { |
||
| 161 | $char6Code = $node->getElementsByTagName('char6Code')->item(0)->nodeValue; |
||
| 162 | if (!$st1->execute(array( |
||
| 163 | $char6Code |
||
| 164 | ))) { |
||
| 165 | print_r($char6Code); |
||
| 166 | print_r($pdo->errorInfo()); |
||
| 167 | exit(1); |
||
| 168 | } |
||
| 169 | if ($r = $st1->fetchAll()) { |
||
| 170 | if (!$st3->execute(array( |
||
| 171 | $char6Code, |
||
| 172 | $node->getElementsByTagName('name') |
||
| 173 | ->item(0)->nodeValue, |
||
| 174 | $node->getElementsByTagName('country') |
||
| 175 | ->item(0)->nodeValue, |
||
| 176 | $r[0]['id'] |
||
| 177 | ))) { |
||
| 178 | print_r($char6Code); |
||
| 179 | print_r($pdo->errorInfo()); |
||
| 180 | exit(1); |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | if (!$st2->execute(array( |
||
| 184 | $node->getElementsByTagName('country') |
||
| 185 | ->item(0)->nodeValue, |
||
| 186 | $node->getElementsByTagName('name') |
||
| 187 | ->item(0)->nodeValue |
||
| 188 | ))) { |
||
| 189 | print_r($char6Code); |
||
| 190 | print_r($pdo->errorInfo()); |
||
| 191 | exit(1); |
||
| 192 | } |
||
| 193 | if ($r = $st2->fetchAll()) { |
||
| 194 | if (!$st3->execute(array( |
||
| 195 | $char6Code, |
||
| 196 | $node->getElementsByTagName('name') |
||
| 197 | ->item(0)->nodeValue, |
||
| 198 | $node->getElementsByTagName('country') |
||
| 199 | ->item(0)->nodeValue, |
||
| 200 | $r[0]['id'] |
||
| 201 | ))) { |
||
| 202 | print_r($char6Code); |
||
| 203 | print_r($pdo->errorInfo()); |
||
| 204 | exit(1); |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | if (!$st4->execute(array( |
||
| 208 | $node->getElementsByTagName('country') |
||
| 209 | ->item(0)->nodeValue, |
||
| 210 | $char6Code, |
||
| 211 | $node->getElementsByTagName('name') |
||
| 212 | ->item(0)->nodeValue |
||
| 213 | ))) { |
||
| 214 | print_r($char6Code); |
||
| 215 | print_r($pdo->errorInfo()); |
||
| 216 | exit(1); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | /* |
||
| 222 | * echo $node->getElementsByTagName('country')->item(0)->nodeValue; echo $node->getElementsByTagName('char6Code')->item(0)->nodeValue; echo $node->getElementsByTagName('name')->item(0)->nodeValue; |
||
| 223 | */ |
||
| 224 | $pdo->commit(); |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | exit(updateByNsg($config) && getCountryPostal($config) ? 0 : 1); |
||
| 231 |