Passed
Branch dev (7cf5cf)
by
unknown
02:40
created

eveApi.php ➔ corpID()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 34
Code Lines 22

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 7
nop 1
dl 34
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use Monolog\Handler\StreamHandler;
27
use Monolog\Logger;
28
29
/**
30
 * @param $url
31
 * @return SimpleXMLElement|null|string
32
 */
33 View Code Duplication
function makeApiRequest($url)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
{
35
    $logger = new Logger('eveApi');
36
    $logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/libraryError.log', Logger::DEBUG));
37
    try {
38
        // Initialize a new request for this URL
39
        $ch = curl_init($url);
40
        // Set the options for this request
41
        curl_setopt_array($ch, array(
42
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
43
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
44
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
45
            CURLOPT_USERAGENT => 'Dramiel Discord Bot - https://github.com/shibdib/Dramiel', // Useragent
46
            CURLOPT_TIMEOUT => 15,
47
        ));
48
        // Fetch the data from the URL
49
        $data = curl_exec($ch);
50
        // Close the connection
51
        curl_close($ch);
52
        // Return a new SimpleXMLElement based upon the received data
53
        return new SimpleXMLElement($data);
54
    } catch (Exception $e) {
55
        $logger->error('EVE API Error: ' . $e->getMessage());
56
        return null;
57
    }
58
}
59
60
/**
61
 * @return mixed|null
62
 */
63
64
function serverStatus()
65
{
66
    $logger = new Logger('eveApi');
67
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
68
    try {
69
        // Initialize a new request for this URL
70
        $ch = curl_init('https://api.eveonline.com/server/ServerStatus.xml.aspx');
71
        // Set the options for this request
72
        curl_setopt_array($ch, array(
73
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
74
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
75
            CURLOPT_TIMEOUT => 8,
76
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
77
        ));
78
        // Fetch the data from the URL
79
        $data = curl_exec($ch);
80
        // Close the connection
81
        curl_close($ch);
82
83
        $true = 'true';
84
        //If server is down return false
85
        if ($data->serverOpen !== 'True') {
86
            return FALSE;
87
        }
88
        //If server is up return true
89
        return $true;
90
    } catch (Exception $e) {
91
        $logger->error('EVE API Error: ' . $e->getMessage());
92
        return null;
93
    }
94
}
95
96
/**
97
 * @param string $characterID
98
 * @return mixed
99
 */
100
////Char ID to name via CCP
101 View Code Duplication
function characterName($characterID)
102
{
103
    $character = characterDetails($characterID);
104
    $name = (string)$character['name'];
105
    if (null === $name || '' === $name) { // Make sure it's always set.
106
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$characterID}";
107
        $xml = makeApiRequest($url);
108
        foreach ($xml->result->rowset->row as $entity) {
109
            $name = $entity->attributes()->name;
110
        }
111
    }
112
    return $name;
113
}
114
115
/**
116
 * @param string $characterName
117
 * @return mixed
118
 */
119
////Character name to ID
120 View Code Duplication
function characterID($characterName)
121
{
122
    $logger = new Logger('eveESI');
123
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
124
    $characterName = urlencode($characterName);
125
126
    try {
127
        // Initialize a new request for this URL
128
        $ch = curl_init("https://esi.tech.ccp.is/latest/search/?search={$characterName}&categories=character&language=en-us&strict=true&datasource=tranquility");
129
        // Set the options for this request
130
        curl_setopt_array($ch, array(
131
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
132
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
133
            CURLOPT_TIMEOUT => 8,
134
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
135
        ));
136
        // Fetch the data from the URL
137
        $data = curl_exec($ch);
138
        // Close the connection
139
        curl_close($ch);
140
        $data = json_decode($data, TRUE);
141
        $id = (int)$data['character'][0];
142
143
    } catch (Exception $e) {
144
        $logger->error('EVE ESI Error: ' . $e->getMessage());
145
        $url = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names={$characterName}";
146
        $xml = makeApiRequest($url);
147
        foreach ($xml->result->rowset->row as $entity) {
148
            $id = $entity->attributes()->characterID;
149
        }
150
    }
151
152
    return $id;
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
153
}
154
155
/**
156
 * @param string $characterID
157
 * @return mixed
158
 */
159
////Char ID to char data via CCP
160 View Code Duplication
function characterDetails($characterID)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
{
162
    $logger = new Logger('eveESI');
163
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
164
165
    try {
166
        // Initialize a new request for this URL
167
        $ch = curl_init("https://esi.tech.ccp.is/latest/characters/{$characterID}/");
168
        // Set the options for this request
169
        curl_setopt_array($ch, array(
170
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
171
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
172
            CURLOPT_TIMEOUT => 8,
173
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
174
        ));
175
        // Fetch the data from the URL
176
        $data = curl_exec($ch);
177
        // Close the connection
178
        curl_close($ch);
179
        $data = json_decode($data, TRUE);
180
181
    } catch (Exception $e) {
182
        $logger->error('EVE ESI Error: ' . $e->getMessage());
183
        return null;
184
    }
185
186
    return $data;
187
}
188
189
/**
190
 * @param string $systemID
191
 * @return mixed
192
 */
