Skip to content

Commit

Permalink
psalm error detection level 1, related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsizemore committed Mar 11, 2024
1 parent 564a9a6 commit f4255af
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Simply replace the version numbers depending on which set of changes you wish to
#### Changed

* Updates throughout to fix psalm-reported issues.
* Initially level 2, now should be valid on Psalm level 1.

#### Added

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Numverify phone number validation and country API client library for PHP.
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ericsizemore/numverify-api-client-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ericsizemore/numverify-api-client-php/?branch=master)
[![Tests](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/tests.yml/badge.svg)](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/tests.yml)
[![PHPStan](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/analysis.yml/badge.svg)](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/analysis.yml)

[![Psalm Static analysis](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/psalm.yml/badge.svg?branch=master)](https://github.com/ericsizemore/numverify-api-client-php/actions/workflows/psalm.yml)

[![Type Coverage](https://shepherd.dev/github/ericsizemore/numverify-api-client-php/coverage.svg)](https://shepherd.dev/github/ericsizemore/numverify-api-client-php)
[![Psalm Level](https://shepherd.dev/github/ericsizemore/numverify-api-client-php/level.svg)](https://shepherd.dev/github/ericsizemore/numverify-api-client-php)
[![Latest Stable Version](https://img.shields.io/packagist/v/esi/numverify-api-client-php.svg)](https://packagist.org/packages/esi/numverify-api-client-php)
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
5 changes: 4 additions & 1 deletion src/Exception/NumverifyApiFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ private function parseMessageFromBody(string $jsonBody): string
return sprintf('Unknown error - %d %s', $this->statusCode, $this->getReasonPhrase());
}

return sprintf('Type:%s Code:%d Info:%s', $body->error->type, $body->error->code, $body->error->info);
/** @var stdClass $error */
$error = $body->error;

return sprintf('Type:%s Code:%d Info:%s', (string) $error->type, (int) $error->code, (string) $error->info);
}
}
2 changes: 1 addition & 1 deletion src/PhoneNumber/InvalidPhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(stdClass $validatedPhoneNumber)
$this->verifyPhoneNumberData($validatedPhoneNumber);

$this->valid = (bool) $validatedPhoneNumber->valid;
$this->number = $validatedPhoneNumber->number;
$this->number = (string) $validatedPhoneNumber->number;
}

public function isValid(): bool
Expand Down
18 changes: 9 additions & 9 deletions src/PhoneNumber/ValidPhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public function __construct(stdClass $validatedPhoneNumberData)
$this->verifyPhoneNumberData($validatedPhoneNumberData);

$this->valid = (bool) $validatedPhoneNumberData->valid;
$this->number = $validatedPhoneNumberData->number;
$this->localFormat = $validatedPhoneNumberData->local_format;
$this->internationalFormat = $validatedPhoneNumberData->international_format;
$this->countryPrefix = $validatedPhoneNumberData->country_prefix;
$this->countryCode = $validatedPhoneNumberData->country_code;
$this->countryName = $validatedPhoneNumberData->country_name;
$this->location = $validatedPhoneNumberData->location;
$this->carrier = $validatedPhoneNumberData->carrier;
$this->lineType = $validatedPhoneNumberData->line_type;
$this->number = (string) $validatedPhoneNumberData->number;
$this->localFormat = (string) $validatedPhoneNumberData->local_format;
$this->internationalFormat = (string) $validatedPhoneNumberData->international_format;
$this->countryPrefix = (string) $validatedPhoneNumberData->country_prefix;
$this->countryCode = (string) $validatedPhoneNumberData->country_code;
$this->countryName = (string) $validatedPhoneNumberData->country_name;
$this->location = (string) $validatedPhoneNumberData->location;
$this->carrier = (string) $validatedPhoneNumberData->carrier;
$this->lineType = (string) $validatedPhoneNumberData->line_type;
}

/**
Expand Down
37 changes: 33 additions & 4 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ApiTest extends TestCase

/**
* @testCase Construction with default Guzzle client.
* @psalm-suppress DeprecatedMethod
*/
#[DataProvider('dataProviderForHttp')]
public function testConstructionWithDefaultClient(bool $useHttps): void
Expand All @@ -54,8 +55,13 @@ public function testConstructionWithDefaultClient(bool $useHttps): void
$reflectionProperty = $reflectionClass->getProperty('client');
self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));

