Completed
Push — master ( 024748...e197a7 )
by Yannick
34:20
created

Weather::buildcloudlayer()   C

Complexity

Conditions 19
Paths 92

Size

Total Lines 52
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 42
nc 92
nop 1
dl 0
loc 52
rs 5.8807
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
class Weather {
3
4
/*	function buildcloud($coord = array(),$c) {
5
		$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
6
		$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
7
		$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
8
		$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
9
*/
10
	function buildcloud($lat,$long,$height,$c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
11
		
12
		$w = 1000.0;
13
		$h = 1000.0;
14
		$hdist = 1;
15
		$vdist = 1;
16
		
17
		$c = 5;
18
		$count = ($c + (mt_rand()-0.5)-$c);
19
		for ($j = 0;$j < $count; $j++) {
20
			$x = 0.0;
21
			$y = 0.0;
22
			$z = 0.0;
23
			for ($k = 0; $k < $hdist; $k++) {
24
				$x += (mt_rand()/$hdist);
25
				$y += (mt_rand()/$hdist);
26
			}
27
			for ($k = 0; $k < $vdist; $k++) {
28
				$z += (mt_rand()/$vdist);
29
			}
30
			$x = $w * ($x - 0.5) + $lat; // N/S
31
			$y = $w * ($y - 0.5) + $long; // E/W
32
			$z = $h * $z + $height; // Up/Down. pos[2] is the cloudbase
33
			// addcloud
34
			echo 'x: '.$x.'/y: '.$y.'/z: '.$z."\n";
35
		}
36
	}
37
	
38
	function buildcloudlayer($metar) {
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...
39
		//print_r($metar);
40
		$result = array();
41
		foreach($metar['cloud'] as $key => $data) {
42
			$alt_m = $metar['cloud'][$key]['level'];
43
			$alt_ft = $alt_m*3.28084;
44
			$pressure = $metar['QNH'];
45
			$cumulus_base = 122.0 * ($metar['temperature'] - $metar['dew']);
46
			$stratus_base = 100.0 * (100.0 * $metar['rh'])*0.3048;
47
			$coverage_norm = 0.0;
48
			if ($metar['cloud'][$key]['type'] == 'Few') {
49
				$coverage_norm = 2.0/8.0;
50
			} elseif ($metar['cloud'][$key]['type'] == 'Scattered') {
51
				$coverage_norm = 4.0/8.0;
52
			} elseif ($metar['cloud'][$key]['type'] == 'Broken') {
53
				$coverage_norm = 6.0/8.0;
54
			} elseif ($metar['cloud'][$key]['type'] == 'Overcast/Full cloud coverage') {
55
				$coverage_norm = 8.0/8.0;
56
			}
57
			$layer_type = 'nn';
0 ignored issues
show
Unused Code introduced by
$layer_type 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...
58
			if ($metar['cloud'][$key]['significant'] == 'cirrus') {
59
				$layer_type = 'ci';
60
			} elseif ($alt_ft > 16500) {
61
				$layer_type = 'ci';
62
			} elseif ($alt_ft > 6500) {
63
				$layer_type = 'ac';
64
				if ($pressure < 1005.0 && $coverage_norm >= 0.5) {
65
					$layer_type = 'ns';
66
				}
67
			} else {
68
				if ($cumulus_base * 0.80 < $alt_m && $cumulus_base * 1.20 > $alt_m) {
69
					$layer_type = 'cu';
70
				} elseif ($stratus_base * 0.80 < $alt_m && $stratus_base * 1.40 > $alt_m) {
71
					$layer_type = 'st';
72
				} else {
73
					if ($alt_ft < 2000) {
74
						$layer_type = 'st';
75
					} elseif ($alt_ft < 4500) {
76
						$layer_type = 'cu';
77
					} else {
78
						$layer_type = 'sc';
79
					}
80
				}
81
			}
82
			//echo 'coverage norm : '.$coverage_norm.' - layer_type: '.$layer_type."\n";
83
			$result[] = array('cov' => $coverage_norm, 'type' => $layer_type,'alt' => $alt_m,'rh' => $metar['rh']);
84
		}
85
		if (count($result) < 2 && $metar['rh'] > 60) {
86
			$result[] = array('cov' => 0.75, 'type' => 'cu','alt' => 4000,'rh' => $metar['rh']);
87
		}
88
		return $result;
89
	}
90
	
91
	function generateRandomPoint ($latitude,$longitude, $radius) {
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...
92
		$x0 = $longitude;
93
		$y0 = $latitude;
94
		
95
		// Convert Radius from meters to degrees.
96
		$rd = $radius / 111300;
97
		$u = (float)rand()/(float)getrandmax();
98
		$v = (float)rand()/(float)getrandmax();
99
		$w = $rd * sqrt($u);
100
		$t = 2 * pi() * $v;
101
		$x = $w * cos($t);
102
		$y = $w * sin($t);
103
		$xp = $x / cos($y0);
104
		return array('latitude' => $y + $y0,'longitude' => $xp + $x0);
105
	}
106
}
107
/*
108
require_once('class.METAR.php');
109
$METAR = new METAR();
110
*/
111
/*
112
$themetar = $METAR->getMETAR('LFLL');
113
print_r($themetar);
114
$result = $METAR->parse($themetar[0]['metar']);
115
*/
116
/*
117
$result = $METAR->parse('LFLL 081330Z 01006KT 340V050 9999 FEW020 BKN080 07/01 Q1018 NOSIG');
118
print_r($result);
119
$Weather = new Weather();
120
//print_r($Weather->buildcloudlayer($result));
121
//print_r($Weather->buildcloud('46.3870','5.2941','2000','0.25'));
122
print_r($Weather->generateRandomPoint('46.3870','5.2941','2000'));
123
*/
124
?>