Skip to content

Commit

Permalink
#8 Remove all phpstan ignoreErrors, exceptp parameter.defaultValue be…
Browse files Browse the repository at this point in the history
…cause of OptionsResolver. Fix code.
  • Loading branch information
njoubert-cleverage committed Dec 12, 2024
1 parent 95dea37 commit aaa5243
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
13 changes: 1 addition & 12 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,4 @@ parameters:
- src
- tests
ignoreErrors:
- '#type has no value type specified in iterable type#'
- '#has parameter .* with no value type specified in iterable type#'
- '#has no value type specified in iterable type array#'
- '#configureOptions\(\) has no return type specified.#'
- '#configure\(\) has no return type specified#'
- '#process\(\) has no return type specified#'
- '#should return Iterator but returns Traversable#'
- '#Negated boolean expression is always false#'
checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: false
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
- identifier: parameter.defaultValue
33 changes: 31 additions & 2 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @phpstan-import-type Options from \CleverAge\RestProcessBundle\Task\RequestTask
*/
class Client implements ClientInterface
{
public function __construct(
Expand Down Expand Up @@ -50,6 +53,8 @@ public function setUri(string $uri): void
}

/**
* @param Options $options
*
* @throws RestRequestException
*/
public function call(array $options = []): ResponseInterface
Expand All @@ -70,7 +75,7 @@ public function call(array $options = []): ResponseInterface
$this->getRequestUri($options),
$this->getRequestOptions($options),
);
} catch (\Exception|\Throwable $e) {
} catch (\Throwable $e) {
$this->logger->error(
'Rest request failed',
[
Expand Down Expand Up @@ -110,6 +115,11 @@ protected function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('data', ['array', 'string', 'null']);
}

/**
* @param Options $options
*
* @return Options
*/
protected function getOptions(array $options = []): array
{
$resolver = new OptionsResolver();
Expand All @@ -118,11 +128,24 @@ protected function getOptions(array $options = []): array
return $resolver->resolve($options);
}

/**
* @param Options $options
*/
protected function getRequestUri(array $options = []): string
{
return $this->replaceParametersInUri($this->constructUri($options), $options);
}

/**
* @param Options $options
*
* @return array{
* 'headers': array<mixed>,
* 'json'?: array<mixed>|string|null,
* 'query'?: array<mixed>|string|null,
* 'body'?: array<mixed>|string|null
* }
*/
protected function getRequestOptions(array $options = []): array
{
$requestOptions = [];
Expand All @@ -144,6 +167,9 @@ protected function getRequestOptions(array $options = []): array
return $requestOptions;
}

/**
* @param Options $options
*/
protected function constructUri(array $options): string
{
$uri = ltrim((string) $options['url'], '/');
Expand All @@ -156,9 +182,12 @@ protected function getApiUrl(): string
return $this->geUri();
}

/**
* @param Options $options
*/
protected function replaceParametersInUri(string $uri, array $options = []): string
{
if (\array_key_exists('url_parameters', $options) && $options['url_parameters']) {
if ($options['url_parameters']) {
$search = array_keys($options['url_parameters']);
array_walk(
$search,
Expand Down
6 changes: 6 additions & 0 deletions src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @phpstan-import-type Options from \CleverAge\RestProcessBundle\Task\RequestTask
*/
interface ClientInterface
{
/**
Expand All @@ -26,5 +29,8 @@ public function geUri(): string;

public function setUri(string $uri): void;

/**
* @param Options $options
*/
public function call(array $options = []): ResponseInterface;
}
27 changes: 25 additions & 2 deletions src/Task/RequestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@
use Symfony\Component\OptionsResolver\Exception\AccessException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
* @phpstan-type Options array{
* 'url': string,
* 'method': string,
* 'headers': array<mixed>,
* 'url_parameters': array<mixed>,
* 'sends': string,
* 'expects': string,
* 'data': array<mixed>|string|null
* }
*/
class RequestTask extends AbstractConfigurableTask
{
public function __construct(protected LoggerInterface $logger, protected ClientRegistry $registry)
Expand All @@ -31,6 +46,11 @@ public function __construct(protected LoggerInterface $logger, protected ClientR

/**
* @throws MissingClientException
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws \Throwable
*/
public function execute(ProcessState $state): void
{
Expand Down Expand Up @@ -68,7 +88,7 @@ public function execute(ProcessState $state): void
}

$state->setOutput($response->getContent());
} catch (\Exception|\Throwable $e) {
} catch (\Throwable $e) {
$this->logger->error(
'REST request failed',
[
Expand Down Expand Up @@ -116,6 +136,9 @@ protected function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('log_response', ['bool']);
}

/**
* @return Options
*/
protected function getRequestOptions(ProcessState $state): array
{
$options = $this->getOptions($state);
Expand Down

0 comments on commit aaa5243

Please sign in to comment.