chore(lint): resolve remaining PHPCS warnings across domain, infra, shared, and unit tests

This commit is contained in:
Jérémy Munsch 2026-11-16 16:34:21 +01:00
parent 14fd20c303
commit b4384c00a7
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
11 changed files with 142 additions and 187 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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()) {

View file

@ -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<array-key,mixed> $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<array-key,mixed> $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.

View file

@ -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);
}
}

View file

@ -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<string, mixed>,
* } $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'],
);
}

View file

@ -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),
);
}

View file

@ -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 () {

View file

@ -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,
);
}