GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GalaxyGenerator   D
last analyzed

Complexity

Total Complexity 81

Size/Duplication

Total Lines 535
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 535
rs 4.8717
c 0
b 0
f 0
wmc 81
lcom 1
cbo 4

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 13 1
A clear() 0 15 1
C save() 0 51 10
A getLog() 0 7 1
A log() 0 3 1
D generateSystem() 0 117 16
D generatePlace() 0 102 18
B generateSector() 0 32 3
D associateSystemToSector() 0 35 10
B getStatisticsSector() 0 28 2
A isPointInMap() 0 20 3
A l2p() 0 3 1
B distToSegment() 0 22 4
B getProportion() 0 26 6
A getSystem() 0 3 1
A getNbOfPlace() 0 6 1
A getTypeOfPlace() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like GalaxyGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GalaxyGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Asylamba\Modules\Gaia\Helper;
4
5
use Asylamba\Classes\Database\DatabaseAdmin;
6
use Asylamba\Classes\Library\Utils;
7
8
use Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration;
9
10
use Asylamba\Modules\Gaia\Model\PointLocation;
11
use Asylamba\Modules\Gaia\Model\Place;
12
use Asylamba\Classes\Library\Format;
13
14
class GalaxyGenerator {
15
	const MAX_QUERY = 5000;
16
17
	# stats
18
	public $nbSystem = 0;
19
	public $listSystem = array();
20
21
	public $nbPlace = 0;
22
	public $popTotal = 0;
23
	public $listPlace = array();
24
25
	public $nbSector = 0;
26
	public $systemDeleted = 0;
27
	public $listSector = array();
28
29
	/** @var string **/
30
	protected $output;
31
	/** @var DatabaseAdmin **/
32
	protected $databaseAdmin;
33
	/** @var GalaxyConfiguration **/
34
	protected $galaxyConfiguration;
35
	
36
	/**
37
	 * @param DatabaseAdmin $databaseAdmin
38
	 * @param GalaxyConfiguration $galaxyConfiguration
39
	 */
40
	public function __construct(DatabaseAdmin $databaseAdmin, GalaxyConfiguration $galaxyConfiguration)
41
	{
42
		$this->databaseAdmin = $databaseAdmin;
43
		$this->galaxyConfiguration = $galaxyConfiguration;
44
	}
45
46
	public function generate() {
47
		$this->clear();
48
49
		# generation
50
		$this->generateSector();
51
		$this->generateSystem();
52
		$this->associateSystemToSector();
53
		$this->generatePlace();
54
55
		$this->save();
56
57
		$this->getStatisticsSector();
58
	}
59
60
	public function clear() {
61
		$this->databaseAdmin->query('SET FOREIGN_KEY_CHECKS = 0;');
62
		
63
		$this->databaseAdmin->query('TRUNCATE place');
64
		$this->log('table `place` vidées');
65
66
		$this->databaseAdmin->query('TRUNCATE system');
67
		$this->log('table `system` vidées');
68
69
		$this->databaseAdmin->query('TRUNCATE sector');
70
		$this->log('table `sector` vidées');
71
72
		$this->databaseAdmin->query('SET FOREIGN_KEY_CHECKS = 1;');
73
		$this->log('_ _ _ _');
74
	}
75
76
	public function save() {
77
		# clean up database
78
		$this->clear();
79
80
		$this->log('sauvegarde des secteurs');
81
		for ($i = 0; $i < ceil(count($this->listSector) / self::MAX_QUERY); $i++) { 
82
			$qr = 'INSERT INTO sector(id, rColor, xPosition, yPosition, xBarycentric, yBarycentric, tax, population, lifePlanet, name, prime, points) VALUES ';
83
			
84
			for ($j = $i * self::MAX_QUERY; $j < (($i + 1) * self::MAX_QUERY) - 1; $j++) { 
85
				if (isset($this->listSector[$j])) {
86
					$qr .= '(\'' . implode('\', \'', $this->listSector[$j]) . '\'), ';
87
				}
88
			}
89
90
			$qr = substr($qr, 0, -2);
91
			$this->databaseAdmin->query($qr);
92
		}
93
		$this->log(ceil(count($this->listSector) / self::MAX_QUERY) . ' requêtes `INSERT`');
94
95
		$this->log('sauvegarde des systèmes');
96
		for ($i = 0; $i < ceil(count($this->listSystem) / self::MAX_QUERY); $i++) { 
97
			$qr = 'INSERT INTO system(id, rSector, rColor, xPosition, yPosition, typeOfSystem) VALUES ';
98
			
99
			for ($j = $i * self::MAX_QUERY; $j < (($i + 1) * self::MAX_QUERY) - 1; $j++) { 
100
				if (isset($this->listSystem[$j])) {
101
					$qr .= '(' . implode(', ', $this->listSystem[$j]) . '), ';
102
				}
103
			}
104
105
			$qr = substr($qr, 0, -2);
106
			$this->databaseAdmin->query($qr);
107
		}
108
		$this->log(ceil(count($this->listSystem) / self::MAX_QUERY) . ' requêtes `INSERT`');
109
110
		$this->log('sauvegarde des places');
111
		for ($i = 0; $i < ceil(count($this->listPlace) / self::MAX_QUERY); $i++) { 
112
			$qr = 'INSERT INTO place(id, rSystem, typeOfPlace, position, population, coefResources, coefHistory, resources, danger, maxDanger, uPlace) VALUES ';
113
			
114
			for ($j = $i * self::MAX_QUERY; $j < (($i + 1) * self::MAX_QUERY) - 1; $j++) { 
115
				if (isset($this->listPlace[$j])) {
116
					$qr .= '(' . implode(', ', $this->listPlace[$j]) . ', "' . Utils::addSecondsToDate(Utils::now(), -259200) . '"), ';
117
				}
118
			}
119
120
			$qr = substr($qr, 0, -2);
121
			$this->databaseAdmin->query($qr);
122
		}
123
		$this->log(ceil(count($this->listPlace) / self::MAX_QUERY) . ' requêtes `INSERT`');
124
125
		$this->log('_ _ _ _');
126
	}
127
128
	public function getLog() {
129
		$rt  = '<pre style="font-family: consolas;">';
130
			$rt .= $this->output;
131
		$rt .= '</pre>';
132
133
		return $rt;
134
	}
135
136
	private function log($text) {
137
		$this->output .= ">_ $text <br />";
138
	}
139
140
	private function generateSystem() {
141
		$this->log('génération des systèmes');
142
143
		# id
144
		$k = 1;
145
146
		# GENERATION DES LINES
147
		for ($w = 0; $w < count($this->galaxyConfiguration->galaxy['lineSystemPosition']); $w++) {
0 ignored issues
show
Bug introduced by
The property galaxy does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
148
			# line point
149
			$xA = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][0][0];
150
			$yA = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][0][1];
151
152
			$xB = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][1][0];
