Skip to content

Commit

Permalink
Merge pull request #32 from gietos/31-types-mismatch
Browse files Browse the repository at this point in the history
Fix #31 Checks for nullable properties
  • Loading branch information
Sergey Kasatkin authored May 9, 2018
2 parents 8fadeae + 47d022f commit 5a7c887
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 57 deletions.
40 changes: 40 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
preset: psr2

risky: false

enabled:
- alpha_ordered_imports
- binary_operator_spaces
- blank_line_after_opening_tag
- cast_spaces
- concat_with_spaces
- function_typehint_space
- hash_to_slash_comment
- include
- lowercase_cast
- method_separation
- native_function_casing
- no_blank_lines_after_class_opening
- no_blank_lines_after_phpdoc
- no_blank_lines_after_return
- no_blank_lines_after_throw
- no_blank_lines_between_imports
- no_blank_lines_between_traits
- no_empty_statement
- no_extra_consecutive_blank_lines
- no_spaces_inside_offset
- no_spaces_outside_offset
- no_trailing_comma_in_singleline_array
- no_unused_imports
- no_whitespace_before_comma_in_array
- normalize_index_brace
- object_operator_without_whitespace
- return_type_declaration
- short_array_syntax
- short_scalar_cast
- single_blank_line_before_namespace
- single_quote
- trailing_comma_in_multiline_array
- unalign_double_arrow
- unalign_equals
- whitespace_after_comma_in_array
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
notifications:
email: false

language: php

php:
- 7.1

script: echo "skip"
122 changes: 68 additions & 54 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
use Dadata\Response\Name;
use Dadata\Response\Passport;
use Dadata\Response\Phone;
use Dadata\Response\Vehicle;
use Dadata\Response\Suggestions\Party;
use Exception;
use Dadata\Response\Vehicle;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionProperty;
use RuntimeException;

/**
* Class Client
Expand All @@ -28,19 +24,21 @@ class Client
* Исходное значение распознано уверенно. Не требуется ручная проверка
*/
const QC_OK = 0;

/**
* Исходное значение распознано с допущениями или не распознано. Требуется ручная проверка
*/
const QC_UNSURE = 1;

/**
* Исходное значение пустое или заведомо "мусорное"
*/
const QC_INVALID = 2;

const METHOD_GET = 'GET';

const METHOD_POST = 'POST';