193
////System ID to name via CCP
194 View Code Duplication
function systemName($systemID)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
{
196
    $logger = new Logger('eveESI');
197
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
198
199
    try {
200
        // Initialize a new request for this URL
201
        $ch = curl_init("https://esi.tech.ccp.is/latest/universe/systems/{$systemID}/");
202
        // Set the options for this request
203
        curl_setopt_array($ch, array(
204
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
205
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
206
            CURLOPT_TIMEOUT => 8,
207
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
208
        ));
209
        // Fetch the data from the URL
210
        $data = curl_exec($ch);
211
        // Close the connection
212
        curl_close($ch);
213
        $data = json_decode($data, TRUE);
214
        $name = (string)$data['solar_system_name'];
215
216
    } catch (Exception $e) {
217
        $logger->error('EVE ESI Error: ' . $e->getMessage());
218
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$systemID}";
219
        $xml = makeApiRequest($url);
220
        foreach ($xml->result->rowset->row as $entity) {
221
            $name = $entity->attributes()->name;
222
        }
223
    }
224
225
    return $name;
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
226
}
227
228
/**
229
 * @param string $corpName
230
 * @return mixed
231
 */
232
////Corp name to ID
233 View Code Duplication
function corpID($corpName)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
{
235
    $logger = new Logger('eveESI');
236
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
237
    $corpName = urlencode($corpName);
238
239
    try {
240
        // Initialize a new request for this URL
241
        $ch = curl_init("https://esi.tech.ccp.is/latest/search/?search={$corpName}&categories=corporation&language=en-us&strict=true&datasource=tranquility");
242
        // Set the options for this request
243
        curl_setopt_array($ch, array(
244
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
245
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
246
            CURLOPT_TIMEOUT => 8,
247
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
248
        ));
249
        // Fetch the data from the URL
250
        $data = curl_exec($ch);
251
        // Close the connection
252
        curl_close($ch);
253
        $data = json_decode($data, TRUE);
254
        $id = (int)$data['corporation'][0];
255
256
    } catch (Exception $e) {
257
        $logger->error('EVE ESI Error: ' . $e->getMessage());
258
        $url = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names={$corpName}";
259
        $xml = makeApiRequest($url);
260
        foreach ($xml->result->rowset->row as $entity) {
261
            $id = $entity->attributes()->characterID;
262
        }
263
    }
264
265
    return $id;
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
266
}
267
268
269
/**
270
 * @param string $corpID
271
 * @return mixed
272
 */
273
////Corp ID to name via CCP
274 View Code Duplication
function corpName($corpID)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
{
276
    $corporation = corpDetails($corpID);
277
    $name = (string)$corporation['corporation_name'];
278
    if (null === $name || '' === $name) { // Make sure it's always set.
279
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$corpID}";
280
        $xml = makeApiRequest($url);
281
        foreach ($xml->result->rowset->row as $entity) {
282
            $name = $entity->attributes()->name;
283
        }
284
    }
285
286
    return $name;
287
}
288
289
/**
290
 * @param string $corpID
291
 * @return mixed
292
 */
293
////Corp ID to corp data via CCP
294 View Code Duplication
function corpDetails($corpID)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
{
296
    $logger = new Logger('eveESI');
297
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
298
299
    try {
300
        // Initialize a new request for this URL
301
        $ch = curl_init("https://esi.tech.ccp.is/latest/corporations/{$corpID}/");
302
        // Set the options for this request
303
        curl_setopt_array($ch, array(
304
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
305
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
306
            CURLOPT_TIMEOUT => 8,
307
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
308
        ));
309
        // Fetch the data from the URL
310
        $data = curl_exec($ch);
311
        // Close the connection
312
        curl_close($ch);
313
        $data = json_decode($data, TRUE);
314
315
    } catch (Exception $e) {
316
        $logger->error('EVE ESI Error: ' . $e->getMessage());
317
        return null;
318
    }
319
320
    return $data;
321
}
322
323
/**
324
 * @param string $allianceID
325
 * @return mixed
326
 */
