Skip to content

Commit d7cc529

Browse files
committed
Refactor PHPDoc comments and improve type hints in ItemMerchantFeeDeductible and MerchantFeeDeductibleTest
- Updated PHPDoc annotations to use 'list<string>' for fillable properties in ItemMerchantFeeDeductible. - Enhanced type hints in MerchantFeeDeductibleTest for better clarity and maintainability. - Removed unnecessary assertions and whitespace for cleaner test code.
1 parent f35bd23 commit d7cc529

File tree

5 files changed

+38
-39
lines changed

5 files changed

+38
-39
lines changed

src/Services/PrepareService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,28 @@ public function transferExtraLazy(
118118

119119
$amountWithoutDiscount = $this->mathService->sub($amount, $discount, $toWallet->decimal_places);
120120
$depositAmount = $this->mathService->compare($amountWithoutDiscount, 0) === -1 ? '0' : $amountWithoutDiscount;
121-
121+
122122
// Check if fee should be deducted from merchant's payout instead of added to customer's payment
123123
// This follows the exact same pattern as TaxService::getFee() which checks $wallet instanceof Taxable
124124
// The $to parameter is the product model that implements Wallet through HasWallet trait
125125
// We can check $to directly since it's the model that implements the interface
126126
$isMerchantFeeDeductible = $to instanceof MerchantFeeDeductible;
127-
127+
128128
if ($isMerchantFeeDeductible) {
129129
// Fee is deducted from merchant's deposit
130130
$withdrawAmount = $depositAmount;
131131
$merchantDepositAmount = $this->mathService->sub($depositAmount, $fee, $toWallet->decimal_places);
132132
// Ensure merchant deposit amount is not negative
133-
$merchantDepositAmount = $this->mathService->compare($merchantDepositAmount, 0) === -1 ? '0' : $merchantDepositAmount;
133+
$merchantDepositAmount = $this->mathService->compare(
134+
$merchantDepositAmount,
135+
0
136+
) === -1 ? '0' : $merchantDepositAmount;
134137
} else {
135138
// Fee is added to customer's withdrawal (current behavior)
136139
$withdrawAmount = $this->mathService->add($depositAmount, $fee, $fromWallet->decimal_places);
137140
$merchantDepositAmount = $depositAmount;
138141
}
139-
142+
140143
$extra = $this->extraDtoAssembler->create($meta);
141144
$withdrawOption = $extra->getWithdrawOption();
142145
$depositOption = $extra->getDepositOption();

src/Traits/HasGift.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ public function gift(Wallet $to, ProductInterface $product, bool $force = false)
127127
$withdrawAmount = $amount;
128128
$merchantDepositAmount = $mathService->sub($amount, $fee);
129129
// Ensure merchant deposit amount is not negative
130-
$merchantDepositAmount = $mathService->compare($merchantDepositAmount, 0) === -1 ? '0' : $merchantDepositAmount;
130+
$merchantDepositAmount = $mathService->compare(
131+
$merchantDepositAmount,
132+
0
133+
) === -1 ? '0' : $merchantDepositAmount;
131134
} else {
132135
// Fee is added to customer's withdrawal (current behavior)
133136
$withdrawAmount = $mathService->add($amount, $fee);

tests/Infra/Factories/ItemMerchantFeeDeductibleFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ public function definition(): array
2424
];
2525
}
2626
}
27-

tests/Infra/Models/ItemMerchantFeeDeductible.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ItemMerchantFeeDeductible extends Model implements ProductLimitedInt
2424
use HasWallet;
2525

2626
/**
27-
* @var array<int, string>
27+
* @var list<string>
2828
*/
2929
protected $fillable = ['name', 'quantity', 'price'];
3030

@@ -69,4 +69,3 @@ public function getFeePercent(): float
6969
return 5.0;
7070
}
7171
}
72-

tests/Units/Domain/MerchantFeeDeductibleTest.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testPay(): void
3636
// With MerchantFeeDeductible, customer pays only the product price (no fee added)
3737
$productPrice = $product->getAmountProduct($buyer);
3838
$fee = (int) $math->div($math->mul($productPrice, $product->getFeePercent()), 100);
39-
39+
4040
// Customer only needs to deposit the product price
4141
$balance = $productPrice;
4242

@@ -45,7 +45,6 @@ public function testPay(): void
4545

4646
self::assertNotSame(0, $buyer->balanceInt);
4747
$transfer = $buyer->pay($product);
48-
self::assertNotNull($transfer);
4948

5049
$withdraw = $transfer->withdraw;
5150
$deposit = $transfer->deposit;
@@ -73,11 +72,12 @@ public function testPay(): void
7372

