Applied rector with PHP8.1 migration rules

This commit is contained in:
Jan Böhmer 2023-06-11 14:15:46 +02:00
parent dc6a67c2f0
commit 7ee01d9a05
303 changed files with 1228 additions and 3465 deletions

View file

@ -54,9 +54,6 @@ class BOMImporter
/**
* Converts the given file into an array of BOM entries using the given options and save them into the given project.
* The changes are not saved into the database yet.
* @param File $file
* @param array $options
* @param Project $project
* @return ProjectBOMEntry[]
*/
public function importFileIntoProject(File $file, Project $project, array $options): array
@ -73,8 +70,6 @@ class BOMImporter
/**
* Converts the given file into an array of BOM entries using the given options.
* @param File $file
* @param array $options
* @return ProjectBOMEntry[]
*/
public function fileToBOMEntries(File $file, array $options): array
@ -94,12 +89,10 @@ class BOMImporter
$resolver = $this->configureOptions($resolver);
$options = $resolver->resolve($options);
switch ($options['type']) {
case 'kicad_pcbnew':
return $this->parseKiCADPCB($data, $options);
default:
throw new InvalidArgumentException('Invalid import type!');
}
return match ($options['type']) {
'kicad_pcbnew' => $this->parseKiCADPCB($data, $options),
default => throw new InvalidArgumentException('Invalid import type!'),
};
}
private function parseKiCADPCB(string $data, array $options = []): array
@ -112,9 +105,7 @@ class BOMImporter
foreach ($csv->getRecords() as $offset => $entry) {
//Translate the german field names to english
$entry = array_combine(array_map(static function ($key) {
return self::MAP_KICAD_PCB_FIELDS[$key] ?? $key;
}, array_keys($entry)), $entry);
$entry = array_combine(array_map(static fn($key) => self::MAP_KICAD_PCB_FIELDS[$key] ?? $key, array_keys($entry)), $entry);
//Ensure that the entry has all required fields
if (!isset ($entry['Designator'])) {

View file

@ -39,11 +39,8 @@ use function Symfony\Component\String\u;
*/
class EntityExporter
{
protected SerializerInterface $serializer;
public function __construct(SerializerInterface $serializer)
public function __construct(protected SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
protected function configureOptions(OptionsResolver $resolver): void
@ -67,7 +64,7 @@ class EntityExporter
* @param array $options The options to use for exporting
* @return string The serialized data
*/
public function exportEntities($entities, array $options): string
public function exportEntities(\App\Entity\Base\AbstractNamedDBElement|array $entities, array $options): string
{
if (!is_array($entities)) {
$entities = [$entities];
@ -111,7 +108,7 @@ class EntityExporter
*
* @throws ReflectionException
*/
public function exportEntityFromRequest($entities, Request $request): Response
public function exportEntityFromRequest(\App\Entity\Base\AbstractNamedDBElement|array $entities, Request $request): Response
{
$options = [
'format' => $request->get('format') ?? 'json',

View file

@ -38,15 +38,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
class EntityImporter
{
protected SerializerInterface $serializer;
protected EntityManagerInterface $em;
protected ValidatorInterface $validator;
public function __construct(SerializerInterface $serializer, EntityManagerInterface $em, ValidatorInterface $validator)
public function __construct(protected SerializerInterface $serializer, protected EntityManagerInterface $em, protected ValidatorInterface $validator)
{
$this->serializer = $serializer;
$this->em = $em;
$this->validator = $validator;
}
/**
@ -68,7 +61,7 @@ class EntityImporter
if (!is_a($class_name, AbstractNamedDBElement::class, true)) {
throw new InvalidArgumentException('$class_name must be a StructuralDBElement type!');
}
if (null !== $parent && !is_a($parent, $class_name)) {
if ($parent instanceof \App\Entity\Base\AbstractStructuralDBElement && !$parent instanceof $class_name) {
throw new InvalidArgumentException('$parent must have the same type as specified in $class_name!');
}
@ -297,20 +290,13 @@ class EntityImporter
//Convert the extension to lower case
$extension = strtolower($extension);
switch ($extension) {
case 'json':
return 'json';
case 'xml':
return 'xml';
case 'csv':
case 'tsv':
return 'csv';
case 'yaml':
case 'yml':
return 'yaml';
default:
return null;
}
return match ($extension) {
'json' => 'json',
'xml' => 'xml',
'csv', 'tsv' => 'csv',
'yaml', 'yml' => 'yaml',
default => null,
};
}
/**

View file

@ -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

View file

@ -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

View file

@ -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]);

View file

@ -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;
}
}

View file

@ -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
{