Completed
Branch master (0e4a87)
by Tomáš
03:29 queued 01:30
created

Progression::progress()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 13
nc 36
nop 1
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Progression
9
{
10
11
	private $from;
12
	private $to;
13
	private $start = 0;
14
	private $len = null;
15
	private $filters = [];
16
17
	public function __construct(Group $from, Group $to, int $start = 0, int $len = null) {
18
		$this->from = $from;
19
		$this->to = $to;
20
		$this->start = $start;
21
		$this->len = $len;
22
	}
23
24
	public function __toString() {
25
		return 'Team from '.$this->from;
26
	}
27
28
	public function addFilter(...$filters) {
29
		foreach ($filters as $filter) {
30
			if (!$filter instanceof TeamFilter) throw new \Exception('Trying to add filter which is not an instance of TeamFilter.');
31
			$this->filters[] = $filter;
32
		}
33
		return $this;
34
	}
35
36
	public function progress(bool $blank = false) {
37
		if ($blank) $teams = $this->from->isPlayed() ? $this->from->sortTeams($this->filters) : $this->from->simulate($this->filters);
38
		else $teams = $this->from->sortTeams($this->filters);
39
40
		if (count($this->filters) === 0 || $this->len !== null || $this->start !== 0) $next = array_splice($teams, $this->start, ($this->len === null ? count($teams) : $this->len));
41
		else $next = $teams;
42
43
		$i = 1;
44
45
		foreach ($next as $team) {
46
			if ($blank) {
47
				$this->to->addTeam(new BlankTeam($this.' - '.$i, $team));
48
				$i++;
49
			}
50
			else $team->sumPoints += $this->from->progressPoints;
51
		}
52
53
		$this->from->addProgressed($next);
54
		if (!$blank) $this->to->addTeam($next);
55
		return $this;
56
	}
57
58
}
59