Skip to content

Commit 5a7c887

Browse files
author
Sergey Kasatkin
authored
Merge pull request #32 from gietos/31-types-mismatch
Fix #31 Checks for nullable properties
2 parents 8fadeae + 47d022f commit 5a7c887

File tree

4 files changed

+151
-57
lines changed

4 files changed

+151
-57
lines changed

.styleci.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
preset: psr2
2+
3+
risky: false
4+
5+
enabled:
6+
- alpha_ordered_imports
7+
- binary_operator_spaces
8+
- blank_line_after_opening_tag
9+
- cast_spaces
10+
- concat_with_spaces
11+
- function_typehint_space
12+
- hash_to_slash_comment
13+
- include
14+
- lowercase_cast
15+
- method_separation
16+
- native_function_casing
17+
- no_blank_lines_after_class_opening
18+
- no_blank_lines_after_phpdoc
19+
- no_blank_lines_after_return
20+
- no_blank_lines_after_throw
21+
- no_blank_lines_between_imports
22+
- no_blank_lines_between_traits
23+
- no_empty_statement
24+
- no_extra_consecutive_blank_lines
25+
- no_spaces_inside_offset
26+
- no_spaces_outside_offset
27+
- no_trailing_comma_in_singleline_array
28+
- no_unused_imports
29+
- no_whitespace_before_comma_in_array
30+
- normalize_index_brace
31+
- object_operator_without_whitespace
32+
- return_type_declaration
33+
- short_array_syntax
34+
- short_scalar_cast
35+
- single_blank_line_before_namespace
36+
- single_quote
37+
- trailing_comma_in_multiline_array
38+
- unalign_double_arrow
39+
- unalign_equals
40+
- whitespace_after_comma_in_array

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
notifications:
2+
email: false
3+
4+
language: php
5+
6+
php:
7+
- 7.1
8+
9+
script: echo "skip"

src/Client.php

+68-54
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
use Dadata\Response\Name;
1010
use Dadata\Response\Passport;
1111
use Dadata\Response\Phone;
12-
use Dadata\Response\Vehicle;
1312
use Dadata\Response\Suggestions\Party;
14-
use Exception;
13+
use Dadata\Response\Vehicle;
1514
use GuzzleHttp\ClientInterface;
15+
use GuzzleHttp\Exception\GuzzleException;
1616
use GuzzleHttp\Psr7\Request;
17-
use InvalidArgumentException;
18-
use ReflectionClass;
19-
use ReflectionProperty;
20-
use RuntimeException;
2117

