Skip to content

Commit

Permalink
phpstan update
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jun 18, 2021
1 parent ec34e82 commit 58d94ce
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 96 deletions.
91 changes: 50 additions & 41 deletions composer.json
@@ -1,43 +1,52 @@
{
"name": "h4kuna/exchange",
"type": "library",
"description": "Exchange between currencies.",
"homepage": "https://github.com/h4kuna/exchange",
"license": "MIT",
"authors": [
{
"name": "Milan Matějček",
"email": "milan.matejcek@gmail.com",
"role": "Developer"
}
],
"require": {
"php": ">=7.1",
"guzzlehttp/guzzle": ">=6.1",
"h4kuna/data-type": "^2.1",
"h4kuna/number-format": "^3.0",
"h4kuna/serialize-polyfill": "^0.0",
"nette/safe-stream": "^2.0",
"nette/utils": "^3.0"
},
"require-dev": {
"nette/http": "^3.0",
"nette/tester": "^2.0",
"phpstan/phpstan": "^0.11",
"tracy/tracy": "^2.0"
},
"autoload": {
"psr-4": {
"h4kuna\\Exchange\\": "src"
}
},
"suggest": {
"nette/http": "If you want to use h4kuna\\Exchange\\Http\\CookieManager"
},
"autoload-dev": {
"psr-4": {
"h4kuna\\Exchange\\": "tests/src"
},
"files": ["tests/lib/Driver.php"]
}
"name": "h4kuna/exchange",
"type": "library",
"description": "Exchange between currencies.",
"homepage": "https://github.com/h4kuna/exchange",
"license": "MIT",
"authors": [
{
"name": "Milan Matějček",
"email": "milan.matejcek@gmail.com",
"role": "Developer"
}
],
"require": {
"php": ">=7.1",
"guzzlehttp/guzzle": ">=6.1",
"h4kuna/data-type": "^2.1",
"h4kuna/number-format": "^3.0",
"h4kuna/serialize-polyfill": "^0.0",
"nette/safe-stream": "^2.0",
"nette/utils": "^3.0"
},
"require-dev": {
"nette/http": "^3.0",
"nette/tester": "^2.0",
"phpstan/phpstan": "^0.12",
"tracy/tracy": "^2.0"
},
"autoload": {
"psr-4": {
"h4kuna\\Exchange\\": "src"
}
},
"suggest": {
"nette/http": "If you want to use h4kuna\\Exchange\\Http\\CookieManager.",
"ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb."
},
"autoload-dev": {
"psr-4": {
"h4kuna\\Exchange\\": "tests/src"
},
"files": [
"tests/lib/Driver.php"
]
},
"config": {
"sort-packages": true
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --level max -c tests/config/phpstan.neon src tests"
}
}
5 changes: 3 additions & 2 deletions src/Caching/Cache.php
Expand Up @@ -19,7 +19,7 @@ class Cache implements ICache
/** @var Currency\ListRates[] */
private $listRates;

/** @var array */
/** @var array<string> */
private $allowedCurrencies = [];

/**
Expand All @@ -29,7 +29,7 @@ class Cache implements ICache
private $refresh = '15:00';


public function __construct($temp)
public function __construct(string $temp)
{
$this->temp = $temp;
}
Expand Down Expand Up @@ -64,6 +64,7 @@ public function invalidForce(Driver\Driver $driver, ?\DateTimeInterface $date =


/**
* @param array<string> $allowedCurrencies
* @return static
*/
public function setAllowedCurrencies(array $allowedCurrencies)
Expand Down
1 change: 1 addition & 0 deletions src/Caching/ICache.php
Expand Up @@ -15,6 +15,7 @@ function flushCache(Driver\Driver $driver, \DateTimeInterface $date = null): voi


/**
* @param array<string> $allowed
* @return static
*/
function setAllowedCurrencies(array $allowed);
Expand Down
13 changes: 10 additions & 3 deletions src/Currency/Formats.php
Expand Up @@ -15,11 +15,11 @@ class Formats
/** @var NumberFormat[] */
private $formats = [];

/** @var array */
/** @var array<string, array<string, string|bool|int|null>> */
private $rawFormats = [];

/** @var NumberFormat */
private $default;
private $default = null;


public function __construct(Number\NumberFormatFactory $numberFormatFactory)
Expand All @@ -29,10 +29,14 @@ public function __construct(Number\NumberFormatFactory $numberFormatFactory)


