Passed
Push — main ( f1b4ad...6ccac6 )
by N.
04:46
created

GridMatcher::eka_tabell()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 1
nop 9
dl 0
loc 47
ccs 24
cts 24
cp 1
crap 4
rs 9.536
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Klass GridMatcher.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser\Omgang;
11
12
use Tips\Klasser\Tips;
13
use Tips\Egenskaper\Eka;
14
15 1
define("ODDSSTIL", array_fill(0, 3, ' class="oddskolumn odefinierad"'));
16 1
define("STRECKSTIL", array_fill(0, 3, ' class="streckkolumn odefinierad"'));
17
18
/**
19
 * Klass GridMatcher.
20
 * Visa matchtabell med lag, odds, streck med mera.
21
	 * Visar:
22
	 * - matchnummer
23
	 * - fördelning av spelade tecken per match
24
	 * - matchstatus
25
	 * - match (lag)
26
	 * - resultat
27
	 * - odds
28
	 * - odddsannolikheter
29
	 * - streck
30
	 * - streckodds
31
 */
32
final class GridMatcher extends GridMatcherFordelning {
33
	use Eka;
34
35
	/**
36
	 * Visa matcher.
37
	 */
38 1
	public function visa(): string {
39
		/**
40
		 * Initiera.
41
		 */
42 1
		$rätt = ['odds' => 0, 'streck' => 0];
43
44
		/**
45
		 * Definiera tabindex för smidig manuell inmatning.
46
		 * Tabulator vandrar över kolumner med matcher, odds och streck.
47
		 */
48 1
		$tix = ['m' => range(100, 99 + MATCHANTAL),
49 1
				'o' => range(200, 199 + 3 * MATCHANTAL),
50 1
				's' => range(300, 299 + 3 * MATCHANTAL)];
51
52
		/**
53
		 * Nollställ stilar.
54
		 */
55 1
		$fördelning = TOM_ODDSMATRIS;
56
57
		/**
58
		 * Beräkna fördelning av tecken i respektive match.
59
		 */
60 1
		$this->fördelning($fördelning);
61
62
		/**
63
		 * Gråskala för fördelning.
64
		 * Vitt motsvarar 0, svart 1.
65
		 */
66 1
		$teckenfördelning = array_map(fn (array $odds): array =>
67 1
			array_map(fn (float $värde): string => stil($värde), $odds), $fördelning);
68
69
		/**
70
		 * Formatera sannolikheter med två decimaler.
71
		 * Fördelning av spelade tecken, odds samt streck.
72
		 */
73 1
		$formaterad_dist = formatera_sannolikheter($fördelning);
74 1
		$odds = formatera_sannolikheter($this->tips->odds->prediktioner);
75 1
		$streckodds = formatera_sannolikheter(sannolikheter_till_odds($this->tips->streck->sannolikheter));
76
77
		/**
78
		 * Iterera över matcher.
79
		 */
80 1
		$grid_matcher = '';
81 1
		foreach (array_keys($this->tips->odds->sannolikheter) as $index) {
82
			/**
83
			 * Nollställ stil.
84
			 */
85 1
			[$oddsstil, $streckstil] = [ODDSSTIL, STRECKSTIL];
86 1
			$this->stila_sannolikheter($index, $oddsstil, $streckstil, $rätt);
87
88
			/**
89
			 * Gulmarkera om odds saknas för match.
90
			 * Streckodds kan överföras till odds för att kunna generera rader.
91
			 */
92 1
			$odds_finns = $this->tips->odds->prediktioner[$index][0] == 0 ? ' förlust' : '';
93
94
			/**
95
			 * Bygg tabellrad.
96
			 * 1: Matchnummer
97
			 * 2–4: Fördelning av tecken för spelade matcher
98
			 * 5–7: Matchstatus, lag, favoritskap
99
			 * 8–9: Resultat, tipstecken
100
			 * 10–12: Odds
101
			 * 13–15: Decimalodds
102
			 * 16–18: Streck
103
			 * 19–21: Sannolikheter
104
			 * 22–24: Streckodds
105
			 * 25: JS streckodds => odds då odds saknas.
106
			 */
107 1
			$grid_matcher .= $this->eka_tabell(
108 1
				$index,
109 1
				$odds_finns,
110 1
				$teckenfördelning,
111 1
				$formaterad_dist,
112 1
				$odds,
113 1
				$streckodds,
114 1
				$oddsstil,
115 1
				$streckstil,
116 1
				$tix
117 1
			);
118
		}
119
120 1
		return $this->matchtabell($rätt, $grid_matcher);
121
	}
122
123
	/**
124
	 * Eka matcher
125
	 * @param array<int, string[]> $teckenfördelning
126
	 * @param array<int, string[]> $formaterad_dist
127
	 * @param array<int, string[]> $odds
128
	 * @param array<int, string[]> $streckodds
129
	 * @param string[] $oddsstil
130
	 * @param string[] $streckstil
131
	 * @param array<string, int[]> $tix
132
	 */
133 1
	private function eka_tabell(
134
		int $index,
135
		string $odds_finns,
136
		array $teckenfördelning,
137
		array $formaterad_dist,
138
		array $odds,
139
		array $streckodds,
140
		array $oddsstil,
141
		array $streckstil,
142
		array $tix
143
	): string {
144
		/**
145
		 * Bygg tabellrad.
146
		 * 1: Matchnummer
147
		 * 2–4: Fördelning av tecken för spelade matcher
148
		 * 5–7: Matchstatus, lag, favoritskap
149
		 * 8–9: Resultat, tipstecken
150
		 * 10–12: Odds
151
		 * 13–15: Decimalodds
152
		 * 16–18: Streck
153
		 * 19–21: Sannolikheter
154
		 * 22–24: Streckodds
155
		 * 25: JS streckodds => odds då odds saknas.
156
		 */
157 1
		return <<< EOT
158 1
								<tr>
159 1
									<td class="match höger">{$this->eka(strval($index + 1))}</td>
160 1
									<td{$teckenfördelning[$index][0]}>{$formaterad_dist[$index][0]}</td>
161 1
									<td{$teckenfördelning[$index][1]}>{$formaterad_dist[$index][1]}</td>
162 1
									<td{$teckenfördelning[$index][2]}>{$formaterad_dist[$index][2]}</td>
163 1
									<td><input type="hidden" name="matchstatus[]" value="{$this->tips->matcher->matchstatus[$index]}">{$this->eka($this->tips->matcher->matchstatus[$index] ? '✔️' : '✖️')}</td>
164 1
									<td{$this->tips->odds->sorteringsstil[$index]} class="mindre"><input{$this->tips->odds->sorteringsstil[$index]} class="vänster" tabindex="{$tix['m'][$index]}" type="text" name="lag[]" size="25" value="{$this->tips->matcher->match[$index]}"></td>
165 1
									<td{$this->tips->odds->sorteringsstil[$index]}>{$this->tips->odds->sortering[$index]}</td>
166 1
									<td class="mindre"><input type="text" name="resultat[]" size="3" value="{$this->tips->matcher->resultat[$index]}"></td>
167 1
									<td class="vinstrad">{$this->eka($this->tips->utdelning->har_tipsrad ? $this->tips->utdelning->tipsrad[$index] : '')}</td>
168 1
									<td class="input$odds_finns o0"><input{$oddsstil[0]} tabindex="{$tix['o'][3 * $index]}" type="text" name="odds[$index][]" size="2" maxlength="5" value="{$odds[$index][0]}"></td>
169 1
									<td class="input$odds_finns o1"><input{$oddsstil[1]} tabindex="{$tix['o'][3 * $index + 1]}" type="text" name="odds[$index][]" size="2" maxlength="5" value="{$odds[$index][1]}"></td>
170 1
									<td class="input$odds_finns o2"><input{$oddsstil[2]} tabindex="{$tix['o'][3 * $index + 2]}" type="text" name="odds[$index][]" size="2" maxlength="5" value="{$odds[$index][2]}"></td>
171 1
									{$this->tips->odds->stil_sannolikheter[$index][0]}{$this->tips->odds->stil_sannolikheter[$index][1]}{$this->tips->odds->stil_sannolikheter[$index][2]}
172 1
									<td class="input"><input{$streckstil[0]} tabindex="{$tix['s'][3 * $index]}" type="text" name="streck[$index][]" size="2" maxlength="2" value="{$this->tips->streck->prediktioner[$index][0]}"></td>
173 1
									<td class="input"><input{$streckstil[1]} tabindex="{$tix['s'][3 * $index + 1]}" type="text" name="streck[$index][]" size="2" maxlength="2" value="{$this->tips->streck->prediktioner[$index][1]}"></td>
174 1
									<td class="input"><input{$streckstil[2]} tabindex="{$tix['s'][3 * $index + 2]}" type="text" name="streck[$index][]" size="2" maxlength="2" value="{$this->tips->streck->prediktioner[$index][2]}"></td>
175 1
									{$this->tips->streck->stil_sannolikheter[$index][0]}{$this->tips->streck->stil_sannolikheter[$index][1]}{$this->tips->streck->stil_sannolikheter[$index][2]}
176 1
									<td class="s0 oddskolumn">{$streckodds[$index][0]}</td>
177 1
									<td class="s1 oddskolumn">{$streckodds[$index][1]}</td>
178 1
									<td class="s2 oddskolumn">{$streckodds[$index][2]}</td>
179 1
									<td class="återvinn">{$this->eka($odds[$index][0] === '0.00' ? '♻️' : '')}</td>
180
								</tr>
181
182 1
EOT;
183
	}
184
}
185