From 303155be8918737c64aa8f6840a3fed583ce41d0 Mon Sep 17 00:00:00 2001 From: Carlos C Soto Date: Tue, 16 Nov 2021 22:01:06 -0600 Subject: [PATCH] Fix PHPStan (1.1.2) issues --- docs/CHANGELOG.md | 9 +++++++++ src/CaptchaAnswer.php | 2 +- src/HttpClient/HttpClient.php | 7 ++++++- .../AntiCaptchaResolver/AntiCaptchaConnector.php | 3 +++ .../CaptchaLocalResolverConnector.php | 4 ++-- src/Resolvers/CommandLineResolver.php | 4 ++-- tests/Extending/HasPreviousException.php | 4 ++-- tests/HttpTestCase.php | 6 +----- .../AntiCaptchaResolver/AntiCaptchaResolverUsageTest.php | 2 +- .../CaptchaLocalResolverUsageTest.php | 4 ++-- .../AntiCaptchaResolver/AntiCaptchaConnectorTest.php | 2 ++ 11 files changed, 31 insertions(+), 16 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 901415d..2b23747 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,15 @@ Usamos [Versionado Semántico 2.0.0](SEMVER.md) por lo que puedes usar esta libr Pueden aparecer cambios no liberados que se integran a la rama principal, pero no ameritan una nueva liberación de versión, aunque sí su incorporación en la rama principal de trabajo. Generalmente se tratan de cambios en el desarrollo. +## Versión 0.2.1 2021-11-16 *Happy birthday Noni* + +La versión más reciente de PHPStan `phpstan/phpstan:1.1.2` encontró algunos puntos de mejora +y uno que otro falso positivo. Se hacen las correcciones: + +- `AntiCaptchaConnector`: Se previene un error de ejecución al verificar la respuesta del servidor. +- `CaptchaLocalResolverConnector`: Se previene un error de ejecución al verificar la respuesta del servidor. +- Se eliminan asignaciones superfluas al usar el operador `Null coalescing`. + ## Versión 0.2.0 2021-07-28 Se agrega el resolvedor `CommandLineResolver` que pasa la imagen del captcha como un archivo temporal diff --git a/src/CaptchaAnswer.php b/src/CaptchaAnswer.php index 20686d0..39f66bd 100644 --- a/src/CaptchaAnswer.php +++ b/src/CaptchaAnswer.php @@ -30,7 +30,7 @@ public function getValue(): string public function equalsTo($value): bool { try { - return $this->value === (string) $value; + return $this->value === strval($value); } catch (Throwable $ex) { return false; } diff --git a/src/HttpClient/HttpClient.php b/src/HttpClient/HttpClient.php index 773961f..14708ac 100644 --- a/src/HttpClient/HttpClient.php +++ b/src/HttpClient/HttpClient.php @@ -140,7 +140,12 @@ private function convertThrowableToClientExceptionInterface(Throwable $exception return new class($exception) extends RuntimeException implements ClientExceptionInterface { public function __construct(Throwable $previous) { - parent::__construct($previous->getMessage(), $previous->getCode(), $previous); + /** + * @see https://github.com/phpstan/phpstan-src/pull/767 + * @var int|string $code + */ + $code = $previous->getCode(); + parent::__construct($previous->getMessage(), (int) $code, $previous); } }; } diff --git a/src/Resolvers/AntiCaptchaResolver/AntiCaptchaConnector.php b/src/Resolvers/AntiCaptchaResolver/AntiCaptchaConnector.php index d9c39ad..c0deeeb 100644 --- a/src/Resolvers/AntiCaptchaResolver/AntiCaptchaConnector.php +++ b/src/Resolvers/AntiCaptchaResolver/AntiCaptchaConnector.php @@ -112,6 +112,9 @@ public function request(string $methodName, array $postData): stdClass } $result = json_decode((string) $response->getBody()); + if (! $result instanceof stdClass) { + $result = (object) ['errorId' => 1, 'errorDescription' => 'Response is not a JSON object']; + } $errorId = intval($result->errorId ?? 0); if ($errorId > 0) { throw new RuntimeException( diff --git a/src/Resolvers/CaptchaLocalResolver/CaptchaLocalResolverConnector.php b/src/Resolvers/CaptchaLocalResolver/CaptchaLocalResolverConnector.php index 8d1a340..c14c490 100644 --- a/src/Resolvers/CaptchaLocalResolver/CaptchaLocalResolverConnector.php +++ b/src/Resolvers/CaptchaLocalResolver/CaptchaLocalResolverConnector.php @@ -86,7 +86,7 @@ public function sendImage(CaptchaImageInterface $image): string } $contents = strval($response->getBody()); $data = json_decode($contents, true); - $code = strval($data['code'] ?? ''); + $code = (is_array($data)) ? strval($data['code'] ?? '') : ''; if ('' === $code) { throw new RuntimeException('Image was sent but service returns empty code'); } @@ -118,7 +118,7 @@ public function checkCode(string $code): string } $contents = strval($response->getBody()); $data = json_decode($contents, true); - return strval($data['answer'] ?? ''); + return (is_array($data)) ? strval($data['answer'] ?? '') : ''; } public function buildUri(string $action): string diff --git a/src/Resolvers/CommandLineResolver.php b/src/Resolvers/CommandLineResolver.php index 344a6f1..b4ceced 100644 --- a/src/Resolvers/CommandLineResolver.php +++ b/src/Resolvers/CommandLineResolver.php @@ -43,8 +43,8 @@ public function __construct( throw new LogicException('Command cannot be "{file}"'); } $this->command = $command; - $this->answerBuilder = $answerBuilder ?? new CommandLineResolver\LastLineAnswerBuilder(); - $this->processRunner = $processRunner ?? new CommandLineResolver\SymfonyProcessRunner(); + $this->answerBuilder = $answerBuilder; + $this->processRunner = $processRunner; } /** diff --git a/tests/Extending/HasPreviousException.php b/tests/Extending/HasPreviousException.php index a6bca50..6e20fd9 100644 --- a/tests/Extending/HasPreviousException.php +++ b/tests/Extending/HasPreviousException.php @@ -32,8 +32,8 @@ protected function failureDescription($other): string '%s &%s has previous exception %s &%s', get_class($this->exception), spl_object_hash($this->exception), - get_class($other), - spl_object_hash($other), + (is_object($other)) ? get_class($other) : gettype($other), + (is_object($other)) ? spl_object_hash($other) : '', ); } diff --git a/tests/HttpTestCase.php b/tests/HttpTestCase.php index b21f62f..4c0e572 100644 --- a/tests/HttpTestCase.php +++ b/tests/HttpTestCase.php @@ -42,11 +42,7 @@ protected function createPhpHttpMockClient(): PhpHttpMockClient protected function createHttpClient(ClientInterface $client): HttpClientInterface { - return new HttpClient( - $client ?? $this->createPhpHttpMockClient(), - $this->requestFactory, - $this->streamFactory - ); + return new HttpClient($client, $this->requestFactory, $this->streamFactory); } /** diff --git a/tests/Integration/AntiCaptchaResolver/AntiCaptchaResolverUsageTest.php b/tests/Integration/AntiCaptchaResolver/AntiCaptchaResolverUsageTest.php index 34e9cb8..0bd8b41 100644 --- a/tests/Integration/AntiCaptchaResolver/AntiCaptchaResolverUsageTest.php +++ b/tests/Integration/AntiCaptchaResolver/AntiCaptchaResolverUsageTest.php @@ -13,7 +13,7 @@ final class AntiCaptchaResolverUsageTest extends TestCase { public function checkTestIsEnabled(): void { - if ('yes' !== $this->getenv('ANTI_CAPTCHA_ENABLED') ?? '') { + if ('yes' !== $this->getenv('ANTI_CAPTCHA_ENABLED')) { $this->markTestSkipped('Anti-captcha resolver tests are not enabled'); } } diff --git a/tests/Integration/CaptchaLocalResolver/CaptchaLocalResolverUsageTest.php b/tests/Integration/CaptchaLocalResolver/CaptchaLocalResolverUsageTest.php index 21a5356..ea5c305 100644 --- a/tests/Integration/CaptchaLocalResolver/CaptchaLocalResolverUsageTest.php +++ b/tests/Integration/CaptchaLocalResolver/CaptchaLocalResolverUsageTest.php @@ -18,11 +18,11 @@ protected function setUp(): void { parent::setUp(); - if ('yes' !== $this->getenv('CAPTCHA_LOCAL_RESOLVER_ENABLED') ?? '') { + if ('yes' !== $this->getenv('CAPTCHA_LOCAL_RESOLVER_ENABLED')) { $this->markTestSkipped('Captcha local resolver tests are not enabled'); } - $localResolverUrl = $this->getenv('CAPTCHA_LOCAL_RESOLVER_BASEURL') ?? ''; + $localResolverUrl = $this->getenv('CAPTCHA_LOCAL_RESOLVER_BASEURL'); if ('' === $localResolverUrl) { $this->fail('Environment CAPTCHA_LOCAL_RESOLVER_BASEURL is not set'); } diff --git a/tests/Unit/Resolvers/AntiCaptchaResolver/AntiCaptchaConnectorTest.php b/tests/Unit/Resolvers/AntiCaptchaResolver/AntiCaptchaConnectorTest.php index 7b2ff02..89907a2 100644 --- a/tests/Unit/Resolvers/AntiCaptchaResolver/AntiCaptchaConnectorTest.php +++ b/tests/Unit/Resolvers/AntiCaptchaResolver/AntiCaptchaConnectorTest.php @@ -12,6 +12,7 @@ use PhpCfdi\ImageCaptchaResolver\Tests\Extending\AssertHasPreviousExceptionTrait; use PhpCfdi\ImageCaptchaResolver\Tests\HttpTestCase; use RuntimeException; +use stdClass; final class AntiCaptchaConnectorTest extends HttpTestCase { @@ -55,6 +56,7 @@ public function testCreateTask(): void $this->assertSame($expectedTaskId, $taskId); $lastRequest = $phpHttpMockClient->getLastRequest(); + /** @var stdClass $sentValues */ $sentValues = json_decode((string) $lastRequest->getBody()); $this->assertSame($this->clientKey, $sentValues->clientKey ?? ''); $this->assertSame('ImageToTextTask', $sentValues->task->type ?? '');