153
			$yB = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][1][1];
154
155
			$l  = sqrt(pow($xB - $xA, 2) + pow($yB - $yA, 2));
0 ignored issues
show
Unused Code introduced by
$l 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...
156
157
			for ($i = 1; $i <= $this->galaxyConfiguration->galaxy['size']; $i++) {
158
				for ($j = 1; $j <= $this->galaxyConfiguration->galaxy['size']; $j++) {
159
					# current cursor position
160
					$xC = $j;
161
					$yC = $i;
162
163
					$d  = $this->distToSegment($xC, $yC, $xA, $yA, $xB, $yB);
164
165
					$thickness = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][2];
166
					$intensity = $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][3];
167
168
					if ($d < $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][2]) {
169
						#$prob = rand(0, $this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][3]);
170
						$prob = rand(0, 100);
171
172
173
						#if ($this->galaxyConfiguration->galaxy['lineSystemPosition'][$w][2] - $d > $prob) {
174
						if (round($intensity - ($d * $intensity / $thickness)) >= $prob) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
175
176
177
							$type = $this->getSystem();
178
179
							$this->nbSystem++;
180
							$this->listSystem[] = array($k, 0, 0, $xC, $yC, $type);
181
182
							$k++;
183
						}
184
					}
185
				}
186
			}
187
		}
188
189
		# GENERATION DES ANNEAUX (circleSystemPosition)
