Passed
Push — main ( e5ef3a...f1ef56 )
by N.
04:15
created

ReduceradKod::beräkna_reducerad_kod()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 0
dl 0
loc 33
ccs 15
cts 15
cp 1
crap 5
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass ReduceradKod.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\System;
11
12
/**
13
 * Klass ReduceradKod.
14
 * Reducera kod enligt reduktionskod.
15
 * R-system definieras i Koder.
16
 */
17
class ReduceradKod extends Reduktion {
18
	/**
19
	 * Beräkna reducerad kod.
20
	 */
21 19
	protected function beräkna_reducerad_kod(): void {
22 19
		$this->beräkna_garderingar();
23 19
		$antal_hg = count($this->halvgarderingar);
24
25
		/**
26
		 * Kräv överensstämmelse i garderingar med system.
27
		 */
28
		if (
29 19
			count($this->helgarderingar) != $this->kod->helgarderingar() ||
30 19
			$antal_hg != $this->kod->halvgarderingar()
31
		) {
32 12
			return;
33
		}
34
35 7
		$kod_halvgarderingar = [];
36 7
		$kod_helgarderingar = [];
37
38
		/**
39
		 * Reducera.
40
		 */
41 7
		foreach ($this->beräkna_reduktion() as $projektion) {
42 7
			foreach ($projektion as $index => $kodord) {
43 7
				$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

43
				$kod = array_map('intval', /** @scrutinizer ignore-type */ str_split($kodord));
Loading history...
44 7
				$temp = NOLLRAD;
45
46
				match ($index) {
47 7
					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\System\ReduceradKod::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...
48 7
					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\System\Redu...dKod::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...
49
				};
50
			}
51
		}
52
53 7
		$this->addera_kodord($antal_hg, $kod_helgarderingar, $kod_halvgarderingar);
54
	}
55
56
	/**
57
	 * Helgarderingar.
58
	 * @param int[] $kod
59
	 * @param int[] $temp
60
	 * @param array<int, int[]> $kod_helgarderingar
61
	 */
62 7
	private function helgarderingar(array $kod, array &$temp, array &$kod_helgarderingar): void {
63
		/**
64
		 * Helgardering.
65
		 * $projektion håller helgarderingar i [0], halvgarderingar i [1]
66
		 */
67 7
		foreach ($kod as $nyckel => $tecken) {
68 7
			$temp[$this->helgarderingar[$nyckel]] = $tecken;
69
		}
70
71 7
		$kod_helgarderingar[] = $temp;
72
	}
73
74
	/**
75
	 * Halvgarderingar.
76
	 * @param int[] $kod
77
	 * @param int[] $temp
78
	 * @param array<int, int[]> $kod_halvgarderingar
79
	 */
80 7
	private function halvgarderingar(int $antal_hg, array $kod, array &$temp, array &$kod_halvgarderingar): void {
81
		/**
82
		 * Halvgardering.
83
		 */
84 7
		if ($antal_hg) {
85 7
			foreach ($kod as $nyckel => $tecken) {
86
				/**
87
				 * Skifta tecken efter odds.
88
				 * Mallar förutsätter 1X för halvgarderingar.
89
				 */
90 7
				match (true) {
91 7
					$this->reduktion[$this->halvgarderingar[$nyckel]][0] === '' =>
92 7
						$temp[$this->halvgarderingar[$nyckel]] = ($tecken === 0) ? 1 : 2, // skifta 1X -> X2
93 7
					$this->reduktion[$this->halvgarderingar[$nyckel]][1] === '' =>
94
						$temp[$this->halvgarderingar[$nyckel]] = ($tecken === 0) ? 0 : 2, // skifta 1X -> 12
95 7
					default =>
96 7
						$temp[$this->halvgarderingar[$nyckel]] = ($tecken === 0) ? 0 : 1 // behåll 1X
97 7
				};
98
			}
99 7
			$kod_halvgarderingar[] = $temp;
100
		}
101
	}
102
}
103