Passed
Push — main ( 78cf6a...332f67 )
by N.
04:14
created

HamtaSpikar::hämta_spikar()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 64
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.064

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
nc 14
nop 0
dl 0
loc 64
ccs 29
cts 33
cp 0.8788
crap 6.064
rs 8.8337
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Klass HamtaSpikar.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\TT;
11
12
use Tips\Moduler\TT\Konstanter;
13
14
/**
15
 * Klass HamtaSpikar.
16
 */
17
class HamtaSpikar extends HamtaData {
18
	use Konstanter;
19
20
	/**
21
	 * @var int[] $antal_spikar
22
	 */
23
	public array $antal_spikar;
24
	/**
25
	 * @var int[] $andel_spikar
26
	 */
27
	public array $andel_spikar;
28
	/**
29
	 * @var array<int, array<int, array<int, string>>> $spikar
30
	 */
31
	public array $spikar;
32
	/**
33
	 * @var array<int, string[]> $reduktion
34
	 */
35
	public array $reduktion;
36
37
	/**
38
	 * Hämta spikar.
39
	 */
40 20
	protected function hämta_spikar(): void {
41
		/**
42
		 * Definiera en tom struktur. Hämta preferens.
43
		 */
44 20
		$this->spikar = array_fill(
45 20
			0,
46 20
			self::TT_MAX_SPIKFÄLT,
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT\HamtaSpikar::TT_MAX_SPIKFÄLT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47 20
			array_fill(0, self::TT_MATCHANTAL, TOM_STRÄNGVEKTOR)
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT\HamtaSpikar::TT_MATCHANTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48 20
		);
49 20
		$spikar = $this->db_preferenser->hämta_preferens("topptips.spikar");
50
51
		/**
52
		 * Spara tom struktur om spikar inte redan sparade.
53
		 * Packa annars upp spikar i fält och ersätt tom struktur.
54
		 */
55
		match ($spikar) {
56 20
			'' => $this->db_preferenser->spara_preferens(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->db_preferenser->s...ay(), $this->spikar)))) targeting Tips\Klasser\DBPreferens...nOut::spara_preferens() 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...
57 20
				"topptips.spikar",
58 20
				implode(',', array_merge([], ...array_merge([], ...$this->spikar)))
59 20
			),
60 20
			default => $this->spikar = array_chunk(
61 20
				array_chunk(explode(',', $spikar), 3),
62 20
				self::TT_MATCHANTAL
63 20
			)
64
		};
65
66 20
		$this->antal_spikar = array_fill(0, self::TT_MAX_SPIKFÄLT, 0);
67 20
		$this->andel_spikar = $this->antal_spikar;
68 20
		$andel_spikar = $this->db_preferenser->hämta_preferens("topptips.andel_spikar");
69
70 20
		if ($andel_spikar !== '') {
71 20
			$this->andel_spikar = array_map('intval', explode(',', $andel_spikar));
72
		}
73
74 20
		foreach ($this->spikar as $index => $gardering) {
75 20
			foreach ($gardering as $match) {
76
				/**
77
				 * Halvgardering = en ruta tom.
78
				 */
79 20
				if (count(array_unique($match)) > 1) {
80 20
					$this->antal_spikar[$index]++;
81
				}
82
			}
83
84
			/**
85
			 * Villor för garderingar.
86
			 */
87 20
			if ($this->andel_spikar[$index] > $this->antal_spikar[$index]) {
88
				$this->antal_spikar[$index] = $this->andel_spikar[$index];
89
			}
90
		}
91
92 20
		$this->reduktion = array_fill(0, self::TT_MATCHANTAL, TOM_STRÄNGVEKTOR);
93 20
		$reduktion = $this->db_preferenser->hämta_preferens("topptips.reduktion");
94
95
		/**
96
		 * Finns reduktion i preferenser är allt väl.
97
		 * Annars sparas en tom datastruktur.
98
		 */
99 20
		match ($reduktion !== '') {
100 20
			true => $this->reduktion = array_chunk(explode(',', $reduktion), 3),
101
			false => $this->db_preferenser->spara_preferens(
102
				"topptips.reduktion",
103
				implode(',', array_merge([], ...$this->reduktion))
104
			)
105 20
		};
106
	}
107
}
108