Skip to content

Commit

Permalink
Merge pull request #65 from aidan-casey/add-psalm
Browse files Browse the repository at this point in the history
Fix some typing and add Psalm
  • Loading branch information
denisdulici committed May 11, 2022
2 parents d3f40cd + db2ec2e commit 2233663
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ tests/ export-ignore
.gitignore export-ignore
.styleci.yml export-ignore
phpunit.xml export-ignore
psalm.xml export-ignore
33 changes: 33 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Psalm

on:
push:
paths:
- '**.php'
- 'psalm.xml'

jobs:
psalm:
name: psalm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}

- name: Run composer install
run: composer install -n --prefer-dist

- name: Run psalm
run: ./vendor/bin/psalm -c psalm.xml
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"require-dev":{
"phpunit/phpunit":"^9.5",
"orchestra/testbench": "^6.23|^7.4"
"orchestra/testbench": "^6.23|^7.4",
"vimeo/psalm": "^4.23"
},
"autoload":{
"psr-4":{
Expand All @@ -49,6 +50,12 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm"
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}
}
30 changes: 30 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<issueHandlers>
<MixedMethodCall>
<errorLevel type="suppress">
<file name="src/Provider.php" />
</errorLevel>
</MixedMethodCall>
<MixedArgument>
<errorLevel type="suppress">
<file name="src/Provider.php" />
</errorLevel>
</MixedArgument>
<RedundantPropertyInitializationCheck errorLevel="suppress" />
<PropertyNotSetInConstructor errorLevel="suppress" />
</issueHandlers>

<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
3 changes: 2 additions & 1 deletion src/Casts/MoneyCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public function get($model, string $key, $value, array $attributes): Money
throw new UnexpectedValueException;
}

/** @var null|array{amount:mixed, currency:string} $value */
$value = json_decode($value, true);

if (! $value || ! isset($value['amount']) || ! isset($value['currency'])) {
if (! is_array($value) || ! isset($value['amount']) || ! isset($value['currency'])) {
throw new UnexpectedValueException;
}

Expand Down
14 changes: 5 additions & 9 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public function __construct(string $currency)
throw new OutOfBoundsException('Invalid currency "' . $currency . '"');
}

$attributes = $currencies[$currency];
$attributes = (array) $currencies[$currency];
$this->currency = $currency;
$this->name = (string) $attributes['name'];
$this->code = (int) $attributes['code'];
$this->rate = (float) isset($attributes['rate']) ? $attributes['rate'] : 1;
$this->rate = (float) (isset($attributes['rate']) ? $attributes['rate'] : 1);
$this->precision = (int) $attributes['precision'];
$this->subunit = (int) $attributes['subunit'];
$this->symbol = (string) $attributes['symbol'];
Expand All @@ -232,7 +232,7 @@ public function __construct(string $currency)

public static function __callStatic(string $method, array $arguments): Currency
{
return new static($method);
return new self($method);
}

