Fix issues reported by static code analyzers

This commit is contained in:
Sebastian Meyer 2024-07-13 19:37:14 +02:00
parent 535f9e813e
commit e6ea34c151
9 changed files with 37 additions and 19 deletions

View File

@ -56,5 +56,5 @@ try {
} catch (Exception $exception) { } catch (Exception $exception) {
echo '[ERROR] Exception ' . $exception->getCode() . ' thrown:' . PHP_EOL; echo '[ERROR] Exception ' . $exception->getCode() . ' thrown:' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL; echo $exception->getMessage() . PHP_EOL;
return 1; exit(1);
} }

View File

@ -71,8 +71,10 @@ abstract class Console extends Command
switch ($unit) { switch ($unit) {
case 'g': case 'g':
$limit *= 1024; $limit *= 1024;
// no break
case 'm': case 'm':
$limit *= 1024; $limit *= 1024;
// no break
case 'k': case 'k':
$limit *= 1024; $limit *= 1024;
} }

View File

@ -91,7 +91,9 @@ class AddRecordCommand extends Console
/** @var string */ /** @var string */
$identifier = $input->getArgument('identifier'); $identifier = $input->getArgument('identifier');
/** @var Format */ /** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $input->getArgument('format')); $format = Database::getInstance()
->getEntityManager()
->getReference(Format::class, $input->getArgument('format'));
/** @var string */ /** @var string */
$file = $input->getArgument('file'); $file = $input->getArgument('file');
/** @var string[] */ /** @var string[] */
@ -105,7 +107,9 @@ class AddRecordCommand extends Console
} }
foreach ($sets as $set) { foreach ($sets as $set) {
/** @var Set */ /** @var Set */
$setSpec = Database::getInstance()->getEntityManager()->getReference(Set::class, $set); $setSpec = Database::getInstance()
->getEntityManager()
->getReference(Set::class, $set);
$record->addSet($setSpec); $record->addSet($setSpec);
} }

View File

@ -125,7 +125,9 @@ class CsvImportCommand extends Console
/** @var array<string, string> */ /** @var array<string, string> */
$arguments = $input->getArguments(); $arguments = $input->getArguments();
/** @var Format */ /** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $arguments['format']); $format = Database::getInstance()
->getEntityManager()
->getReference(Format::class, $arguments['format']);
/** @var bool */ /** @var bool */
$noValidation = $input->getOption('noValidation'); $noValidation = $input->getOption('noValidation');
/** @var resource */ /** @var resource */
@ -152,7 +154,9 @@ class CsvImportCommand extends Console
$sets = $row[$columns['setColumn']]; $sets = $row[$columns['setColumn']];
foreach (explode(',', $sets) as $set) { foreach (explode(',', $sets) as $set) {
/** @var Set */ /** @var Set */
$setSpec = Database::getInstance()->getEntityManager()->getReference(Set::class, trim($set)); $setSpec = Database::getInstance()
->getEntityManager()
->getReference(Set::class, trim($set));
$record->addSet($setSpec); $record->addSet($setSpec);
} }
} }
@ -164,7 +168,9 @@ class CsvImportCommand extends Console
// Flush to database if memory usage reaches 50% or every 10.000 records. // Flush to database if memory usage reaches 50% or every 10.000 records.
if ((memory_get_usage() / $phpMemoryLimit) > 0.5 || ($count % 10000) === 0) { if ((memory_get_usage() / $phpMemoryLimit) > 0.5 || ($count % 10000) === 0) {
$progressIndicator->setMessage('Importing... ' . (string) $count . ' records processed. Flushing to database...'); $progressIndicator->setMessage(
'Importing... ' . (string) $count . ' records processed. Flushing to database...'
);
Database::getInstance()->flush([Record::class]); Database::getInstance()->flush([Record::class]);
} }
} }
@ -211,7 +217,7 @@ class CsvImportCommand extends Console
'', '',
sprintf( sprintf(
' [ERROR] File "%s" does not contain valid CSV. ', ' [ERROR] File "%s" does not contain valid CSV. ',
stream_get_meta_data($file)['uri'] stream_get_meta_data($file)['uri'] ?? 'unknown'
), ),
'' ''
]); ]);
@ -228,7 +234,7 @@ class CsvImportCommand extends Console
'', '',
sprintf( sprintf(
' [ERROR] File "%s" does not contain valid CSV. ', ' [ERROR] File "%s" does not contain valid CSV. ',
stream_get_meta_data($file)['uri'] stream_get_meta_data($file)['uri'] ?? 'unknown'
), ),
'' ''
]); ]);

View File

@ -56,7 +56,9 @@ class UpdateFormatsCommand extends Console
{ {
$formats = Configuration::getInstance()->metadataPrefix; $formats = Configuration::getInstance()->metadataPrefix;
$this->clearResultCache(); $this->clearResultCache();
$inDatabase = Database::getInstance()->getMetadataFormats()->getQueryResult(); $inDatabase = Database::getInstance()
->getMetadataFormats()
->getQueryResult();
$added = 0; $added = 0;
$deleted = 0; $deleted = 0;
$failure = false; $failure = false;

View File

@ -322,8 +322,7 @@ class Database
?DateTime $from = null, ?DateTime $from = null,
?DateTime $until = null, ?DateTime $until = null,
?Set $set = null ?Set $set = null
): Result ): Result {
{
$maxRecords = Configuration::getInstance()->maxRecords; $maxRecords = Configuration::getInstance()->maxRecords;
$cursor = $counter * $maxRecords; $cursor = $counter * $maxRecords;

View File

@ -107,8 +107,10 @@ abstract class Entity
new Assert\NotBlank() new Assert\NotBlank()
] ]
); );
if ($violations->count() > 0 if (
or simplexml_load_string($xml) === false) { $violations->count() > 0
or simplexml_load_string($xml) === false
) {
throw new ValidationFailedException(null, $violations); throw new ValidationFailedException(null, $violations);
} }
return $xml; return $xml;

View File

@ -201,7 +201,10 @@ class Set extends Entity
public function __construct(string $spec, ?string $name = null, string $description = null) public function __construct(string $spec, ?string $name = null, string $description = null)
{ {
try { try {
$this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/'); $this->spec = $this->validateRegEx(
$spec,
'/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/'
);
$this->setName($name); $this->setName($name);
$this->setDescription($description); $this->setDescription($description);
$this->records = new ArrayCollection(); $this->records = new ArrayCollection();