Completed
Pull Request — master (#18)
by Randy
01:46
created

JSendSuccess   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 9
dl 0
loc 66
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDefaultHttpStatusCode() 0 4 1
A asArray() 0 7 1
A decode() 0 6 1
A from() 0 7 1
1
<?php
2
3
namespace Demv\JSend;
4
5
use function Dgame\Ensurance\enforce;
6
use function Dgame\Extraction\export;
7
8
/**
9
 * Class JSendSuccess
10
 * @package Demv\JSend
11
 */
12
final class JSendSuccess implements JSendInterface
13
{
14
    use JSendStatusTrait;
15
    use JSendDataTrait;
16
    use JSendResponseTrait;
17
    use JSendJsonTrait;
18
    use ResponseDataTrait;
19
20
    /**
21
     * JSendSuccess constructor.
22
     *
23
     * @param array|null $data
24
     */
25
    public function __construct(array $data = null)
26
    {
27
        $this->status = Status::success();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Demv\JSend\Status::success() of type object<self> is incompatible with the declared type object<Demv\JSend\Status> of property $status.

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...
28
        $this->data   = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data can be null. However, the property $data is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
29
30
        $this->initResponseData();
31
    }
32
33
    /**
34
     * @return int
35
     */
36
    public function getDefaultHttpStatusCode(): int
37
    {
38
        return 200;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function asArray(): array
45
    {
46
        return [
47
            'status' => (string) $this->status,
48
            'data'   => $this->data
49
        ];
50
    }
51
52
    /**
53
     * @param string $json
54
     *
55
     * @return JSendSuccess
56
     * @throws InvalidJsonException
57
     */
58
    public static function decode(string $json): self
59
    {
60
        $decoded = JSend::safeDecode($json);
61
62
        return self::from($decoded);
63
    }
64
65
    /**
66
     * @param array $decoded
67
     *
68
     * @return JSendSuccess
69
     */
70
    public static function from(array $decoded): self
71
    {
72
        ['status' => $status, 'data' => $data] = export('status', 'data')->requireAll()->from($decoded);
0 ignored issues
show
Bug introduced by
The variable $status does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
        enforce(Status::from($status)->isSuccess())->orThrow('Decode non-success in JSendSuccess');
74
75
        return new self($data);
76
    }
77
}
78