Skip to content

Commit

Permalink
PHPStan Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaoka committed Jul 12, 2024
1 parent adbab60 commit 94a0b91
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 42 deletions.
16 changes: 2 additions & 14 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@ parameters:

paths:
- src
- tests

level: 9

checkMissingIterableValueType: false

ignoreErrors:
-
message: '!Property Sunaoka\\PushNotifications\\Drivers\\FCM\\V1::\$httpClient!'
path: src/Drivers/FCM/V1.php
-
message: '!Parameter #1 \$errors of class!'
path: src/Drivers/DriverOption.php
-
message: '!Else branch is unreachable because previous condition is always true!'
path: src/Drivers/Driver.php
-
message: "!Offset 'class' does not exist on array{function: string, line\\?: int, file\\?: string, class\\?: class-string, type\\?: '->'\\|'::', args\\?: array, object\\?: object}!"
path: src/Exceptions/OptionTypeError.php
- identifier: missingType.iterableValue
2 changes: 1 addition & 1 deletion src/Drivers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function parseErrorResponse($e)
if ($response !== null) {
$message = $response->getReasonPhrase();
$contents = $response->getBody()->getContents();
} else {
} else { // @phpstan-ignore else.unreachable
$message = $e->getMessage();
}
} else {
Expand Down
1 change: 1 addition & 0 deletions src/Drivers/DriverOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function validate()
$validator = new Validator((array)$this);
$validator->mapFieldsRules(array_merge($this->defaultValidationRules, $this->validationRules));
if (!$validator->validate()) {
// @phpstan-ignore argument.type
throw new ValidationException($validator->errors());
}

Expand Down
1 change: 1 addition & 0 deletions src/Drivers/FCM/V1.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function send()
'scopes' => 'https://www.googleapis.com/auth/firebase.messaging',
]);

// @phpstan-ignore assign.propertyType
$this->httpClient = $client->authorize($this->getHttpClient($this->options->httpOptions));