327
////Alliance ID to name via CCP
328 View Code Duplication
function allianceName($allianceID)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
329
{
330
    $logger = new Logger('eveESI');
331
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
332
333
    try {
334
        // Initialize a new request for this URL
335
        $ch = curl_init("https://esi.tech.ccp.is/latest/alliances/{$allianceID}/");
336
        // Set the options for this request
337
        curl_setopt_array($ch, array(
338
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
339
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
340
            CURLOPT_TIMEOUT => 8,
341
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
342
        ));
343
        // Fetch the data from the URL
344
        $data = curl_exec($ch);
345
        // Close the connection
346
        curl_close($ch);
347
        $data = json_decode($data, TRUE);
348
        $name = (string)$data['alliance_name'];
349
350
    } catch (Exception $e) {
351
        $logger->error('EVE ESI Error: ' . $e->getMessage());
352
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$allianceID}";
353
        $xml = makeApiRequest($url);
354
        foreach ($xml->result->rowset->row as $entity) {
355
            $name = $entity->attributes()->name;
356
        }
357
    }
358
359
    return $name;
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
360
}
361
362
/**
363
 * @param string $systemName
364
 * @return mixed
365
 */
366
////System name to ID
367 View Code Duplication
function systemID($systemName)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
368
{
369
    $logger = new Logger('eveESI');
370
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
371
    $systemName = urlencode($systemName);
372
373
    try {
374
        // Initialize a new request for this URL
375
        $ch = curl_init("https://esi.tech.ccp.is/latest/search/?search={$systemName}&categories=solarsystem&language=en-us&strict=true&datasource=tranquility");
376
        // Set the options for this request
377
        curl_setopt_array($ch, array(
378
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
379
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
380
            CURLOPT_TIMEOUT => 8,
381
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
382
        ));
383
        // Fetch the data from the URL
384
        $data = curl_exec($ch);
385
        // Close the connection
386
        curl_close($ch);
387
        $data = json_decode($data, TRUE);
388
        $id = (int)$data['solarsystem'][0];
389
390
    } catch (Exception $e) {
391
        $logger->error('EVE ESI Error: ' . $e->getMessage());
392
        $url = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names={$systemName}";
393
        $xml = makeApiRequest($url);
394
        foreach ($xml->result->rowset->row as $entity) {
395
            $id = $entity->attributes()->characterID;
396
        }
397
    }
398
399
    return $id;
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
400
}
401
402
/**
403
 * @param string $typeID
404
 * @return mixed
405
 */
406
////TypeID to TypeName via CCP
407 View Code Duplication
function apiTypeName($typeID)
408
{
409
    $logger = new Logger('eveESI');
410
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
411
412
    try {
413
        // Initialize a new request for this URL
414
        $ch = curl_init("https://esi.tech.ccp.is/latest/universe/types/{$typeID}/");
415
        // Set the options for this request
416
        curl_setopt_array($ch, array(
417
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
418
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
419
            CURLOPT_TIMEOUT => 8,
420
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
421
        ));
422
        // Fetch the data from the URL
423
        $data = curl_exec($ch);
424
        // Close the connection
425
        curl_close($ch);
426
        $data = json_decode($data, TRUE);
427
        $name = (string)$data['type_name'];
428
429
    } catch (Exception $e) {
430
        $logger->error('EVE ESI Error: ' . $e->getMessage());
431
        $url = "https://api.eveonline.com/eve/TypeName.xml.aspx?ids={$typeID}";
432
        $xml = makeApiRequest($url);
433
        foreach ($xml->result->rowset->row as $entity) {
434
            $name = $entity->attributes()->typeName;
435
        }
436
    }
437
438
    return $name;
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
439
}
440
441
/**
442
 * @param string $typeName
443
 * @return mixed
444
 */
445
////TypeName to TypeID via fuzz
446
function apiTypeID($typeName)
447
{
448
    $logger = new Logger('eveESI');
449
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
450
    $typeName = urlencode($typeName);
451
    try {
452
        // Initialize a new request for this URL
453
        $ch = curl_init("https://www.fuzzwork.co.uk/api/typeid.php?typename={$typeName}");
454
        // Set the options for this request
455
        curl_setopt_array($ch, array(
456
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
457
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
458
            CURLOPT_TIMEOUT => 8,
459
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
460
        ));
461
        // Fetch the data from the URL
462
        $data = curl_exec($ch);
463
        // Close the connection
464
        curl_close($ch);
465
        $data = json_decode($data, TRUE);
466
467
    } catch (Exception $e) {
468
        $logger->error('Fuzzwork Error: ' . $e->getMessage());
469
        return null;
470
    }
471
    $id = (int)$data['typeID'];
472
473
    if (null === $id) { // Make sure it's always set.
474
        $id = 'Unknown';
475
    }
476
477
    return $id;
478
}
479
480
////Char/Object ID to name via CCP
481
function apiMoonName($moonID)
482
{
483
    $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?IDs={$moonID}";
484
    $xml = makeApiRequest($url);
485
    $name = null;
486
    foreach ($xml->result->rowset->row as $entity) {
487
        $name = $entity->attributes()->name;
488
    }
489
    if (null === $name) { // Make sure it's always set.
490
        $name = 'Unknown';
491
    }
492
    return $name;
493
}