Passed
Push — master ( e46fda...6a2c9a )
by KwangSeob
02:20
created

IssueField::addLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Issue;
4
5
use JiraRestApi\ClassSerialize;
6
7
class IssueField implements \JsonSerializable
8
{
9
    use ClassSerialize;
10
11
    /** @var string */
12
    public $summary;
13
14
    /** @var array */
15
    public $progress;
16
17
    /** @var TimeTracking */
18
    public $timeTracking;
19
20
    /** @var IssueType */
21
    public $issuetype;
22
23
    /** @var Reporter */
24
    public $reporter;
25
26
    /** @var \DateTime */
27
    public $created;
28
29
    /** @var \DateTime */
30
    public $updated;
31
32
    /** @var string|null */
33
    public $description;
34
35
    /** @var Priority|null */
36
    public $priority;
37
38
    /** @var IssueStatus */
39
    public $status;
40
41
    /** @var array */
42
    public $labels;
43
44
    /** @var \JiraRestApi\Project\Project */
45
    public $project;
46
47
    /** @var string|null */
48
    public $environment;
49
50
    /** @var \JiraRestApi\Issue\Component[] */
51
    public $components;
52
53
    /** @var Comments */
54
    public $comment;
55
56
    /** @var object */
57
    public $votes;
58
59
    /** @var object|null */
60
    public $resolution;
61
62
    /** @var array */
63
    public $fixVersions;
64
65
    /** @var Reporter|null */
66
    public $creator;
67
68
    /** @var object|null */
69
    public $watches;
70
71
    /** @var object|null */
72
    public $worklog;
73
74
    /** @var Reporter|null */
75
    public $assignee;
76
77
    /** @var \JiraRestApi\Issue\Version[] */
78
    public $versions;
79
80
    /** @var \JiraRestApi\Issue\Attachment[] */
81
    public $attachment;
82
83
    /** @var string|null */
84
    public $aggregatetimespent;
85
86
    /** @var string|null */
87
    public $timeestimate;
88
89
    /** @var string|null */
90
    public $aggregatetimeoriginalestimate;
91
92
    /** @var string|null */
93
    public $resolutiondate;
94
95
    /** @var \DateTime|null */
96
    public $duedate;
97
98
    /** @var array */
99
    public $issuelinks;
100
101
    /** @var Issue[] */
102
    public $subtasks;
103
104
    /** @var int */
105
    public $workratio;
106
107
    /** @var object|null */
108
    public $aggregatetimeestimate;
109
110
    /** @var object|null */
111
    public $aggregateprogress;
112
113
    /** @var object|null */
114
    public $lastViewed;
115
116
    /** @var object|null */
117
    public $timeoriginalestimate;
118
119
    /** @var object|null */
120
    public $parent;
121
122
    /** @var array|null */
123
    public $customFields;
124
125
    /** @var SecurityScheme|null */
126
    public $security;
127
128
    public function __construct($updateIssue = false)
129
    {
130
        if ($updateIssue != true) {
131
            $this->project = new \JiraRestApi\Project\Project();
132
133
            $this->assignee = new Reporter();
134
            // priority maybe empty.
135
            //$this->priority = new Priority();
136
137
            $this->issuetype = new IssueType();
138
        }
139
    }
140
141
    public function jsonSerialize()
142
    {
143
        $vars = array_filter(get_object_vars($this), function ($var) {
144
            return !is_null($var);
145
        });
146
147
        // if assignee property has empty value then remove it.
148
        // @see https://github.com/lesstif/php-jira-rest-client/issues/126
149
        if (empty($this->assignee) || $this->assignee->isEmpty()) {
150
            unset($vars['assignee']);
151
        }
152
153
        // clear undefined json property
154
        unset($vars['customFields']);
155
156
        // repackaging custom field
157
        if (!empty($this->customFields)) {
158
            foreach ($this->customFields as $key => $value) {
159
                $vars[$key] = $value;
160
            }
161
        }
162
163
        return $vars;
164
    }
165
166
    public function getCustomFields()
167
    {
168
        return $this->customFields;
169
    }
170
171
    public function addCustomField($key, $value)
172
    {
173
        $this->customFields[$key] = $value;
174
175
        return $this;
176
    }
177
178
    public function getProjectKey()
179
    {
180
        return $this->project->key;
181
    }
182
183
    public function getProjectId()
184
    {
185
        return $this->project->id;
186
    }
187
188
    public function setProjectKey($key)
189
    {
190
        $this->project->key = $key;
191
192
        return $this;
193
    }
194
195
    public function setProjectId($id)
196
    {
197
        $this->project->id = $id;
198
199
        return $this;
200
    }
201
202
    public function setSummary($summary)
203
    {
204
        $this->summary = $summary;
205
206
        return $this;
207
    }
208
209
    /**
210
     * set issue reporter name.
211
     *
212
     * @param string $name
213
     *
214
     * @return $this
215
     */
216
    public function setReporterName($name)
217
    {
218
        if (is_null($this->reporter)) {
219
            $this->reporter = new Reporter();
220
        }
221
222
        $this->reporter->name = $name;
223
224
        return $this;
225
    }
226
227
    /**
228
     * set issue assignee name.
229
     *
230
     * @param string $name
231
     *
232
     * @return $this
233
     */
234
    public function setAssigneeName($name)
235
    {
236
        if (is_null($this->assignee)) {
237
            $this->assignee = new Reporter();
238
        }
239
240
        $this->assignee->name = $name;
241
242
        return $this;
243
    }
244
245
    /**
246
     * set issue priority name.
247
     *
248
     * @param string $name
249
     *
250
     * @return $this
251
     */
252
    public function setPriorityName($name)
253
    {
254
        if (is_null($this->priority)) {
255
            $this->priority = new Priority();
256
        }
257
258
        $this->priority->name = $name;
259
260
        return $this;
261
    }
262
263
    /**
264
     * @param string|null $description
265
     *
266
     * @return $this
267
     */
268
    public function setDescription($description)
269
    {
270
        if (!empty($description)) {
271
            $this->description = $description;
272
        }
273
274
        return $this;
275
    }
276
277
    /**
278
     * add a Affects version.
279
     *
280
     * @param mixed string or array $name
281
     *
282
     * @return $this
283
     */
284
    public function addVersion($name)
285
    {
286
        if (is_null($this->versions)) {
0 ignored issues
show
introduced by
The condition is_null($this->versions) is always false.
Loading history...
287
            $this->versions = [];
288
        }
289
290
        if (is_string($name)) {
291
            array_push($this->versions, new Version($name));
292
        } elseif (is_array($name)) {
293
            foreach ($name as $v) {
294
                array_push($this->versions, new Version($v));
295
            }
296
        }
297
298
        return $this;
299
    }
300
301
    /**
302
     * add issue label.
303
     *
304
     * @param string $label
305
     *
306
     * @return $this
307
     */
308
    public function addLabel($label)
309
    {
310
        if (is_null($this->labels)) {
0 ignored issues
show
introduced by
The condition is_null($this->labels) is always false.
Loading history...
311
            $this->labels = [];
312
        }
313
314
        array_push($this->labels, $label);
315
316
        return $this;
317
    }
318
319
    /**
320
     * set issue type.
321
     *
322
     * @param mixed IssueType or string $name
323
     *
324
     * @return $this
325
     */
326
    public function setIssueType($name)
327
    {
328
        if (is_string($name)) {
329
            if (is_null($this->issuetype)) {
330
                $this->issuetype = new IssueType();
331
            }
332
333
            $this->issuetype->name = $name;
334
        } else {
335
            $this->issuetype = $name;
336
        }
337
338
        return $this;
339
    }
340
341
    public function getIssueType()
342
    {
343
        return $this->issuetype;
344
    }
345
346
    /**
347
     *  set parent issue.
348
     *
349
     * @param string $keyOrId
350
     */
351
    public function setParentKeyOrId($keyOrId)
352
    {
353
        if (is_numeric($keyOrId)) {
354
            $this->parent['id'] = $keyOrId;
355
        } elseif (is_string($keyOrId)) {
356
            $this->parent['key'] = $keyOrId;
357
        }
358
    }
359
360
    public function setParent(Issue $parent)
361
    {
362
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent of type JiraRestApi\Issue\Issue is incompatible with the declared type object|null of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
363
    }
364
365
    /**
366
     * add issue component.
367
     *
368
     * @param mixed string or array $component
369
     *
370
     * @return $this
371
     */
372
    public function addComponents($component)
373
    {
374
        if (is_null($this->components)) {
0 ignored issues
show
introduced by
The condition is_null($this->components) is always false.
Loading history...
375
            $this->components = [];
376
        }
377
378
        if (is_string($component)) {
379
            array_push($this->components, new Component($component));
380
        } elseif (is_array($component)) {
381
            foreach ($component as $c) {
382
                array_push($this->components, new Component($c));
383
            }
384
        }
385
386
        return $this;
387
    }
388
389
    /**
390
     * set security level.
391
     *
392
     * @param int $id issue's security id
393
     *
394
     * @return $this
395
     */
396
    public function setSecurityId($id)
397
    {
398
        if (is_empty($this->security)) {
0 ignored issues
show
Bug introduced by
The function is_empty was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

398
        if (/** @scrutinizer ignore-call */ is_empty($this->security)) {
Loading history...
399
            $this->security = new SecurityScheme();
400
        }
401
402
        $this->security->id = $id;
403
404
        return $this;
405
    }
406
}
407