/** @var Client $client */
$client = $reflectionProperty->getValue($api);

$client = self::parseGuzzleConfig($client);

$expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
$actual = (string) $reflectionProperty->getValue($api)->getConfig('base_uri'); // @phpstan-ignore-line
$actual = (string) $client['base_uri']; // @phpstan-ignore-line
self::assertSame($expected, $actual);
}

Expand All @@ -72,8 +78,13 @@ public function testConstructionWithCustomClient(bool $useHttps): void
$reflectionProperty = $reflectionClass->getProperty('client');
self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));

/** @var Client $client */
$client = $reflectionProperty->getValue($api);

$client = self::parseGuzzleConfig($client);

$expected = 'http://apilayer.net/api';
$actual = (string) $reflectionProperty->getValue($api)->getConfig('base_uri'); // @phpstan-ignore-line
$actual = (string) $client['base_uri']; // @phpstan-ignore-line
self::assertSame($expected, $actual);
}

Expand All @@ -90,10 +101,28 @@ public function testConstructionWithDefaultClientExtraOptions(bool $useHttps): v
$reflectionProperty = $reflectionClass->getProperty('client');
self::assertInstanceOf(ClientInterface::class, $reflectionProperty->getValue($api));

/** @var Client $client */
$client = $reflectionProperty->getValue($api);

$client = self::parseGuzzleConfig($client);

$expected = ($useHttps ? 'https' : 'http') . '://apilayer.net/api';
$actual = (string) $reflectionProperty->getValue($api)->getConfig('base_uri'); // @phpstan-ignore-line
$actual = (string) $client['base_uri']; // @phpstan-ignore-line
self::assertSame($expected, $actual);
self::assertSame(10, $reflectionProperty->getValue($api)->getConfig('timeout'));
self::assertSame(10, $client['timeout']);
}

/**
* @return array<array-key, mixed>
*/
private static function parseGuzzleConfig(Client $client): array
{
$client = (array) $client;

/** @var array<array-key, mixed> $config */
$config = \array_shift($client);

return $config;
}

/**
Expand Down
26 changes: 17 additions & 9 deletions tests/Country/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,23 @@ public function testJsonSerialize(): void
self::assertObjectHasProperty('US', $object);
self::assertObjectHasProperty('GB', $object);
self::assertObjectHasProperty('JP', $object);
self::assertSame('US', $object->US->countryCode);
self::assertSame('GB', $object->GB->countryCode);
self::assertSame('JP', $object->JP->countryCode);
self::assertSame('United States', $object->US->countryName);
self::assertSame('United Kingdom', $object->GB->countryName);
self::assertSame('Japan', $object->JP->countryName);
self::assertSame('+1', $object->US->diallingCode);
self::assertSame('+44', $object->GB->diallingCode);
self::assertSame('+81', $object->JP->diallingCode);

/** @var stdClass $countryUs */
$countryUs = $object->US;
/** @var stdClass $countryGb */
$countryGb = $object->GB;
/** @var stdClass $countryJp */
$countryJp = $object->JP;

self::assertSame('US', (string) $countryUs->countryCode);
self::assertSame('GB', (string) $countryGb->countryCode);
self::assertSame('JP', (string) $countryJp->countryCode);
self::assertSame('United States', (string) $countryUs->countryName);
self::assertSame('United Kingdom', (string) $countryGb->countryName);
self::assertSame('Japan', (string) $countryJp->countryName);
self::assertSame('+1', (string) $countryUs->diallingCode);
self::assertSame('+44', (string) $countryGb->diallingCode);
self::assertSame('+81', (string) $countryJp->diallingCode);
}

/**
Expand Down

0 comments on commit f4255af

Please sign in to comment.