From db8871110a1cd5b15a6da400adce02d5ac8a0a5a Mon Sep 17 00:00:00 2001 From: Joel Male Date: Sat, 11 Apr 2026 08:12:00 +1000 Subject: [PATCH] refactor: address code quality - readonly props, const fields, void return, rss+xml content type --- src/LaravelGoogleShoppingFeed.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/LaravelGoogleShoppingFeed.php b/src/LaravelGoogleShoppingFeed.php index d0592b2..868c0b0 100644 --- a/src/LaravelGoogleShoppingFeed.php +++ b/src/LaravelGoogleShoppingFeed.php @@ -11,13 +11,13 @@ class LaravelGoogleShoppingFeed { - public string $title = ''; - public string $description = ''; - public string $link = ''; + public readonly string $title; + public readonly string $description; + public readonly string $link; protected array $products = []; - protected array $requiredProductFields = [ + protected const array REQUIRED_PRODUCT_FIELDS = [ 'id', 'link', 'title', @@ -35,17 +35,15 @@ public static function init(string $title = '', string $description = '', string return $feed; } - public function addItem(array $item): bool + public function addItem(array $item): void { - foreach ($this->requiredProductFields as $field) { + foreach (self::REQUIRED_PRODUCT_FIELDS as $field) { if (! isset($item[$field])) { throw new MissingRequiredFieldException("Required field '{$field}' is missing"); } } $this->products[] = $item; - - return true; } public function toXml(): string @@ -70,6 +68,8 @@ public function toXml(): string $xml = ArrayToXml::convert($data, ''); $xml = str_replace([' ', '', '', "\n", "\r", 'remove'], '', $xml); + // spatie/array-to-xml does not support repeated keys, so products are keyed as + // item_0, item_1, etc. and renamed back to tags here. $xml = (string) preg_replace('/item_\d+/', 'item', $xml); return $xml; @@ -77,6 +77,6 @@ public function toXml(): string public function generate(): Response { - return ResponseFacade::make($this->toXml(), 200, ['Content-Type' => 'text/xml']); + return ResponseFacade::make($this->toXml(), headers: ['Content-Type' => 'application/rss+xml']); } }