190
		for ($w = 0; $w < count($this->galaxyConfiguration->galaxy['circleSystemPosition']); $w++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
191
			# line point
192
			$xC = $this->galaxyConfiguration->galaxy['circleSystemPosition'][$w][0][0];
193
			$yC = $this->galaxyConfiguration->galaxy['circleSystemPosition'][$w][0][1];
194
195
			$radius 	= $this->galaxyConfiguration->galaxy['circleSystemPosition'][$w][1];
196
			$thickness 	= $this->galaxyConfiguration->galaxy['circleSystemPosition'][$w][2];
197
			$intensity	= $this->galaxyConfiguration->galaxy['circleSystemPosition'][$w][3];
198
199
			for ($i = 1; $i <= $this->galaxyConfiguration->galaxy['size']; $i++) {
200
				for ($j = 1; $j <= $this->galaxyConfiguration->galaxy['size']; $j++) {
201
					# current cursor position
202
					$xPosition = $j;
203
					$yPosition = $i;
204
205
					# calcul de la distance entre la case et le centre
206
					$d = sqrt(
207
						pow(abs($xC - $xPosition), 2) + 
208
						pow(abs($yC - $yPosition), 2)
209
					);
210
	
211
					if ($d >= ($radius - $thickness) && $d <= ($radius + $thickness)) {
212
						$dtoseg = abs($d - $radius);
213
						$prob 	= rand(0, 100);
214
215
						if (round($intensity - ($dtoseg * $intensity / $thickness)) >= $prob) {
216
							$type = $this->getSystem();
217
218
							$this->nbSystem++;
219
							$this->listSystem[] = array($k, 0, 0, $xPosition, $yPosition, $type);
220
221
							$k++;
222
						}
223
					}
224
				}
225
			}
226
		}
227
228
		# GENERATION PAR VAGUES
229
		if ($this->galaxyConfiguration->galaxy['systemPosition'] !== NULL) {
230
			for ($i = 1; $i <= $this->galaxyConfiguration->galaxy['size']; $i++) {
231
				for ($j = 1; $j <= $this->galaxyConfiguration->galaxy['size']; $j++) {
232
					# current cursor position
233
					$xPosition = $j;
234
					$yPosition = $i;
235
					
236
					# calcul de la distance entre la case et le centre
237
					$d2o = sqrt(
238
						pow(abs(($this->galaxyConfiguration->galaxy['size'] / 2) - $xPosition), 2) + 
239
						pow(abs(($this->galaxyConfiguration->galaxy['size'] / 2) - $yPosition), 2)
240
					);
241
					
242
					if ($this->isPointInMap($d2o)) {
243
						$type = $this->getSystem();
244
245
						$this->nbSystem++;
246
						$this->listSystem[] = array($k, 0, 0, $xPosition, $yPosition, $type);
247
248
						$k++;
249
					}
250
				}
251
			}
252
		}
253
254
		$this->log($this->nbSystem . ' systèmes générés');
255
		$this->log('_ _ _ _');
256
	}
257
258
	public function generatePlace() {
259
		$this->log('génération des places');
260
		$k = 1;
261
262
		foreach ($this->listSystem AS $system) {
0 ignored issues
show
Coding Style introduced by
AS keyword must be lowercase; expected "as" but found "AS"
Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected as, but found AS.
Loading history...
263
			$sectorDanger = 0;
264
			foreach ($this->galaxyConfiguration->sectors as $sector) {
0 ignored issues
show
Bug introduced by
The property sectors does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
265
				if ($system[1] == $sector['id']) {
266
					$sectorDanger = $sector['danger'];
267
					break;
268
				}
269
			}
270
271
			$place = $this->getNbOfPlace($system[5]);
272
273
			for ($i = 0; $i < $place; $i++) {
274
				$type = $this->getTypeOfPlace($system[5]);
275
276
				if ($type == 1) {
277
					$pointsRep = rand(1, 10);
278
					$abilities = [
279
						'population' => 0,
280
						'history' => 0,
281
						'resources' => 0
282
					];
283
284
					# nombre de point a distribuer
285
					if ($pointsRep < 2) {
286
						$pointsTot = rand(90, 100);
287
					} elseif ($pointsRep < 10) {
288
						$pointsTot = 100;
289
					} else {
290
						$pointsTot = rand(100, 120);
291
					}
292
293
					# brassage du tableau
294
					Utils::shuffle($abilities);
295
296
					# répartition
297
					$z = 1;
298
					foreach ($abilities as $l => $v) {
299
						if ($z < 3) {
300
							$max = $pointsTot - ($z * 10);
301
							$max = $max < 10 ? 10 : $max;
302
303
							$points = rand(10, $max);
304
							$abilities[$l] = $points;
305
							$pointsTot -= $points;
306
						} else {
307
							$abilities[$l] = $pointsTot < 5 ? 5 : $pointsTot;
308
						}
309
310
						$z++;
311
					}
312
313
					$population = $abilities['population'] * 250 / 100;
314
					$history 	= $abilities['history'];
315
					$resources 	= $abilities['resources'];
316
					$stRES		= 0;
317
				} elseif ($type == 6) {
318
					$population = 0;
319
					$history 	= 0;
320
					$resources 	= 0;
321
					$stRES		= 0;
322
				} else {
323
					$population = $this->galaxyConfiguration->places[$type - 1]['credits'];
0 ignored issues
show
Bug introduced by
The property places does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
324
					$resources 	= $this->galaxyConfiguration->places[$type - 1]['resources'];
325
					$history 	= $this->galaxyConfiguration->places[$type - 1]['history'];
326
					$stRES		= rand(2000000, 20000000);
327
				}
328
329
				# TODO DANGER
330
				switch ($sectorDanger) {
331
					case GalaxyConfiguration::DNG_CASUAL:
332
						$danger = rand(0,  Place::DNG_CASUAL);
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
333
					break;
334
					case GalaxyConfiguration::DNG_EASY:
335
						$danger = rand(3,  Place::DNG_EASY);
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
336
					break;
337
					case GalaxyConfiguration::DNG_MEDIUM:
338
						$danger = rand(6, Place::DNG_MEDIUM);
339
					break;
340
					case GalaxyConfiguration::DNG_HARD:
341
						$danger = rand(9, Place::DNG_HARD);
342
					break;
343
					case GalaxyConfiguration::DNG_VERY_HARD:
344
						$danger = rand(12, Place::DNG_VERY_HARD);
345
					break;
346
					default: $danger = 0; break;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
347
				}
348
349
				$this->nbPlace++;
350
				$this->popTotal += $population;
351
				$this->listPlace[] = array($k, $system[0], $type, ($i + 1), $population, $resources, $history, $stRES, $danger, $danger);
352
				$k++;
353
			}
354
		}
355
356
		$this->log($this->nbPlace . ' places générées');
357
		$this->log(Format::numberFormat($this->popTotal * 1000000) . ' de population');
358
		$this->log('_ _ _ _');
359
	}