/**
* @param array|NumberFormat $setup
* @param array<string, string|bool|int|null>|NumberFormat $setup
*/
public function setDefaultFormat($setup): void
{
if ($this->default !== null) {
throw new Exchange\Exceptions\InvalidState('Default format could be setup only onetime.');
}

if (is_array($setup)) {
$setup = $this->numberFormatFactory->createUnit($setup);
} elseif (!$setup instanceof NumberFormat) {
Expand All @@ -43,6 +47,9 @@ public function setDefaultFormat($setup): void
}


/**
* @param array<string, string|bool|int|null> $setup
*/
public function addFormat(string $code, array $setup): void
{
$code = strtoupper($code);
Expand Down
13 changes: 12 additions & 1 deletion src/Currency/ListRates.php
Expand Up @@ -5,13 +5,17 @@
use h4kuna\Exchange;
use h4kuna\Exchange\Exceptions\FrozenMethod;

/**
* @implements \ArrayAccess<string, Property>
* @implements \Iterator<string, Property>
*/
class ListRates implements \ArrayAccess, \Iterator
{

/** @var \DateTimeInterface */
private $date;

/** @var Property[] */
/** @var array<string, Property> */
private $currencies = [];


Expand Down Expand Up @@ -53,12 +57,14 @@ public function getDate(): \DateTimeInterface

public function offsetExists($offset)
{
assert($this->currencies !== []);
return isset($this->currencies[$offset]);
}


public function offsetGet($offset)
{
assert($this->currencies !== []);
return $this->currencies[$offset];
}

Expand All @@ -77,18 +83,23 @@ public function offsetUnset($offset)

public function current()
{
assert($this->currencies !== []);
return current($this->currencies);
}


public function next()
{
assert($this->currencies !== []);
next($this->currencies);
}


public function key()
{
if ($this->currencies === []) {
throw new Exchange\Exceptions\EmptyExchangeRate();
}
return key($this->currencies);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Currency/Property.php
Expand Up @@ -2,15 +2,20 @@

namespace h4kuna\Exchange\Currency;

use h4kuna\DataType\Immutable;

/**
* @property-read float $home
* @property-read int $foreign
* @property-read float $rate
* @property-read string $code
*/
class Property extends \h4kuna\DataType\Immutable\Messenger
class Property extends Immutable\Messenger
{

/**
* @param array<string, int|float|string> $data
*/
public function __construct(array $data)
{
$data['foreign'] = (int) $data['foreign'];
Expand All @@ -23,7 +28,7 @@ public function __construct(array $data)

public function __toString()
{
return (string) $this->code;
return $this->code;
}

}
7 changes: 5 additions & 2 deletions src/Driver/Cnb/Day.php
Expand Up @@ -19,7 +19,7 @@ class Day extends Exchange\Driver\Driver
/**
* Load data from remote source.
* @param DateTime $date
* @return array
* @return array<int, string>
*/
protected function loadFromSource(?\DateTimeInterface $date): iterable
{
Expand All @@ -34,6 +34,9 @@ protected function loadFromSource(?\DateTimeInterface $date): iterable
}


/**
* @param string $row
*/
protected function createProperty($row): Property
{
$currency = explode('|', $row);
Expand All @@ -47,7 +50,7 @@ protected function createProperty($row): Property
}


private function createUrl($url, ?\DateTimeInterface $date): string
private function createUrl(string $url, ?\DateTimeInterface $date): string
{
if ($date === null) {
return $url;
Expand Down
8 changes: 5 additions & 3 deletions src/Driver/Driver.php
Expand Up @@ -17,6 +17,7 @@ abstract class Driver

/**
* Download data from remote source and save.
* @param array<string> $allowedCurrencies
*/
public function download(?\DateTimeInterface $date = null, array $allowedCurrencies = []): Exchange\Currency\ListRates
{
Expand All @@ -29,7 +30,7 @@ public function download(?\DateTimeInterface $date = null, array $allowedCurrenc
}
$property = $this->createProperty($row);

if (!$property || !$property->rate || ($allowedCurrencies !== [] && !isset($allowedCurrencies[$property->code]))) {
if ($property->rate === 0.0 || ($allowedCurrencies !== [] && !isset($allowedCurrencies[$property->code]))) {
continue;
}
$currencies->addProperty($property);
Expand All @@ -39,7 +40,7 @@ public function download(?\DateTimeInterface $date = null, array $allowedCurrenc
}


protected function setDate(string $format, $value): void
protected function setDate(string $format, string $value): void
{
$date = DateTime::createFromFormat($format, $value);
if ($date === false) {
Expand Down Expand Up @@ -70,7 +71,8 @@ abstract protected function loadFromSource(?\DateTimeInterface $date): iterable;

/**
* Modify data before save to cache.
* @return Exchange\Currency\Property|NULL
* @param mixed $row
* @return Exchange\Currency\Property
*/
abstract protected function createProperty($row);

Expand Down
8 changes: 6 additions & 2 deletions src/Driver/Ecb/Day.php
Expand Up @@ -22,7 +22,7 @@ class Day extends Exchange\Driver\Driver
/**
* Load data from remote source
* @param \DateTimeInterface $date
* @return array
* @return array<array{rate: float, currency: string}>
*/
protected function loadFromSource(?\DateTimeInterface $date): iterable
{
Expand All @@ -38,17 +38,21 @@ protected function loadFromSource(?\DateTimeInterface $date): iterable
$eur = $xml->Cube->Cube->addChild("Cube");
$eur->addAttribute('currency', 'EUR');
$eur->addAttribute('rate', '1');
assert(isset($xml->Cube->Cube) && $xml->Cube->Cube->attributes() !== null);
$this->setDate('Y-m-d', (string) $xml->Cube->Cube->attributes()['time']);
return $xml->Cube->Cube->Cube;
}


/**
* @param array{rate: float, currency: string} $row
*/
protected function createProperty($row): Exchange\Currency\Property
{
return new Exchange\Currency\Property([
'code' => $row['currency'],
'home' => $row['rate'],
'foreign' => 1
'foreign' => 1,
]);
}

Expand Down

0 comments on commit 58d94ce

Please sign in to comment.