Passed
Push — main ( a92fbe...d29be2 )
by N.
05:41 queued 01:24
created

Prediktera::prediktera()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 24
ccs 0
cts 19
cp 0
crap 12
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass Prediktera.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\FANN;
11
12
/**
13
 * Klass Prediktera.
14
 */
15
class Prediktera extends Preferenser {
16
	/**
17
	 * @var resource $fann
18
	 */
19
	public $fann;
20
21
	/**
22
	 * @var float[] $rådata
23
	 */
24
	public array $rådata = NOLLRAD;
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\FANN\NOLLRAD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
26
	 /**
27
	  * @var string[] $utdata
28
	  */
29
	public array $utdata = TOMRAD;
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\FANN\TOMRAD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
31
	protected bool $exists_fann = false; // huruvida anslutning till FANN existerar
32
	protected int $fann_rätt = 0;
33
	protected int $halvgarderingar = 0;
34
35
	/**
36
	 * Beräkna FANN-rad för aktuella sannolikheter.
37
	 */
38
	protected function prediktera(): void {
39
		if ($this->exists_fann) {
40
			foreach ($this->odds->sannolikheter as $index => $o_sannolikhet) {
41
				$s_sannolikhet = $this->streck->sannolikheter[$index];
42
				/**
43
				 * FANN-modellen består av en kombination av streck och odds.
44
				 */
45
				$res = fann_run(
46
					$this->fann,
47
					[$o_sannolikhet[0],
48
					$s_sannolikhet[0],
49
					$o_sannolikhet[1],
50
					$s_sannolikhet[1],
51
					$o_sannolikhet[2],
52
					$s_sannolikhet[2]]
53
				);
54
				$this->rådata[$index] = $res[0];
55
			}
56
57
			$this->utdata = $this->beräkna_utdata($this->rådata, $this->limiter);
58
			$this->halvgarderingar = array_sum(
59
				array_map(fn ($str): int => count(str_split($str)) - 1, $this->utdata)
0 ignored issues
show
Bug introduced by
It seems like str_split($str) can also be of type true; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

59
				array_map(fn ($str): int => count(/** @scrutinizer ignore-type */ str_split($str)) - 1, $this->utdata)
Loading history...
60
			);
61
			$this->fann_rätt = $this->fann_rätt($this->utdata, $this->utdelning->tipsrad_012);
62
		}
63
	}
64
65
	/**
66
	 * Beräkna utdata.
67
	 * @param float[] $rådata
68
	 * @param float[] $limiter
69
	 * @return string[]
70
	 */
71
	public function beräkna_utdata(array $rådata, array $limiter): array {
72
		$utdata = TOMRAD;
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\FANN\TOMRAD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
		$sort = $rådata;
74
		asort($sort);
75
		$sort = array_slice($sort, 0, 3, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $sort is dead and can be removed.
Loading history...
76
77
		/**
78
		 * Spik eller halvgardering bestäms av resultatet av FANN-körningen.
79
		 */
80
		foreach ($rådata as $index => $värde) {
81
			$utdata[$index] = match (true) {
82
				$värde < $limiter[0] => '0', // 1
83
				$värde < $limiter[1] => '01', // 1X
84
				$värde < $limiter[2] => '12', // X2
85
				default => '2', // 2
86
			};
87
		}
88
		return $utdata;
89
	}
90
91
	/**
92
	 * Beräkna antal rätt för FANN-rad.
93
	 * @param string[] $utdata
94
	 */
95
	protected function fann_rätt(array $utdata, string $tipsrad_012): int {
96
		return ($tipsrad_012 === '') ? 0 : antal_rätt($utdata, $tipsrad_012);
97
	}
98
99
	/**
100
	 * Beräkna antal rader som täcks av helgarderingar givna av FANN.
101
	 */
102
	protected function beräkna_rader(int $fann_min): int {
103
		for ($index = $fann_min, $antal_rader = 0; $index <= MATCHANTAL; $index++) {
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\FANN\MATCHANTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
			$antal_rader += UTFALL_PER_HALVGARDERINGAR[$this->halvgarderingar][$index];
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\FANN\UTFALL_PER_HALVGARDERINGAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
		}
106
		return $antal_rader;
107
	}
108
}
109