/**
* @var string
*/
Expand Down Expand Up @@ -92,8 +90,9 @@ public function __construct(ClientInterface $httpClient, array $config = [])
*
* @return Address
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanAddress($address)
{
Expand All @@ -111,14 +110,15 @@ public function cleanAddress($address)
*
* @return Phone
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanPhone($phone)
{
$response = $this->query($this->prepareUri('clean/phone'), [$phone]);
/** @var Phone $result */
$result = $this->populate(new Phone, $response);
$result = $this->populate(new Phone(), $response);

return $result;
}
Expand All @@ -130,8 +130,9 @@ public function cleanPhone($phone)
*
* @return Passport
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanPassport($passport)
{
Expand All @@ -149,8 +150,9 @@ public function cleanPassport($passport)
*
* @return Name
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanName($name)
{
Expand All @@ -168,14 +170,15 @@ public function cleanName($name)
*
* @return Email
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanEmail($email)
{
$response = $this->query($this->prepareUri('clean/email'), [$email]);
/** @var Email $result */
$result = $this->populate(new Email, $response);
$result = $this->populate(new Email(), $response);

return $result;
}
Expand All @@ -187,14 +190,15 @@ public function cleanEmail($email)
*
* @return Date
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanDate($date)
{
$response = $this->query($this->prepareUri('clean/birthdate'), [$date]);
/** @var Date $result */
$result = $this->populate(new Date, $response);
$result = $this->populate(new Date(), $response);

return $result;
}
Expand All @@ -206,14 +210,15 @@ public function cleanDate($date)
*
* @return Vehicle
* @throws \ReflectionException
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function cleanVehicle($vehicle)
{
$response = $this->query($this->prepareUri('clean/vehicle'), [$vehicle]);
/** @var Vehicle $result */
$result = $this->populate(new Vehicle, $response);
$result = $this->populate(new Vehicle(), $response);

return $result;
}
Expand All @@ -222,45 +227,47 @@ public function cleanVehicle($vehicle)
* Gets balance.
*
* @return float
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function getBalance()
{
$response = $this->query($this->prepareUri('profile/balance'), [], self::METHOD_GET);
return (double) $response;
return (float) $response;
}

/**
* Requests API.
*
* @param string $uri
* @param array $params
* @param array $params
*
* @param string $method
*
* @return array
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
protected function query($uri, array $params = [], $method = self::METHOD_POST)
{
$request = new Request($method, $uri, [
'Content-Type' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Token ' . $this->token,
'X-Secret' => $this->secret,
'X-Secret' => $this->secret,
], 0 < count($params) ? json_encode($params) : null);

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

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

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
}

if (empty($result)) {
throw new RuntimeException('Empty result');
throw new \RuntimeException('Empty result');
}

return array_shift($result);
Expand Down Expand Up @@ -288,9 +295,9 @@ protected function prepareUri($endpoint)
*/
protected function populate(AbstractResponse $object, array $data)
{
$reflect = new ReflectionClass($object);
$reflect = new \ReflectionClass($object);

$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
$properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);

foreach ($properties as $property) {
if (array_key_exists($property->name, $data)) {
Expand All @@ -309,10 +316,14 @@ protected function populate(AbstractResponse $object, array $data)
* @return Party\Party
* @throws \ReflectionException
*/
protected function populateParty (array $response)
protected function populateParty(array $response)
{
list($name, $post) = array_values($response['data']['management']);
$management = new Party\ManagementDto($name, $post);
$management = null;
$managementData = $response['data']['management'];
if (is_array($managementData) && array_key_exists('name', $managementData) && array_key_exists('post', $managementData)) {
list($name, $post) = array_values($response['data']['management']);
$management = new Party\ManagementDto($name, $post);
}

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

$address = $this->populate(new Address(), $response['data']['address']['data']);
$address = null;
if (is_array($response['data']['address']['data'])) {
$address = $this->populate(new Address(), $response['data']['address']['data']);
}

return new Party\Party(
$response['value'],
Expand All @@ -350,11 +364,11 @@ protected function populateParty (array $response)
/**
* Guesses and converts property type by phpdoc comment.
*
* @param ReflectionProperty $property
* @param \ReflectionProperty $property
* @param mixed $value
* @return mixed
*/
protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
protected function getValueByAnnotatedType(\ReflectionProperty $property, $value)
{
$comment = $property->getDocComment();
if (preg_match('/@var (.+?)(\|null)? /', $comment, $matches)) {
Expand All @@ -375,12 +389,13 @@ protected function getValueByAnnotatedType(ReflectionProperty $property, $value)
/**
* @param string $ip
* @return null|Address
* @throws Exception
* @throws \Exception
* @throws GuzzleException
*/
public function detectAddressByIp($ip)
{
$request = new Request('get', $this->baseSuggestionsUrl . 'detectAddressByIp' . '?ip=' . $ip, [
'Accept' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->token,
]);

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

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('Error parsing response: ' . json_last_error_msg());
throw new \RuntimeException('Error parsing response: ' . json_last_error_msg());
}

if (!array_key_exists('location', $result)) {
throw new Exception('Required key "location" is missing');
throw new \Exception('Required key "location" is missing');
}

if (null === $result['location']) {
return null;
}

if (!array_key_exists('data', $result['location'])) {
throw new Exception('Required key "data" is missing');
throw new \Exception('Required key "data" is missing');
}

if (null === $result['location']['data']) {
Expand All @@ -427,6 +442,7 @@ public function detectAddressByIp($ip)
* @throws \ReflectionException
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws GuzzleException
*/
public function getAddressById($addressId)
{
Expand All @@ -450,10 +466,10 @@ public function getAddressById($addressId)
* ФИО руководителя компании;
* адресу до улицы.
*
* @param $party
* @param string $party
*
* @return \SplObjectStorage
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
* @throws \ReflectionException
* @throws \InvalidArgumentException
* @throws \RuntimeException
Expand Down Expand Up @@ -481,6 +497,4 @@ protected function prepareSuggestionsUri($endpoint)
{
return $this->baseSuggestionsUrl . $endpoint;
}


}
Loading

0 comments on commit 5a7c887

Please sign in to comment.