mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-13 21:59:34 +00:00
Applied rector with PHP8.1 migration rules
This commit is contained in:
parent
dc6a67c2f0
commit
7ee01d9a05
303 changed files with 1228 additions and 3465 deletions
|
|
@ -87,7 +87,7 @@ class PKDatastructureImporter
|
|||
|
||||
$this->em->flush();
|
||||
|
||||
return count($distributor_data);
|
||||
return is_countable($distributor_data) ? count($distributor_data) : 0;
|
||||
}
|
||||
|
||||
public function importManufacturers(array $data): int
|
||||
|
|
@ -130,7 +130,7 @@ class PKDatastructureImporter
|
|||
|
||||
$this->importAttachments($data, 'manufacturericlogo', Manufacturer::class, 'manufacturer_id', ManufacturerAttachment::class);
|
||||
|
||||
return count($manufacturer_data);
|
||||
return is_countable($manufacturer_data) ? count($manufacturer_data) : 0;
|
||||
}
|
||||
|
||||
public function importPartUnits(array $data): int
|
||||
|
|
@ -151,7 +151,7 @@ class PKDatastructureImporter
|
|||
|
||||
$this->em->flush();
|
||||
|
||||
return count($partunit_data);
|
||||
return is_countable($partunit_data) ? count($partunit_data) : 0;
|
||||
}
|
||||
|
||||
public function importCategories(array $data): int
|
||||
|
|
@ -180,15 +180,11 @@ class PKDatastructureImporter
|
|||
}
|
||||
$this->em->flush();
|
||||
|
||||
return count($partcategory_data);
|
||||
return is_countable($partcategory_data) ? count($partcategory_data) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The common import functions for footprints and storeloactions
|
||||
* @param array $data
|
||||
* @param string $target_class
|
||||
* @param string $data_prefix
|
||||
* @return int
|
||||
*/
|
||||
private function importElementsWithCategory(array $data, string $target_class, string $data_prefix): int
|
||||
{
|
||||
|
|
@ -249,7 +245,7 @@ class PKDatastructureImporter
|
|||
|
||||
$this->em->flush();
|
||||
|
||||
return count($footprint_data) + count($footprintcategory_data);
|
||||
return (is_countable($footprint_data) ? count($footprint_data) : 0) + (is_countable($footprintcategory_data) ? count($footprintcategory_data) : 0);
|
||||
}
|
||||
|
||||
public function importFootprints(array $data): int
|
||||
|
|
|
|||
|
|
@ -28,18 +28,14 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
*/
|
||||
class PKImportHelper
|
||||
{
|
||||
protected EntityManagerInterface $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
public function __construct(protected EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the database tables for the import, so that all data can be created from scratch.
|
||||
* Existing users and groups are not purged.
|
||||
* This is needed to avoid ID collisions.
|
||||
* @return void
|
||||
*/
|
||||
public function purgeDatabaseForImport(): void
|
||||
{
|
||||
|
|
@ -50,8 +46,6 @@ class PKImportHelper
|
|||
|
||||
/**
|
||||
* Extracts the current database schema version from the PartKeepr XML dump.
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function getDatabaseSchemaVersion(array $data): string
|
||||
{
|
||||
|
|
@ -64,7 +58,6 @@ class PKImportHelper
|
|||
|
||||
/**
|
||||
* Checks that the database schema of the PartKeepr XML dump is compatible with the importer
|
||||
* @param array $data
|
||||
* @return bool True if the schema is compatible, false otherwise
|
||||
*/
|
||||
public function checkVersion(array $data): bool
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ trait PKImportHelperTrait
|
|||
* @param array $attachment_row The attachment row from the PartKeepr database
|
||||
* @param string $target_class The target class for the attachment
|
||||
* @param string $type The type of the attachment (attachment or image)
|
||||
* @return Attachment
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function convertAttachmentDataToEntity(array $attachment_row, string $target_class, string $type): Attachment
|
||||
|
|
@ -87,10 +86,10 @@ trait PKImportHelperTrait
|
|||
//Use mime type to determine the extension like PartKeepr does in legacy implementation (just use the second part of the mime type)
|
||||
//See UploadedFile.php:291 in PartKeepr (https://github.com/partkeepr/PartKeepr/blob/f6176c3354b24fa39ac8bc4328ee0df91de3d5b6/src/PartKeepr/UploadedFileBundle/Entity/UploadedFile.php#L291)
|
||||
if (!empty ($attachment_row['mimetype'])) {
|
||||
$attachment_row['extension'] = explode('/', $attachment_row['mimetype'])[1];
|
||||
$attachment_row['extension'] = explode('/', (string) $attachment_row['mimetype'])[1];
|
||||
} else {
|
||||
//If the mime type is empty, we use the original extension
|
||||
$attachment_row['extension'] = pathinfo($attachment_row['originalname'], PATHINFO_EXTENSION);
|
||||
$attachment_row['extension'] = pathinfo((string) $attachment_row['originalname'], PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -115,7 +114,6 @@ trait PKImportHelperTrait
|
|||
* @param string $target_class The target class (e.g. Part)
|
||||
* @param string $target_id_field The field name where the target ID is stored
|
||||
* @param string $attachment_class The attachment class (e.g. PartAttachment)
|
||||
* @return void
|
||||
*/
|
||||
protected function importAttachments(array $data, string $table_name, string $target_class, string $target_id_field, string $attachment_class): void
|
||||
{
|
||||
|
|
@ -156,12 +154,9 @@ trait PKImportHelperTrait
|
|||
|
||||
/**
|
||||
* Assigns the parent to the given entity, using the numerical IDs from the imported data.
|
||||
* @param string $class
|
||||
* @param int|string $element_id
|
||||
* @param int|string $parent_id
|
||||
* @return AbstractStructuralDBElement The structural element that was modified (with $element_id)
|
||||
*/
|
||||
protected function setParent(string $class, $element_id, $parent_id): AbstractStructuralDBElement
|
||||
protected function setParent(string $class, int|string $element_id, int|string $parent_id): AbstractStructuralDBElement
|
||||
{
|
||||
$element = $this->em->find($class, (int) $element_id);
|
||||
if (!$element) {
|
||||
|
|
@ -184,7 +179,6 @@ trait PKImportHelperTrait
|
|||
|
||||
/**
|
||||
* Sets the given field of the given entity to the entity with the given ID.
|
||||
* @return AbstractDBElement
|
||||
*/
|
||||
protected function setAssociationField(AbstractDBElement $element, string $field, string $other_class, $other_id): AbstractDBElement
|
||||
{
|
||||
|
|
@ -205,11 +199,8 @@ trait PKImportHelperTrait
|
|||
|
||||
/**
|
||||
* Set the ID of an entity to a specific value. Must be called before persisting the entity, but before flushing.
|
||||
* @param AbstractDBElement $element
|
||||
* @param int|string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function setIDOfEntity(AbstractDBElement $element, $id): void
|
||||
protected function setIDOfEntity(AbstractDBElement $element, int|string $id): void
|
||||
{
|
||||
if (!is_int($id) && !is_string($id)) {
|
||||
throw new \InvalidArgumentException('ID must be an integer or string');
|
||||
|
|
@ -217,7 +208,7 @@ trait PKImportHelperTrait
|
|||
|
||||
$id = (int) $id;
|
||||
|
||||
$metadata = $this->em->getClassMetadata(get_class($element));
|
||||
$metadata = $this->em->getClassMetadata($element::class);
|
||||
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
|
||||
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
|
||||
$metadata->setIdentifierValues($element, ['id' => $id]);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ class PKOptionalImporter
|
|||
|
||||
/**
|
||||
* Import the projects from the given data.
|
||||
* @param array $data
|
||||
* @return int The number of imported projects
|
||||
*/
|
||||
public function importProjects(array $data): int
|
||||
|
|
@ -99,12 +98,11 @@ class PKOptionalImporter
|
|||
|
||||
$this->importAttachments($data, 'projectattachment', Project::class, 'project_id', ProjectAttachment::class);
|
||||
|
||||
return count($projects_data);
|
||||
return is_countable($projects_data) ? count($projects_data) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the users from the given data.
|
||||
* @param array $data
|
||||
* @return int The number of imported users
|
||||
*/
|
||||
public function importUsers(array $data): int
|
||||
|
|
@ -144,6 +142,6 @@ class PKOptionalImporter
|
|||
|
||||
$this->em->flush();
|
||||
|
||||
return count($users_data);
|
||||
return is_countable($users_data) ? count($users_data) : 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -45,13 +45,10 @@ class PKPartImporter
|
|||
{
|
||||
use PKImportHelperTrait;
|
||||
|
||||
private string $base_currency;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, PropertyAccessorInterface $propertyAccessor, string $default_currency)
|
||||
public function __construct(EntityManagerInterface $em, PropertyAccessorInterface $propertyAccessor, private readonly string $base_currency)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->propertyAccessor = $propertyAccessor;
|
||||
$this->base_currency = $default_currency;
|
||||
}
|
||||
|
||||
public function importParts(array $data): int
|
||||
|
|
@ -128,7 +125,7 @@ class PKPartImporter
|
|||
//Import attachments
|
||||
$this->importAttachments($data, 'partattachment', Part::class, 'part_id', PartAttachment::class);
|
||||
|
||||
return count($part_data);
|
||||
return is_countable($part_data) ? count($part_data) : 0;
|
||||
}
|
||||
|
||||
protected function importPartManufacturers(array $data): void
|
||||
|
|
@ -146,7 +143,7 @@ class PKPartImporter
|
|||
throw new \RuntimeException(sprintf('Could not find part with ID %s', $partmanufacturer['part_id']));
|
||||
}
|
||||
$manufacturer = $this->em->find(Manufacturer::class, (int) $partmanufacturer['manufacturer_id']);
|
||||
if (!$manufacturer) {
|
||||
if (!$manufacturer instanceof \App\Entity\Parts\Manufacturer) {
|
||||
throw new \RuntimeException(sprintf('Could not find manufacturer with ID %s', $partmanufacturer['manufacturer_id']));
|
||||
}
|
||||
$part->setManufacturer($manufacturer);
|
||||
|
|
@ -190,7 +187,7 @@ class PKPartImporter
|
|||
}
|
||||
|
||||
$part = $this->em->find(Part::class, (int) $partparameter['part_id']);
|
||||
if (!$part) {
|
||||
if (!$part instanceof \App\Entity\Parts\Part) {
|
||||
throw new \RuntimeException(sprintf('Could not find part with ID %s', $partparameter['part_id']));
|
||||
}
|
||||
|
||||
|
|
@ -203,8 +200,6 @@ class PKPartImporter
|
|||
/**
|
||||
* Returns the currency for the given ISO code. If the currency does not exist, it is created.
|
||||
* This function returns null if the ISO code is the base currency.
|
||||
* @param string $currency_iso_code
|
||||
* @return Currency|null
|
||||
*/
|
||||
protected function getOrCreateCurrency(string $currency_iso_code): ?Currency
|
||||
{
|
||||
|
|
@ -240,12 +235,12 @@ class PKPartImporter
|
|||
foreach ($data['partdistributor'] as $partdistributor) {
|
||||
//Retrieve the part
|
||||
$part = $this->em->find(Part::class, (int) $partdistributor['part_id']);
|
||||
if (!$part) {
|
||||
if (!$part instanceof \App\Entity\Parts\Part) {
|
||||
throw new \RuntimeException(sprintf('Could not find part with ID %s', $partdistributor['part_id']));
|
||||
}
|
||||
//Retrieve the distributor
|
||||
$supplier = $this->em->find(Supplier::class, (int) $partdistributor['distributor_id']);
|
||||
if (!$supplier) {
|
||||
if (!$supplier instanceof \App\Entity\Parts\Supplier) {
|
||||
throw new \RuntimeException(sprintf('Could not find supplier with ID %s', $partdistributor['distributor_id']));
|
||||
}
|
||||
|
||||
|
|
@ -305,9 +300,6 @@ class PKPartImporter
|
|||
|
||||
/**
|
||||
* Returns the (parameter) unit symbol for the given ID.
|
||||
* @param array $data
|
||||
* @param int $id
|
||||
* @return string
|
||||
*/
|
||||
protected function getUnitSymbol(array $data, int $id): string
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue