Error::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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 Javis\JsonApi\Schema;
3
4
class Error
5
{
6
    /**
7
     * @var string
8
     */
9
    protected $id;
10
11
    /**
12
     * @var array
13
     */
14
    protected $meta;
15
16
    /**
17
     * @var \Javis\JsonApi\Schema\Links
18
     */
19
    protected $links;
20
21
    /**
22
     * @var int
23
     */
24
    protected $status;
25
26
    /**
27
     * @var string
28
     */
29
    protected $code;
30
31
    /**
32
     * @var string
33
     */
34
    protected $title;
35
36
    /**
37
     * @var string
38
     */
39
    protected $detail;
40
41
    /**
42
     * @var \Javis\JsonApi\Schema\ErrorSource
43
     */
44
    protected $source;
45
46
    /**
47
     * @param array $error
48
     * @return $this
49
     */
50
    public static function createFromArray(array $error)
51
    {
52
        $id = empty($error["id"]) === false ? $error["id"] : "";
53
        $meta = isset($error["meta"]) && is_array($error["meta"]) ? $error["meta"] : [];
54
        $links = Links::createFromArray(isset($error["links"]) && is_array($error["links"]) ? $error["links"] : []);
55
        $status = (int)(empty($error["status"]) === false ? $error["status"] : "");
56
        $code = empty($error["code"]) === false ? $error["code"] : "";
57
        $title = empty($error["title"]) === false ? $error["title"] : "";
58
        $detail = empty($error["detail"]) === false ? $error["detail"] : "";
59
        $source = ErrorSource::fromArray(isset($error["source"]) && is_array($error["source"]) ? $error["source"] : []);
60
61
        return new self($id, $meta, $links, $status, $code, $title, $detail, $source);
62
    }
63
64
    /**
65
     * @param string $id
66
     * @param array $meta
67
     * @param \Javis\JsonApi\Schema\Links $links
68
     * @param int $status
69
     * @param string $code
70
     * @param string $title
71
     * @param string $detail
72
     * @param \Javis\JsonApi\Schema\ErrorSource $source
73
     */
74
    public function __construct($id, array $meta, Links $links, $status, $code, $title, $detail, ErrorSource $source)
75
    {
76
        $this->id = $id;
77
        $this->meta = $meta;
78
        $this->links = $links;
79
        $this->status = $status;
80
        $this->code = $code;
81
        $this->title = $title;
82
        $this->detail = $detail;
83
        $this->source = $source;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function toArray()
90
    {
91
        $content = [];
92
93
        if ($this->id) {
94
            $content["id"] = $this->id;
95
        }
96
97
        if (empty($this->meta) === false) {
98
            $content["meta"] = $this->meta;
99
        }
100
101
        if ($this->hasLinks()) {
102
            $content["links"] = $this->links->toArray();
103
        }
104
105
        if ($this->status) {
106
            $content["status"] = $this->status;
107
        }
108
109
        if ($this->code) {
110
            $content["code"] = $this->code;
111
        }
112
113
        if ($this->title) {
114
            $content["title"] = $this->title;
115
        }
116
117
        if ($this->detail) {
118
            $content["detail"] = $this->detail;
119
        }
120
121
        if ($this->hasSource()) {
122
            $content["source"] = $this->source->toArray();
123
        }
124
125
        return $content;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function id()
132
    {
133
        return $this->id;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function hasMeta()
140
    {
141
        return empty($this->meta) === false;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function meta()
148
    {
149
        return $this->meta;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function hasLinks()
156
    {
157
        return $this->links->hasLinks();
158
    }
159
160
    /**
161
     * @return \Javis\JsonApi\Schema\Links
162
     */
163
    public function links()
164
    {
165
        return $this->links;
166
    }
167
168
    /**
169
     * @return int
170
     */
171
    public function status()
172
    {
173
        return $this->status;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function code()
180
    {
181
        return $this->code;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function title()
188
    {
189
        return $this->title;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function detail()
196
    {
197
        return $this->detail;
198
    }
199
200
    /**
201
     * @return bool
202
     */
203
    public function hasSource()
204
    {
205
        return $this->source->hasSource();
206
    }
207
208
    /**
209
     * @return \Javis\JsonApi\Schema\ErrorSource
210
     */
211
    public function source()
212
    {
213
        return $this->source;
214
    }
215
}
216