FactoryLivereport::createLivereport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 15
dl 0
loc 33
rs 9.7333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Model\Livereport;
6
7
class FactoryLivereport 
8
{
9
    public static function create(array $data): \One\Model\Livereport
10
    {
11
        $data = self::validateArray($data);
12
        $uniqueId = self::validateString(
13
            (string) self::checkData($data, 'unique_id', '')
14
        );
15
        $title = self::validateString(
16
            (string) self::checkData($data, 'title', '')
17
        );
18
        $shortDesc = self::validateString(
19
            (string) self::checkData($data, 'short_desc', '')
20
        );
21
        $publishDate = self::validateString(
22
            (string) self::checkData($data, 'publish_date', '')
23
        );
24
        $endDate = self::validateString(
25
            (string) self::checkData($data, 'end_date', '')
26
        );
27
        $tag = self::validateString(
28
            (string) self::checkData($data, 'tag', '')
29
        );
30
        $isHeadline = (bool) self::checkData($data, 'is_headline', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of One\FactoryLivereport::checkData(). ( Ignorable by Annotation )

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

30
        $isHeadline = (bool) self::checkData($data, 'is_headline', /** @scrutinizer ignore-type */ false);
Loading history...
31
        $published = (bool) self::checkData($data, 'published', false);
32
        $livereportChild = self::validateString(
33
            (string) self::checkData($data, 'livereport_child', '')
34
        );
35
        $imageThumbnail = self::validateString(
36
            (string) self::checkData($data, 'image_thumbnail', '')
37
        );
38
        $reporterName = self::validateString(
39
            (string) self::checkData($data, 'reporter_name', '')
40
        );
41
        $reporterAvatar = self::validateString(
42
            (string) self::checkData($data, 'reporter_avatar', '')
43
        );
44
        $editorName = self::validateString(
45
            (string) self::checkData($data, 'editor_name', '')
46
        );
47
        $editorAvatar = self::validateString(
48
            (string) self::checkData($data, 'editor_avatar', '')
49
        );
50
        $identifier = self::validateInteger(
51
            (int) self::checkData($data, 'identifier', null)
52
        );
53
54
        return self::createLivereport(
55
            $uniqueId,
56
            $title,
57
            $shortDesc,
58
            $publishDate,
59
            $endDate,
60
            $tag,
61
            $isHeadline,
62
            $published,
63
            $livereportChild,
64
            $imageThumbnail,
65
            $reporterName,
66
            $reporterAvatar,
67
            $editorName,
68
            $editorAvatar,
69
            $identifier
70
        );
71
    }
72
73
    public static function createLivereport(
74
        string $uniqueId,
75
        string $title,
76
        string $shortDesc,
77
        string $publishDate,
78
        string $endDate,
79
        string $tag,
80
        bool $isHeadline,
81
        bool $published,
82
        string $livereportChild,
83
        string $imageThumbnail,
84
        string $reporterName,
85
        string $reporterAvatar,
86
        string $editorName,
87
        string $editorAvatar,
88
        int $identifier
89
    ): Livereport {
90
        return new Livereport(
91
            $uniqueId,
92
            $title,
93
            $shortDesc,
94
            $publishDate,
95
            $endDate,
96
            $tag,
97
            $isHeadline,
98
            $published,
99
            $livereportChild,
100
            $imageThumbnail,
101
            $reporterName,
102
            $reporterAvatar,
103
            $editorName,
104
            $editorAvatar,
105
            $identifier
106
        );
107
    }
108
109
    /**
110
     * functionality to check whether a variable is set or not.
111
     * @param mixed $key
112
     * @param string $default
113
     * @return mixed
114
     */
115
    private static function checkData(array $data, $key, $default = '')
116
    {
117
        return $data[$key] ?? $default;
118
    }
119
120
    /**
121
     * functionality validity for array variables
122
     */
123
    private static function validateArray(array $var): array
124
    {
125
        if (gettype($var) === 'array') {
126
            return $var;
127
        }
128
        throw new \Exception('The variable type must Array :');
129
    }
130
131
    /**
132
     * functionality validity for string variables
133
     */
134
    private static function validateString(String $var): String
135
    {
136
        if (gettype($var) === 'string') {
137
            return $var;
138
        }
139
        throw new \Exception('The variable type must String :' . $var);
140
    }
141
142
    /**
143
     * functionality validity for int variables
144
     */
145
    private static function validateInteger(int $var): int
146
    {
147
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
148
            throw new \Exception('The variable type must Integer :' . $var);
149
        }
150
        return $var;
151
    }
152
}
153
154