/**
Expand All @@ -250,11 +250,7 @@ public static function setCurrencies(array $currencies): void

public static function getCurrencies(): array
{
if (! isset(static::$currencies)) {
static::$currencies = require __DIR__ . '/../config/money.php';
}

return static::$currencies;
return static::$currencies ??= require __DIR__ . '/../config/money.php';
}

public function equals(Currency $currency): bool
Expand All @@ -277,7 +273,7 @@ public function getCode(): int
return $this->code;
}

public function getRate(): int
public function getRate(): float
{
return $this->rate;
}
Expand Down
28 changes: 16 additions & 12 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function __construct(mixed $amount, Currency $currency, bool $convert = f
*/
protected function parseAmount(mixed $amount, bool $convert = false): int|float
{
/** @var int|float|Money $amount */
$amount = $this->parseAmountFromString($this->parseAmountFromCallable($amount));

if (is_int($amount)) {
Expand Down Expand Up @@ -281,7 +282,7 @@ public static function __callStatic(string $method, array $arguments): Money
{
$convert = isset($arguments[1]) && is_bool($arguments[1]) && $arguments[1];

return new static($arguments[0], new Currency($method), $convert);
return new self($arguments[0], new Currency($method), $convert);
}

/**
Expand Down Expand Up @@ -320,7 +321,7 @@ protected function assertSameCurrency(Money $other): void
}
}

protected function assertRoundingMode($mode)
protected function assertRoundingMode(int $mode): void
{
$modes = [self::ROUND_HALF_UP, self::ROUND_HALF_DOWN, self::ROUND_HALF_EVEN, self::ROUND_HALF_ODD];

Expand Down Expand Up @@ -418,9 +419,9 @@ public function convert(Currency $currency, int|float $ratio, int $roundingMode
return $this->multiply($ratio, $roundingMode);
}

public function add(mixed $addend, int $roundingMode = self::ROUND_HALF_UP): Money
public function add(int|float|Money $addend, int $roundingMode = self::ROUND_HALF_UP): Money
{
if ($addend instanceof static) {
if ($addend instanceof Money) {
$this->assertSameCurrency($addend);

$addend = $addend->getAmount();
Expand All @@ -429,17 +430,17 @@ public function add(mixed $addend, int $roundingMode = self::ROUND_HALF_UP): Mon
$amount = $this->round($this->amount + $addend, $roundingMode);

if ($this->isImmutable()) {
return new static($amount, $this->currency);
return new self($amount, $this->currency);
}

$this->amount = $amount;

return $this;
}

public function subtract(mixed $subtrahend, int $roundingMode = self::ROUND_HALF_UP): Money
public function subtract(int|float|Money $subtrahend, int $roundingMode = self::ROUND_HALF_UP): Money
{
if ($subtrahend instanceof static) {
if ($subtrahend instanceof Money) {
$this->assertSameCurrency($subtrahend);

$subtrahend = $subtrahend->getAmount();
Expand All @@ -448,7 +449,7 @@ public function subtract(mixed $subtrahend, int $roundingMode = self::ROUND_HALF
$amount = $this->round($this->amount - $subtrahend, $roundingMode);

if ($this->isImmutable()) {
return new static($amount, $this->currency);
return new self($amount, $this->currency);
}

$this->amount = $amount;
Expand All @@ -461,7 +462,7 @@ public function multiply(int|float $multiplier, int $roundingMode = self::ROUND_
$amount = $this->round($this->amount * $multiplier, $roundingMode);

if ($this->isImmutable()) {
return new static($amount, $this->currency);
return new self($amount, $this->currency);
}

$this->amount = $amount;
Expand All @@ -476,7 +477,7 @@ public function divide(int|float $divisor, int $roundingMode = self::ROUND_HALF_
$amount = $this->round($this->amount / $divisor, $roundingMode);

if ($this->isImmutable()) {
return new static($amount, $this->currency);
return new self($amount, $this->currency);
}

$this->amount = $amount;
Expand All @@ -491,6 +492,9 @@ public function round(int|float $amount, int $mode = self::ROUND_HALF_UP): float
return round($amount, $this->currency->getPrecision(), $mode);
}

/**
* @param array<array-key,int|float> $ratios
*/
public function allocate(array $ratios): array
{
$remainder = $this->amount;
Expand All @@ -499,7 +503,7 @@ public function allocate(array $ratios): array

foreach ($ratios as $ratio) {
$share = floor($this->amount * $ratio / $total);
$results[] = new static($share, $this->currency);
$results[] = new self($share, $this->currency);
$remainder -= $share;
}

Expand Down Expand Up @@ -618,7 +622,7 @@ public function immutable(): Money
{
$this->mutable = false;

return new static($this->amount, $this->currency);
return new self($this->amount, $this->currency);
}

public function mutable(): Money
Expand Down
4 changes: 2 additions & 2 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function boot(): void
public function registerBladeDirectives(): void
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$bladeCompiler->directive('money', function ($expression) {
$bladeCompiler->directive('money', function (?string $expression) {
return "<?php echo money($expression); ?>";
});

$bladeCompiler->directive('currency', function ($expression) {
$bladeCompiler->directive('currency', function (?string $expression) {
return "<?php echo currency($expression); ?>";
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/View/Components/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Akaunting\Money\View\Components;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use Illuminate\View\View;

class Currency extends Component
{
Expand All @@ -12,7 +13,10 @@ public function __construct(public string $currency)
//
}

public function render(): View
/**
* @psalm-suppress InvalidReturnType,InvalidReturnStatement
*/
public function render(): View|Factory
{
return view('money::components.currency');
}
Expand Down
8 changes: 6 additions & 2 deletions src/View/Components/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Akaunting\Money\View\Components;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use Illuminate\View\View;

class Money extends Component
{
Expand All @@ -15,7 +16,10 @@ public function __construct(
//
}

public function render(): View
/**
* @psalm-suppress InvalidReturnType,InvalidReturnStatement
*/
public function render(): View|Factory
{
return view('money::components.money');
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
function money(mixed $amount, string $currency = null, bool $convert = false): Money
{
if (is_null($currency)) {
/** @var string $currency */
$currency = env('DEFAULT_CURRENCY', 'USD');
}

Expand Down

0 comments on commit 2233663

Please sign in to comment.