Passed
Pull Request — master (#43)
by Yasin
02:31
created

FactoryArticle::createArticle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 11
dl 0
loc 14
rs 9.8666
c 0
b 0
f 0

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
2
namespace One;
3
4
use One\Model\Article;
5
6
/**
7
 * FactoryArticle Class
8
 *
9
 * @method create
10
 * @method createArticle
11
 * @method validateArray
12
 * @method validateUrl
13
 * @method validateInteger
14
 * @method validateString
15
 * @method checkData
16
 *
17
 */
18
class FactoryArticle
19
{
20
21
    /**
22
     * Create Article from array
23
     *
24
     * @param array $data
25
     * @return object Article
26
     */
27
    public static function create($data)
28
    {
29
        $data = self::validateArray($data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type integer expected by parameter $var of One\FactoryArticle::validateArray(). ( Ignorable by Annotation )

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

29
        $data = self::validateArray(/** @scrutinizer ignore-type */ $data);
Loading history...
30
        $title = self::validateString((string) self::checkData($data, 'title', ''));
31
        $body = self::validateString((string) self::checkData($data, 'body', ''));
32
        $source = self::validateUrl((string) self::checkData($data, 'source', ''));
33
        $uniqueId = self::validateString((string) self::checkData($data, 'unique_id', ''));
34
        $typeId = self::validateInteger((int) self::checkData($data, 'type_id', ''));
35
        $categoryId = self::validateInteger((int) self::checkData($data, 'category_id', ''));
36
        $reporter = self::validateString((string) self::checkData($data, 'reporter', ''));
37
        $lead = self::validateString((string) self::checkData($data, 'lead', ''));
38
        $tags = self::validateString((string) self::checkData($data, 'tags', ''));
39
        $publishedAt = self::validateString((string) self::checkData($data, 'published_at', ''));
40
        $identifier = self::validateInteger((int) self::checkData($data, 'identifier', null));
41
        return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier);
42
    }
43
44
    /**
45
     * functionality to check whether a variable is set or not.
46
     *
47
     * @param array $parts
48
     * @return array
49
     */
50
    private static function checkData($data, $key, $default = '')
51
    {
52
        return isset($data[$key]) ? $data[$key] : $default;
53
    }
54
55
    /**
56
     * Create Article Object
57
     *
58
     * @param String $title
59
     * @param string $body
60
     * @param string $source
61
     * @param string $uniqueId
62
     * @param int $typeId
63
     * @param int $categoryId
64
     * @param string $reporter
65
     * @param string $lead
66
     * @param string $tags
67
     * @param string $publishedAt
68
     * @param int $identifier
69
     * @return Article Object
70
     */
71
    public static function createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier)
72
    {
73
        return new Article(
74
            $title,
75
            $body,
76
            $source,
77
            $uniqueId,
78
            $typeId,
79
            $categoryId,
80
            $reporter,
81
            $lead,
82
            $tags,
83
            $publishedAt,
84
            $identifier
85
        );
86
    }
87
88
    /**
89
     * functionality validity for array variables
90
     *
91
     * @param int $var
92
     * @return int
93
     */
94
    private static function validateArray($var)
95
    {
96
        if (is_array($var)) {
0 ignored issues
show
introduced by
The condition is_array($var) is always false.
Loading history...
97
            return $var;
98
        }
99
        throw new \Exception("The variable type must Array :");
100
    }
101
102
    /**
103
     * Make Sure Url in string with correct url format
104
     *
105
     * @param String $string
106
     * @return string
107
     */
108
    private static function validateUrl($var)
109
    {
110
        if (filter_var($var, FILTER_VALIDATE_URL) === false) {
111
            throw new \Exception("Invalid url : $var");
112
        }
113
        return $var;
114
    }
115
116
    /**
117
     * functionality validity for int variables
118
     *
119
     * @param int $var
120
     * @return int
121
     */
122
    private static function validateInteger($var)
123
    {
124
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
125
            throw new \Exception("The variable type must Integer :" . $var);
126
        }
127
        return $var;
128
    }
129
130
    /**
131
     * functionality validity for string variables
132
     *
133
     * @param String $var
134
     * @return String
135
     */
136
    private static function validateString($var)
137
    {
138
        if (is_string($var) == true) {
0 ignored issues
show
introduced by
The condition is_string($var) == true is always true.
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
139
            return $var;
140
        }
141
        throw new \Exception("The variable type must String :" . $var);
142
    }
143
}
144