FillFromArrayTrait::fill()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 7
nop 2
crap 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