360
361
	public function generateSector() {
362
		$this->log('génération des secteurs');
363
		$k = 1;
364
365
		foreach ($this->galaxyConfiguration->sectors as $sector) {
0 ignored issues
show
Bug introduced by
The property sectors does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
366
			$this->nbSector++;
367
368
			$prime = ($sector['beginColor'] != 0)
369
				? 1
370
				: 0;
371
372
			$this->listSector[] = array(
373
				$k, 
374
				$sector['beginColor'], 
375
				$sector['display'][0], 
376
				$sector['display'][1], 
377
				$sector['barycentre'][0], 
378
				$sector['barycentre'][1], 
379
				5, 
380
				0,
381
				0,
382
				$sector['name'],
383
				$prime,
384
				$sector['points']
385
			);
386
387
			$k++;
388
		}
389
390
		$this->log($this->nbSector . ' secteurs générés');
391
		$this->log('_ _ _ _');
392
	}
393
394
	public function associateSystemToSector() {
395
		$pl = new PointLocation();
396
		$systemToDelete = array();
397
		$k = 0;
398
399
		foreach ($this->listSystem as $v) {
400
			foreach ($this->galaxyConfiguration->sectors as $w) {
0 ignored issues
show
Bug introduced by
The property sectors does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
401
				$place = $pl->pointInPolygon($v[3] . ', ' . $v[4], $w['vertices']);
402
403
				if ($place == 1 OR $place == 2) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
404
					$systemToDelete[] = $v[0];
405
					break;
406
				} elseif ($place == 3) {
407
					$this->listSystem[$k][1] = $w['id'];
408
					break;
409
				}
410
			}
411
			$k++;
412
		}
413
414
		foreach ($this->listSystem as $v) {
415
			if ($v[1] == 0) {
416
				$systemToDelete[] = $v[0];
417
			}
418
		}
419
		
420
		# suppression des systemes sur des lignes ou des angles
421
		for ($i = count($this->listSystem) - 1; $i >= 0; $i--) { 
422
			if (in_array($this->listSystem[$i][0], $systemToDelete)) {
423
				unset($this->listSystem[$i]);
424
			}
425
		}
426
427
		$this->systemDeleted = count($systemToDelete);
428
	}
