Test Setup Failed
Pull Request — master (#2)
by Craig
02:55
created

Response::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 17
rs 9.7998
1
<?php
2
3
namespace Phpsa\LaravelYourlsPlugin;
4
5
class Response {
6
7
     /**
8
     * status of the server response - fail is not nessisary a fail in this case
9
     *
10
     * @var string
11
     */
12
    public $statusCode;
13
    /**
14
     * status code of the server response
15
     *
16
     * @var int
17
     */
18
    public $statusCode;
19
    /**
20
     * message from the response
21
     *
22
     * @var string
23
     * @author Craig Smith <[email protected]>
24
     */
25
    public $message;
26
27
28
    public function __construct($body, $format){
29
        switch ($format) {
30
            case 'xml':
31
                libxml_use_internal_errors(true);
32
                $res = simplexml_load_string((string) $body);
33
                if ($res) {
34
                    $res = json_decode(json_encode($res));
35
                }
36
                break;
37
            case 'json':
38
                $res = json_decode((string) $body);
39
                break;
40
            default:
41
                $res = (string) $body;
42
            break;
43
        }
44
        $this->parseResponseBody($res);
45
    }
46
47
    protected function parseResponseBody($body){
48
        if(empty($body)){
49
                throw new \Exception("No response recieved");
50
        }
51
52
        if(\is_string($body) ){
53
            $this->status = 'success';
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
            $this->statusCode = '200';
0 ignored issues
show
Documentation Bug introduced by
The property $statusCode was declared of type integer, but '200' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
55
            $this->shorturl = $url;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
Bug Best Practice introduced by
The property shorturl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
            return;
57
        }
58
59
        foreach((array)$body as $key => $value){
60
            $this->{$key} = $value;
61
        }
62
63
    }
64
65
}
66