CharacterEffectsCollection::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Utils\Arrays;
7
8
/**
9
 * @author Jakub Konečný
10
 * @internal
11
 */
12
final class CharacterEffectsCollection extends \Nexendrie\Utils\Collection
13
{
14
    protected string $class = CharacterEffect::class;
15
16 1
    public function __construct(private readonly Character $character)
17
    {
18 1
        parent::__construct();
19 1
    }
20
21
    /**
22
     * @param int|NULL $index
23
     * @param CharacterEffect $item
24
     * @throws \OutOfRangeException
25
     * @throws \InvalidArgumentException
26
     * @throws \RuntimeException
27
     */
28
    public function offsetSet($index, $item): void
29
    {
30 1
        parent::offsetSet($index, $item);
31 1
        $item->onApply($this->character, $item);
32 1
    }
33
34
    /**
35
     * @param int $index
36
     * @throws \RuntimeException
37
     * @throws \OutOfRangeException
38
     */
39
    public function offsetUnset($index): void
40
    {
41
        try {
42
            /** @var CharacterEffect $item */
43 1
            $item = Arrays::get($this->items, $index);
44
        } catch (\Nette\InvalidArgumentException) {
45
            throw new \OutOfRangeException("Offset invalid or out of range.");
46
        }
47 1
        parent::offsetUnset($index);
48 1
        $item->onRemove($this->character, $item);
49 1
    }
50
}
51