2218
/**
2319
* Class Client
@@ -28,19 +24,21 @@ class Client
2824
* Исходное значение распознано уверенно. Не требуется ручная проверка
2925
*/
3026
const QC_OK = 0;
27+
3128
/**
3229
* Исходное значение распознано с допущениями или не распознано. Требуется ручная проверка
3330
*/
3431
const QC_UNSURE = 1;
32+
3533
/**
3634
* Исходное значение пустое или заведомо "мусорное"
3735
*/
3836
const QC_INVALID = 2;
39-
37+
4038
const METHOD_GET = 'GET';
41-
39+
4240
const METHOD_POST = 'POST';
43-
41+
4442
/**
4543
* @var string
4644
*/
@@ -92,8 +90,9 @@ public function __construct(ClientInterface $httpClient, array $config = [])
9290
*
9391
* @return Address
9492
* @throws \ReflectionException
95-
* @throws RuntimeException
96-
* @throws InvalidArgumentException
93+
* @throws \RuntimeException
94+
* @throws \InvalidArgumentException
95+
* @throws GuzzleException
9796
*/
9897
public function cleanAddress($address)
9998
{
@@ -111,14 +110,15 @@ public function cleanAddress($address)
111110
*
112111
* @return Phone
113112
* @throws \ReflectionException
114-
* @throws RuntimeException
115-
* @throws InvalidArgumentException
113+
* @throws \RuntimeException
114+
* @throws \InvalidArgumentException
115+
* @throws GuzzleException
116116
*/
117117
public function cleanPhone($phone)
118118
{
119119
$response = $this->query($this->prepareUri('clean/phone'), [$phone]);
120120
/** @var Phone $result */
121-
$result = $this->populate(new Phone, $response);
121+
$result = $this->populate(new Phone(), $response);
122122

123123
return $result;
124124
}
@@ -130,8 +130,9 @@ public function cleanPhone($phone)
130130
*
131131
* @return Passport
132132
* @throws \ReflectionException
133-
* @throws RuntimeException
134-
* @throws InvalidArgumentException
133+
* @throws \RuntimeException
134+
* @throws \InvalidArgumentException
135+
* @throws GuzzleException
135136
*/
136137
public function cleanPassport($passport)
137138
{
@@ -149,8 +150,9 @@ public function cleanPassport($passport)
149150
*
150151
* @return Name
151152
* @throws \ReflectionException
152-
* @throws RuntimeException
153-
* @throws InvalidArgumentException
153+
* @throws \RuntimeException
154+
* @throws \InvalidArgumentException
155+
* @throws GuzzleException
154156
*/
155157
public function cleanName($name)
156158
{
@@ -168,14 +170,15 @@ public function cleanName($name)
168170
*
169171
* @return Email
170172
* @throws \ReflectionException
171-
* @throws RuntimeException
172-
* @throws InvalidArgumentException
173+
* @throws \RuntimeException
174+
* @throws \InvalidArgumentException
175+
* @throws GuzzleException
173176
*/
174177
public function cleanEmail($email)
175178
{
176179
$response = $this->query($this->prepareUri('clean/email'), [$email]);
177180
/** @var Email $result */
178-
$result = $this->populate(new Email, $response);
181+
$result = $this->populate(new Email(), $response);
179182

180183
return $result;
181184
}
@@ -187,14 +190,15 @@ public function cleanEmail($email)
187190
*
188191
* @return Date
189192
* @throws \ReflectionException
190-
* @throws RuntimeException
191-
* @throws InvalidArgumentException
193+
* @throws \RuntimeException
194+
* @throws \InvalidArgumentException
195+
* @throws GuzzleException
192196
*/
193197
public function cleanDate($date)
194198
{
195199
$response = $this->query($this->prepareUri('clean/birthdate'), [$date]);
196200
/** @var Date $result */
197-
$result = $this->populate(new Date, $response);
201+
$result = $this->populate(new Date(), $response);
198202

199203
return $result;
200204
}
@@ -206,14 +210,15 @@ public function cleanDate($date)
206210
*
207211
* @return Vehicle
208212
* @throws \ReflectionException
209-
* @throws RuntimeException
210-
* @throws InvalidArgumentException
213+
* @throws \RuntimeException
214+
* @throws \InvalidArgumentException
215+
* @throws GuzzleException
211216
*/
212217
public function cleanVehicle($vehicle)
213218
{
214219
$response = $this->query($this->prepareUri('clean/vehicle'), [$vehicle]);
215220
/** @var Vehicle $result */
216-
$result = $this->populate(new Vehicle, $response);
221+
$result = $this->populate(new Vehicle(), $response);
217222

218223
return $result;
219224
}
@@ -222,45 +227,47 @@ public function cleanVehicle($vehicle)
222227
* Gets balance.
223228
*
224229
* @return float
225-
* @throws RuntimeException
226-
* @throws InvalidArgumentException
230+
* @throws \RuntimeException
231+
* @throws \InvalidArgumentException
232+
* @throws GuzzleException
227233
*/
228234
public function getBalance()
229235
{
230236
$response = $this->query($this->prepareUri('profile/balance'), [], self::METHOD_GET);
231-
return (double) $response;
237+
return (float) $response;
232238
}
233239

234240
/**
235241
* Requests API.
236242
*
237243
* @param string $uri
238-
* @param array $params
244+
* @param array $params
239245
*
240246
* @param string $method
241247
*
242248
* @return array
243-
* @throws RuntimeException
244-
* @throws InvalidArgumentException
249+
* @throws \RuntimeException
250+
* @throws \InvalidArgumentException
251+
* @throws GuzzleException
245252
*/
246253
protected function query($uri, array $params = [], $method = self::METHOD_POST)
247254
{
248255
$request = new Request($method, $uri, [
249-
'Content-Type' => 'application/json',
256+
'Content-Type' => 'application/json',
250257
'Authorization' => 'Token ' . $this->token,
251-
'X-Secret' => $this->secret,
258+
'X-Secret' => $this->secret,
252259
], 0 < count($params) ? json_encode($params) : null);
253260

254261
$response = $this->httpClient->send($request, $this->httpOptions);
255262

256263
$result = json_decode($response->getBody(), true);
257264

258265
if (json_last_error() !== JSON_ERROR_NONE) {
259-
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
266+
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
260267
}
261268

262269
if (empty($result)) {
263-
throw new RuntimeException('Empty result');
270+
throw new \RuntimeException('Empty result');
264271
}
265272

266273
return array_shift($result);
@@ -288,9 +295,9 @@ protected function prepareUri($endpoint)
288295
*/
289296
protected function populate(AbstractResponse $object, array $data)
290297
{
291-
$reflect = new ReflectionClass($object);
298+
$reflect = new \ReflectionClass($object);
292299

293-
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
300+
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
294301

295302
foreach ($properties as $property) {
296303
if (array_key_exists($property->name, $data)) {
@@ -309,10 +316,14 @@ protected function populate(AbstractResponse $object, array $data)
309316
* @return Party\Party
310317
* @throws \ReflectionException
311318
*/
312-
protected function populateParty (array $response)
319+
protected function populateParty(array $response)
313320
{
314-
list($name, $post) = array_values($response['data']['management']);
315-
$management = new Party\ManagementDto($name, $post);
321+
$management = null;
322+
$managementData = $response['data']['management'];
323+
if (is_array($managementData) && array_key_exists('name', $managementData) && array_key_exists('post', $managementData)) {
324+
list($name, $post) = array_values($response['data']['management']);
325+
$management = new Party\ManagementDto($name, $post);
326+
}
316327

317328
list($code, $full, $short) = array_values($response['data']['opf']);
318329
$opf = new Party\OpfDto($code, $full, $short);
@@ -326,7 +337,10 @@ protected function populateParty (array $response)
326337
list($value, $unrestrictedValue) = array_values($response['data']['address']);
327338
$simpleAddress = new Party\AddressDto($value, $unrestrictedValue);
328339

329-
$address = $this->populate(new Address(), $response['data']['address']['data']);
340+
$address = null;
341+
if (is_array($response['data']['address']['data'])) {
342+
$address = $this->populate(new Address(), $response['data']['address']['data']);
343+
}
330344

331345
return new Party\Party(
332346
$response['value'],
@@ -350,11 +364,11 @@ protected function populateParty (array $response)
350364
/**
351365
* Guesses and converts property type by phpdoc comment.
352366
*
353-
* @param ReflectionProperty $property
367+
* @param \ReflectionProperty $property
354368
* @param mixed $value
355369
* @return mixed
356370
*/
357-
protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
371+
protected function getValueByAnnotatedType(\ReflectionProperty $property, $value)
358372
{
359373
$comment = $property->getDocComment();
360374
if (preg_match('/@var (.+?)(\|null)? /', $comment, $matches)) {
@@ -375,12 +389,13 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
375389
/**
376390
* @param string $ip
377391
* @return null|Address
378-
* @throws Exception
392+
* @throws \Exception
393+
* @throws GuzzleException
379394
*/
380395
public function detectAddressByIp($ip)
381396
{
382397
$request = new Request('get', $this->baseSuggestionsUrl . 'detectAddressByIp' . '?ip=' . $ip, [
383-
'Accept' => 'application/json',
398+
'Accept' => 'application/json',
384399
'Authorization' => 'Token ' . $this->token,
385400
]);
386401

@@ -389,19 +404,19 @@ public function detectAddressByIp($ip)
389404
$result = json_decode($response->getBody(), true);
390405

391406
if (json_last_error() !== JSON_ERROR_NONE) {
392-
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
407+
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
393408
}
394409

395410
if (!array_key_exists('location', $result)) {
396-
throw new Exception('Required key "location" is missing');
411+
throw new \Exception('Required key "location" is missing');
397412
}
398413

399414
if (null === $result['location']) {
400415
return null;
401416
}
402417

403418
if (!array_key_exists('data', $result['location'])) {
404-
throw new Exception('Required key "data" is missing');
419+
throw new \Exception('Required key "data" is missing');
405420
}
406421

407422
if (null === $result['location']['data']) {
@@ -427,6 +442,7 @@ public function detectAddressByIp($ip)
427442
* @throws \ReflectionException
428443
* @throws \RuntimeException
429444
* @throws \InvalidArgumentException
445+
* @throws GuzzleException
430446
*/
431447
public function getAddressById($addressId)
432448
{
@@ -450,10 +466,10 @@ public function getAddressById($addressId)
450466
* ФИО руководителя компании;
451467
* адресу до улицы.
452468
*
453-
* @param $party
469+
* @param string $party
454470
*
455471
* @return \SplObjectStorage
456-
* @throws \GuzzleHttp\Exception\GuzzleException
472+
* @throws GuzzleException
457473
* @throws \ReflectionException
458474
* @throws \InvalidArgumentException
459475
* @throws \RuntimeException
@@ -481,6 +497,4 @@ protected function prepareSuggestionsUri($endpoint)
481497
{
482498
return $this->baseSuggestionsUrl . $endpoint;
483499
}
484-
485-
486500
}

0 commit comments

Comments
 (0)