FactoryArticle::createArticle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
eloc 15
c 3
b 1
f 1
nc 1
nop 14
dl 0
loc 31
rs 9.7666

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\Article;
6
7
/**
8
 * FactoryArticle Class
9
 *
10
 * @method create
11
 * @method createArticle
12
 * @method validateArray
13
 * @method validateUrl
14
 * @method validateInteger
15
 * @method validateString
16
 * @method checkData
17
 */
18
class FactoryArticle
19
{
20
    /**
21
     * Create article
22
     */
23
    public static function create(array $data): \One\Model\Article
24
    {
25
        $data = self::validateArray($data);
26
        $title = self::validateString(
27
            (string) self::checkData($data, 'title', '')
28
        );
29
        $body = self::validateString(
30
            (string) self::checkData($data, 'body', '')
31
        );
32
        $source = self::validateUrl(
33
            (string) self::checkData($data, 'source', '')
34
        );
35
        $uniqueId = self::validateString(
36
            (string) self::checkData($data, 'unique_id', '')
37
        );
38
        $typeId = self::validateInteger(
39
            (int) self::checkData($data, 'type_id', '')
40
        );
41
        $categoryId = self::validateInteger(
42
            (int) self::checkData($data, 'category_id', '')
43
        );
44
        $reporter = self::validateString(
45
            (string) self::checkData($data, 'reporter', '')
46
        );
47
        $lead = self::validateString(
48
            (string) self::checkData($data, 'lead', '')
49
        );
50
        $tags = self::validateString(
51
            (string) self::checkData($data, 'tags', '')
52
        );
53
        $publishedAt = self::validateString(
54
            (string) self::checkData($data, 'published_at', '')
55
        );
56
        $headline = (bool) self::checkData($data, '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\FactoryArticle::checkData(). ( Ignorable by Annotation )

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

56
        $headline = (bool) self::checkData($data, 'headline', /** @scrutinizer ignore-type */ false);
Loading history...
57
        $headlineLip6 = (bool) self::checkData($data, 'headline_lip6', false);
58
        $aiType = self::validateInteger(
59
            (int) self::checkData($data, 'ai_type', 0)
60
        );
61
        $identifier = self::validateInteger(
62
            (int) self::checkData($data, 'identifier', null)
63
        );
64
        return self::createArticle(
65
            $title,
66
            $body,
67
            $source,
68
            $uniqueId,
69
            $typeId,
70
            $categoryId,
71
            $reporter,
72
            $lead,
73
            $tags,
74
            $publishedAt,
75
            $headline,
76
            $headlineLip6,
77
            $aiType,
78
            $identifier
79
        );
80
    }
81
82
    /**
83
     * Create Article Object
84
     *
85
     * @return Article Object
86
     */
87
    public static function createArticle(
88
        string $title,
89
        string $body,
90
        string $source,
91
        string $uniqueId,
92
        int $typeId,
93
        int $categoryId,
94
        string $reporter,
95
        string $lead,
96
        string $tags,
97
        string $publishedAt,
98
        bool $headline,
99
        bool $headlineLip6,
100
        int $aiType,
101
        int $identifier
102
    ): Article {
103
        return new Article(
104
            $title,
105
            $body,
106
            $source,
107
            $uniqueId,
108
            $typeId,
109
            $categoryId,
110
            $reporter,
111
            $lead,
112
            $tags,
113
            $publishedAt,
114
            $identifier,
115
            $headline,
116
            $headlineLip6,
117
            $aiType
0 ignored issues
show
Bug introduced by
$aiType of type integer is incompatible with the type boolean expected by parameter $seo of One\Model\Article::__construct(). ( Ignorable by Annotation )

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

117
            /** @scrutinizer ignore-type */ $aiType
Loading history...
118
        );
119
    }
120
121
    /**
122
     * functionality to check whether a variable is set or not.
123
     * @param mixed $key
124
     * @param string $default
125
     * @return mixed
126
     */
127
    private static function checkData(array $data, $key, $default = '')
128
    {
129
        return $data[$key] ?? $default;
130
    }
131
132
    /**
133
     * functionality validity for array variables
134
     */
135
    private static function validateArray(array $var): array
136
    {
137
        if (gettype($var) === 'array') {
138
            return $var;
139
        }
140
        throw new \Exception('The variable type must Array :');
141
    }
142
143
    /**
144
     * Make Sure Url in string with correct url format
145
     */
146
    private static function validateUrl(string $var): string
147
    {
148
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
149
            throw new \Exception("Invalid url : ${var}");
150
        }
151
        return $var;
152
    }
153
154
    /**
155
     * functionality validity for int variables
156
     */
157
    private static function validateInteger(int $var): int
158
    {
159
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
160
            throw new \Exception('The variable type must Integer :' . $var);
161
        }
162
        return $var;
163
    }
164
165
    /**
166
     * functionality validity for string variables
167
     */
168
    private static function validateString(String $var): String
169
    {
170
        if (gettype($var) === 'string') {
171
            return $var;
172
        }
173
        throw new \Exception('The variable type must String :' . $var);
174
    }
175
}
176