Passed
Push — master ( 1ba277...ff87af )
by
unknown
03:35
created

FactoryLivereport::createLivereport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 23
rs 9.9

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
        $identifier = self::validateInteger(
0 ignored issues
show
Bug introduced by
The method validateInteger() does not exist on One\FactoryLivereport. Did you maybe mean validateString()? ( Ignorable by Annotation )

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

35
        /** @scrutinizer ignore-call */ 
36
        $identifier = self::validateInteger(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
            (int) self::checkData($data, 'identifier', null)
37
        );
38
39
        return self::createLivereport(
40
            $uniqueId,
41
            $title,
42
            $shortDesc,
43
            $publishDate,
44
            $endDate,
45
            $tag,
46
            $isHeadline,
47
            $published,
48
            $livereportChild,
49
            $identifier
50
        );
51
    }
52
53
    public static function createLivereport(
54
        string $uniqueId,
55
        string $title,
56
        string $shortDesc,
57
        string $publishDate,
58
        string $endDate,
59
        string $tag,
60
        bool $isHeadline,
61
        bool $published,
62
        string $livereportChild,
63
        int $identifier
64
    ): Livereport {
65
        return new Livereport(
66
            $uniqueId,
67
            $title,
68
            $shortDesc,
69
            $publishDate,
70
            $endDate,
71
            $tag,
72
            $isHeadline,
73
            $published,
74
            $livereportChild,
75
            $identifier
76
        );
77
    }
78
79
    /**
80
     * functionality to check whether a variable is set or not.
81
     * @param mixed $key
82
     * @param string $default
83
     * @return mixed
84
     */
85
    private static function checkData(array $data, $key, $default = '')
86
    {
87
        return $data[$key] ?? $default;
88
    }
89
90
    /**
91
     * functionality validity for array variables
92
     */
93
    private static function validateArray(array $var): array
94
    {
95
        if (gettype($var) === 'array') {
96
            return $var;
97
        }
98
        throw new \Exception('The variable type must Array :');
99
    }
100
101
    /**
102
     * functionality validity for string variables
103
     */
104
    private static function validateString(String $var): String
105
    {
106
        if (gettype($var) === 'string') {
107
            return $var;
108
        }
109
        throw new \Exception('The variable type must String :' . $var);
110
    }
111
112
}
113
114