SpotterServer::checkAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/class.Spotter.php');
4
require_once(dirname(__FILE__).'/class.SpotterLive.php');
5
require_once(dirname(__FILE__).'/class.SpotterArchive.php');
6
require_once(dirname(__FILE__).'/class.Scheduler.php');
7
require_once(dirname(__FILE__).'/class.Translation.php');
8
9
class SpotterServer {
10
	public $dbs = null;
11
12
	function __construct($dbs = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
		if ($dbs === null) {
14
			$Connection = new Connection(null,'server');
15
			$this->dbs = $Connection->dbs;
16
			$query = "CREATE TABLE IF NOT EXISTS `spotter_temp` ( `id_data` INT NOT NULL AUTO_INCREMENT , `id_user` INT NOT NULL , `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `hex` VARCHAR(20) NOT NULL , `ident` VARCHAR(20) NULL , `latitude` FLOAT NULL , `longitude` FLOAT NULL , `verticalrate` INT NULL , `speed` INT NULL , `squawk` INT NULL , `altitude` INT NULL , `heading` INT NULL , `registration` VARCHAR(10) NULL , `aircraft_icao` VARCHAR(10) NULL , `waypoints` VARCHAR(255) NULL , `noarchive` BOOLEAN NOT NULL DEFAULT FALSE, `id_source` INT NOT NULL DEFAULT '1', `format_source` VARCHAR(25) NULL, `source_name` VARCHAR(25) NULL, `over_country` VARCHAR(255) NULL, PRIMARY KEY (`id_data`) ) ENGINE = MEMORY;";
17
			try {
18
				$sth = $this->dbs['server']->exec($query);
0 ignored issues
show
Unused Code introduced by
$sth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
19
			} catch(PDOException $e) {
20
				return "error : ".$e->getMessage();
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
21
			}
22
		}
23
	}
24
25
	function checkAll() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
		return true;
27
	}
28
29
	function add($line) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
		global $globalDebug, $globalServerUserID;
31
		date_default_timezone_set('UTC');
32
		//if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'aprs')) {
33
		if (isset($line['format_source'])) {
34
			if(is_array($line) && isset($line['hex'])) {
35
				if ($line['hex'] != '' && $line['hex'] != '00000' && $line['hex'] != '000000' && $line['hex'] != '111111' && ctype_xdigit($line['hex']) && strlen($line['hex']) === 6) {
36
					$data['hex'] = trim($line['hex']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
37
					if (preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) {
38
						$data['datetime'] = $line['datetime'];
39
					} else $data['datetime'] = date('Y-m-d H:i:s');
40
					if (!isset($line['aircraft_icao'])) {
41
						$Spotter = new Spotter();
42
						$aircraft_icao = $Spotter->getAllAircraftType($data['hex']);
43
						$Spotter->db = null;
44
						if ($aircraft_icao == '' && isset($line['aircraft_type'])) {
45
							if ($line['aircraft_type'] == 'PARA_GLIDER') $aircraft_icao = 'GLID';
46
							elseif ($line['aircraft_type'] == 'HELICOPTER_ROTORCRAFT') $aircraft_icao = 'UHEL';
47
							elseif ($line['aircraft_type'] == 'TOW_PLANE') $aircraft_icao = 'TOWPLANE';
48
							elseif ($line['aircraft_type'] == 'POWERED_AIRCRAFT') $aircraft_icao = 'POWAIRC';
49
						}
50
						$data['aircraft_icao'] = $aircraft_icao;
51
					} else $data['aircraft_icao'] = $line['aircraft_icao'];
52
					//if ($globalDebug) echo "*********** New aircraft hex : ".$data['hex']." ***********\n";
53
				}
54
				if (isset($line['registration']) && $line['registration'] != '') {
55
					$data['registration'] = $line['registration'];
0 ignored issues
show
Bug introduced by
The variable $data 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...
56
				} else $data['registration'] = null;
57
				if (isset($line['waypoints']) && $line['waypoints'] != '') {
58
					$data['waypoints'] = $line['waypoints'];
59
				} else $data['waypoints'] = null;
60
				if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && preg_match('/^[a-zA-Z0-9]+$/', $line['ident'])) {
61
					$data['ident'] = trim($line['ident']);
62
				} else $data['ident'] = null;
63
				if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '') {
64
					if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) {
65
						$data['latitude'] = $line['latitude'];
66
					} else $data['latitude'] = null;
67
					if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) {
68
						if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360;
69
						$data['longitude'] = $line['longitude'];
70
					} else $data['longitude'] = null;
