Skip to content

Commit

Permalink
phpstan updates and generics
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-sz committed May 6, 2023
1 parent c58d668 commit 11eaeb3
Show file tree
Hide file tree
Showing 29 changed files with 162 additions and 36 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ parameters:
- *DEPRECATED_*
parallel:
maximumNumberOfProcesses: 4
stubFiles:
- stubs/Psr/Cache/CacheItemInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MaciejSz\Nbp\CurrencyAverageRates\Domain\CurrencyAveragesTable;

/**
* @implements \IteratorAggregate<int, CurrencyAverageRate>
* @internal
*/
final class RatesFlatCollection implements \IteratorAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
*/
final class TablesDictionary
{
/** @var array<string, CurrencyAveragesTable> */
/** @var array<string, ?CurrencyAveragesTable> */
private $tables;

/**
* @param array<string, ?CurrencyAveragesTable> $tables
*/
public function __construct(array $tables)
{
$this->tables = $tables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CurrencyAverageRatesMapper
private $rateValidator;

/**
* @param ThrowableValidator<mixed>|null $rateValidator
* @param ?ThrowableValidator<mixed> $rateValidator
*/
public function __construct(?ThrowableValidator $rateValidator = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function rawDataToDomainObject(array $tableData): CurrencyAveragesTable
$tableLetter = $dataAccess->extractString('table');
$tableNumber = $dataAccess->extractString('no');
$effectiveDate = $dataAccess->extractDateTime('effectiveDate');
/** @var array<array{currency: string, code: string, mid: float}> $rates */
$rates = $dataAccess->extractArray('rates');

return new CurrencyAveragesTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MaciejSz\Nbp\CurrencyTradingRates\Domain\CurrencyTradingTable;

/**
* @implements \IteratorAggregate<int, CurrencyTradingRate>
* @internal
*/
final class RatesFlatCollection implements \IteratorAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CurrencyTradingRatesMapper
private $rateValidator;

/**
* @param ThrowableValidator<mixed>|null $rateValidator
* @param ?ThrowableValidator<mixed> $rateValidator
*/
public function __construct(?ThrowableValidator $rateValidator = null)
{
Expand All @@ -26,24 +26,48 @@ public function __construct(?ThrowableValidator $rateValidator = null)
}

/**
* @param array<mixed> $tableData
* @param array<array<mixed>> $ratesData
* @param array{
* table: string,
* no: string,
* tradingDate: string,
* effectiveDate: string,
* rates: array<mixed>,
* } $tableData
* @param array<
* array{
* currency: string,
* code: string,
* bid: float,
* ask: float,
* }
* > $rates
* @return array<CurrencyTradingRate>
*/
public function rawDataToDomainObjectCollection(array $tableData, array $ratesData): array
public function rawDataToDomainObjectCollection(array $tableData, array $rates): array
{
$collection = [];
foreach ($ratesData as $rateData) {
$currencyTradingRate = $this->rawDataToDomainObject($tableData, $rateData);
foreach ($rates as $rate) {
$currencyTradingRate = $this->rawDataToDomainObject($tableData, $rate);
$collection[$currencyTradingRate->getCurrencyCode()] = $currencyTradingRate;
}

return $collection;
}

/**
* @param array<mixed> $tableData
* @param array<mixed> $ratesData
* @param array{
* table: string,
* no: string,
* tradingDate: string,
* effectiveDate: string,
* rates: array<mixed>,
* } $tableData
* @param array{
* currency: string,
* code: string,
* bid: float,
* ask: float,
* } $ratesData
*/
public function rawDataToDomainObject(array $tableData, array $ratesData): CurrencyTradingRate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public function __construct(?CurrencyTradingRatesMapper $ratesMapper = null)
}

/**
* @param array<mixed> $tableData
* @param array{
* table: string,
* no: string,
* tradingDate: string,
* effectiveDate: string,
* rates: array<mixed>,
* } $tableData
*/
public function rawDataToDomainObject(array $tableData): CurrencyTradingTable
{
Expand All @@ -35,6 +41,16 @@ public function rawDataToDomainObject(array $tableData): CurrencyTradingTable
$tableNumber = $dataAccess->extractString('no');
$tradingDate = $dataAccess->extractDateTime('tradingDate');
$effectiveDate = $dataAccess->extractDateTime('effectiveDate');
/**
* @var array<
* array{
* currency: string,
* code: string,
* bid: float,
* ask: float,
* }
* > $rates
*/
$rates = $dataAccess->extractArray('rates');

return new CurrencyTradingTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use MaciejSz\Nbp\GoldRates\Domain\GoldRate;

/**
* @implements \IteratorAggregate<int, GoldRate>
*/
class GoldRatesCollection implements \IteratorAggregate
{
/** @var iterable<GoldRate> */
Expand Down
4 changes: 2 additions & 2 deletions src/GoldRates/Infrastructure/Mapper/GoldRatesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
use MaciejSz\Nbp\Shared\Infrastructure\Validator\ThrowableValidator;

/**
* @implements TableMapper<array<GoldRate>>
* @implements TableMapper<GoldRate>
*/
class GoldRatesMapper implements TableMapper
{
/** @var ThrowableValidator<mixed> */
private $rateValidator;

/**
* @param ThrowableValidator<mixed>|null $rateValidator
* @param ?ThrowableValidator<mixed> $rateValidator
*/
public function __construct(?ThrowableValidator $rateValidator = null)
{
Expand Down
19 changes: 16 additions & 3 deletions src/Shared/Domain/DateFormatter/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@

function first_day_of_month(int $year, int $month): string
{
return date('Y-m-d', strtotime("{$year}-{$month}-01"));
return date('Y-m-d', safe_strtotime("{$year}-{$month}-01"));
}

function last_day_of_month(int $year, int $month): string
{
return date('Y-m-t', strtotime("{$year}-{$month}"));
return date('Y-m-t', safe_strtotime("{$year}-{$month}"));
}

function safe_strtotime(string $datetime): int
{
$result = strtotime($datetime);
if (false === $result) {
throw new InvalidDateException("Cannot convert string to time");
}

return $result;
}

/**
Expand Down Expand Up @@ -91,7 +101,10 @@ function next_month($date): \DateTimeInterface
return middle_of_month($date)->add(new \DateInterval('P1M'));
}

function middle_of_month($date): \DateTimeInterface
/**
* @param string|\DateTimeInterface $date
*/
function middle_of_month($date): \DateTimeImmutable
{
return new \DateTimeImmutable(ensure_date_obj($date)->format('Y-m-') . '15');
}
Expand Down
7 changes: 5 additions & 2 deletions src/Shared/Domain/DateTimeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ class DateTimeBuilder

/** @var string */
private $timezone;
/** @var Validator<string>|null */
/** @var ?Validator<string> */
private $validator;

/**
* @param ?Validator<string> $validator
*/
public function __construct(
string $timezone = self::DEFAULT_TIMEZONE,
?Validator $validator = null
Expand All @@ -23,7 +26,7 @@ public function __construct(
$this->validator = $validator;
}

public function build(?string $date = null): \DateTimeInterface
public function build(string $date): \DateTimeInterface
{
if ($this->validator) {
$this->validator->validate($date);
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Infrastructure/Client/NbpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
interface NbpClient
{
/**
* @return array<mixed>
* @return array<array<mixed>>
*/
public function send(NbpClientRequest $request): array;
}
2 changes: 1 addition & 1 deletion src/Shared/Infrastructure/Mapper/TableMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface TableMapper
* @param array<mixed> $tableData
* @return T
*/
public function rawDataToDomainObject(array $tableData): object;
public function rawDataToDomainObject(array $tableData);
}
5 changes: 3 additions & 2 deletions src/Shared/Infrastructure/Repository/NbpWebRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public function getGoldRates(int $year, int $month): iterable
}

/**
* @template T
* @param iterable<array<mixed>> $results
* @param TableMapper $mapper
* @return iterable
* @param TableMapper<T> $mapper
* @return iterable<T>
*/
private function hydrate(iterable $results, TableMapper $mapper): iterable
{
Expand Down
10 changes: 8 additions & 2 deletions src/Shared/Infrastructure/Serializer/ArrayDataAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

class ArrayDataAccess implements DataAccess
{
/** @var array */
/** @var array<mixed> */
private $data;
/** @var DateTimeBuilder|null */
/** @var ?DateTimeBuilder */
private $dateTimeBuilder = null;

/**
* @param array<mixed> $data
*/
public function __construct(array $data)
{
$this->data = $data;
Expand Down Expand Up @@ -65,6 +68,9 @@ public function extractFloat(string $key): float
public function extractDateTime(string $key): \DateTimeInterface
{
$value = $this->extract($key);
if (!is_string($value)) {
throw new Exception\UnexpectedDataType('valid date', gettype($value));
}
try {
return $this->getDateTimeBuilder()->build($value);
} catch (\Exception $e) {
Expand Down
4 changes: 4 additions & 0 deletions src/Shared/Infrastructure/Serializer/DataAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

interface DataAccess
{
/**
* @return mixed
*/
public function extract(string $key);

/**
Expand All @@ -25,6 +28,7 @@ public function extractDateTime(string $key): \DateTimeInterface;

/**
* @throws Exception\UnexpectedDataType
* @return array<mixed>
*/
public function extractArray(string $key): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DataKeyDoesNotExist extends \RuntimeException
{
public function __construct($key = '', $code = 0, \Throwable $previous = null)
public function __construct(string $key = '', int $code = 0, \Throwable $previous = null)
{
parent::__construct("Data key '{$key}' does not exist", $code, $previous);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/Infrastructure/Transport/CachingTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MaciejSz\Nbp\Shared\Infrastructure\Transport;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

class CachingTransport implements Transport
Expand All @@ -29,6 +30,7 @@ public function __construct(

public function get(string $path): array
{
/** @var CacheItemInterface<array<array<mixed>>> $item */
$item = $this->cache->getItem($path);
if (!$item->isHit()) {
$item->set($this->backend->get($path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function get(string $path): array
$path = trim($path, '/');
$uri = "{$baseUri}/{$path}?format=json";
$contents = file_get_contents($uri);
/** @var array<mixed>|null $data */
if (false === $contents) {
throw new TransportException("Cannot get contents from {$uri}");
}
/** @var ?array<array<mixed>> $data */
$data = json_decode($contents, true);
if (null === $data) {
throw new TransportException("Cannot decode JSON data from {$uri}");
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Infrastructure/Transport/GuzzleTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function get(string $path): array
],
]);

/** @var array<mixed>|null $data */
/** @var ?array<array<mixed>> $data */
$data = json_decode($response->getBody()->getContents(), true);
if (null === $data) {
/** @var Uri $uri */
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Infrastructure/Transport/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface Transport
{
/**
* @return array<mixed>
* @return array<array<mixed>>
*/
public function get(string $path): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function isValid($value): bool
public function validate($value): void
{
if (!$this->isValid($value)) {
throw new ValidationException("Invalid numeric rate value: '{$value}'");
$stringRepresentation = (is_string($value) || is_numeric($value))? $value : 'value';
throw new ValidationException("Invalid numeric rate value: '{$stringRepresentation}'");
}
}
}
22 changes: 22 additions & 0 deletions stubs/Psr/Cache/CacheItemInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Psr\Cache;

/**
* @template T
*/
interface CacheItemInterface
{
/**
* @return T
*/
public function get();

/**
* @param T $value
* @return $this
*/
public function set($value): self;
}
Loading

0 comments on commit 11eaeb3

Please sign in to comment.