Passed
Push — main ( d29be2...38376b )
by N.
03:16
created

Kontrollera::kontrollera_nya_moduler()   B

Complexity

Conditions 9
Paths 45

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9.0266

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 45
nop 0
dl 0
loc 53
ccs 27
cts 29
cp 0.931
crap 9.0266
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

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
3
/**
4
 * Klass Kontrollera.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser\Moduler;
11
12
use PDO;
13
use PDOStatement;
14
15
/**
16
 * Klass Kontrollera.
17
 */
18
class Kontrollera extends Annonsera {
19
	/**
20
	 * Kontrollera nya moduler.
21
	 */
22 4
	public function kontrollera_nya_moduler(): void {
23
		/**
24
		 * Denna kontroll har ingen mening vid generering.
25
		 */
26 4
		if (defined("GENERERA")) {
27
			return;
28
		}
29
30 4
		$moduler = []; // aktiva moduler
31 4
		$modulbeteckning = []; // modulnamn
32
33 4
		$glob = (array) glob(MODULER . '/*.php');
0 ignored issues
show
Bug introduced by
The constant Tips\Klasser\Moduler\MODULER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34 4
		$befintliga_moduler = array_map(fn ($filnamn): string => basename((string) $filnamn, '.php'), $glob);
35
36 4
		$index = 1;
37
		/** @var PDOStatement $sats */
38 4
		$sats = $this->utdelning->spel->db->instans->query('SELECT * FROM `moduler` ORDER BY `prioritet`');
39
40 4
		foreach ($sats->fetchAll(PDO::FETCH_ASSOC) as $rad) {
41 4
			if (isset($_REQUEST['uppdatera_moduler'])) {
42 1
				$rad['aktiv'] = isset($_REQUEST['modul'][$rad['namn']]) &&
43 1
					$_REQUEST['modul'][$rad['namn']]['aktiv'] === '1' ? 1 : 0;
44
			}
45
46 4
			if (in_array($rad['namn'], $befintliga_moduler, true)) {
47 4
				$moduler[] = [$index, $rad['namn'], $rad['aktiv']];
48 4
				$modulbeteckning[] = $rad['namn'];
49 4
				$index++;
50
			}
51
		}
52
53 4
		$antal_moduler = count($modulbeteckning);
54 4
		$nya_moduler = array_diff($befintliga_moduler, $modulbeteckning);
55
56
		/**
57
		 * Lägg till nya moduler i filsystem.
58
		 */
59 4
		foreach ($nya_moduler as $ny_modul) {
60
			$moduler[] = [$antal_moduler++, $ny_modul, 0];
61
		}
62
63
		/**
64
		 * Nyskapa struktur.
65
		 */
66 4
		$this->utdelning->spel->db->instans->query("DELETE FROM `moduler`");
67
68 4
		foreach ($moduler as $modul) {
69 4
			$sats = $this->utdelning->spel->db->instans->prepare("INSERT INTO `moduler`
70 4
				(`prioritet`, `namn`, `aktiv`) VALUES (:prioritet, :namn, :aktiv)");
71 4
			$sats->bindValue(':prioritet', $modul[0], PDO::PARAM_INT);
72 4
			$sats->bindValue(':namn', $modul[1], PDO::PARAM_STR);
73 4
			$sats->bindValue(':aktiv', $modul[2], PDO::PARAM_BOOL);
74 4
			$sats->execute();
75
		}
76
	}
77
}
78