Passed
Push — main ( e005e2...1bd9df )
by N.
03:40
created

Utfallshistorik::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass Utfallshistorik.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use PDO;
13
14
/**
15
 * Klass Utfallshistorik.
16
 * Historik över utfall i matcher inom oddsintervall.
17
 * Nyttjas i grid för favoriter, i kontrast till givna odds.
18
 * Data hämtas från samtliga stryk- och europatipsomgångar.
19
 */
20
final class Utfallshistorik {
21
	/**
22
	 * @var array<int, float[]> $utfallshistorik
23
	 */
24
	public array $utfallshistorik;
25
	/**
26
	 * @var int[] $ordnad_historik
27
	 */
28
	public array $ordnad_historik;
29
30
	/**
31
	 * Initiera.
32
	 */
33 4
	public function __construct(public Prediktioner $prediktioner) {
34 4
		$this->utfallshistorik();
35
	}
36
37
	/**
38
	 * Beäkna historik över utfall.
39
	 * Redovisas i favorittabell under historik.
40
	 */
41 4
	private function utfallshistorik(): void {
42 4
		[$pmin, $pmax] = [0.93, 1.07]; // ± 7 % oddsintervall
43 4
		$resultat = [];
44
45
		/**
46
		 * Analysera odds från alla tipsomgångar.
47
		 */
48 4
		foreach ($this->prediktioner->prediktioner as $match => $odds) {
49 4
			$query = '';
50
51 4
			for ($index = 1; $index <= MATCHANTAL; $index++) {
0 ignored issues
show
Bug introduced by
The constant Tips\Klasser\MATCHANTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52 4
				$bas = 3 * ($index - 1);
53 4
				$pred = [$bas + 1, $bas + 2, $bas + 3]; // p1–p39, d.v.s. alla enskilda odds
54 4
				$query .= "SELECT `resultat$index` AS `res` FROM `odds` NATURAL JOIN `matcher`
55 4
				NATURAL JOIN `utdelning` WHERE `tipsrad_012` AND `resultat$index` AND
56 4
					$pmin * `p$pred[0]` < $odds[0] AND $pmax * `p$pred[0]` > $odds[0] AND
57 4
					$pmin * `p$pred[1]` < $odds[1] AND $pmax * `p$pred[1]` > $odds[1] AND
58 4
					$pmin * `p$pred[2]` < $odds[2] AND $pmax * `p$pred[2]` > $odds[2]";
59 4
				if ($index < MATCHANTAL) {
60 4
					$query .= " UNION ALL ";
61
				}
62
			}
63
64 4
			$sats = $this->prediktioner->spel->db->instans->prepare($query);
65 4
			$sats->execute();
66
67
			/**
68
			 * Addera varje utfall per match.
69
			 */
70 4
			$resultat[$match] = [0, 0, 0];
71 4
			foreach ($sats->fetchAll(PDO::FETCH_ASSOC) as $rad) {
72 4
				$res = explode('-', $rad['res']);
73
				/**
74
				 * $r1 > $r2: 2; $r1 === $r2: 1; $r1 < $r2: 0
75
				 * Observera: omkastad ordning
76
				 */
77 4
				$tecken = ($res[1] <=> $res[0]) + 1;
78 4
				$resultat[$match][$tecken]++;
79
			}
80
		}
81
82
		/**
83
		 * Sammanställ historik.
84
		 */
85 4
		$this->utfallshistorik = [];
86 4
		foreach ($resultat as $index => $rad) {
87
			/**
88
			 * Format: andel 1X2 samt antal statistikposter.
89
			 * Andel i procent, avrundad till heltal.
90
			 * Nollvektor vid otillräcklig statistik.
91
			 */
92 4
			$summa = array_sum($rad);
93 4
			$this->utfallshistorik[$index] = match (true) {
94 4
				$summa > 0 => [
95 4
					round(100 * $rad[0] / $summa, 0),
96 4
					round(100 * $rad[1] / $summa, 0),
97 4
					round(100 * $rad[2] / $summa, 0),
98 4
					array_sum($rad)
99 4
				],
100 4
				default => [0, 0, 0, 0]
101 4
			};
102
		}
103
104 4
		$historik = array_map(fn (array $utfall): array =>
105 4
			[$utfall[0], $utfall[1], $utfall[2]], $this->utfallshistorik);
106
107
		/**
108
		 * Sortera.
109
		 */
110 4
		$sortering = array_map(fn (array $sort): float => (float) ne_max($sort), $historik);
0 ignored issues
show
Bug introduced by
The function ne_max was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
		$sortering = array_map(fn (array $sort): float => (float) /** @scrutinizer ignore-call */ ne_max($sort), $historik);
Loading history...
111 4
		arsort($sortering);
112 4
		$this->ordnad_historik = array_flip(array_keys($sortering));
113
	}
114
}
115