FillFromArrayTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 22
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fill() 0 14 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase\Method\Traits;
6
7
use TgBotApi\BotApiBase\Exception\BadArgumentException;
8
9
/**
10
 * Trait FillFromArrayTrait.
11
 */
12
trait FillFromArrayTrait
13
{
14
    /**
15
     * @param array $data
16
     * @param array $forbidden
17
     *
18
     * @throws BadArgumentException
19
     */
20 61
    public function fill(array $data, array $forbidden = []): void
21
    {
22 61
        foreach ($forbidden as $item) {
23 17
            if (isset($data[$item])) {
24 1
                throw new BadArgumentException(
25 1
                    \sprintf('Argument %s is forbidden in %s constructor', $item, static::class)
26
                );
27
            }
28
        }
29 60
        foreach ($data as $key => $value) {
30 60
            if (!\property_exists($this, $key)) {
31 1
                throw new BadArgumentException(\sprintf('Argument %s not found in %s', $key, static::class));
32
            }
33 59
            $this->{$key} = $value;
34
        }
35 59
    }
36
}
37