Completed
Push — master ( a743c4...8e8354 )
by Tomáš
02:35
created

GameContainer::incrementId()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 6
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
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 178
	public function incrementId(?GameContainer $sender = null) : void {
40
		// Increment
41 178
		$this->autoIncrement++;
42
		// Propagate to parent
43 178
		if (isset($this->parent) && $this->parent !== $sender) {
44 110
			$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 178
		foreach ($this->children as $child) {
48 110
			if ($child !== $sender) {
49 97
				$child->incrementId($this);
50
			}
51
		}
52 178
	}
53
54
	/**
55
	 * @return int
56
	 * @since 0.5
57
	 */
58 178
	public function getAutoIncrement() : int {
59 178
		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 162
	public function setAutoIncrement(int $autoIncrement) : GameContainer {
74 162
		$this->autoIncrement = $autoIncrement;
75 162
		$this->firstIncrement = $autoIncrement;
76 162
		foreach ($this->children as $child) {
77 1
			$child->setAutoIncrement($autoIncrement);
78
		}
79 162
		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 5
	public function resetAutoIncrement(?GameContainer $sender = null) : GameContainer {
94 5
		$this->autoIncrement = $this->firstIncrement;
95
		// Propagate to parent
96 5
		if (isset($this->parent) && $this->parent !== $sender) {
97 1
			$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 5
		foreach ($this->children as $child) {
101 1
			if ($child !== $sender) {
102 1
				$child->resetAutoIncrement($this);
103
			}
104
		}
105 5
		return $this;
106
	}
107
108
	/**
109
	 * @return int
110
	 * @since 0.5
111
	 */
112 1
	public function getFirstIncrement() : int {
113 1
		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 144
	public function addChild(BaseContainer ...$containers) : BaseContainer {
128 144
		foreach ($containers as $container) {
129 144
			if (!$container instanceof self) {
130 1
				throw new InvalidArgumentException('GameContainer must contain only other GameContainers.');
131
			}
132 143
			$container->setAutoIncrement($this->autoIncrement);
133
		}
134 143
		parent::addChild(...$containers);
135 143
		return $this;
136
	}
137
138
}