TTReduceradKod   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 23.53%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 94
ccs 8
cts 34
cp 0.2353
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A halvgarderingar() 0 20 6
A beräkna_reducerad_kod() 0 37 5
A helgarderingar() 0 10 2
1
<?php
2
3
/**
4
 * Klass TTReduceradKod.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\TT;
11
12
use Tips\Moduler\TT;
13
14
/**
15
 * Klass TTReduceradKod.
16
 * Reducera kod enligt reduktionskod.
17
 * R-system definieras i Koder.
18
 * Nyttjas av klasser Prova och Tackning.
19
 */
20
final class TTReduceradKod extends Reduktion {
21
	/**
22
	 * Initiera.
23
	 */
24 1
	public function __construct(protected TT $tt) {
25 1
		$this->beräkna_reducerad_kod();
26
	}
27
28
	/**
29
	 * Beräkna reducerad kod.
30
	 */
31 1
	private function beräkna_reducerad_kod(): void {
32 1
		$this->beräkna_garderingar();
33 1
		$antal_hg = count($this->halvgarderingar);
34
35
		/**
36
		 * Kräv överensstämmelse i garderingar med system.
37
		 */
38
		if (
39 1
			count($this->helgarderingar) != $this->tt->rkod->helgarderingar() ||
40 1
			$antal_hg != $this->tt->rkod->halvgarderingar()
41
		) {
42 1
			return;
43
		}
44
45
		/**
46
		 * Nollställ.
47
		 */
48
		$kod_halvgarderingar = [];
49
		$kod_helgarderingar = [];
50
		$nollrad = array_fill(0, $this->tt::TT_MATCHANTAL, 0);
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT::TT_MATCHANTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
51
52
		/**
53
		 * Reducera.
54
		 */
55
		foreach ($this->beräkna_reduktion() as $projektion) {
56
			foreach ($projektion as $index => $kodord) {
57
				$kod = array_map('intval', str_split($kodord));
0 ignored issues
show
Bug introduced by
It seems like str_split($kodord) can also be of type true; however, parameter $array of array_map() does only seem to accept 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

57
				$kod = array_map('intval', /** @scrutinizer ignore-type */ str_split($kodord));
Loading history...
58
				$temp = $nollrad;
59
60
				match ($index) {
61
					0 => $this->helgarderingar($kod, $temp, $kod_helgarderingar),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->helgarderingar($k...p, $kod_helgarderingar) targeting Tips\Moduler\TT\TTReduceradKod::helgarderingar() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
					default => $this->halvgarderingar($antal_hg, $kod, $temp, $kod_halvgarderingar)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->halvgarderingar($..., $kod_halvgarderingar) targeting Tips\Moduler\TT\TTReduceradKod::halvgarderingar() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
				};
64
			}
65
		}
66
67
		$this->addera_kodord($antal_hg, $kod_helgarderingar, $kod_halvgarderingar);
68
	}
69
70
	/**
71
	 * Helgarderingar.
72
	 * @param int[] $kod
73
	 * @param int[] $temp
74
	 * @param array<int, int[]> $kod_helgarderingar
75
	 */
76
	private function helgarderingar(array $kod, array &$temp, array &$kod_helgarderingar): void {
77
		/**
78
		 * Helgardering.
79
		 * $projektion håller helgarderingar i [0], halvgarderingar i [1]
80
		 */
81
		foreach ($kod as $nyckel => $tecken) {
82
			$temp[$this->helgarderingar[$nyckel]] = $tecken;
83
		}
84
85
		$kod_helgarderingar[] = $temp;
86
	}
87
88
	/**
89
	 * Halvgarderingar.
90
	 * @param int[] $kod
91
	 * @param int[] $temp
92
	 * @param array<int, int[]> $kod_halvgarderingar
93
	 */
94
	private function halvgarderingar(int $antal_hg, array $kod, array &$temp, array &$kod_halvgarderingar): void {
95
		/**
96
		 * Halvgardering.
97
		 */
98
		if ($antal_hg) {
99
			foreach ($kod as $nyckel => $tecken) {
100
				/**
101
				 * Skifta tecken efter odds.
102
				 * Mallar förutsätter 1X för halvgarderingar.
103
				 */
104
				match (true) {
105
					$this->tt->reduktion[$this->halvgarderingar[$nyckel]][0] === '' =>
106
						$temp[$this->halvgarderingar[$nyckel]] = $tecken === 0 ? 1 : 2, // skifta 1X -> X2
107
					$this->tt->reduktion[$this->halvgarderingar[$nyckel]][1] === '' =>
108
						$temp[$this->halvgarderingar[$nyckel]] = $tecken === 0 ? 0 : 2, // skifta 1X -> 12
109
					default =>
110
						$temp[$this->halvgarderingar[$nyckel]] = $tecken === 0 ? 0 : 1 // behåll 1X
111
				};
112
			}
113
			$kod_halvgarderingar[] = $temp;
114
		}
115
	}
116
}
117