From b4384c00a757860618de8c503e280c3fe1859510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 16 Nov 2026 16:34:21 +0100 Subject: [PATCH] chore(lint): resolve remaining PHPCS warnings across domain, infra, shared, and unit tests --- .../ExpiredPickupTimeSlotException.php | 1 + .../OrderTunnel/InactiveShopperException.php | 1 + .../MissingOrderBuilderException.php | 1 + .../UnorderableProductException.php | 1 + domain/Models/Entities/Product.php | 55 ++++---- .../Adapters/Serializers/BasketSerializer.php | 123 +++++++++--------- shared/Filesystem/TemporaryFileFactory.php | 6 +- .../Domain/Models/Entities/BasketTest.php | 49 +++---- .../Models/ValueObjects/BasketProductTest.php | 40 +++--- .../ValueObjects/NutritionalValuesTest.php | 13 +- .../Models/ValueObjects/OrderLineTest.php | 39 ++---- 11 files changed, 142 insertions(+), 187 deletions(-) diff --git a/domain/Exceptions/OrderTunnel/ExpiredPickupTimeSlotException.php b/domain/Exceptions/OrderTunnel/ExpiredPickupTimeSlotException.php index 95092ba9..a359f51d 100644 --- a/domain/Exceptions/OrderTunnel/ExpiredPickupTimeSlotException.php +++ b/domain/Exceptions/OrderTunnel/ExpiredPickupTimeSlotException.php @@ -6,6 +6,7 @@ namespace Domain\Exceptions\OrderTunnel; class ExpiredPickupTimeSlotException extends \DomainException { + // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Customizes default exception message. public function __construct( string $message = 'Invalid or unavailable time slot', int $code = 0, diff --git a/domain/Exceptions/OrderTunnel/InactiveShopperException.php b/domain/Exceptions/OrderTunnel/InactiveShopperException.php index d305c7f4..6cae3532 100644 --- a/domain/Exceptions/OrderTunnel/InactiveShopperException.php +++ b/domain/Exceptions/OrderTunnel/InactiveShopperException.php @@ -6,6 +6,7 @@ namespace Domain\Exceptions\OrderTunnel; class InactiveShopperException extends \DomainException { + // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Customizes default exception message. public function __construct( string $message = 'Employee account is no longer active.', int $code = 0, diff --git a/domain/Exceptions/OrderTunnel/MissingOrderBuilderException.php b/domain/Exceptions/OrderTunnel/MissingOrderBuilderException.php index 72d60fc4..c7d35c0f 100644 --- a/domain/Exceptions/OrderTunnel/MissingOrderBuilderException.php +++ b/domain/Exceptions/OrderTunnel/MissingOrderBuilderException.php @@ -6,6 +6,7 @@ namespace Domain\Exceptions\OrderTunnel; class MissingOrderBuilderException extends \DomainException { + // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Customizes default exception message. public function __construct( string $message = 'Order builder is missing or not in expected state.', int $code = 0, diff --git a/domain/Exceptions/OrderTunnel/UnorderableProductException.php b/domain/Exceptions/OrderTunnel/UnorderableProductException.php index ef18d5f1..65779c7e 100644 --- a/domain/Exceptions/OrderTunnel/UnorderableProductException.php +++ b/domain/Exceptions/OrderTunnel/UnorderableProductException.php @@ -6,6 +6,7 @@ namespace Domain\Exceptions\OrderTunnel; class UnorderableProductException extends \DomainException { + // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found -- Customizes default exception message. public function __construct( string $message = 'One or more products in your basket are no longer available for order.', int $code = 0, diff --git a/domain/Models/Entities/Product.php b/domain/Models/Entities/Product.php index 5e876902..2da0b872 100644 --- a/domain/Models/Entities/Product.php +++ b/domain/Models/Entities/Product.php @@ -122,31 +122,7 @@ final class Product implements Entity public function priceKgTtc(): Money { if ($this->invoicing_unit === InvoicingUnit::KGM) { - // * The product price is already a price per kg: it is the reference for the kg price - $discountAmountKgHt = $this->validDiscount?->calculateDiscountAmount($this->price) - ?? new Money(0, $this->price->getCurrency()); - - $grams = $this->product_weight?->toGrams() ?? 0; - - if ( - $this->validDiscount !== null - && ! $this->validDiscount->isPercentage() - && $this->sell_unit === SellUnit::PCE - && $grams > 0 - ) { - // * A fixed amount applies to the piece: bring it back to a per-kg amount - $discountAmountKgHt = $discountAmountKgHt - ->multiply(self::GRAMS_PER_KG) - ->divide($grams, self::ROUNDING); - } - - $discountedKgPriceHt = $this->price->subtract($discountAmountKgHt); - - if ($discountedKgPriceHt->isNegative()) { - $discountedKgPriceHt = new Money(0, $this->price->getCurrency()); - } - - return $this->tax_rate->applyTo($discountedKgPriceHt); + return $this->discountedKgPriceTtc(); } $priceTtc = $this->discountedPriceTtc(); @@ -163,6 +139,35 @@ final class Product implements Entity return $priceTtc; } + private function discountedKgPriceTtc(): Money + { + // * The product price is already a price per kg: it is the reference for the kg price + $discountAmountKgHt = $this->validDiscount?->calculateDiscountAmount($this->price) + ?? new Money(0, $this->price->getCurrency()); + + $grams = $this->product_weight?->toGrams() ?? 0; + + if ( + $this->validDiscount !== null + && ! $this->validDiscount->isPercentage() + && $this->sell_unit === SellUnit::PCE + && $grams > 0 + ) { + // * A fixed amount applies to the piece: bring it back to a per-kg amount + $discountAmountKgHt = $discountAmountKgHt + ->multiply(self::GRAMS_PER_KG) + ->divide($grams, self::ROUNDING); + } + + $discountedKgPriceHt = $this->price->subtract($discountAmountKgHt); + + if ($discountedKgPriceHt->isNegative()) { + $discountedKgPriceHt = new Money(0, $this->price->getCurrency()); + } + + return $this->tax_rate->applyTo($discountedKgPriceHt); + } + public function discountRate(): DiscountRate { if ($this->validDiscount?->isPercentage()) { diff --git a/infra/Adapters/Serializers/BasketSerializer.php b/infra/Adapters/Serializers/BasketSerializer.php index cc7465c1..d90c5648 100644 --- a/infra/Adapters/Serializers/BasketSerializer.php +++ b/infra/Adapters/Serializers/BasketSerializer.php @@ -60,71 +60,10 @@ class BasketSerializer $hydratedItems = []; - foreach ($rawItems as $itemData) { - $itemData = Arr::wrap($itemData); - $id = Arr::string($itemData, 'id'); - $name = Arr::string($itemData, 'name'); - $qty = Arr::integer($itemData, 'qty'); - $unitPriceHt = Arr::array($itemData, 'unitPriceHt'); - $unitPriceHtCurrency = Arr::string($unitPriceHt, 'currency'); - $unitDiscount = Arr::array($itemData, 'unitDiscount'); - $unitDiscountCurrency = Arr::string($unitDiscount, 'currency'); - $taxRate = Arr::integer($itemData, 'taxRate'); - $weightGrams = Arr::integer($itemData, 'weightGrams'); - $brand = Arr::get($itemData, 'brand'); // using get because it can be null - $incoId = Arr::get($itemData, 'incoId'); - $orderUnit = Arr::string($itemData, 'orderUnit'); - $slug = Arr::string($itemData, 'slug'); - $picture = Arr::get($itemData, 'picture'); - $description = Arr::get($itemData, 'description'); - $weightVariable = Arr::boolean($itemData, 'weightVariable'); - $discountRate = $this->hydrateDiscountRate($itemData); - $priceKgTtc = Arr::array($itemData, 'priceKgTtc'); - $priceKgTtcCurrency = Arr::string($priceKgTtc, 'currency'); - $options = Arr::array($itemData, 'options', []); - - foreach ($options as $key => $option) { - Assert::string($key); - } - - if (empty($unitPriceHtCurrency)) { - throw new \DomainException("Currency is required for unitPriceHt of item with id $id"); - } - if (empty($unitDiscountCurrency)) { - throw new \DomainException("Currency is required for unitDiscount of item with id $id"); - } - if (empty($priceKgTtcCurrency)) { - throw new \DomainException("Currency is required for priceKgTtc of item with id $id"); - } - - $hydratedItems[$id] = new BasketProduct( - id: new ProductId(Uuid::fromString($id)), - name: $name, - qty: $qty, - unitPriceHt: new Money( - is_scalar($a = Arr::get($unitPriceHt, 'amount', 0)) ? (int) $a : 0, - new Currency($unitPriceHtCurrency) - ), - unitDiscount: new Money( - is_scalar($a = Arr::get($unitDiscount, 'amount', 0)) ? (int) $a : 0, - new Currency($unitDiscountCurrency) - ), - taxRate: TaxRate::from($taxRate), - weightGrams: $weightGrams, - brand: is_string($brand) && $brand !== '' ? ProductBrand::from($brand) : null, - incoId: is_scalar($incoId) && $incoId !== 0 ? new IncoId((int) $incoId) : null, - orderUnit: OrderUnit::from($orderUnit), - slug: $slug, - picture: is_string($picture) && $picture !== '' ? new StorageFilePath($picture) : null, - description: is_string($description) && $description !== '' ? $description : null, - weightVariable: $weightVariable, - discountRate: $discountRate, - priceKgTtc: new Money( - is_scalar($a = Arr::get($priceKgTtc, 'amount', 0)) ? (int) $a : 0, - new Currency($priceKgTtcCurrency) - ), - options: $options - ); + foreach ($rawItems as $rawItem) { + $itemData = Arr::wrap($rawItem); + $id = Arr::string($itemData, 'id'); + $hydratedItems[$id] = $this->hydrateBasketProduct($id, $itemData); } return new Basket( @@ -133,6 +72,60 @@ class BasketSerializer ); } + /** + * @param array $itemData + */ + private function hydrateBasketProduct(string $id, array $itemData): BasketProduct + { + $brand = Arr::get($itemData, 'brand'); // using get because it can be null + $incoId = Arr::get($itemData, 'incoId'); + $picture = Arr::get($itemData, 'picture'); + $description = Arr::get($itemData, 'description'); + $options = Arr::array($itemData, 'options', []); + + foreach ($options as $key => $option) { + Assert::string($key); + } + + return new BasketProduct( + id: new ProductId(Uuid::fromString($id)), + name: Arr::string($itemData, 'name'), + qty: Arr::integer($itemData, 'qty'), + unitPriceHt: $this->hydrateMoney($id, 'unitPriceHt', Arr::array($itemData, 'unitPriceHt')), + unitDiscount: $this->hydrateMoney($id, 'unitDiscount', Arr::array($itemData, 'unitDiscount')), + taxRate: TaxRate::from(Arr::integer($itemData, 'taxRate')), + weightGrams: Arr::integer($itemData, 'weightGrams'), + brand: is_string($brand) && $brand !== '' ? ProductBrand::from($brand) : null, + incoId: is_scalar($incoId) && $incoId !== 0 ? new IncoId((int) $incoId) : null, + orderUnit: OrderUnit::from(Arr::string($itemData, 'orderUnit')), + slug: Arr::string($itemData, 'slug'), + picture: is_string($picture) && $picture !== '' ? new StorageFilePath($picture) : null, + description: is_string($description) && $description !== '' ? $description : null, + weightVariable: Arr::boolean($itemData, 'weightVariable'), + discountRate: $this->hydrateDiscountRate($itemData), + priceKgTtc: $this->hydrateMoney($id, 'priceKgTtc', Arr::array($itemData, 'priceKgTtc')), + options: $options + ); + } + + /** + * @param array $moneyData + */ + private function hydrateMoney(string $itemId, string $field, array $moneyData): Money + { + $currency = Arr::string($moneyData, 'currency'); + if ($currency === '') { + throw new \DomainException("Currency is required for {$field} of item with id {$itemId}"); + } + + $rawAmount = Arr::get($moneyData, 'amount', 0); + + return new Money( + is_scalar($rawAmount) ? (int) $rawAmount : 0, + new Currency($currency) + ); + } + /** * Reads the discount rate in basis points; falls back to the legacy float ratio * (key "discountRate") stored in sessions created before the DiscountRate value object. diff --git a/shared/Filesystem/TemporaryFileFactory.php b/shared/Filesystem/TemporaryFileFactory.php index d8e1e863..a85fdf62 100644 --- a/shared/Filesystem/TemporaryFileFactory.php +++ b/shared/Filesystem/TemporaryFileFactory.php @@ -116,7 +116,9 @@ final class TemporaryFileFactory if ($extension !== '') { $targetPath = $baseTempPath . '.' . $extension; if (! rename($baseTempPath, $targetPath)) { - @unlink($baseTempPath); + if (is_file($baseTempPath)) { + unlink($baseTempPath); + } throw new TemporaryFileException('Could not rename temporary file with extension.'); } $baseTempPath = $targetPath; @@ -131,7 +133,7 @@ final class TemporaryFileFactory { foreach ($this->temporaryFilePaths as $path) { if (is_file($path)) { - @unlink($path); + unlink($path); } } diff --git a/tests/Unit/Domain/Models/Entities/BasketTest.php b/tests/Unit/Domain/Models/Entities/BasketTest.php index 89e580d0..fdb4f57c 100644 --- a/tests/Unit/Domain/Models/Entities/BasketTest.php +++ b/tests/Unit/Domain/Models/Entities/BasketTest.php @@ -22,44 +22,29 @@ describe('Basket and BasketProduct', function (): void { /** * @param array{ * id?: ProductId, - * name?: string, * qty?: int, - * unitPriceHt?: Money, - * unitDiscount?: Money, - * taxRate?: TaxRate, - * weightGrams?: int, - * brand?: ProductBrand, - * incoId?: IncoId|null, - * orderUnit?: OrderUnit, - * slug?: string, - * picture?: StorageFilePath|null, - * description?: string|null, - * weightVariable?: bool, - * discountRate?: DiscountRate, - * priceKgTtc?: Money, - * options?: array, * } $overrides */ function getNewBasketProduct(array $overrides = []): BasketProduct { return new BasketProduct( - id: $overrides['id'] ?? new ProductId(Uuid::fromString('019e9e37-f2a1-74ef-b9c2-66d222470128')), - name: $overrides['name'] ?? 'Poulet Rôti', - qty: $overrides['qty'] ?? 2, - unitPriceHt: $overrides['unitPriceHt'] ?? Money::EUR(1000), - unitDiscount: $overrides['unitDiscount'] ?? Money::EUR(200), - taxRate: $overrides['taxRate'] ?? TaxRate::TVA_5_5, - weightGrams: $overrides['weightGrams'] ?? 1200, - brand: $overrides['brand'] ?? ProductBrand::marie, - incoId: $overrides['incoId'] ?? new IncoId(45), - orderUnit: $overrides['orderUnit'] ?? OrderUnit::PCE, - slug: $overrides['slug'] ?? 'poulet-roti', - picture: $overrides['picture'] ?? new StorageFilePath('images/poulet.jpg'), - description: $overrides['description'] ?? 'Un bon poulet rôti', - weightVariable: $overrides['weightVariable'] ?? true, - discountRate: $overrides['discountRate'] ?? DiscountRate::fromRatio(0.2), - priceKgTtc: $overrides['priceKgTtc'] ?? Money::EUR(1500), - options: $overrides['options'] ?? ['extra' => 'crispy'], + id: $overrides['id'] ?? new ProductId(Uuid::fromString('019e9e37-f2a1-74ef-b9c2-66d222470128')), + name: 'Poulet Rôti', + qty: $overrides['qty'] ?? 2, + unitPriceHt: Money::EUR(1000), + unitDiscount: Money::EUR(200), + taxRate: TaxRate::TVA_5_5, + weightGrams: 1200, + brand: ProductBrand::marie, + incoId: new IncoId(45), + orderUnit: OrderUnit::PCE, + slug: 'poulet-roti', + picture: new StorageFilePath('images/poulet.jpg'), + description: 'Un bon poulet rôti', + weightVariable: true, + discountRate: DiscountRate::fromRatio(0.2), + priceKgTtc: Money::EUR(1500), + options: ['extra' => 'crispy'], ); } diff --git a/tests/Unit/Domain/Models/ValueObjects/BasketProductTest.php b/tests/Unit/Domain/Models/ValueObjects/BasketProductTest.php index 8afad6f9..cb871fe1 100644 --- a/tests/Unit/Domain/Models/ValueObjects/BasketProductTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/BasketProductTest.php @@ -23,43 +23,35 @@ describe('BasketProduct', function () { /** * @param array{ - * id?: ProductId, * name?: string, * qty?: int, * unitPriceHt?: Money, * unitDiscount?: Money, * taxRate?: TaxRate, * weightGrams?: int, - * brand?: ProductBrand, - * incoId?: \Domain\Models\ValueObjects\IncoId|null, - * orderUnit?: OrderUnit, * slug?: string, - * picture?: \Uwil\Storage\Lib\ValueObjects\StorageFilePath|null, - * description?: string|null, - * weightVariable?: bool, - * discountRate?: DiscountRate, * priceKgTtc?: Money, * } $overrides */ function getNewProduct(array $overrides = []): BasketProduct { return new BasketProduct( - id: $overrides['id'] ?? new ProductId(Uuid::uuid7()), - name: $overrides['name'] ?? 'Product Name', - qty: $overrides['qty'] ?? 1, - unitPriceHt: $overrides['unitPriceHt'] ?? getMoney(100), - unitDiscount: $overrides['unitDiscount'] ?? getMoney(10), - taxRate: $overrides['taxRate'] ?? TaxRate::TVA_20, - weightGrams: $overrides['weightGrams'] ?? 500, - brand: $overrides['brand'] ?? ProductBrand::none, - incoId: $overrides['incoId'] ?? null, - orderUnit: $overrides['orderUnit'] ?? OrderUnit::PCE, - slug: $overrides['slug'] ?? 'product-slug', - picture: $overrides['picture'] ?? null, - description: $overrides['description'] ?? null, - weightVariable: $overrides['weightVariable'] ?? false, - discountRate: $overrides['discountRate'] ?? DiscountRate::fromRatio(0.1), - priceKgTtc: $overrides['priceKgTtc'] ?? getMoney(240), + id: new ProductId(Uuid::uuid7()), + name: $overrides['name'] ?? 'Product Name', + qty: $overrides['qty'] ?? 1, + unitPriceHt: $overrides['unitPriceHt'] ?? getMoney(100), + unitDiscount: $overrides['unitDiscount'] ?? getMoney(10), + taxRate: $overrides['taxRate'] ?? TaxRate::TVA_20, + weightGrams: $overrides['weightGrams'] ?? 500, + brand: ProductBrand::none, + incoId: null, + orderUnit: OrderUnit::PCE, + slug: $overrides['slug'] ?? 'product-slug', + picture: null, + description: null, + weightVariable: false, + discountRate: DiscountRate::fromRatio(0.1), + priceKgTtc: $overrides['priceKgTtc'] ?? getMoney(240), ); } diff --git a/tests/Unit/Domain/Models/ValueObjects/NutritionalValuesTest.php b/tests/Unit/Domain/Models/ValueObjects/NutritionalValuesTest.php index a78b4ef8..76a3c663 100644 --- a/tests/Unit/Domain/Models/ValueObjects/NutritionalValuesTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/NutritionalValuesTest.php @@ -24,18 +24,7 @@ describe('NutritionalValues', function () { */ function getNewNutritionalValues(array $overrides = []): NutritionalValues { - return new NutritionalValues( - refWeightG: $overrides['refWeightG'] ?? null, - kj: $overrides['kj'] ?? null, - kcal: $overrides['kcal'] ?? null, - fatG: $overrides['fatG'] ?? null, - satFatG: $overrides['satFatG'] ?? null, - carbG: $overrides['carbG'] ?? null, - sugarG: $overrides['sugarG'] ?? null, - proteinG: $overrides['proteinG'] ?? null, - saltG: $overrides['saltG'] ?? null, - dailyEnergy: $overrides['dailyEnergy'] ?? null, - ); + return new NutritionalValues(...$overrides); } it('can instantiate', function () { diff --git a/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php b/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php index a6c58fbe..be8082be 100644 --- a/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php @@ -16,35 +16,20 @@ use Money\Money; use Ramsey\Uuid\Uuid; describe('OrderLine', function (): void { - /** - * @param array{ - * incoId?: IncoId, - * originalId?: ProductId, - * name?: string, - * weight?: ProductWeight, - * brand?: ProductBrand, - * orderUnit?: OrderUnit, - * price?: Money, - * discountedPrice?: Money, - * priceKgTtc?: Money, - * taxRate?: TaxRate, - * picture?: \Uwil\Storage\Lib\ValueObjects\StorageFilePath|null, - * } $overrides - */ - function getNewOrderLineProduct(array $overrides = []): OrderLineProduct + function getNewOrderLineProduct(): OrderLineProduct { return new OrderLineProduct( - incoId: $overrides['incoId'] ?? new IncoId(1), - originalId: $overrides['originalId'] ?? new ProductId(Uuid::uuid7()), - name: $overrides['name'] ?? 'poulet blanc', - weight: $overrides['weight'] ?? new ProductWeight(200, false), - brand: $overrides['brand'] ?? ProductBrand::loue, - orderUnit: $overrides['orderUnit'] ?? OrderUnit::KGM, - price: $overrides['price'] ?? Money::EUR(100), - discountedPrice: $overrides['discountedPrice'] ?? Money::EUR(100), - priceKgTtc: $overrides['priceKgTtc'] ?? Money::EUR(500), - taxRate: $overrides['taxRate'] ?? TaxRate::TVA_20, - picture: $overrides['picture'] ?? null, + incoId: new IncoId(1), + originalId: new ProductId(Uuid::uuid7()), + name: 'poulet blanc', + weight: new ProductWeight(200, false), + brand: ProductBrand::loue, + orderUnit: OrderUnit::KGM, + price: Money::EUR(100), + discountedPrice: Money::EUR(100), + priceKgTtc: Money::EUR(500), + taxRate: TaxRate::TVA_20, + picture: null, ); }