Passed
Push — main ( 9152fd...203f91 )
by N.
03:37
created

Scheman::visa_scheman()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 55
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0702

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 13
nop 0
dl 0
loc 55
ccs 21
cts 24
cp 0.875
crap 6.0702
rs 8.9457
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 Scheman.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use Tips\Klasser\Scheman\Hamta;
13
14
/**
15
 * Klass Scheman.
16
 * Förinställda konfigurationer för olika moduler med värden.
17
 */
18
final class Scheman extends Hamta {
19 1
	public function __construct(protected Tips $tips) {
20 1
		parent::__construct($this->tips);
21 1
		$this->hämta_scheman();
22 1
		$this->visa_scheman();
23
	}
24
25
	/**
26
	 * Visa scheman.
27
	 */
28 1
	public function visa_scheman(): void {
29 1
		$db_preferenser = new DBPreferenser($this->tips->spel->db);
30
31 1
		$aktiva_moduler = array_keys($this->tips->moduler->moduldata);
32
33 1
		$scheman = '';
34 1
		foreach ($this->scheman as $id => $schema) {
35 1
			$schemarad = '';
36 1
			$data = [];
37 1
			$schemamoduler = [];
38
39
			/**
40
			 * Associativt fält $data[$namn]:
41
			 * [schema_namn] => R_4_5_238_1 F2/4
42
			 * [schema_antal_rader] => 100
43
			 */
44 1
			foreach (explode(',', $schema['data']) as $dat) {
45 1
				[$namn, $data[$namn]] = explode(':', $dat);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $namn does not seem to be defined for all execution paths leading up to this point.
Loading history...
46
			}
47
48
			/**
49
			 * Lägg till rader för aktiva moduler.
50
			 */
51 1
			foreach (explode(',', $schema['faktorer']) as $faktor) {
52 1
				[$modul, $attraktionsfaktor] = explode(':', $faktor);
53 1
				$schemamoduler[] = $modul;
54
55 1
				[$klass, $radera] = match (in_array($modul, $aktiva_moduler)) {
56 1
					true => ['', ''],
57
					false => [' class="streck radera"', '❌ ']
58 1
				};
59
60 1
				$schemarad .= $this->schemarad($modul, $attraktionsfaktor, $klass, $radera);
61
			}
62
63
			/**
64
			 * Lägg till rader för nyaktiverade moduler.
65
			 */
66 1
			foreach ($aktiva_moduler as $modul) {
67 1
				if (!in_array($modul, $schemamoduler)) {
68
					$attraktionsfaktor = $db_preferenser->hämta_preferens(mb_strtolower($modul) . '.attraktionsfaktor');
69
					$schemarad .= $this->schemarad($modul, $attraktionsfaktor);
70
				}
71
			}
72
73
			/**
74
			 * Lägg samman rader.
75
			 */
76 1
			$scheman .= $this->enskilt_schema($id, $data, $schemarad);
77
		}
78
79
			/**
80
			 * Eka ut tabeller.
81
			 */
82 1
		echo $this->schema($scheman);
83
	}
84
}
85