FactoryTag   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 57
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 1
A validateString() 0 6 2
A validateArray() 0 6 2
A createTag() 0 7 1
A checkData() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Model\Tag;
6
7
class FactoryTag
8
{
9
    public static function create(array $data): Tag
10
    {
11
        $data = self::validateArray($data);
12
        $name = self::validateString(
13
            (string) self::checkData($data, 'name', '')
14
        );
15
        $trending = (bool) self::checkData($data, 'trending', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of One\FactoryTag::checkData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

15
        $trending = (bool) self::checkData($data, 'trending', /** @scrutinizer ignore-type */ false);
Loading history...
16
17
        return self::createTag(
18
            $name,
19
            $trending
20
        );
21
    }
22
23
    public static function createTag(
24
        string $name,
25
        bool $trending
26
    ): Tag {
27
        return new Tag(
28
            $name,
29
            $trending
30
        );
31
    }
32
33
    /**
34
     * functionality to check whether a variable is set or not.
35
     * @param mixed $key
36
     * @param string $default
37
     * @return mixed
38
     */
39
    private static function checkData(array $data, $key, $default = '')
40
    {
41
        return $data[$key] ?? $default;
42
    }
43
44
    /**
45
     * functionality validity for array variables
46
     */
47
    private static function validateArray(array $var): array
48
    {
49
        if (gettype($var) === 'array') {
50
            return $var;
51
        }
52
        throw new \Exception('The variable type must Array :');
53
    }
54
55
    /**
56
     * functionality validity for string variables
57
     */
58
    private static function validateString(String $var): String
59
    {
60
        if (gettype($var) === 'string') {
61
            return $var;
62
        }
63
        throw new \Exception('The variable type must String :' . $var);
64
    }
65
}
66
67