ResponseMessage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Controlabs\Http;
4
5
class ResponseMessage
6
{
7
    private $code;
8
    private $message;
9
    private $data = [];
10
11 3
    public function __construct(int $code, ?string $message, array $data = [])
12
    {
13 3
        $this->code = $code;
14 3
        $this->message = $message;
15 3
        $this->data = $data;
16 3
    }
17
18 2
    public function toArray()
19
    {
20 2
        $error = [];
21 2
        $error['error'] = [
22 2
            'code' => $this->code,
23 2
            'message' => $this->message,
24
        ];
25 2
        ($this->data) && $error['error']['errors'] = $this->data;
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26 2
        return $error;
27
    }
28
29 1
    public function toJson()
30
    {
31 1
        return json_encode($this->toArray());
32
    }
33
}
34