Passed
Branch master (6d737b)
by Laurens
01:46
created

AbstractFactory::extractOrNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\__shared;
6
7
use DateTimeImmutable;
8
9
abstract class AbstractFactory
10
{
11 10
    protected function extractString(string $key, array $data): ?string
12
    {
13 10
        return $data[$key];
14
    }
15
16 4
    protected function extractStringOrNull(string $key, array $data): ?string
17
    {
18 4
        return $this->extractOrNull($key, $data);
19
    }
20
21 2
    protected function extractInteger(string $key, array $data): int
22
    {
23 2
        return (int)$data[$key];
24
    }
25
26 2
    protected function extractFloat(string $key, array $data): float
27
    {
28 2
        return (float)$data[$key];
29
    }
30
31 10
    protected function extractBoolean(string $key, array $data): bool
32
    {
33 10
        return (bool)$this->extractOrNull($key, $data);
34
    }
35
36 6
    protected function extractArrayOrNull(string $key, array $data): ?array
37
    {
38 6
        return $this->extractOrNull($key, $data);
39
    }
40
41 4
    protected function extractDateTimeImmutableOrNull(string $key, array $data): ?DateTimeImmutable
42
    {
43 4
        $dateTime = $this->extractStringOrNull($key, $data);
44
45 4
        if ($dateTime) {
46 2
            return new DateTimeImmutable($dateTime);
47
        }
48
49 3
        return null;
50
    }
51
52 10
    private function extractOrNull(string $key, array $data)
53
    {
54 10
        if (array_key_exists($key, $data)) {
55 10
            return $data[$key];
56
        }
57
58 7
        return null;
59
    }
60
}
61