429
430
	protected function getStatisticsSector() {
431
		foreach ($this->listSector as $sector) {
432
			$id = $sector[0];
433
434
			$qr = $this->databaseAdmin->prepare('SELECT
435
					COUNT(pl.id) AS planet,
436
					SUM(pl.population) AS population
437
				FROM sector AS se
438
				LEFT JOIN system AS sy
439
					ON se.id = sy.rSector
440
				LEFT JOIN place AS pl
441
					ON sy.id = pl.rSystem
442
				WHERE pl.typeOfPlace = 1
443
				AND se.id = ?');
444
			$qr->execute(array($id));
445
			$aw = $qr->fetch();
446
447
			$nbrPlanet = $aw['planet'];
448
			$population = ceil($aw['population']);
449
450
			$qr->closeCursor();
451
452
			$qr = $this->databaseAdmin->prepare('UPDATE sector SET lifePlanet = ?, population = ? WHERE id = ?');
453
			$qr->execute(array($nbrPlanet, $population, $id));
454
455
			$qr->closeCursor();
456
		}
457
	}
458
459
	protected function isPointInMap($d2o) {
460
		$mask = rand(1, $this->galaxyConfiguration->galaxy['mask']);
0 ignored issues
show
Bug introduced by
The property galaxy does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
461
462
		if ($mask < 3) {
463
			$realPosition = $this->galaxyConfiguration->galaxy['diag'] - $d2o;
464
			$step 		  = $this->galaxyConfiguration->galaxy['diag'] / count($this->galaxyConfiguration->galaxy['systemPosition']);
465
			$currentStep  = floor($realPosition / $step);
466
467
			$random = rand(0, 100);
468
469
			if ($this->galaxyConfiguration->galaxy['systemPosition'][$currentStep] > $random) {
470
				return TRUE;
471
			} else {
472
				return FALSE;
473
			}
474
		} else {
475
			return FALSE;
476
		}
477
478
	}
479
480
	protected function l2p($x1, $x2, $y1, $y2) {
481
		return (pow($x1 - $y1, 2) + pow($x2 - $y2, 2));
482
	}
483
484
	protected function distToSegment($p1, $p2, $v1, $v2, $w1, $w2) {
485
		$l2 = $this->l2p($v1, $v2, $w1, $w2);
486
487
		if ($l2 == 0) {
488
			return sqrt($this->l2p($p1, $p2, $v1, $v2));
489
		}
490
491
		$t  = (($p1 - $v1) * ($w1 - $v1) + ($p2 - $v2) * ($w2 - $v2)) / $l2;
492
493
		if ($t < 0) {
494
			return sqrt($this->l2p($p1, $p2, $v1, $v2));
495
		}
496
497
		if ($t > 1) {
498
			return sqrt($this->l2p($p1, $p2, $w1, $w2));
499
		}
500
501
		$tx = $v1 + $t * ($w1 - $v1);
502
		$ty = $v2 + $t * ($w2 - $v2);
503
504
		return sqrt($this->l2p($p1, $p2, $tx, $ty));
505
	}
506
507
	protected function getProportion($params, $value) {
508
		$cursor	= 0;
509
		$type 	= 0;
510
		$min 	= 0;
511
		$max 	= 0;
0 ignored issues
show
Unused Code introduced by
$max 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...
512
513
		for ($i = 0; $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
514
			if ($i == 0) {
515
				$max = $params[$i];
516
			} elseif ($i < count($params) - 1) {
517
				$min = $cursor;
518
				$max = $cursor + $params[$i];
519
			} else {
520
				$min = $cursor;
521
				$max = 100;
522
			}
523
524
			$cursor = $max;
525
			$type += 1;
526
527
528
			if ($value > $min && $value <= $max) {
529
				return $type;
530
			}
531
		}
532
	}
533
534
	protected function getSystem() {
535
		return $this->getProportion($this->galaxyConfiguration->galaxy['systemProportion'], rand(1, 100));
0 ignored issues
show
Bug introduced by
The property galaxy does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
536
	}
537
538
	protected function getNbOfPlace($systemType) {
539
		return rand(
540
			$this->galaxyConfiguration->systems[$systemType - 1]['nbrPlaces'][0],
0 ignored issues
show
Bug introduced by
The property systems does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
541
			$this->galaxyConfiguration->systems[$systemType - 1]['nbrPlaces'][1]
542
		);
543
	}
544
545
	protected function getTypeOfPlace($systemType) {
546
		return $this->getProportion($this->galaxyConfiguration->systems[$systemType - 1]['placesPropotion'], rand(1, 100));
0 ignored issues
show
Bug introduced by
The property systems does not seem to exist in Asylamba\Modules\Gaia\Galaxy\GalaxyConfiguration.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
547
	}
548
}