ResponseMessage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 29
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toArray() 0 10 2
A toJson() 0 4 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