Passed
Push — main ( b436ac...f1b4ad )
by N.
04:39
created

Graf::uppdatera_graf()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 70
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4.0011

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 0
dl 0
loc 70
ccs 23
cts 24
cp 0.9583
crap 4.0011
rs 9.584
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 Graf.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\DistributionGenerera;
11
12
/**
13
 * Klass Graf.
14
 * Rita graf över distribution.
15
 * 25 % av alla utfall finns inom 1 % distribution.
16
 * 50 % inom 5 %.
17
 * 65 % inom 10 %.
18
 * Jättevinsterna faller utanför detta intervall.
19
 */
20
class Graf extends Rendera {
21
	/**
22
	 * @var int[] $distribution
23
	 */
24
	protected array $distribution = [];
25
26
	/**
27
	 * Uppdatera graf.
28
	 */
29 1
	protected function uppdatera_graf(): void {
30 1
		if (count($this->distribution) === 0) {
31
			return;
32
		}
33
34
		/**
35
		 * Oddssummor.
36
		 */
37 1
		[$oddssumma_min, $oddssumma_max, $oddssumma_utfall] = $this->oddssummor();
38 1
		$distmax_antal_rader = max($this->distribution);
39
40
		/**
41
		 * Deltan i korordinater.
42
		 */
43 1
		[$dx, $dy] = [
44 1
			$this->dist->graf->bredd / ($oddssumma_max - $oddssumma_min),
45 1
			$this->dist->graf->höjd / $distmax_antal_rader
46 1
		];
47
48
		/**
49
		 * Nollställ parametrar.
50
		 */
51 1
		$delsumma = 0.0;
52 1
		[$this->dist->maxsumma, $this->dist->minsumma, $this->dist->andelssumma] = [0, 0, 0];
53
54
		/**
55
		 * Iterera över koordinater.
56
		 */
57 1
		foreach ($this->distribution as $xkoord => $ykoord) {
58
			/**
59
			 * Koordinater.
60
			 */
61 1
			[$x, $y] = [
62 1
				intval(($xkoord - $oddssumma_min) * $dx),
63 1
				intval($this->dist->graf->höjd - $ykoord * $dy)
64 1
			];
65
66
			/**
67
			 * Plussa andelssumma.
68
			 */
69 1
			if ($xkoord > $oddssumma_utfall) {
70 1
				$this->dist->andelssumma += $ykoord;
71
			}
72
73
			/**
74
			 * Gränslinjer.
75
			 */
76 1
			$delsumma += $ykoord;
77 1
			$andel = 100 * $delsumma / MATCHRYMD;
78
79
			/**
80
			 * Rendera linjer.
81
			 */
82 1
			$this->rendera_linjer($andel, $x, $y, $xkoord, $oddssumma_utfall);
83
		}
84
85
		/**
86
		 * Uppdatera procentandel för distribution, relativt totala matchrymden 3^13.
87
		 */
88 1
		$this->dist->procentandel = round(100 * $this->dist->andelssumma / MATCHRYMD, 3);
89
90
		/**
91
		 * Rendera text.
92
		 */
93 1
		$this->rendera_text((int) $distmax_antal_rader, $oddssumma_min, $oddssumma_max, $oddssumma_utfall);
94
95
		/**
96
		 * Spara graf.
97
		 */
98 1
		$this->spara_graf();
99
	}
100
101
	/**
102
	 * Rendera gränslinjer i dist.
103
	 */
104 1
	private function rendera_linjer(float $andel, int $x, int $y, string $xkoord, float $oddssumma_utfall): void {
105
		/**
106
		 * Rita procentsatser med linjer i gröna nyanser.
107
		 * Intensitet ökar med avstånd från origo.
108
		 * Använder 0.5 %, 1 %, 2%, 3 %, 5 % och 10 %.
109
		 * En procentenhet motsvarar 15943 rader.
110
		 */
111 1
		$this->percentage_lines($andel, $x, $xkoord);
112
113
		/**
114
		 * Rita aktuellt utfall med röd linje.
115
		 * Motsvarar den momentana sannolikhetssumman för odds för omgången.
116
		 */
117 1
		if ($oddssumma_utfall == $xkoord) {
118 1
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd, $this->dist->graf->röd);
119
		}
120
121
		/**
122
		 * Rita maxprocent med vit linje.
123
		 */
124 1
		if ($this->dist->maxsumma == 0 && $andel > $this->dist->minprocent) {
125 1
			$this->dist->maxsumma = (float) $xkoord;
126 1
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
127
		}
128
129
		/**
130
		 * Rita minprocent med vit linje.
131
		 * Reducera höjd på linje för att ge plats åt eventuell undre linje.
132
		 */
133 1
		if ($this->dist->minsumma == 0 && $andel > $this->dist->maxprocent) {
134 1
			$this->dist->minsumma = (float) $xkoord;
135 1
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
136
		}
137
138
		/**
139
		 * Rita kurva med blå färg.
140
		 */
141 1
		$this->dist->graf->sätt_pixel($x, $y, $this->dist->graf->blå);
142
	}
143
144
	/**
145
	 * Rita ut textinformation i graf.
146
	 */
147 1
	private function rendera_text(
148
		int $distmax_antal_rader,
149
		float $oddssumma_min,
150
		float $oddssumma_max,
151
		float $oddssumma_utfall
152
	): void {
153
		/**
154
		 * Rendera omgångsdata med gul text.
155
		 */
156 1
		$this->dist->graf->sätt_text(20, 20, "{$this->tips->odds->spel->speltyp->produktnamn()} {$this->tips->odds->spel->omgång}: utdelning {$this->tips->utdelning->utdelning[0]}", $this->dist->graf->gul);
157
158
		/**
159
		 * Rendera distributionsdata med gul text.
160
		 */
161 1
		$distmax = array_search($distmax_antal_rader, $this->distribution, true);
162 1
		$this->dist->graf->sätt_text(20, 40, "x_min: $oddssumma_min, mitt: $distmax ($distmax_antal_rader), x_max: $oddssumma_max", $this->dist->graf->gul);
163
164
		/**
165
		 * Rendera aktuellt utfall med röd text.
166
		 */
167 1
		if ($oddssumma_utfall > 0) {
168 1
			$this->dist->graf->sätt_text(20, 60, "utfall: $oddssumma_utfall ({$this->dist->andelssumma}) | andel: {$this->dist->procentandel} %", $this->dist->graf->röd);
169
		}
170
	}
171
172
	/**
173
	 * Spara distributionsgraf.
174
	 */
175 1
	private function spara_graf(): void {
176
		/**
177
		 * Spara och beräkna sannolikhetssummor.
178
		 * Logga händelse.
179
		 */
180 1
		$this->dist->graf->spara_tipsgraf($this->dist->bildfil);
181 1
		$this->dist->beräkna_sannolikhetssummor($this->tips->utdelning->tipsrad_012);
182 1
		$this->spara_distribution();
183 1
		$this->tips->odds->spel->db->logg->logga(self::class . ' ✅ Uppdaterade graf, sparade distribution.');
184
	}
185
}
186