Skip to content

Commit 0620e42

Browse files
3m1n3nc3HPWebdeveloper
authored andcommitted
Improve type annotations and PHPDoc
1 parent aa174d1 commit 0620e42

File tree

8 files changed

+153
-25
lines changed

8 files changed

+153
-25
lines changed

src/Facades/LaravelPayPocket.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
/**
88
* @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices
9+
*
10+
* @method static void pay(\Illuminate\Database\Eloquent\Model $user, int|float $orderValue, ?string $notes = null)
11+
* @method static bool deposit(\Illuminate\Database\Eloquent\Model $user, string $type, int|float $amount, ?string $notes = null)
12+
* @method static int|float checkBalance(\Illuminate\Database\Eloquent\Model $user)
13+
* @method static int|float walletBalanceByType(\Illuminate\Database\Eloquent\Model $user, string $type)
914
*/
1015
class LaravelPayPocket extends Facade
1116
{

src/Interfaces/WalletOperations.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,50 @@
44

55
interface WalletOperations
66
{
7-
public function getWalletBalanceAttribute();
7+
/**
8+
* Get User's Wallet Balance
9+
*
10+
* @return int|float
11+
*/
12+
public function getWalletBalanceAttribute(): int|float;
813

9-
public function getWalletBalanceByType(string $walletType);
14+
/**
15+
* Get the balance of a specific wallet type.
16+
*
17+
*
18+
* @param string $walletType
19+
*
20+
* @return float|int
21+
*/
22+
public function getWalletBalanceByType(string $walletType): float|int;
1023

11-
public function hasSufficientBalance($value): bool;
24+
/**
25+
* Check if User's wallet balance is more than given value
26+
*
27+
* @param int|float $value
28+
*
29+
* @return bool
30+
*/
31+
public function hasSufficientBalance(int|float $value): bool;
1232

13-
public function pay(int|float $orderValue, ?string $notes = null);
33+
/**
34+
* Pay the order value from the user's wallets.
35+
*
36+
* @param int|float $orderValue
37+
* @param ?string $notes
38+
*
39+
* @throws InsufficientBalanceException
40+
*/
41+
public function pay(int|float $orderValue, ?string $notes = null): void;
1442

43+
/**
44+
* Deposit an amount to the user's wallet of a specific type.
45+
*
46+
* @param string $type
47+
* @param int|float $amount
48+
* @param ?string $notes
49+
*
50+
* @return bool
51+
*/
1552
public function deposit(string $type, int|float $amount, ?string $notes = null): bool;
1653
}

src/Services/PocketServices.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,63 @@
22

33
namespace HPWebdeveloper\LaravelPayPocket\Services;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
57
class PocketServices
68
{
7-
public function deposit($user, $type, $amount, $notes = null)
9+
/**
10+
* Deposit an amount to the user's wallet of a specific type.
11+
*
12+
* @param \Illuminate\Database\Eloquent\Model $user
13+
* @param string $type
14+
* @param int|float $amount
15+
* @param ?string $notes
16+
*
17+
* @return bool
18+
*/
19+
public function deposit(Model $user, string $type, int|float $amount, ?string $notes = null): bool
820
{
921
return $user->deposit($type, $amount, $notes);
1022
}
1123

12-
public function pay($user, $orderValue, $notes = null)
24+
/**
25+
* Pay the order value from the user's wallets.
26+
*
27+
* @param \Illuminate\Database\Eloquent\Model $user
28+
* @param int|float $orderValue
29+
* @param ?string $notes
30+
*
31+
* @return void
32+
* @throws InsufficientBalanceException
33+
*/
34+
public function pay(Model $user, int|float $orderValue, ?string $notes = null): void
1335
{
14-
return $user->pay($orderValue, $notes);
36+
$user->pay($orderValue, $notes);
1537
}
1638

17-
public function checkBalance($user)
39+
/**
40+
* Get the balance of the user.
41+
*
42+
*
43+
* @param \Illuminate\Database\Eloquent\Model $user
44+
*
45+
* @return float|int
46+
*/
47+
public function checkBalance(Model $user): int|float
1848
{
1949
return $user->walletBalance;
2050
}
2151

22-
public function walletBalanceByType($user, $type)
52+
/**
53+
* Get the balance of a specific wallet type.
54+
*
55+
*
56+
* @param \Illuminate\Database\Eloquent\Model $user
57+
* @param string $type
58+
*
59+
* @return float|int
60+
*/
61+
public function walletBalanceByType(Model $user, string $type): float|int
2362
{
2463
return $user->getWalletBalanceByType($type);
2564
}

src/Traits/BalanceOperation.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,53 @@ trait BalanceOperation
99
protected $createdLog;
1010

1111
/**
12-
* Check if Balance is more than zero.
12+
* Check if Balance is more than zero.
13+
*
14+
* @return bool
1315
*/
1416
public function hasBalance(): bool
1517
{
1618
return $this->balance > 0;
1719
}
1820

1921
/**
20-
* Decrement Balance and create a log entry.
22+
* Decrement Balance and create a log entry.
23+
*
24+
* @param int|float $value
25+
* @param ?string $notes
26+
*
27+
* @return void
2128
*/
22-
public function decrementAndCreateLog($value, $notes = null): void
29+
public function decrementAndCreateLog(int|float $value, ?string $notes = null): void
2330
{
2431
$this->createLog('dec', $value, $notes);
2532
$this->decrement('balance', $value);
2633
}
2734

2835
/**
29-
* Increment Balance and create a log entry.
36+
* Increment Balance and create a log entry.
37+
*
38+
* @param int|float $value
39+
* @param ?string $notes
40+
*
41+
* @return void
3042
*/
31-
public function incrementAndCreateLog($value, $notes = null): void
43+
public function incrementAndCreateLog(int|float $value, ?string $notes = null): void
3244
{
3345
$this->createLog('inc', $value, $notes);
3446
$this->increment('balance', $value);
3547
}
3648

3749
/**
38-
* Create a new log record
50+
* Create a new log record
51+
*
52+
* @param string $logType
53+
* @param int|float $value
54+
* @param ?string $notes
55+
*
56+
* @return void
3957
*/
40-
protected function createLog($logType, $value, $notes = null): void
58+
protected function createLog(string $logType, int|float $value, ?string $notes = null): void
4159
{
4260
$currentBalance = $this->balance ?? 0;
4361

@@ -46,6 +64,10 @@ protected function createLog($logType, $value, $notes = null): void
4664
$refGen = config('pay-pocket.log_reference_generator', [
4765
Str::class, 'random', [config('pay-pocket.log_reference_length', 12)],
4866
]);
67+
$refGen = [
68+
Str::class, 'random', [config('pay-pocket.log_reference_length', 12)],
69+
];
70+
4971
$reference = config('pay-pocket.reference_string_prefix', '');
5072
$reference .= isset($refGen[0], $refGen[1])
5173
? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? [])

src/Traits/HandlesDeposit.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ trait HandlesDeposit
1414
{
1515
/**
1616
* Deposit an amount to the user's wallet of a specific type.
17+
*
18+
* @param string $type
19+
* @param int|float $amount
20+
* @param ?string $notes
21+
*
22+
* @return bool
23+
* @throws InvalidDepositException|InvalidValueException|InvalidWalletTypeException
1724
*/
1825
public function deposit(string $type, int|float $amount, ?string $notes = null): bool
1926
{
@@ -38,6 +45,8 @@ public function deposit(string $type, int|float $amount, ?string $notes = null):
3845

3946
/**
4047
* Get depositable types from WalletEnums.
48+
*
49+
* @return array
4150
*/
4251
private function getDepositableTypes(): array
4352
{
@@ -50,12 +59,15 @@ private function getDepositableTypes(): array
5059
}
5160

5261
/**
53-
* Check if the given tyep is valid.
62+
* Check if the given type is valid.
63+
*
64+
* @var string $type
65+
* @var array $depositable
5466
*
55-
* @param string $type
5667
* @return bool
68+
* @throws InvalidWalletTypeException
5769
*/
58-
private function isRequestValid($type, array $depositable)
70+
private function isRequestValid($type, array $depositable): bool
5971
{
6072
if (! array_key_exists($type, $depositable)) {
6173
throw new InvalidWalletTypeException('Invalid deposit type.');

src/Traits/HandlesPayment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ trait HandlesPayment
1010
/**
1111
* Pay the order value from the user's wallets.
1212
*
13+
* @param int|float $orderValue
14+
* @param ?string $notes
1315
*
16+
* @return void
1417
* @throws InsufficientBalanceException
1518
*/
1619
public function pay(int|float $orderValue, ?string $notes = null): void

src/Traits/HasWallet.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public function wallets()
2222
}
2323

2424
/**
25-
* Get User's Wallet Balance
25+
* Get User's Wallet Balance
26+
*
27+
* @return int|float
2628
*/
27-
public function getWalletBalanceAttribute()
29+
public function getWalletBalanceAttribute(): int|float
2830
{
2931

3032
$totalBalance = 0;
@@ -39,23 +41,30 @@ public function getWalletBalanceAttribute()
3941
}
4042

4143
return $totalBalance;
42-
4344
}
4445

4546
/**
4647
* Check if User's wallet balance is more than given value
48+
*
49+
* @param int|float $value
50+
*
51+
* @return bool
4752
*/
48-
public function hasSufficientBalance($value): bool
53+
public function hasSufficientBalance(int|float $value): bool
4954
{
5055
return (int) $this->walletBalance >= (int) $value;
5156
}
5257

58+
5359
/**
5460
* Get the balance of a specific wallet type.
5561
*
62+
*
63+
* @param string $walletType
64+
*
5665
* @return float|int
5766
*/
58-
public function getWalletBalanceByType(string $walletType)
67+
public function getWalletBalanceByType(string $walletType): float|int
5968
{
6069
if (! WalletEnums::isValid($walletType)) {
6170
throw new InvalidWalletTypeException("Invalid wallet type '{$walletType}'.");

tests/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
class User extends Authenticatable implements WalletOperations
1313
{
14-
use HasFactory, Notifiable;
14+
use HasFactory;
15+
use Notifiable;
1516
use ManagesWallet;
1617

1718
/**

0 commit comments

Comments
 (0)