Applied rector suggestions

This commit is contained in:
Jan Böhmer 2024-06-22 00:31:43 +02:00
parent 4106bcef5f
commit 20f32c7f12
170 changed files with 808 additions and 761 deletions

View file

@ -26,6 +26,7 @@ namespace App\Services\InfoProviderSystem\DTOs;
/**
* This DTO represents a file that can be downloaded from a URL.
* This could be a datasheet, a 3D model, a picture or similar.
* @see \App\Tests\Services\InfoProviderSystem\DTOs\FileDTOTest
*/
class FileDTO
{

View file

@ -26,6 +26,7 @@ namespace App\Services\InfoProviderSystem\DTOs;
/**
* This DTO represents a parameter of a part (similar to the AbstractParameter entity).
* This could be a voltage, a current, a temperature or similar.
* @see \App\Tests\Services\InfoProviderSystem\DTOs\ParameterDTOTest
*/
class ParameterDTO
{
@ -76,7 +77,7 @@ class ParameterDTO
$parts = preg_split('/\s*(\.{3}|~)\s*/', $value);
if (count($parts) === 2) {
//Try to extract number and unit from value (allow leading +)
if (empty($unit)) {
if ($unit === null || trim($unit) === '') {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
} else {
$number = $parts[0];

View file

@ -25,6 +25,7 @@ namespace App\Services\InfoProviderSystem\DTOs;
/**
* This DTO represents a purchase information for a part (supplier name, order number and prices).
* @see \App\Tests\Services\InfoProviderSystem\DTOs\PurchaseInfoDTOTest
*/
class PurchaseInfoDTO
{

View file

@ -27,6 +27,7 @@ use App\Entity\Parts\ManufacturingStatus;
/**
* This DTO represents a search result for a part.
* @see \App\Tests\Services\InfoProviderSystem\DTOs\SearchResultDTOTest
*/
class SearchResultDTO
{

View file

@ -45,6 +45,7 @@ use Doctrine\ORM\EntityManagerInterface;
/**
* This class converts DTOs to entities which can be persisted in the DB
* @see \App\Tests\Services\InfoProviderSystem\DTOtoEntityConverterTest
*/
final class DTOtoEntityConverter
{
@ -127,7 +128,7 @@ final class DTOtoEntityConverter
$entity->setAttachmentType($type);
//If no name is given, try to extract the name from the URL
if (empty($dto->name)) {
if ($dto->name === null || $dto->name === '' || $dto->name === '0') {
$entity->setName($this->getAttachmentNameFromURL($dto->url));
} else {
$entity->setName($dto->name);

View file

@ -27,6 +27,7 @@ use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
/**
* This class keeps track of all registered info providers and allows to find them by their key
* @see \App\Tests\Services\InfoProviderSystem\ProviderRegistryTest
*/
final class ProviderRegistry
{

View file

@ -93,7 +93,7 @@ class DigikeyProvider implements InfoProviderInterface
public function isActive(): bool
{
//The client ID has to be set and a token has to be available (user clicked connect)
return !empty($this->clientId) && $this->authTokenManager->hasToken(self::OAUTH_APP_NAME);
return $this->clientId !== '' && $this->authTokenManager->hasToken(self::OAUTH_APP_NAME);
}
public function searchByKeyword(string $keyword): array
@ -210,7 +210,7 @@ class DigikeyProvider implements InfoProviderInterface
$footprint_name = $parameter['Value'];
}
if (in_array(trim($parameter['Value']), array('', '-'), true)) {
if (in_array(trim((string) $parameter['Value']), ['', '-'], true)) {
continue;
}

View file

@ -65,7 +65,7 @@ class Element14Provider implements InfoProviderInterface
public function isActive(): bool
{
return !empty($this->api_key);
return $this->api_key !== '';
}
/**

View file

@ -96,7 +96,7 @@ class LCSCProvider implements InfoProviderInterface
*/
private function getRealDatasheetUrl(?string $url): string
{
if (!empty($url) && preg_match("/^https:\/\/(datasheet\.lcsc\.com|www\.lcsc\.com\/datasheet)\/.*(C\d+)\.pdf$/", $url, $matches) > 0) {
if ($url !== null && trim($url) !== '' && preg_match("/^https:\/\/(datasheet\.lcsc\.com|www\.lcsc\.com\/datasheet)\/.*(C\d+)\.pdf$/", $url, $matches) > 0) {
$response = $this->lcscClient->request('GET', $url, [
'headers' => [
'Referer' => 'https://www.lcsc.com/product-detail/_' . $matches[2] . '.html'
@ -139,7 +139,7 @@ class LCSCProvider implements InfoProviderInterface
// LCSC does not display LCSC codes in the search, instead taking you directly to the
// detailed product listing. It does so utilizing a product tip field.
// If product tip exists and there are no products in the product list try a detail query
if (count($products) === 0 && !($tipProductCode === null)) {
if (count($products) === 0 && $tipProductCode !== null) {
$result[] = $this->queryDetail($tipProductCode);
}
@ -174,11 +174,11 @@ class LCSCProvider implements InfoProviderInterface
{
// Get product images in advance
$product_images = $this->getProductImages($product['productImages'] ?? null);
$product['productImageUrl'] = $product['productImageUrl'] ?? null;
$product['productImageUrl'] ??= null;
// If the product does not have a product image but otherwise has attached images, use the first one.
if (count($product_images) > 0) {
$product['productImageUrl'] = $product['productImageUrl'] ?? $product_images[0]->url;
$product['productImageUrl'] ??= $product_images[0]->url;
}
// LCSC puts HTML in footprints and descriptions sometimes randomly
@ -321,7 +321,7 @@ class LCSCProvider implements InfoProviderInterface
foreach ($attributes as $attribute) {
//Skip this attribute if it's empty
if (in_array(trim($attribute['paramValueEn']), array('', '-'), true)) {
if (in_array(trim((string) $attribute['paramValueEn']), ['', '-'], true)) {
continue;
}

View file

@ -74,7 +74,7 @@ class MouserProvider implements InfoProviderInterface
public function isActive(): bool
{
return !empty($this->api_key);
return $this->api_key !== '';
}
public function searchByKeyword(string $keyword): array
@ -247,7 +247,7 @@ class MouserProvider implements InfoProviderInterface
private function parseDataSheets(?string $sheetUrl, ?string $sheetName): ?array
{
if (empty($sheetUrl)) {
if ($sheetUrl === null || $sheetUrl === '' || $sheetUrl === '0') {
return null;
}
$result = [];

View file

@ -183,7 +183,7 @@ class OctopartProvider implements InfoProviderInterface
{
//The client ID has to be set and a token has to be available (user clicked connect)
//return /*!empty($this->clientId) && */ $this->authTokenManager->hasToken(self::OAUTH_APP_NAME);
return !empty($this->clientId) && !empty($this->secret);
return $this->clientId !== '' && $this->secret !== '';
}
private function mapLifeCycleStatus(?string $value): ?ManufacturingStatus
@ -243,11 +243,14 @@ class OctopartProvider implements InfoProviderInterface
//If we encounter the mass spec, we save it for later
if ($spec['attribute']['shortname'] === "weight") {
$mass = (float) $spec['siValue'];
} else if ($spec['attribute']['shortname'] === "case_package") { //Package
} elseif ($spec['attribute']['shortname'] === "case_package") {
//Package
$package = $spec['value'];
} else if ($spec['attribute']['shortname'] === "numberofpins") { //Pin Count
} elseif ($spec['attribute']['shortname'] === "numberofpins") {
//Pin Count
$pinCount = $spec['value'];
} else if ($spec['attribute']['shortname'] === "lifecyclestatus") { //LifeCycleStatus
} elseif ($spec['attribute']['shortname'] === "lifecyclestatus") {
//LifeCycleStatus
$mStatus = $this->mapLifeCycleStatus($spec['value']);
}
@ -295,7 +298,7 @@ class OctopartProvider implements InfoProviderInterface
$category = null;
if (!empty($part['category']['name'])) {
$category = implode(' -> ', array_map(static fn($c) => $c['name'], $part['category']['ancestors'] ?? []));
if (!empty($category)) {
if ($category !== '' && $category !== '0') {
$category .= ' -> ';
}
$category .= $part['category']['name'];

View file

@ -47,7 +47,7 @@ class TMEClient
public function isUsable(): bool
{
return !($this->token === '' || $this->secret === '');
return $this->token !== '' && $this->secret !== '';
}

View file

@ -80,7 +80,7 @@ class TMEProvider implements InfoProviderInterface
$result[] = new SearchResultDTO(
provider_key: $this->getProviderKey(),
provider_id: $product['Symbol'],
name: !empty($product['OriginalSymbol']) ? $product['OriginalSymbol'] : $product['Symbol'],
name: empty($product['OriginalSymbol']) ? $product['Symbol'] : $product['OriginalSymbol'],
description: $product['Description'],
category: $product['Category'],
manufacturer: $product['Producer'],
@ -116,7 +116,7 @@ class TMEProvider implements InfoProviderInterface
return new PartDetailDTO(
provider_key: $this->getProviderKey(),
provider_id: $product['Symbol'],
name: !empty($product['OriginalSymbol']) ? $product['OriginalSymbol'] : $product['Symbol'],
name: empty($product['OriginalSymbol']) ? $product['Symbol'] : $product['OriginalSymbol'],
description: $product['Description'],
category: $product['Category'],
manufacturer: $product['Producer'],