Skip to content

Commit

Permalink
Merge pull request #3 from eclipxe13/phpstan-1.1.2
Browse files Browse the repository at this point in the history
Corregir los problemas detectados por PHPStan (1.1.2)
  • Loading branch information
eclipxe13 committed Nov 17, 2021
2 parents 686be43 + 303155b commit d4cd964
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 16 deletions.
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/CaptchaAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/Resolvers/AntiCaptchaResolver/AntiCaptchaConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Resolvers/CommandLineResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Extending/HasPreviousException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) : '',
);
}

Expand Down
6 changes: 1 addition & 5 deletions tests/HttpTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpCfdi\ImageCaptchaResolver\Tests\Extending\AssertHasPreviousExceptionTrait;
use PhpCfdi\ImageCaptchaResolver\Tests\HttpTestCase;
use RuntimeException;
use stdClass;

final class AntiCaptchaConnectorTest extends HttpTestCase
{
Expand Down Expand Up @@ -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 ?? '');
Expand Down

0 comments on commit d4cd964

Please sign in to comment.