Skip to content

Commit

Permalink
Merge pull request #305 from cultuurnet/III-3558-increase-php-stan-to…
Browse files Browse the repository at this point in the history
…-level-6

III-3558 Increase PHPStan to level 6
  • Loading branch information
LucWollants authored Aug 19, 2024
2 parents e01c7ff + 9a5f472 commit c77e2d3
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function addShared(string $serviceName, callable $function, ?string $t

/**
* Get parameter from config
*
* @return mixed|null
*/
protected function parameter(string $parameter)
{
Expand All @@ -40,7 +40,7 @@ protected function parameter(string $parameter)

/**
* Get service from container
*
* @return object
*/
protected function get(string $name)
{
Expand Down
2 changes: 1 addition & 1 deletion app/Console/AbstractMappingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class AbstractMappingCommand extends AbstractElasticSearchCommand
*/
protected $documentType;

public function __construct(Client $client, $indexName, $documentType)
public function __construct(Client $client, string $indexName, string $documentType)
{
parent::__construct($client);
$this->indexName = $indexName;
Expand Down
5 changes: 1 addition & 4 deletions app/Console/AbstractReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,11 @@ public function __construct(
$this->indexationStrategy = $indexationStrategy;
}

/**
* @inheritdoc
*/
protected function runOperation(
InputInterface $input,
OutputInterface $output,
AbstractReindexUDB3CoreOperation $operation
) {
): void {
$indexationStrategy = $this->getIndexationStrategy();
$logger = $this->getLogger($output);

Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
parameters:
level: 5
level: 6
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
paths:
- app
- src
Expand Down
1 change: 1 addition & 0 deletions src/Deserializer/DeserializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

interface DeserializerInterface
{
// @phpstan-ignore-next-line
public function deserialize(string $data);
}
4 changes: 4 additions & 0 deletions src/ElasticSearch/AbstractElasticSearchQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ protected function withRangeQuery(string $fieldName, $from = null, $to = null)
return $c;
}

/**
* @param string|int|null $from
* @param string|int|null $to
*/
protected function createRangeQuery(string $fieldName, $from = null, $to = null): ?RangeQuery
{
$parameters = array_filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function indexDocument(
string $indexName,
string $documentType,
JsonDocument $jsonDocument
);
): void;

public function finish(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use Broadway\Serializer\Serializable;
use Exception;
use Broadway\Domain\DomainEventStream;
use Broadway\Domain\DomainMessage;
Expand Down Expand Up @@ -166,7 +167,7 @@ private function dispatchEventForHit(array $hit): void
}


private function getReadableEventType($event): string
private function getReadableEventType(Serializable $event): string
{
$parts = explode('\\', get_class($event));
return end($parts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface ElasticSearchResponseValidatorInterface
*
* @throws InvalidElasticSearchResponseException
*/
public function validate(array $responseData);
public function validate(array $responseData): void;
}
8 changes: 8 additions & 0 deletions src/Http/ApiRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
interface ApiRequestInterface extends ServerRequestInterface
{
/**
* @param mixed|null $default
* @return mixed|null
*/
public function getQueryParam(string $name, $default = null);

public function hasQueryParam(string $name): bool;
Expand All @@ -25,5 +29,9 @@ public function getQueryParamsKeys(): ?array;

public function getQueryParameterBag(): ParameterBagInterface;

/**
* @param mixed|null $default
* @return mixed|null
*/
public function getServerParam(string $name, $default = null);
}
11 changes: 8 additions & 3 deletions src/Http/Authentication/Auth0/Auth0MetadataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class Auth0MetadataGenerator implements MetadataGenerator
{
use LoggerAwareTrait;

private Client $client;

private string $domain;

private LoggerInterface $logger;

public function __construct(
Client $client,
string $domain
Expand Down Expand Up @@ -56,4 +56,9 @@ public function get(string $clientId, string $token): ?array
$res = Json::decodeAssociatively($response->getBody()->getContents());
return $res['client_metadata'] ?? [];
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}
10 changes: 7 additions & 3 deletions src/Http/Authentication/Keycloak/KeycloakMetadataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class KeycloakMetadataGenerator implements MetadataGenerator
{
use LoggerAwareTrait;

private Client $client;
private string $domain;
private string $realm;
private LoggerInterface $logger;

public function __construct(
Client $client,
Expand Down Expand Up @@ -94,4 +93,9 @@ private function convertDefaultScopes(array $defaultScopes): array
$result['publiq-apis'] = implode(' ', $scopes);
return $result;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}
2 changes: 1 addition & 1 deletion src/Http/Authentication/MetadataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface MetadataGenerator
{
public function get(string $clientId, string $token): ?array;

public function setLogger(LoggerInterface $logger);
public function setLogger(LoggerInterface $logger): void;
}
13 changes: 8 additions & 5 deletions src/Http/Parameters/ArrayParameterBagAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public function getIntegerFromParameter(
}

public function getExplodedStringFromParameter(
$parameterName,
$defaultValueAsString = null,
string $parameterName,
?string $defaultValueAsString = null,
callable $callback = null,
$delimiter = ','
string $delimiter = ','
): array {
$callback = $this->ensureCallback($callback);

Expand Down Expand Up @@ -148,10 +148,13 @@ public function getDateTimeFromParameter(string $queryParameter, ?string $defaul
return $this->getStringFromParameter($queryParameter, $defaultValueAsString, $callback);
}

private function get(string $queryParameter, $default = null)
/**
* @return mixed|null
*/
private function get(string $queryParameter)
{
if (!isset($this->parameterBag[$queryParameter])) {
return $default;
return null;
}

return $this->parameterBag[$queryParameter];
Expand Down
3 changes: 3 additions & 0 deletions src/Http/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static function apiProblem($data, int $code = StatusCodeInterface::STATUS
return self::jsonWithCustomContentType('application/problem+json', $data, $code);
}

/**
* @param array|object $data
*/
private static function jsonWithCustomContentType(
string $contentType,
$data,
Expand Down
4 changes: 2 additions & 2 deletions src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function encodeForHttpResponse($value): string
* @param string $data
* Encoded JSON data.
*
* @returns mixed
* @return mixed|null
* Decoded data, usually as an array or stdClass object but can also be a string, integer, boolean, etc depending
* on the encoded data.
*
Expand All @@ -81,7 +81,7 @@ public static function decode(string $data)
* @param string $data
* Encoded JSON data.
*
* @returns mixed
* @return mixed|null
* Decoded data, usually as an array but can also be a string, integer, boolean, etc depending on the encoded
* data.
*
Expand Down
2 changes: 1 addition & 1 deletion src/ReadModel/DocumentGone.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class DocumentGone extends RuntimeException
{
public function __construct($message = '', $code = 410, Exception $previous = null)
public function __construct(string $message = '', int $code = 410, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 3 additions & 0 deletions src/ReadModel/JsonDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function getRawBody(): string
return $this->body;
}

/**
* @param object|array $body
*/
public function withBody($body): JsonDocument
{
return new self($this->id, Json::encode($body));
Expand Down
3 changes: 2 additions & 1 deletion tests/ElasticSearch/Operations/AbstractOperationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractOperationTestCase extends TestCase
*/
protected $logger;


// @phpstan-ignore-next-line
protected $operation;

protected function setUp(): void
Expand All @@ -43,5 +43,6 @@ protected function setUp(): void
$this->operation = $this->createOperation($this->client, $this->logger);
}

// @phpstan-ignore-next-line
abstract protected function createOperation(Client $client, LoggerInterface $logger);
}
3 changes: 3 additions & 0 deletions tests/Http/OfferSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ public function it_works_with_a_min_price_and_max_price_if_no_exact_price_is_set

/**
* @test
* @param bool|string|int $stringValue
* @dataProvider booleanStringDataProvider
*/
public function it_converts_the_media_objects_toggle_parameter_to_a_correct_boolean(
Expand Down Expand Up @@ -704,6 +705,7 @@ public function it_converts_the_media_objects_toggle_parameter_to_a_correct_bool

/**
* @test
* @param bool|string|int $stringValue
* @dataProvider booleanStringDataProvider
*/
public function it_converts_the_uitpas_toggle_parameter_to_a_correct_boolean(
Expand Down Expand Up @@ -907,6 +909,7 @@ public function it_transforms_the_request_address_country_to_uppercase(): void
/**
* @test
* @dataProvider malformedDateTimeProvider
* @param bool|string|int $malformedDateTimeAsString
* @todo PHP8 upgrade: change Type hint to string|bool
*/
public function it_throws_an_exception_for_a_malformed_date_from($malformedDateTimeAsString): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Parameters/ArrayParameterBagAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ public function it_should_return_an_empty_array_for_a_delimited_string_parameter

/**
* @test
* @param bool|string|int $parameterValue
* @dataProvider booleanDataProvider
*
*/
public function it_should_parse_a_boolean_value_from_a_parameter(
$parameterValue,
Expand Down

0 comments on commit c77e2d3

Please sign in to comment.