Skip to content

Commit

Permalink
make CountryResolver more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 12, 2018
1 parent a7ffe70 commit f7d3df2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ parameters:
# known value, container is already created
- '#Method Fop\\DependencyInjection\\ContainerFactory::create\(\) should return Psr\\Container\\ContainerInterface but returns Symfony\\Component\\DependencyInjection\\ContainerInterface\|null#'

# false positive, checked above
# false positive, checked or @var doc above
- '#Parameter \#1 \$str of function strtolower expects string, string\|null given#'
- '#Method Fop\\Country\\CountryResolver::resolveFromGroup\(\) should return string but returns string\|null#'
31 changes: 8 additions & 23 deletions src/Country/CountryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
use Rinvex\Country\Country;
use Rinvex\Country\CountryLoader;
use Throwable;

final class CountryResolver
{
/**
* @var string
*/
private const UNKNOWN_COUNTRY = 'unknown';

/**
* @var string
*/
Expand Down Expand Up @@ -79,22 +74,16 @@ public function resolveFromGroup(array $group): string
}

$countryCode = $this->resolveCountryCodeFromGroup($group);
if ($countryCode === null) {
return self::UNKNOWN_COUNTRY;
}

try {
$countryOrCountries = CountryLoader::country($countryCode);
if (is_array($countryOrCountries)) {
$country = array_pop($countryOrCountries);
} else {
$country = $countryOrCountries;
}
$countryOrCountries = CountryLoader::country($countryCode);

if (is_array($countryOrCountries)) {
/** @var Country $country */
$country = array_pop($countryOrCountries);
return $country->getName();
} catch (Throwable $throwable) {
return self::UNKNOWN_COUNTRY;
}

return $countryOrCountries->getName();
}

/**
Expand Down Expand Up @@ -148,16 +137,12 @@ private function getCountryJsonByLatitudeAndLongitude(float $latitude, float $lo
*
* @param mixed[] $group
*/
private function resolveCountryCodeFromGroup(array $group): ?string
private function resolveCountryCodeFromGroup(array $group): string
{
if (isset($group['country']) && $group['country'] && $group['country'] !== '-') {
return $group['country'];
}

if (! isset($group['latitude'], $group['longitude'])) {
return null;
}

$countryJson = $this->getCountryJsonByLatitudeAndLongitude($group['latitude'], $group['latitude']);

return $countryJson['address']['country_code'];
Expand Down
1 change: 0 additions & 1 deletion tests/Country/CountryResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ public function provideData(): Iterator
'latitude' => 50.847572953654,
'longitude' => 4.3535041809082,
], 'Belgium'];
yield [['country' => 'random'], 'unknown'];
}
}

0 comments on commit f7d3df2

Please sign in to comment.