Skip to content

Commit

Permalink
refactor: return types - type hints - possible exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
SSEsmaeeli committed Jan 4, 2024
1 parent ab404be commit 2501af7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 32 deletions.
9 changes: 2 additions & 7 deletions src/Exceptions/InsufficientBalanceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InsufficientBalanceException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Insufficient balance to cover the order', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidDepositException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidDepositException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid deposit operation', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidValueException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid value to deposit', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/InvalidWalletTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidWalletTypeException extends Exception
{
public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid wallet type', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/WalletNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class WalletNotFoundException extends Exception
{
public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Wallet not found', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace HPWebdeveloper\LaravelPayPocket\Services;

use Illuminate\Database\Eloquent\Model;

class PocketServices
{
public function deposit($user, $type, $amount)
public function deposit(Model $user, string $type, int|float $amount): bool
{
return $user->deposit($type, $amount);

Check failure on line 11 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::deposit().
}

public function pay($user, $orderValue)
public function pay(Model $user, int|float $orderValue): void
{
return $user->pay($orderValue);
$user->pay($orderValue);

Check failure on line 16 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::pay().
}

public function checkBalance($user)
public function checkBalance(Model $user): int|float
{
return $user->walletBalance;

Check failure on line 21 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Database\Eloquent\Model::$walletBalance.
}

public function walletBalanceByType($user, $type)
public function walletBalanceByType(Model $user, string $type): int|float
{
return $user->getWalletBalanceByType($type);

Check failure on line 26 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::getWalletBalanceByType().
}
Expand Down
9 changes: 5 additions & 4 deletions src/Traits/HandlesDeposit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ trait HandlesDeposit
{
/**
* Deposit an amount to the user's wallet of a specific type.
* @throws InvalidDepositException
* @throws InvalidValueException|InvalidWalletTypeException
*/
public function deposit(string $type, int|float $amount): bool
{
Expand Down Expand Up @@ -50,12 +52,11 @@ private function getDepositableTypes(): array
}

/**
* Check if the given tyep is valid.
* Check if the given type is valid.
*
* @param string $type
* @return bool
* @throws InvalidWalletTypeException
*/
private function isRequestValid($type, array $depositable)
private function isRequestValid($type, array $depositable): bool
{
if (! array_key_exists($type, $depositable)) {
throw new InvalidWalletTypeException('Invalid deposit type.');
Expand Down

0 comments on commit 2501af7

Please sign in to comment.