Add suggested improvements from SonarQube

This commit is contained in:
Marc Kreidler 2025-12-17 18:01:11 +01:00
parent bbd551e625
commit 87c7402414

View file

@ -370,8 +370,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
*/ */
private function getProductImages(?array $images): array private function getProductImages(?array $images): array
{ {
if (!is_array($images)) if (!is_array($images)) {
return []; return [];
}
// 1) Only real image entries with URL // 1) Only real image entries with URL
$imgs = array_values(array_filter($images, fn($i) => is_array($i) && !empty($i['url']))); $imgs = array_values(array_filter($images, fn($i) => is_array($i) && !empty($i['url'])));
@ -395,8 +396,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
if (!str_starts_with($url, 'http://') && !str_starts_with($url, 'https://')) { if (!str_starts_with($url, 'http://') && !str_starts_with($url, 'https://')) {
$url = 'https://www.buerklin.com' . $url; $url = 'https://www.buerklin.com' . $url;
} }
if (!filter_var($url, FILTER_VALIDATE_URL)) if (!filter_var($url, FILTER_VALIDATE_URL)) {
continue; continue;
}
$byUrl[$url] = $url; $byUrl[$url] = $url;
} }
@ -412,12 +414,14 @@ class BuerklinProvider implements BatchInfoProviderInterface
$out = []; $out = [];
foreach ($features as $f) { foreach ($features as $f) {
if (!is_array($f)) if (!is_array($f)) {
continue; continue;
}
$name = $f['name'] ?? null; $name = $f['name'] ?? null;
if (!is_string($name) || trim($name) === '') if (!is_string($name) || trim($name) === '') {
continue; continue;
}
$vals = []; $vals = [];
foreach (($f['featureValues'] ?? []) as $fv) { foreach (($f['featureValues'] ?? []) as $fv) {
@ -425,8 +429,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
$vals[] = trim($fv['value']); $vals[] = trim($fv['value']);
} }
} }
if (count($vals) === 0) if (empty($vals)) {
continue; continue;
}
// Multiple values: join with comma // Multiple values: join with comma
$value = implode(', ', array_values(array_unique($vals))); $value = implode(', ', array_values(array_unique($vals)));
@ -462,29 +467,32 @@ class BuerklinProvider implements BatchInfoProviderInterface
public function searchByKeyword(string $keyword): array public function searchByKeyword(string $keyword): array
{ {
$keyword = strtoupper(trim($keyword)); $keyword = strtoupper(trim($keyword));
if ($keyword === '') { if (!empty($keyword)) {
return [];
$response = $this->makeAPICall('/products/search/', [
'pageSize' => 50,
'currentPage' => 0,
'query' => $keyword,
'sort' => 'relevance',
]);
$products = $response['products'] ?? [];
// Normal case: products found in search results
if (is_array($products) && !empty($products)) {
return array_map(fn($p) => $this->getPartDetail($p), $products);
}
// Fallback: try direct lookup by code
try {
$product = $this->getProduct($keyword);
return [$this->getPartDetail($product)];
} catch (\Throwable $e) {
return [];
}
} }
else
$response = $this->makeAPICall('/products/search/', [ {
'pageSize' => 50,
'currentPage' => 0,
'query' => $keyword,
'sort' => 'relevance',
]);
$products = $response['products'] ?? [];
// Normal case: products found in search results
if (is_array($products) && count($products) > 0) {
return array_map(fn($p) => $this->getPartDetail($p), $products);
}
// Fallback: try direct lookup by code
try {
$product = $this->getProduct($keyword);
return [$this->getPartDetail($product)];
} catch (\Throwable $e) {
return []; return [];
} }
} }
@ -502,7 +510,7 @@ class BuerklinProvider implements BatchInfoProviderInterface
return [ return [
ProviderCapabilities::BASIC, ProviderCapabilities::BASIC,
ProviderCapabilities::PICTURE, ProviderCapabilities::PICTURE,
//ProviderCapabilities::DATASHEET, // currently not implemented //ProviderCapabilities::DATASHEET, // currently not implemented
ProviderCapabilities::PRICE, ProviderCapabilities::PRICE,
ProviderCapabilities::FOOTPRINT, ProviderCapabilities::FOOTPRINT,
]; ];
@ -513,8 +521,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
$params = []; $params = [];
$add = function (string $name, $value) use (&$params, $group) { $add = function (string $name, $value) use (&$params, $group) {
if ($value === null) if ($value === null) {
return; return;
}
if (is_bool($value)) { if (is_bool($value)) {
$value = $value ? 'Yes' : 'No'; $value = $value ? 'Yes' : 'No';
@ -523,8 +532,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
return; return;
} else { } else {
$value = trim((string) $value); $value = trim((string) $value);
if ($value === '') if ($value === '') {
return; return;
}
} }
$params[] = ParameterDTO::parseValueField( $params[] = ParameterDTO::parseValueField(