1 | <?php |
||
36 | abstract class AbstractAPI |
||
37 | { |
||
38 | /** |
||
39 | * Http instance. |
||
40 | * |
||
41 | * @var \EasyWeChat\Core\Http |
||
42 | */ |
||
43 | protected $http; |
||
44 | |||
45 | /** |
||
46 | * The request token. |
||
47 | * |
||
48 | * @var \EasyWeChat\Core\AccessToken |
||
49 | */ |
||
50 | protected $accessToken; |
||
51 | |||
52 | const GET = 'get'; |
||
53 | const POST = 'post'; |
||
54 | const JSON = 'json'; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected static $maxRetries = 2; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param \EasyWeChat\Core\AccessToken $accessToken |
||
65 | */ |
||
66 | 208 | public function __construct(AccessToken $accessToken) |
|
67 | { |
||
68 | 208 | $this->setAccessToken($accessToken); |
|
69 | 208 | } |
|
70 | |||
71 | /** |
||
72 | * Return the http instance. |
||
73 | * |
||
74 | * @return \EasyWeChat\Core\Http |
||
75 | */ |
||
76 | 9 | public function getHttp() |
|
77 | { |
||
78 | 9 | if (is_null($this->http)) { |
|
79 | 1 | $this->http = new Http(); |
|
80 | 1 | } |
|
81 | |||
82 | 9 | if (count($this->http->getMiddlewares()) === 0) { |
|
83 | 8 | $this->registerHttpMiddlewares(); |
|
84 | 8 | } |
|
85 | |||
86 | 9 | return $this->http; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Set the http instance. |
||
91 | * |
||
92 | * @param \EasyWeChat\Core\Http $http |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | 13 | public function setHttp(Http $http) |
|
97 | { |
||
98 | 13 | $this->http = $http; |
|
99 | |||
100 | 13 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Return the current accessToken. |
||
105 | * |
||
106 | * @return \EasyWeChat\Core\AccessToken |
||
107 | */ |
||
108 | 20 | public function getAccessToken() |
|
109 | { |
||
110 | 20 | return $this->accessToken; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set the request token. |
||
115 | * |
||
116 | * @param \EasyWeChat\Core\AccessToken $accessToken |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | 208 | public function setAccessToken(AccessToken $accessToken) |
|
121 | { |
||
122 | 208 | $this->accessToken = $accessToken; |
|
123 | |||
124 | 208 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param int $retries |
||
129 | */ |
||
130 | 8 | public static function maxRetries($retries) |
|
131 | { |
||
132 | 8 | self::$maxRetries = abs($retries); |
|
|
|||
133 | 8 | } |
|
134 | |||
135 | /** |
||
136 | * Parse JSON from response and check error. |
||
137 | * |
||
138 | * @param string $method |
||
139 | * @param array $args |
||
140 | * |
||
141 | * @return \EasyWeChat\Support\Collection |
||
142 | */ |
||
143 | 7 | public function parseJSON($method, array $args) |
|
153 | |||
154 | /** |
||
155 | * Register Guzzle middlewares. |
||
156 | */ |
||
157 | 8 | protected function registerHttpMiddlewares() |
|
158 | { |
||
159 | // log |
||
166 | |||
167 | /** |
||
168 | * Attache access token to request query. |
||
169 | * |
||
170 | * @return \Closure |
||
171 | */ |
||
172 | 8 | protected function accessTokenMiddleware() |
|
189 | |||
190 | /** |
||
191 | * Log the request. |
||
192 | * |
||
193 | * @return \Closure |
||
194 | */ |
||
195 | 8 | protected function logMiddleware() |
|
202 | |||
203 | /** |
||
204 | * Return retry middleware. |
||
205 | * |
||
206 | * @return \Closure |
||
207 | */ |
||
208 | protected function retryMiddleware() |
||
234 | |||
235 | /** |
||
236 | * Check the array data errors, and Throw exception when the contents contains error. |
||
237 | * |
||
238 | * @param array $contents |
||
239 | * |
||
240 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
241 | */ |
||
242 | 8 | protected function checkAndThrow(array $contents) |
|
252 | } |
||
253 |
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.