Completed
Push — master ( 5c3586...68bdab )
by Tomáš
02:53
created

GameContainer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 78.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 118
ccs 26
cts 33
cp 0.7879
rs 10
c 1
b 0
f 0
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A incrementId() 0 11 5
A getFirstIncrement() 0 2 1
A addChild() 0 9 3
A getAutoIncrement() 0 2 1
A setAutoIncrement() 0 7 2
A resetAutoIncrement() 0 13 5
1
<?php
2
3
4
namespace TournamentGenerator\Containers;
5
6
use Exception;
7
use InvalidArgumentException;
8
9
/**
10
 * Class GameContainer
11
 *
12
 * Special container for games.
13
 *
14
 * @package TournamentGenerator\Containers
15
 * @author  Tomáš Vojík <[email protected]>
16
 * @since   0.4
17
 */
18
class GameContainer extends BaseContainer
19
{
20
21
	/** @var GameContainer[] Direct child containers */
22
	protected array $children = [];
23
	/** @var int First auto increment value for recalculating the ids */
24
	protected int $firstIncrement = 1;
25
	/** @var int Autoincrement for contained games */
26
	protected int $autoIncrement = 1;
27
	/** @var GameContainer|null Parent container reference */
28
	protected ?BaseContainer $parent;
29
30
	/**
31
	 * Increments the auto-incremented id
32
	 *
33
	 * @param GameContainer|null $sender
34
	 *
35
	 * @post  Propagates to all children but the sender
36
	 * @post  Propagates to the parent if not the sender
37
	 * @since 0.5
38
	 */
39 85
	public function incrementId(?GameContainer $sender = null) : void {
40
		// Increment
41 85
		$this->autoIncrement++;
42
		// Propagate to parent
43 85
		if (isset($this->parent) && $this->parent !== $sender) {
44 39
			$this->parent->incrementId($this);
0 ignored issues
show
Bug introduced by
The method incrementId() does not exist on TournamentGenerator\Containers\BaseContainer. It seems like you code against a sub-type of TournamentGenerator\Containers\BaseContainer such as TournamentGenerator\Containers\GameContainer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
			$this->parent->/** @scrutinizer ignore-call */ 
45
                  incrementId($this);
Loading history...
Bug introduced by
The method incrementId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
			$this->parent->/** @scrutinizer ignore-call */ 
45
                  incrementId($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
		}
46
		// Propagate to children
47 85
		foreach ($this->children as $child) {
48 39
			if ($child !== $sender) {
49 27
				$child->incrementId($this);
50
			}
51
		}
52 85
	}
53
54
	/**
55
	 * @return int
56
	 * @since 0.5
57
	 */
58 85
	public function getAutoIncrement() : int {
59 85
		return $this->autoIncrement;
60
	}
61
62
	/**
63
	 * Sets the autoincrement number for games
64
	 *
65
	 * @param int $autoIncrement
66
	 *
67
	 * @post  The value is propagated to child containers
68
	 * @post  The firstIncrement value is set too
69
	 *
70
	 * @return GameContainer
71
	 * @since 0.5
72
	 */
73 70
	public function setAutoIncrement(int $autoIncrement) : GameContainer {
74 70
		$this->autoIncrement = $autoIncrement;
75 70
		$this->firstIncrement = $autoIncrement;
76 70
		foreach ($this->children as $child) {
77
			$child->setAutoIncrement($autoIncrement);
78
		}
79 70
		return $this;
80
	}
81
82
	/**
83
	 * Resets the autoincrement number for games to the FirstIncrement
84
	 *
85
	 * @param GameContainer|null $sender
86
	 *
87
	 * @return GameContainer
88
	 * @post  Propagates to all children but the sender
89
	 * @post  Propagates to the parent if not the sender
90
	 *
91
	 * @since 0.5
92
	 */
93 4
	public function resetAutoIncrement(?GameContainer $sender = null) : GameContainer {
94 4
		$this->autoIncrement = $this->firstIncrement;
95
		// Propagate to parent
96 4
		if (isset($this->parent) && $this->parent !== $sender) {
97
			$this->parent->resetAutoIncrement($this);
0 ignored issues
show
Bug introduced by
The method resetAutoIncrement() does not exist on TournamentGenerator\Containers\BaseContainer. It seems like you code against a sub-type of TournamentGenerator\Containers\BaseContainer such as TournamentGenerator\Containers\GameContainer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
			$this->parent->/** @scrutinizer ignore-call */ 
98
                  resetAutoIncrement($this);
Loading history...
98
		}
99
		// Propagate to children
100 4
		foreach ($this->children as $child) {
101
			if ($child !== $sender) {
102
				$child->resetAutoIncrement($this);
103
			}
104
		}
105 4
		return $this;
106
	}
107
108
	/**
109
	 * @return int
110
	 * @since 0.5
111
	 */
112
	public function getFirstIncrement() : int {
113
		return $this->firstIncrement;
114
	}
115
116
117
	/**
118
	 * Adds a child container
119
	 *
120
	 * @param GameContainer[] $containers
121
	 *
122
	 * @return $this
123
	 * @post Parent container is set for the added children
124
	 * @post Autoincrement value is propagated to added child containers
125
	 * @throws Exception
126
	 */
127 70
	public function addChild(BaseContainer ...$containers) : BaseContainer {
128 70
		foreach ($containers as $container) {
129 70
			if (!$container instanceof self) {
130
				throw new InvalidArgumentException('GameContainer must contain only other GameContainers.');
131
			}
132 70
			$container->setAutoIncrement($this->autoIncrement);
133
		}
134 70
		parent::addChild(...$containers);
135 70
		return $this;
136
	}
137
138
}