1 | <?php |
||||
2 | /** |
||||
3 | * ActiveRecord for API |
||||
4 | * |
||||
5 | * @link https://github.com/hiqdev/yii2-hiart |
||||
6 | * @package yii2-hiart |
||||
7 | * @license BSD-3-Clause |
||||
8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||||
9 | */ |
||||
10 | |||||
11 | namespace hiqdev\hiart; |
||||
12 | |||||
13 | use yii\helpers\Json; |
||||
14 | |||||
15 | abstract class AbstractResponse implements ResponseInterface |
||||
16 | { |
||||
17 | /** |
||||
18 | * @var RequestInterface |
||||
19 | */ |
||||
20 | protected $request; |
||||
21 | |||||
22 | /** |
||||
23 | * @var string response data. The property contains RAW response data |
||||
24 | * @see decodeData() |
||||
25 | * @see isDecoded |
||||
26 | */ |
||||
27 | protected $data; |
||||
28 | |||||
29 | /** |
||||
30 | * @var bool whether response is already decoded |
||||
31 | */ |
||||
32 | protected $isDecoded = false; |
||||
33 | |||||
34 | public function getRequest() |
||||
35 | { |
||||
36 | return $this->request; |
||||
37 | } |
||||
38 | |||||
39 | 2 | public function getData() |
|||
40 | { |
||||
41 | 2 | if (!$this->isDecoded) { |
|||
42 | 2 | $this->data = $this->decodeData(); |
|||
0 ignored issues
–
show
|
|||||
43 | 2 | $this->isDecoded = true; |
|||
44 | } |
||||
45 | |||||
46 | 2 | return $this->data; |
|||
47 | } |
||||
48 | |||||
49 | 2 | public function decodeData() |
|||
50 | { |
||||
51 | 2 | $data = $this->getRawData(); |
|||
52 | |||||
53 | 2 | if ($this->isRaw()) { |
|||
54 | return $data; |
||||
55 | } |
||||
56 | |||||
57 | 2 | if ($this->isJson()) { |
|||
58 | 2 | return $this->decodeJson($data); |
|||
59 | } |
||||
60 | |||||
61 | // TODO: implement decoding for XML and other types |
||||
62 | |||||
63 | // throw new ResponseDecodingException('Failed to detect response data type', $this); |
||||
64 | // TODO: throw exception instead of returning |
||||
65 | return $data; |
||||
66 | } |
||||
67 | |||||
68 | /** |
||||
69 | * Method returns RAW request data. |
||||
70 | * |
||||
71 | * @return string |
||||
72 | */ |
||||
73 | abstract public function getRawData(); |
||||
74 | |||||
75 | /** |
||||
76 | * Whether the request is RAW and should not be decoded. |
||||
77 | * @return bool |
||||
78 | */ |
||||
79 | 2 | public function isRaw() |
|||
80 | { |
||||
81 | 2 | return $this->request->isRaw(); |
|||
0 ignored issues
–
show
The method
isRaw() does not exist on hiqdev\hiart\RequestInterface . Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\hiart\RequestInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * Method checks whether response is a JSON response. |
||||
86 | * @return bool |
||||
87 | */ |
||||
88 | 2 | public function isJson() |
|||
89 | { |
||||
90 | 2 | $value = $this->getHeader('Content-Type'); |
|||
91 | 2 | if ($value === null) { |
|||
0 ignored issues
–
show
|
|||||
92 | return false; |
||||
93 | } |
||||
94 | |||||
95 | 2 | return !empty(preg_grep('|application/json|i', $value)); |
|||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * @param $name |
||||
100 | * @return array |
||||
101 | */ |
||||
102 | abstract public function getHeader($name); |
||||
103 | |||||
104 | /** |
||||
105 | * @param string $data JSON data |
||||
106 | * @return array |
||||
107 | */ |
||||
108 | 2 | protected function decodeJson($data) |
|||
109 | { |
||||
110 | 2 | return Json::decode($data); |
|||
111 | } |
||||
112 | } |
||||
113 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.