7473
public function testGift(): void
7574
{
76-
/**
77-
* @var Buyer $santa
78-
* @var Buyer $child
79-
*/
80-
[$santa, $child] = BuyerFactory::times(2)->create();
75+
/** @var \Illuminate\Database\Eloquent\Collection<int, Buyer> $buyers */
76+
$buyers = BuyerFactory::times(2)->create();
77+
/** @var Buyer $santa */
78+
$santa = $buyers[0];
79+
/** @var Buyer $child */
80+
$child = $buyers[1];
8181
/** @var ItemMerchantFeeDeductible $product */
8282
$product = ItemMerchantFeeDeductibleFactory::new()->create([
8383
'quantity' => 1,
@@ -88,7 +88,7 @@ public function testGift(): void
8888

8989
$productPrice = $product->getAmountProduct($santa);
9090
$fee = (int) $math->div($math->mul($productPrice, $product->getFeePercent()), 100);
91-
91+
9292
// With MerchantFeeDeductible, customer pays only the product price
9393
$balance = $productPrice;
9494

@@ -99,7 +99,6 @@ public function testGift(): void
9999
self::assertNotSame($santa->balanceInt, 0);
100100
self::assertSame($child->balanceInt, 0);
101101
$transfer = $santa->wallet->gift($child, $product);
102-
self::assertNotNull($transfer);
103102

104103
$withdraw = $transfer->withdraw;
105104
$deposit = $transfer->deposit;
@@ -131,11 +130,12 @@ public function testGiftFail(): void
131130
$this->expectExceptionCode(ExceptionInterface::INSUFFICIENT_FUNDS);
132131
$this->expectExceptionMessageStrict(trans('wallet::errors.insufficient_funds'));
133132

134-
/**
135-
* @var Buyer $santa
136-
* @var Buyer $child
137-
*/
138-
[$santa, $child] = BuyerFactory::times(2)->create();
133+
/** @var \Illuminate\Database\Eloquent\Collection<int, Buyer> $buyers */
134+
$buyers = BuyerFactory::times(2)->create();
135+
/** @var Buyer $santa */
136+
$santa = $buyers[0];
137+
/** @var Buyer $child */
138+
$child = $buyers[1];
139139
/** @var ItemMerchantFeeDeductible $product */
140140
$product = ItemMerchantFeeDeductibleFactory::new()->create([
141141
'price' => 200,
@@ -173,12 +173,11 @@ public function testPayWithExactAmount(): void
173173
$buyer->deposit($productPrice);
174174
self::assertSame($buyer->balanceInt, $productPrice);
175175

176-
$transfer = $buyer->pay($product);
177-
self::assertNotNull($transfer);
176+
$buyer->pay($product);
178177

179178
// Customer balance should be 0 after payment
180179
self::assertSame($buyer->balanceInt, 0);
181-
180+
182181
// Merchant should receive product price minus fee
183182
$expectedMerchantAmount = $productPrice - $fee;
184183
self::assertSame($product->balanceInt, $expectedMerchantAmount);
@@ -216,19 +215,17 @@ public function testPayMultiWallet(): void
216215

217216
// Pay from first wallet
218217
$transfer1 = $wallet1->pay($product);
219-
self::assertNotNull($transfer1);
220218
self::assertSame($transfer1->status, Transfer::STATUS_PAID);
221219
self::assertSame($wallet1->balanceInt, 0);
222-
220+
223221
$expectedMerchantAmount1 = $productPrice1 - $fee1;
224222
self::assertSame($product->balanceInt, $expectedMerchantAmount1);
225223

226224
// Pay from second wallet
227225
$transfer2 = $wallet2->pay($product);
228-
self::assertNotNull($transfer2);
229226
self::assertSame($transfer2->status, Transfer::STATUS_PAID);
230227
self::assertSame($wallet2->balanceInt, 0);
231-
228+
232229
$expectedMerchantAmount2 = $productPrice2 - $fee2;
233230
self::assertSame($product->balanceInt, $expectedMerchantAmount1 + $expectedMerchantAmount2);
234231

@@ -247,11 +244,12 @@ public function testPayMultiWallet(): void
247244

248245
public function testTransfer(): void
249246
{
250-
/**
251-
* @var Buyer $from
252-
* @var Buyer $to
253-
*/
254-
[$from, $to] = BuyerFactory::times(2)->create();
247+
/** @var \Illuminate\Database\Eloquent\Collection<int, Buyer> $buyers */
248+
$buyers = BuyerFactory::times(2)->create();
249+
/** @var Buyer $from */
250+
$from = $buyers[0];
251+
/** @var Buyer $to */
252+
$to = $buyers[1];
255253

256254
$math = app(MathServiceInterface::class);
257255
$amount = 100;
@@ -274,22 +272,19 @@ public function testTransfer(): void
274272
// Transfer from buyer to product (merchant)
275273
// With MerchantFeeDeductible, the merchant receives amount minus fee
276274
$transfer = $from->transfer($product, $amount);
277-
self::assertNotNull($transfer);
278275
self::assertSame($transfer->status, Transfer::STATUS_TRANSFER);
279276

280277
// From wallet should be empty
281278
self::assertSame($from->balanceInt, 0);
282-
279+
283280
// Product (merchant) should receive amount minus fee
284281
$expectedMerchantAmount = $amount - $fee;
285282
self::assertSame($product->balanceInt, $expectedMerchantAmount);
286283
self::assertSame((int) $transfer->fee, $fee);
287284

288285
// Transfer back from product to buyer
289-
$transferBack = $product->transfer($from, $product->balanceInt);
290-
self::assertNotNull($transferBack);
286+
$product->transfer($from, $product->balanceInt);
291287
self::assertSame($from->balanceInt, $expectedMerchantAmount);
292288
self::assertSame($product->balanceInt, 0);
293289
}
294290
}
295-

0 commit comments

Comments
 (0)