71
				} else {
72
					$data['latitude'] = null;
73
					$data['longitude'] = null;
74
				}
75
				if (isset($line['verticalrate']) && $line['verticalrate'] != '') {
76
					$data['verticalrate'] = $line['verticalrate'];
77
				} else $data['verticalrate'] = null;
78
				if (isset($line['emergency']) && $line['emergency'] != '') {
79
					$data['emergency'] = $line['emergency'];
80
				} else $data['emergency'] = null;
81
				if (isset($line['ground']) && $line['ground'] != '') {
82
					$data['ground'] = $line['ground'];
83
				} else $data['ground'] = null;
84
				if (isset($line['speed']) && $line['speed'] != '') {
85
					$data['speed'] = round($line['speed']);
86
				} else $data['speed'] = null;
87
				if (isset($line['squawk']) && $line['squawk'] != '') {
88
					$data['squawk'] = $line['squawk'];
89
				} else $data['squawk'] = null;
90
				if (isset($line['altitude']) && $line['altitude'] != '') {
91
					$data['altitude'] = round($line['altitude']);
92
		  		} else $data['altitude'] = null;
93
				if (isset($line['heading']) && $line['heading'] != '') {
94
					$data['heading'] = round($line['heading']);
95
		 		} else $data['heading'] = null;
96
				if (isset($line['source_name']) && $line['source_name'] != '') {
97
					$data['source_name'] = $line['source_name'];
98
				} else $data['source_name'] = null;
99
				if (isset($line['over_country']) && $line['over_country'] != '') {
100
					$data['over_country'] = $line['over_country'];
101
		 		} else $data['over_country'] = null;
102
				if (isset($line['noarchive']) && $line['noarchive']) {
103
					$data['noarchive'] = true;
104
				} else $data['noarchive'] = false;
105
				$data['format_source'] = $line['format_source'];
106
				if (isset($line['id_source'])) $id_source = $line['id_source'];
107
				if (isset($data['hex'])) {
108
					echo '.';
109
					$id_user = $globalServerUserID;
110
					if ($id_user == NULL) $id_user = 1;
111
					if (!isset($id_source)) $id_source = 1;
112
					$query = 'INSERT INTO spotter_temp (id_user,datetime,hex,ident,latitude,longitude,verticalrate,speed,squawk,altitude,heading,registration,aircraft_icao,waypoints,id_source,noarchive,format_source,source_name,over_country) VALUES (:id_user,:datetime,:hex,:ident,:latitude,:longitude,:verticalrate,:speed,:squawk,:altitude,:heading,:registration,:aircraft_icao,:waypoints,:id_source,:noarchive, :format_source, :source_name, :over_country)';
113
					$query_values = array(':id_user' => $id_user,':datetime' => $data['datetime'],':hex' => $data['hex'],':ident' => $data['ident'],':latitude' => $data['latitude'],':longitude' => $data['longitude'],':verticalrate' => $data['verticalrate'],':speed' => $data['speed'],':squawk' => $data['squawk'],':altitude' => $data['altitude'],':heading' => $data['heading'],':registration' => $data['registration'],':aircraft_icao' => $data['aircraft_icao'],':waypoints' => $data['waypoints'],':id_source' => $id_source,':noarchive' => $data['noarchive'], ':format_source' => $data['format_source'], ':source_name' => $data['source_name'],':over_country' => $data['over_country']);
114
					try {
115
						$sth = $this->dbs['server']->prepare($query);
116
						$sth->execute($query_values);
117
					} catch(PDOException $e) {
118
						return "error : ".$e->getMessage();
119
					}
120
				}
121
			}
122
		}
123
	}
124
}
125
?>
126