foreach ($this->devices as $device) {
Expand Down
5 changes: 4 additions & 1 deletion src/Exceptions/OptionTypeError.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public function __construct($expected, $actual)
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

if (isset($trace[1])) {
// @phpstan-ignore offsetAccess.notFound
$method = sprintf('%s::%s()', $trace[1]['class'], $trace[1]['function']);
} else {
$method = 'Unknown'; // @codeCoverageIgnore
}

$message = sprintf(
'%s: Argument #1 ($options) must be of type %s, %s given, called',
$method, $expected, is_object($actual) ? get_class($actual) : gettype($actual)
$method,
$expected,
is_object($actual) ? get_class($actual) : gettype($actual)
);

parent::__construct($message);
Expand Down
4 changes: 3 additions & 1 deletion tests/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
trait Assert
{
/**
* @param string $exception
* @param class-string<\Throwable> $exception
*
* @return void
*/
public function expectExceptionCompat($exception)
{
Expand Down
51 changes: 50 additions & 1 deletion tests/Drivers/APNs/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

class CertificateTest extends TestCase
{
/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testSingleToken()
{
$payload = [
Expand Down Expand Up @@ -45,6 +51,12 @@ public function testSingleToken()
self::assertSame('01234567-0123-0123-0123-01234567890A', $feedback->success('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testMultipleToken()
{
$payload = [
Expand Down Expand Up @@ -80,6 +92,12 @@ public function testMultipleToken()
self::assertSame('01234567-0123-0123-0123-01234567890B', $feedback->success('abcdefghij'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testSingleFailure()
{
$payload = [
Expand Down Expand Up @@ -109,6 +127,12 @@ public function testSingleFailure()
self::assertSame('BadDeviceToken', $feedback->failure('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testMultipleFailure()
{
$payload = [
Expand Down Expand Up @@ -144,6 +168,9 @@ public function testMultipleFailure()
self::assertSame('BadDeviceToken', $feedback->failure('abcdefghij'));
}

/**
* @return void
*/
public function testMakeOption()
{
$payload = [
Expand All @@ -165,6 +192,11 @@ public function testMakeOption()
self::assertSame('com.example.app', $options->topic);
}

/**
* @return void
*
* @throws ValidationException
*/
public function testValidateOption()
{
$this->expectExceptionCompat(ValidationException::class);
Expand All @@ -173,13 +205,24 @@ public function testValidateOption()
$options->validate();
}

/**
* @return void
*
* @throws OptionTypeError
*/
public function testInvalidOption()
{
$this->expectExceptionCompat(OptionTypeError::class);

new APNs\Certificate(new FakeOption());
new APNs\Certificate(new FakeOption()); // @phpstan-ignore argument.type
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testRequestFailure()
{
$payload = [
Expand Down Expand Up @@ -209,6 +252,12 @@ public function testRequestFailure()
self::assertSame('Internal Server Error', $feedback->failure('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testRequestException()
{
$payload = [
Expand Down
65 changes: 60 additions & 5 deletions tests/Drivers/APNs/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

class TokenTest extends TestCase
{
/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testSingleToken()
{
$payload = [
Expand All @@ -26,7 +32,7 @@ public function testSingleToken()

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = file_get_contents($this->certs('/fake.p8'));
$options->authKey = (string)file_get_contents($this->certs('/fake.p8'));
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';
Expand All @@ -46,6 +52,12 @@ public function testSingleToken()
self::assertSame('01234567-0123-0123-0123-01234567890A', $feedback->success('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testMultipleToken()
{
$payload = [
Expand All @@ -56,7 +68,7 @@ public function testMultipleToken()

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = file_get_contents($this->certs('/fake.p8'));
$options->authKey = (string)file_get_contents($this->certs('/fake.p8'));
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';
Expand All @@ -82,6 +94,12 @@ public function testMultipleToken()
self::assertSame('01234567-0123-0123-0123-01234567890B', $feedback->success('abcdefghij'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testAuthKeyIsFile()
{
$payload = [
Expand Down Expand Up @@ -112,6 +130,12 @@ public function testAuthKeyIsFile()
self::assertSame('01234567-0123-0123-0123-01234567890A', $feedback->success('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testSingleFailure()
{
$payload = [
Expand Down Expand Up @@ -142,6 +166,12 @@ public function testSingleFailure()
self::assertSame('BadDeviceToken', $feedback->failure('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testMultipleFailure()
{
$payload = [
Expand Down Expand Up @@ -178,6 +208,9 @@ public function testMultipleFailure()
self::assertSame('BadDeviceToken', $feedback->failure('abcdefghij'));
}

/**
* @return void
*/
public function testMakeOption()
{
$payload = [
Expand All @@ -201,6 +234,11 @@ public function testMakeOption()
self::assertSame('com.example.app', $options->topic);
}

/**
* @return void
*
* @throws ValidationException
*/
public function testValidateOption()
{
$this->expectExceptionCompat(ValidationException::class);
Expand All @@ -209,13 +247,24 @@ public function testValidateOption()
$options->validate();
}

/**
* @return void
*
* @throws OptionTypeError
*/
public function testInvalidOption()
{
$this->expectExceptionCompat(OptionTypeError::class);

new APNs\Token(new FakeOption());
new APNs\Token(new FakeOption()); // @phpstan-ignore argument.type
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testRequestFailure()
{
$payload = [
Expand All @@ -226,7 +275,7 @@ public function testRequestFailure()

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = file_get_contents($this->certs('/fake.p8'));
$options->authKey = (string)file_get_contents($this->certs('/fake.p8'));
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';
Expand All @@ -246,6 +295,12 @@ public function testRequestFailure()
self::assertSame('Internal Server Error', $feedback->failure('1234567890'));
}

/**
* @return void
*
* @throws OptionTypeError
* @throws ValidationException
*/
public function testRequestException()
{
$payload = [
Expand All @@ -256,7 +311,7 @@ public function testRequestException()

$options = new APNs\Token\Option();
$options->payload = $payload;
$options->authKey = file_get_contents($this->certs('/fake.p8'));
$options->authKey = (string)file_get_contents($this->certs('/fake.p8'));
$options->keyId = 'ABCDE12345';
$options->teamId = 'ABCDE12345';
$options->topic = 'com.example.app';
Expand Down
Loading

0 comments on commit 94a0b91

Please sign in to comment.