Skip to content

Commit d521ecf

Browse files
committed
Fixed phpstan, rector, ecs errors
1 parent b259def commit d521ecf

File tree

8 files changed

+29
-27
lines changed

8 files changed

+29
-27
lines changed

src/Internal/Dto/BasketDto.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function total(): int
3939
public function cursor(): Generator
4040
{
4141
foreach ($this->items as $item) {
42-
yield from $item->items();
42+
yield from $item->getItems();
4343
}
4444
}
4545

src/Internal/Dto/ItemDto.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ final class ItemDto implements ItemDtoInterface
1212
public function __construct(
1313
private ProductInterface $product,
1414
private int $quantity,
15-
private int|string|null $price,
15+
private int|string|null $pricePerItem,
1616
) {
1717
}
1818

1919
/**
2020
* @return ProductInterface[]
2121
*/
22-
public function items(): array
22+
public function getItems(): array
2323
{
2424
return array_fill(0, $this->quantity, $this->product);
2525
}
2626

27-
public function getPrice(): int|string|null
27+
public function getPricePerItem(): int|string|null
2828
{
29-
return $this->price;
29+
return $this->pricePerItem;
3030
}
3131

32-
public function product(): ProductInterface
32+
public function getProduct(): ProductInterface
3333
{
3434
return $this->product;
3535
}

src/Internal/Dto/ItemDtoInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface ItemDtoInterface extends Countable
1212
/**
1313
* @return ProductInterface[]
1414
*/
15-
public function items(): array;
15+
public function getItems(): array;
1616

1717
public function count(): int;
1818

19-
public function getPrice(): int|string|null;
19+
public function getPricePerItem(): int|string|null;
2020

21-
public function product(): ProductInterface;
21+
public function getProduct(): ProductInterface;
2222
}

src/Objects/Cart.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ public function withMeta(array $meta): self
4646
return $self;
4747
}
4848

49-
public function withItem(ProductInterface $product, int $quantity = 1, int|string|null $price = null): self
49+
public function withItem(ProductInterface $product, int $quantity = 1, int|string|null $pricePerItem = null): self
5050
{
5151
$self = clone $this;
5252

5353
$productId = $self->productId($product);
54-
if (!isset($self->items[$productId])) {
55-
$self->items[$productId] = [];
56-
}
5754

58-
$self->items[$productId][] = new ItemDto($product, $quantity, $price);
55+
$self->items[$productId] ??= [];
56+
$self->items[$productId][] = new ItemDto($product, $quantity, $pricePerItem);
5957

6058
return $self;
6159
}
@@ -78,7 +76,7 @@ public function getItems(): array
7876
$items = [];
7977
foreach ($this->items as $item) {
8078
foreach ($item as $datum) {
81-
$items[] = $datum->items();
79+
$items[] = $datum->getItems();
8280
}
8381
}
8482

@@ -91,10 +89,12 @@ public function getTotal(Customer $customer): string
9189
$prices = [];
9290
foreach ($this->items as $productId => $_items) {
9391
foreach ($_items as $item) {
94-
$product = $item->product();
95-
$prices[$productId] = $item->getPrice()
92+
$product = $item->getProduct();
93+
$prices[$productId] = $item->getPricePerItem()
9694
?? $prices[$productId]
97-
?? $item->product()->getAmountProduct($customer);
95+
?? $item->getProduct()
96+
->getAmountProduct($customer)
97+
;
9898

9999
$price = $this->math->mul($this->getQuantity($product), $prices[$productId]);
100100
$result = $this->math->add($result, $price);
@@ -111,7 +111,6 @@ public function count(): int
111111

112112
public function getQuantity(ProductInterface $product): int
113113
{
114-
/** @var ItemDtoInterface[] $items */
115114
$quantity = 0;
116115
$items = $this->items[$this->productId($product)] ?? [];
117116
foreach ($items as $item) {
@@ -126,11 +125,13 @@ public function getQuantity(ProductInterface $product): int
126125
*/
127126
public function getBasketDto(): BasketDtoInterface
128127
{
129-
if ($this->items === []) {
128+
$items = array_merge(...array_values($this->items));
129+
130+
if ($items === []) {
130131
throw new CartEmptyException('Cart is empty', ExceptionInterface::CART_EMPTY);
131132
}
132133

133-
return new BasketDto(array_merge(...array_values($this->items)), $this->getMeta());
134+
return new BasketDto($items, $this->getMeta());
134135
}
135136

136137
private function productId(ProductInterface $product): string

src/Services/BasketService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function availability(AvailabilityDtoInterface $availabilityDto): bool
1414
$basketDto = $availabilityDto->getBasketDto();
1515
$customer = $availabilityDto->getCustomer();
1616
foreach ($basketDto->items() as $itemDto) {
17-
$product = $itemDto->product();
17+
$product = $itemDto->getProduct();
1818
if ($product instanceof ProductLimitedInterface && !$product->canBuy(
1919
$customer,
2020
$itemDto->count(),

src/Services/PurchaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool
2323
$arrays = [];
2424
$query = $customer->transfers();
2525
foreach ($basketDto->items() as $itemDto) {
26-
$wallet = $this->castService->getWallet($itemDto->product());
26+
$wallet = $this->castService->getWallet($itemDto->getProduct());
2727

2828
/**
2929
* As part of my work, "with" was added, it gives a 50x boost for a huge number of returns. In this case,

src/Traits/CartPay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ public function payCart(CartInterface $cart, bool $force = false): array
121121
$prepareService = app(PrepareServiceInterface::class);
122122
$assistantService = app(AssistantServiceInterface::class);
123123
foreach ($cart->getBasketDto()->items() as $item) {
124-
foreach ($item->items() as $_item) {
125-
$product = $item->product();
124+
foreach ($item->getItems() as $_item) {
125+
$product = $item->getProduct();
126126
$transfers[] = $prepareService->transferLazy(
127127
$this,
128128
$product,
129129
Transfer::STATUS_PAID,
130-
$item->getPrice() ?? $product->getAmountProduct($this),
130+
$item->getPricePerItem() ?? $product->getAmountProduct($this),
131131
$assistantService->getMeta($basketDto, $product)
132132
);
133133
}

tests/Units/Domain/ProductTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ public function testPayCustomPrice(): void
277277
self::assertSame(1_000, $buyer->balanceInt);
278278

279279
$cart = app(Cart::class)
280-
->withItem($product, price: 1_000);
280+
->withItem($product, pricePerItem: 1_000)
281+
;
281282

282283
$transfers = $buyer->payCart($cart);
283284
self::assertCount(1, $transfers);

0 commit comments

Comments
 (0)