diff --git a/composer.json b/composer.json index 79734e6..3b1ab6a 100755 --- a/composer.json +++ b/composer.json @@ -61,6 +61,7 @@ }, "scripts": { "test": "vendor/bin/phpunit tests --colors=always --testdox", + "static": "vendor/bin/phpstan analyse", "post-update-cmd": [ "vendor/bin/phpstan analyse", "composer test" diff --git a/src/Models/Transaction.php b/src/Models/Transaction.php index 58d5ca5..204506e 100755 --- a/src/Models/Transaction.php +++ b/src/Models/Transaction.php @@ -118,7 +118,7 @@ public function getAmountAttribute() return $this->getAmountWithSign(); } - public function setAmountAttribute($amount) + public function setAmountAttribute(int|float $amount) { if ($this->shouldConvertToAbsoluteAmount()) { $amount = abs($amount); diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index b0f9481..2caaf05 100755 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -61,9 +61,15 @@ public function owner(): MorphTo * * @param int $amount * @return MannikJ\Laravel\Wallet\Models\Transaction + * + * @throws UnacceptedTransactionException */ - public function deposit(float $amount, array $meta = [], string $type = 'deposit', bool $forceFail = false): Transaction - { + public function deposit( + float $amount, + array $meta = [], + string $type = 'deposit', + bool $forceFail = false + ): Transaction { $accepted = $amount >= 0 && !$forceFail ? true : false; @@ -71,6 +77,7 @@ public function deposit(float $amount, array $meta = [], string $type = 'deposit $this->save(); } + /** @var Transaction */ $transaction = $this->transactions() ->create([ 'amount' => $amount, @@ -90,12 +97,14 @@ public function deposit(float $amount, array $meta = [], string $type = 'deposit /** * Fail to move credits to this account. - * - * @param float $amount - * @return MannikJ\Laravel\Wallet\Models\Transaction + * + * @throws UnacceptedTransactionException */ - public function failDeposit(float $amount, array $meta = [], string $type = 'deposit'): Transaction - { + public function failDeposit( + float $amount, + array $meta = [], + string $type = 'deposit' + ) { return $this->deposit($amount, $meta, $type, true); } @@ -105,8 +114,12 @@ public function failDeposit(float $amount, array $meta = [], string $type = 'dep * @param float $amount Only the absolute value will be considered * @return MannikJ\Laravel\Wallet\Models\Transaction */ - public function withdraw(float $amount, array $meta = [], string $type = 'withdraw', bool $guarded = true) - { + public function withdraw( + float $amount, + array $meta = [], + string $type = 'withdraw', + bool $guarded = true + ) { $accepted = $guarded ? $this->canWithdraw($amount) : true; @@ -137,8 +150,11 @@ public function withdraw(float $amount, array $meta = [], string $type = 'withdr * * @param float $amount */ - public function forceWithdraw(int|float $amount, array $meta = [], string $type = 'withdraw') - { + public function forceWithdraw( + int|float $amount, + array $meta = [], + string $type = 'withdraw' + ) { return $this->withdraw($amount, $meta, $type, false); } @@ -163,8 +179,10 @@ public function canWithdraw(float $amount = null) * @param string $comment * @return MannikJ\Laravel\Wallet\Models\Transaction */ - public function setBalance(float $amount, string $comment = 'Manual offset transaction') - { + public function setBalance( + float $amount, + string $comment = 'Manual offset transaction' + ) { $actualBalance = $this->actualBalance(); $difference = $amount - $actualBalance; diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index b61bb71..b676a01 100755 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -2,6 +2,9 @@ namespace MannikJ\Laravel\Wallet\Traits; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; +use Illuminate\Database\Eloquent\Relations\MorphOne; + // use MannikJ\Laravel\Wallet\Models\Transaction; // use MannikJ\Laravel\Wallet\Models\Wallet; @@ -10,7 +13,7 @@ trait HasWallet /** * Retrieve the balance of this user's wallet */ - public function getBalanceAttribute() + public function getBalanceAttribute(): int|float { return $this->wallet->refresh()->balance; } @@ -18,7 +21,7 @@ public function getBalanceAttribute() /** * Retrieve the wallet of this user */ - public function wallet() + public function wallet(): MorphOne { return $this->morphOne(config('wallet.wallet_model'), 'owner')->withDefault(); } @@ -26,7 +29,7 @@ public function wallet() /** * Retrieve all transactions of this user */ - public function walletTransactions() + public function walletTransactions(): HasManyThrough { return $this->hasManyThrough( config('wallet.transaction_model'),