diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 984113f0..fc7b3524 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -57,4 +57,7 @@ parameters: - '#Part::getParameters\(\) should return .*AbstractParameter#' # Ignore doctrine type mapping mismatch - - '#Property .* type mapping mismatch: property can contain .* but database expects .*#' \ No newline at end of file + - '#Property .* type mapping mismatch: property can contain .* but database expects .*#' + + # Ignore error of unused WithPermPresetsTrait, as it is used in the migrations which are not analyzed by Phpstan + - '#Trait App\\Migration\\WithPermPresetsTrait is used zero times and is not analysed#' diff --git a/src/Command/CheckRequirementsCommand.php b/src/Command/CheckRequirementsCommand.php index b5395f35..5e15e8e2 100644 --- a/src/Command/CheckRequirementsCommand.php +++ b/src/Command/CheckRequirementsCommand.php @@ -79,7 +79,7 @@ class CheckRequirementsCommand extends Command //Checking 32-bit system if (PHP_INT_SIZE === 4) { $io->warning('You are using a 32-bit system. You will have problems with working with dates after the year 2038, therefore a 64-bit system is recommended.'); - } elseif (PHP_INT_SIZE === 8) { + } elseif (PHP_INT_SIZE === 8) { //@phpstan-ignore-line //PHP_INT_SIZE is always 4 or 8 if (!$only_issues) { $io->success('You are using a 64-bit system.'); } diff --git a/src/Command/Migrations/ConvertBBCodeCommand.php b/src/Command/Migrations/ConvertBBCodeCommand.php index 2b6e4382..201263ff 100644 --- a/src/Command/Migrations/ConvertBBCodeCommand.php +++ b/src/Command/Migrations/ConvertBBCodeCommand.php @@ -79,6 +79,7 @@ class ConvertBBCodeCommand extends Command /** * Returns a list which entities and which properties need to be checked. + * @return array, string[]> */ protected function getTargetsLists(): array { @@ -109,7 +110,6 @@ class ConvertBBCodeCommand extends Command $class )); //Determine which entities of this type we need to modify - /** @var EntityRepository $repo */ $repo = $this->em->getRepository($class); $qb = $repo->createQueryBuilder('e') ->select('e'); diff --git a/src/Command/User/UsersPermissionsCommand.php b/src/Command/User/UsersPermissionsCommand.php index 021853bb..6408e9c9 100644 --- a/src/Command/User/UsersPermissionsCommand.php +++ b/src/Command/User/UsersPermissionsCommand.php @@ -206,12 +206,15 @@ class UsersPermissionsCommand extends Command return 'Allow'; } elseif ($permission_value === false) { return 'Disallow'; - } elseif ($permission_value === null && !$inherit) { + } + // Permission value is null by this point + elseif (!$inherit) { return 'Inherit'; - } elseif ($permission_value === null && $inherit) { + } elseif ($inherit) { return 'Disallow (Inherited)'; } + //@phpstan-ignore-next-line This line is never reached, but PHPstorm complains otherwise return '???'; } } diff --git a/src/Controller/AdminPages/BaseAdminController.php b/src/Controller/AdminPages/BaseAdminController.php index 3c219e29..1d19fb82 100644 --- a/src/Controller/AdminPages/BaseAdminController.php +++ b/src/Controller/AdminPages/BaseAdminController.php @@ -221,7 +221,6 @@ abstract class BaseAdminController extends AbstractController } } - /** @var AbstractPartsContainingRepository $repo */ $repo = $this->entityManager->getRepository($this->entity_class); return $this->render($this->twig_template, [ @@ -397,7 +396,7 @@ abstract class BaseAdminController extends AbstractController { if ($entity instanceof AbstractPartsContainingDBElement) { /** @var AbstractPartsContainingRepository $repo */ - $repo = $this->entityManager->getRepository($this->entity_class); + $repo = $this->entityManager->getRepository($this->entity_class); //@phpstan-ignore-line if ($repo->getPartsCount($entity) > 0) { $this->addFlash('error', t('entity.delete.must_not_contain_parts', ['%PATH%' => $entity->getFullPath()])); diff --git a/src/Controller/LabelController.php b/src/Controller/LabelController.php index d1bcfdbf..09bec781 100644 --- a/src/Controller/LabelController.php +++ b/src/Controller/LabelController.php @@ -152,7 +152,7 @@ class LabelController extends AbstractController { $id_array = $this->rangeParser->parse($ids); - /** @var DBElementRepository $repo */ + /** @var DBElementRepository $repo */ $repo = $this->em->getRepository($type->getEntityClass()); return $repo->getElementsFromIDArray($id_array); diff --git a/src/Controller/PartListsController.php b/src/Controller/PartListsController.php index 677ad987..fde2805d 100644 --- a/src/Controller/PartListsController.php +++ b/src/Controller/PartListsController.php @@ -60,6 +60,7 @@ class PartListsController extends AbstractController $ids = $request->request->get('ids'); $action = $request->request->get('action'); $target = $request->request->get('target'); + $redirectResponse = null; if (!$this->isCsrfTokenValid('table_action', $request->request->get('_token'))) { $this->addFlash('error', 'csfr_invalid'); @@ -80,7 +81,7 @@ class PartListsController extends AbstractController } //If the action handler returned a response, we use it, otherwise we redirect back to the previous page. - if (isset($redirectResponse) && $redirectResponse instanceof Response) { + if ($redirectResponse !== null) { return $redirectResponse; } diff --git a/src/Controller/TypeaheadController.php b/src/Controller/TypeaheadController.php index b1344532..89eac7ff 100644 --- a/src/Controller/TypeaheadController.php +++ b/src/Controller/TypeaheadController.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace App\Controller; +use App\Entity\Parameters\AbstractParameter; use Symfony\Component\HttpFoundation\Response; use App\Entity\Attachments\Attachment; use App\Entity\Parts\Category; @@ -92,7 +93,7 @@ class TypeaheadController extends AbstractController /** * This function map the parameter type to the class, so we can access its repository - * @return class-string + * @return class-string */ private function typeToParameterClass(string $type): string { @@ -155,7 +156,7 @@ class TypeaheadController extends AbstractController //Ensure user has the correct permissions $this->denyAccessUnlessGranted('read', $test_obj); - /** @var ParameterRepository $repository */ + /** @var ParameterRepository $repository */ $repository = $entityManager->getRepository($class); $data = $repository->autocompleteParamName($query); diff --git a/src/Controller/UserSettingsController.php b/src/Controller/UserSettingsController.php index f84547dc..4e56015a 100644 --- a/src/Controller/UserSettingsController.php +++ b/src/Controller/UserSettingsController.php @@ -240,7 +240,10 @@ class UserSettingsController extends AbstractController $page_need_reload = true; } - /** @var Form $form We need a form implementation for the next calls */ + if (!$form instanceof Form) { + throw new RuntimeException('Form is not an instance of Form, so we cannot retrieve the clicked button!'); + } + //Remove the avatar attachment from the user if requested if ($form->getClickedButton() && 'remove_avatar' === $form->getClickedButton()->getName() && $user->getMasterPictureAttachment() instanceof Attachment) { $em->remove($user->getMasterPictureAttachment()); diff --git a/src/DataTables/Filters/Constraints/Part/TagsConstraint.php b/src/DataTables/Filters/Constraints/Part/TagsConstraint.php index 5e16025a..cb17de5c 100644 --- a/src/DataTables/Filters/Constraints/Part/TagsConstraint.php +++ b/src/DataTables/Filters/Constraints/Part/TagsConstraint.php @@ -133,6 +133,7 @@ class TagsConstraint extends AbstractConstraint return; } + //@phpstan-ignore-next-line Keep this check to ensure that everything has the same structure even if we add a new operator if ($this->operator === 'NONE') { $queryBuilder->andWhere($queryBuilder->expr()->not($queryBuilder->expr()->orX(...$tagsExpressions))); return; diff --git a/src/DataTables/ProjectBomEntriesDataTable.php b/src/DataTables/ProjectBomEntriesDataTable.php index 84a89320..fcb06984 100644 --- a/src/DataTables/ProjectBomEntriesDataTable.php +++ b/src/DataTables/ProjectBomEntriesDataTable.php @@ -87,16 +87,14 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface if(!$context->getPart() instanceof Part) { return htmlspecialchars((string) $context->getName()); } - if($context->getPart() instanceof Part) { - $tmp = $this->partDataTableHelper->renderName($context->getPart()); - if($context->getName() !== null && $context->getName() !== '') { - $tmp .= '
'.htmlspecialchars($context->getName()).''; - } - return $tmp; - } - //@phpstan-ignore-next-line - throw new \RuntimeException('This should never happen!'); + //Part exists if we reach this point + + $tmp = $this->partDataTableHelper->renderName($context->getPart()); + if($context->getName() !== null && $context->getName() !== '') { + $tmp .= '
'.htmlspecialchars($context->getName()).''; + } + return $tmp; }, ]) ->add('ipn', TextColumn::class, [ diff --git a/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php index 80a81612..ad572d4c 100644 --- a/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php +++ b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php @@ -44,15 +44,13 @@ class SQLiteRegexExtensionMiddlewareDriver extends AbstractDriverMiddleware $native_connection = $connection->getNativeConnection(); //Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation - if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) { + if($native_connection instanceof \PDO) { $native_connection->sqliteCreateFunction('REGEXP', self::regexp(...), 2, \PDO::SQLITE_DETERMINISTIC); $native_connection->sqliteCreateFunction('FIELD', self::field(...), -1, \PDO::SQLITE_DETERMINISTIC); $native_connection->sqliteCreateFunction('FIELD2', self::field2(...), 2, \PDO::SQLITE_DETERMINISTIC); //Create a new collation for natural sorting - if (method_exists($native_connection, 'sqliteCreateCollation')) { - $native_connection->sqliteCreateCollation('NATURAL_CMP', strnatcmp(...)); - } + $native_connection->sqliteCreateCollation('NATURAL_CMP', strnatcmp(...)); } } diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 8dc01641..30d9e257 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -531,7 +531,7 @@ abstract class Attachment extends AbstractNamedDBElement $url = str_replace(' ', '%20', $url); //Only set if the URL is not empty - if ($url !== null && $url !== '') { + if ($url !== '') { if (str_contains($url, '%BASE%') || str_contains($url, '%MEDIA%')) { throw new InvalidArgumentException('You can not reference internal files via the url field! But nice try!'); } diff --git a/src/Entity/Attachments/AttachmentContainingDBElement.php b/src/Entity/Attachments/AttachmentContainingDBElement.php index 3c863e85..a78cb1f4 100644 --- a/src/Entity/Attachments/AttachmentContainingDBElement.php +++ b/src/Entity/Attachments/AttachmentContainingDBElement.php @@ -33,7 +33,7 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; /** - * @template-covariant AT of Attachment + * @template AT of Attachment */ #[ORM\MappedSuperclass(repositoryClass: AttachmentContainingDBElementRepository::class)] abstract class AttachmentContainingDBElement extends AbstractNamedDBElement implements HasMasterAttachmentInterface, HasAttachmentsInterface diff --git a/src/Entity/Base/AbstractCompany.php b/src/Entity/Base/AbstractCompany.php index e3d4fd61..ccb74633 100644 --- a/src/Entity/Base/AbstractCompany.php +++ b/src/Entity/Base/AbstractCompany.php @@ -33,8 +33,8 @@ use Symfony\Component\Validator\Constraints as Assert; /** * This abstract class is used for companies like suppliers or manufacturers. * - * @template-covariant AT of Attachment - * @template-covariant PT of AbstractParameter + * @template AT of Attachment + * @template PT of AbstractParameter * @extends AbstractPartsContainingDBElement */ #[ORM\MappedSuperclass] diff --git a/src/Entity/Base/AbstractPartsContainingDBElement.php b/src/Entity/Base/AbstractPartsContainingDBElement.php index e2209987..70d88fa9 100644 --- a/src/Entity/Base/AbstractPartsContainingDBElement.php +++ b/src/Entity/Base/AbstractPartsContainingDBElement.php @@ -31,8 +31,8 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; /** - * @template-covariant AT of Attachment - * @template-covariant PT of AbstractParameter + * @template AT of Attachment + * @template PT of AbstractParameter * @extends AbstractStructuralDBElement */ #[ORM\MappedSuperclass(repositoryClass: AbstractPartsContainingRepository::class)] diff --git a/src/Entity/Base/AbstractStructuralDBElement.php b/src/Entity/Base/AbstractStructuralDBElement.php index 09ec94a6..f1cab493 100644 --- a/src/Entity/Base/AbstractStructuralDBElement.php +++ b/src/Entity/Base/AbstractStructuralDBElement.php @@ -53,8 +53,8 @@ use Symfony\Component\Serializer\Annotation\Groups; * * @see \App\Tests\Entity\Base\AbstractStructuralDBElementTest * - * @template-covariant AT of Attachment - * @template-covariant PT of AbstractParameter + * @template AT of Attachment + * @template PT of AbstractParameter * @template-use ParametersTrait * @extends AttachmentContainingDBElement * @uses ParametersTrait diff --git a/src/Entity/LabelSystem/LabelSupportedElement.php b/src/Entity/LabelSystem/LabelSupportedElement.php index 2f9afa85..7649e586 100644 --- a/src/Entity/LabelSystem/LabelSupportedElement.php +++ b/src/Entity/LabelSystem/LabelSupportedElement.php @@ -22,6 +22,8 @@ declare(strict_types=1); */ namespace App\Entity\LabelSystem; +use App\Entity\Base\AbstractDBElement; +use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Parts\Part; use App\Entity\Parts\PartLot; use App\Entity\Parts\StorageLocation; @@ -34,7 +36,7 @@ enum LabelSupportedElement: string /** * Returns the entity class for the given element type - * @return string + * @return class-string */ public function getEntityClass(): string { diff --git a/src/Entity/Parts/PartLot.php b/src/Entity/Parts/PartLot.php index 70475efd..c1b7da8a 100644 --- a/src/Entity/Parts/PartLot.php +++ b/src/Entity/Parts/PartLot.php @@ -185,7 +185,6 @@ class PartLot extends AbstractDBElement implements TimeStampableInterface, Named * * @return bool|null True, if the part lot is expired. Returns null, if no expiration date was set. * - * @throws Exception If an error with the DateTime occurs */ public function isExpired(): ?bool { diff --git a/src/Entity/Parts/Supplier.php b/src/Entity/Parts/Supplier.php index 6b01a92c..2c004e9e 100644 --- a/src/Entity/Parts/Supplier.php +++ b/src/Entity/Parts/Supplier.php @@ -103,7 +103,7 @@ class Supplier extends AbstractCompany protected ?AbstractStructuralDBElement $parent = null; /** - * @var Collection|Orderdetail[] + * @var Collection */ #[ORM\OneToMany(mappedBy: 'supplier', targetEntity: Orderdetail::class)] protected Collection $orderdetails; diff --git a/src/Entity/ProjectSystem/Project.php b/src/Entity/ProjectSystem/Project.php index 1d1ed413..a103d694 100644 --- a/src/Entity/ProjectSystem/Project.php +++ b/src/Entity/ProjectSystem/Project.php @@ -333,7 +333,6 @@ class Project extends AbstractStructuralDBElement { //If this project has subprojects, and these have builds part, they must be included in the BOM foreach ($this->getChildren() as $child) { - /** @var $child Project */ if (!$child->getBuildPart() instanceof Part) { continue; } diff --git a/src/Entity/UserSystem/User.php b/src/Entity/UserSystem/User.php index f46dfd15..b39bea4f 100644 --- a/src/Entity/UserSystem/User.php +++ b/src/Entity/UserSystem/User.php @@ -893,8 +893,6 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe * @param string[] $codes An array containing the backup codes * * @return $this - * - * @throws Exception If an error with the datetime occurs */ public function setBackupCodes(array $codes): self { diff --git a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php index e8e19ad6..1210d188 100644 --- a/src/Form/Type/Helper/StructuralEntityChoiceHelper.php +++ b/src/Form/Type/Helper/StructuralEntityChoiceHelper.php @@ -43,7 +43,7 @@ class StructuralEntityChoiceHelper /** * Generates the choice attributes for the given AbstractStructuralDBElement. - * @return array|string[] + * @return array */ public function generateChoiceAttr(AbstractNamedDBElement $choice, Options|array $options): array { diff --git a/src/Form/Type/Helper/StructuralEntityChoiceLoader.php b/src/Form/Type/Helper/StructuralEntityChoiceLoader.php index b79fad1b..e2e4e841 100644 --- a/src/Form/Type/Helper/StructuralEntityChoiceLoader.php +++ b/src/Form/Type/Helper/StructuralEntityChoiceLoader.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace App\Form\Type\Helper; use App\Entity\Base\AbstractNamedDBElement; +use App\Entity\Base\AbstractStructuralDBElement; use App\Repository\StructuralDBElementRepository; use App\Services\Trees\NodesListBuilder; use Doctrine\ORM\EntityManagerInterface; @@ -33,6 +34,9 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\Options; use Symfony\Contracts\Translation\TranslatorInterface; +/** + * @template T of AbstractStructuralDBElement + */ class StructuralEntityChoiceLoader extends AbstractChoiceLoader { private ?string $additional_element = null; @@ -90,10 +94,14 @@ class StructuralEntityChoiceLoader extends AbstractChoiceLoader } } + + /** @var class-string $class */ $class = $this->options['class']; - /** @var StructuralDBElementRepository $repo */ + + /** @var StructuralDBElementRepository $repo */ $repo = $this->entityManager->getRepository($class); + $entities = $repo->getNewEntityFromPath($value, '->'); $results = []; diff --git a/src/Form/Type/TriStateCheckboxType.php b/src/Form/Type/TriStateCheckboxType.php index 7d1e1c7c..4523a839 100644 --- a/src/Form/Type/TriStateCheckboxType.php +++ b/src/Form/Type/TriStateCheckboxType.php @@ -99,7 +99,6 @@ final class TriStateCheckboxType extends AbstractType implements DataTransformer * * @return mixed The value in the transformed representation * - * @throws TransformationFailedException when the transformation fails */ public function transform(mixed $value) { @@ -142,8 +141,6 @@ final class TriStateCheckboxType extends AbstractType implements DataTransformer * @param mixed $value The value in the transformed representation * * @return mixed The value in the original representation - * - * @throws TransformationFailedException when the transformation fails */ public function reverseTransform(mixed $value) { diff --git a/src/Repository/StructuralDBElementRepository.php b/src/Repository/StructuralDBElementRepository.php index 47c85db3..781c7622 100644 --- a/src/Repository/StructuralDBElementRepository.php +++ b/src/Repository/StructuralDBElementRepository.php @@ -151,7 +151,7 @@ class StructuralDBElementRepository extends AttachmentContainingDBElementReposit } if (null === $entity) { $class = $this->getClassName(); - /** @var AbstractStructuralDBElement $entity */ + /** @var TEntityClass $entity */ $entity = new $class; $entity->setName($name); $entity->setParent($parent); @@ -265,7 +265,7 @@ class StructuralDBElementRepository extends AttachmentContainingDBElementReposit } $class = $this->getClassName(); - /** @var AbstractStructuralDBElement $entity */ + /** @var TEntityClass $entity */ $entity = new $class; $entity->setName($name); diff --git a/src/Security/UserChecker.php b/src/Security/UserChecker.php index fd53a295..16afb37e 100644 --- a/src/Security/UserChecker.php +++ b/src/Security/UserChecker.php @@ -40,12 +40,10 @@ final class UserChecker implements UserCheckerInterface /** * Checks the user account before authentication. - * - * @throws AccountStatusException */ public function checkPreAuth(UserInterface $user): void { - // TODO: Implement checkPreAuth() method. + //We don't need to check the user before authentication, just implemented to fulfill the interface } /** diff --git a/src/Serializer/StructuralElementDenormalizer.php b/src/Serializer/StructuralElementDenormalizer.php index 17b3d81e..92af8db3 100644 --- a/src/Serializer/StructuralElementDenormalizer.php +++ b/src/Serializer/StructuralElementDenormalizer.php @@ -69,6 +69,15 @@ class StructuralElementDenormalizer implements DenormalizerInterface, Denormaliz && in_array('import', $context['groups'] ?? [], true); } + /** + * @template T of AbstractStructuralDBElement + * @param $data + * @phpstan-param class-string $type + * @param string|null $format + * @param array $context + * @return AbstractStructuralDBElement|null + * @phpstan-return T|null + */ public function denormalize($data, string $type, string $format = null, array $context = []): ?AbstractStructuralDBElement { //Do not use API Platform's denormalizer @@ -85,7 +94,7 @@ class StructuralElementDenormalizer implements DenormalizerInterface, Denormaliz $deserialized_entity = $this->denormalizer->denormalize($data, $type, $format, $context); //Check if we already have the entity in the database (via path) - /** @var StructuralDBElementRepository $repo */ + /** @var StructuralDBElementRepository $repo */ $repo = $this->entityManager->getRepository($type); $path = $deserialized_entity->getFullPath(AbstractStructuralDBElement::PATH_DELIMITER_ARROW); diff --git a/src/Serializer/StructuralElementFromNameDenormalizer.php b/src/Serializer/StructuralElementFromNameDenormalizer.php index ef72aaea..92da353f 100644 --- a/src/Serializer/StructuralElementFromNameDenormalizer.php +++ b/src/Serializer/StructuralElementFromNameDenormalizer.php @@ -54,7 +54,7 @@ class StructuralElementFromNameDenormalizer implements DenormalizerInterface public function denormalize($data, string $type, string $format = null, array $context = []): AbstractStructuralDBElement|null { //Retrieve the repository for the given type - /** @var StructuralDBElementRepository $repo */ + /** @var StructuralDBElementRepository $repo */ $repo = $this->em->getRepository($type); $path_delimiter = $context['path_delimiter'] ?? '->'; diff --git a/src/Services/Formatters/MoneyFormatter.php b/src/Services/Formatters/MoneyFormatter.php index d49b77cf..44a49cb5 100644 --- a/src/Services/Formatters/MoneyFormatter.php +++ b/src/Services/Formatters/MoneyFormatter.php @@ -46,7 +46,7 @@ class MoneyFormatter public function format(string|float $value, ?Currency $currency = null, int $decimals = 5, bool $show_all_digits = false): string { $iso_code = $this->base_currency; - if ($currency instanceof Currency && ($currency->getIsoCode() !== null && $currency->getIsoCode() !== '')) { + if ($currency instanceof Currency && ($currency->getIsoCode() !== '')) { $iso_code = $currency->getIsoCode(); } diff --git a/src/Services/ImportExportSystem/BOMImporter.php b/src/Services/ImportExportSystem/BOMImporter.php index 246ccf32..d4876445 100644 --- a/src/Services/ImportExportSystem/BOMImporter.php +++ b/src/Services/ImportExportSystem/BOMImporter.php @@ -153,6 +153,7 @@ class BOMImporter break; } + //@phpstan-ignore-next-line We want to keep this check just to be safe when something changes $new_index = self::MAP_KICAD_PCB_FIELDS[$index] ?? throw new \UnexpectedValueException('Invalid field index!'); $out[$new_index] = $field; } diff --git a/src/Services/ImportExportSystem/EntityImporter.php b/src/Services/ImportExportSystem/EntityImporter.php index 1318c658..fb12c3bc 100644 --- a/src/Services/ImportExportSystem/EntityImporter.php +++ b/src/Services/ImportExportSystem/EntityImporter.php @@ -64,7 +64,7 @@ class EntityImporter * @phpstan-param class-string $class_name * @param AbstractStructuralDBElement|null $parent the element which will be used as parent element for new elements * @param array $errors an associative array containing all validation errors - * @param-out array $errors + * @param-out list $errors * * @return AbstractNamedDBElement[] An array containing all valid imported entities (with the type $class_name) * @return T[] @@ -133,13 +133,15 @@ class EntityImporter if ($repo instanceof StructuralDBElementRepository) { $entities = $repo->getNewEntityFromPath($new_path); $entity = end($entities); + if ($entity === false) { + throw new InvalidArgumentException('getNewEntityFromPath returned an empty array!'); + } } else { //Otherwise just create a new entity $entity = new $class_name; $entity->setName($name); } - //Validate entity $tmp = $this->validator->validate($entity); //If no error occured, write entry to DB: @@ -227,6 +229,11 @@ class EntityImporter //Iterate over each $entity write it to DB. foreach ($entities as $key => $entity) { + //Ensure that entity is a NamedDBElement + if (!$entity instanceof AbstractNamedDBElement) { + throw new \RuntimeException("Encountered an entity that is not a NamedDBElement!"); + } + //Validate entity $tmp = $this->validator->validate($entity); @@ -281,7 +288,7 @@ class EntityImporter * * @param File $file the file that should be used for importing * @param array $options options for the import process - * @param AbstractNamedDBElement[] $entities The imported entities are returned in this array + * @param-out AbstractNamedDBElement[] $entities The imported entities are returned in this array * * @return array An associative array containing an ConstraintViolationList and the entity name as key are returned, * if an error happened during validation. When everything was successfully, the array should be empty. @@ -317,7 +324,7 @@ class EntityImporter * @param array $options options for the import process * @param-out array $errors * - * @return array an array containing the deserialized elements + * @return AbstractNamedDBElement[] an array containing the deserialized elements */ public function importFile(File $file, array $options = [], array &$errors = []): array { diff --git a/src/Services/ImportExportSystem/PartKeeprImporter/PKImportHelperTrait.php b/src/Services/ImportExportSystem/PartKeeprImporter/PKImportHelperTrait.php index 574bc32b..5489fc8c 100644 --- a/src/Services/ImportExportSystem/PartKeeprImporter/PKImportHelperTrait.php +++ b/src/Services/ImportExportSystem/PartKeeprImporter/PKImportHelperTrait.php @@ -205,10 +205,6 @@ trait PKImportHelperTrait */ 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'); - } - $id = (int) $id; $metadata = $this->em->getClassMetadata($element::class); diff --git a/src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php b/src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php index f3b59adc..57c5b815 100644 --- a/src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php +++ b/src/Services/InfoProviderSystem/Providers/OEMSecretsProvider.php @@ -771,11 +771,6 @@ class OEMSecretsProvider implements InfoProviderInterface // Logic to extract parameters from the description $extractedParameters = $this->parseDescriptionToParameters($description) ?? []; - // Ensure that $extractedParameters is an array - if (!is_array($extractedParameters)) { - $extractedParameters = []; - } - foreach ($extractedParameters as $newParam) { $isDuplicate = false; foreach ($parameters as $existingParam) { diff --git a/src/Services/LabelSystem/LabelExampleElementsGenerator.php b/src/Services/LabelSystem/LabelExampleElementsGenerator.php index 199d5f46..d344c929 100644 --- a/src/Services/LabelSystem/LabelExampleElementsGenerator.php +++ b/src/Services/LabelSystem/LabelExampleElementsGenerator.php @@ -146,11 +146,11 @@ final class LabelExampleElementsGenerator throw new InvalidArgumentException('$class must be an child of AbstractStructuralDBElement'); } - /** @var AbstractStructuralDBElement $parent */ + /** @var T $parent */ $parent = new $class(); $parent->setName('Example'); - /** @var AbstractStructuralDBElement $child */ + /** @var T $child */ $child = new $class(); $child->setName((new ReflectionClass($class))->getShortName()); $child->setParent($parent); diff --git a/src/Services/LabelSystem/LabelGenerator.php b/src/Services/LabelSystem/LabelGenerator.php index d2b20c92..bfb8d27b 100644 --- a/src/Services/LabelSystem/LabelGenerator.php +++ b/src/Services/LabelSystem/LabelGenerator.php @@ -62,10 +62,6 @@ final class LabelGenerator */ public function generateLabel(LabelOptions $options, object|array $elements): string { - if (!is_array($elements) && !is_object($elements)) { - throw new InvalidArgumentException('$element must be an object or an array of objects!'); - } - if (!is_array($elements)) { $elements = [$elements]; } diff --git a/src/Services/LabelSystem/LabelProfileDropdownHelper.php b/src/Services/LabelSystem/LabelProfileDropdownHelper.php index ac5020a2..773923ab 100644 --- a/src/Services/LabelSystem/LabelProfileDropdownHelper.php +++ b/src/Services/LabelSystem/LabelProfileDropdownHelper.php @@ -74,8 +74,7 @@ final class LabelProfileDropdownHelper $secure_class_name = $this->tagGenerator->getElementTypeCacheTag(LabelProfile::class); $key = 'profile_dropdown_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.'_'.$type->value; - - /** @var LabelProfileRepository $repo */ + $repo = $this->entityManager->getRepository(LabelProfile::class); return $this->cache->get($key, function (ItemInterface $item) use ($repo, $type, $secure_class_name) { diff --git a/src/Services/Parts/PartsTableActionHandler.php b/src/Services/Parts/PartsTableActionHandler.php index bcfb3a8a..1bd6209d 100644 --- a/src/Services/Parts/PartsTableActionHandler.php +++ b/src/Services/Parts/PartsTableActionHandler.php @@ -53,7 +53,6 @@ final class PartsTableActionHandler { $id_array = explode(',', $ids); - /** @var PartRepository $repo */ $repo = $this->entityManager->getRepository(Part::class); return $repo->getElementsFromIDArray($id_array); diff --git a/src/Services/System/BannerHelper.php b/src/Services/System/BannerHelper.php index 1b6da52a..3d5daef9 100644 --- a/src/Services/System/BannerHelper.php +++ b/src/Services/System/BannerHelper.php @@ -40,9 +40,6 @@ class BannerHelper public function getBanner(): string { $banner = $this->partdb_banner; - if (!is_string($banner)) { - throw new \RuntimeException('The parameter "partdb.banner" must be a string.'); - } if ($banner === '') { $banner_path = $this->project_dir .DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'banner.md'; diff --git a/src/Services/Tools/StatisticsHelper.php b/src/Services/Tools/StatisticsHelper.php index c1cace21..00bb05c9 100644 --- a/src/Services/Tools/StatisticsHelper.php +++ b/src/Services/Tools/StatisticsHelper.php @@ -122,7 +122,6 @@ class StatisticsHelper throw new InvalidArgumentException('No count for the given type available!'); } - /** @var EntityRepository $repo */ $repo = $this->em->getRepository($arr[$type]); return $repo->count([]); diff --git a/src/Services/Trees/NodesListBuilder.php b/src/Services/Trees/NodesListBuilder.php index 0f94d79d..a82996eb 100644 --- a/src/Services/Trees/NodesListBuilder.php +++ b/src/Services/Trees/NodesListBuilder.php @@ -23,9 +23,11 @@ declare(strict_types=1); namespace App\Services\Trees; use App\Entity\Base\AbstractDBElement; +use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractStructuralDBElement; use App\Repository\AttachmentContainingDBElementRepository; use App\Repository\DBElementRepository; +use App\Repository\NamedDBElementRepository; use App\Repository\StructuralDBElementRepository; use App\Services\Cache\ElementCacheTagGenerator; use App\Services\Cache\UserCacheKeyGenerator; @@ -51,7 +53,7 @@ class NodesListBuilder * Gets a flattened hierarchical tree. Useful for generating option lists. * In difference to the Repository Function, the results here are cached. * - * @template T of AbstractDBElement + * @template T of AbstractNamedDBElement * * @param string $class_name the class name of the entity you want to retrieve * @phpstan-param class-string $class_name @@ -69,7 +71,7 @@ class NodesListBuilder $ids = $this->getFlattenedIDs($class_name, $parent); //Retrieve the elements from the IDs, the order is the same as in the $ids array - /** @var DBElementRepository $repo */ + /** @var NamedDBElementRepository $repo */ $repo = $this->em->getRepository($class_name); if ($repo instanceof AttachmentContainingDBElementRepository) { @@ -81,7 +83,9 @@ class NodesListBuilder /** * This functions returns the (cached) list of the IDs of the elements for the flattened tree. + * @template T of AbstractNamedDBElement * @param string $class_name + * @phpstan-param class-string $class_name * @param AbstractStructuralDBElement|null $parent * @return int[] */ @@ -96,10 +100,11 @@ class NodesListBuilder // Invalidate when groups, an element with the class or the user changes $item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]); - /** @var StructuralDBElementRepository $repo */ + /** @var NamedDBElementRepository $repo */ $repo = $this->em->getRepository($class_name); - return array_map(static fn(AbstractDBElement $element) => $element->getID(), $repo->getFlatList($parent)); + return array_map(static fn(AbstractDBElement $element) => $element->getID(), + $repo instanceof AbstractStructuralDBElement ? $repo->getFlatList($parent) : $repo->getFlatList()); }); } diff --git a/src/Services/Trees/TreeViewGenerator.php b/src/Services/Trees/TreeViewGenerator.php index 81b64840..23d6a406 100644 --- a/src/Services/Trees/TreeViewGenerator.php +++ b/src/Services/Trees/TreeViewGenerator.php @@ -33,6 +33,7 @@ use App\Entity\Parts\Supplier; use App\Entity\ProjectSystem\Project; use App\Helpers\Trees\TreeViewNode; use App\Helpers\Trees\TreeViewNodeIterator; +use App\Repository\NamedDBElementRepository; use App\Repository\StructuralDBElementRepository; use App\Services\Cache\ElementCacheTagGenerator; use App\Services\Cache\UserCacheKeyGenerator; @@ -219,6 +220,7 @@ class TreeViewGenerator * The treeview is generic, that means the href are null and ID values are set. * * @param string $class The class for which the tree should be generated + * @phpstan-param class-string $class * @param AbstractStructuralDBElement|null $parent the parent the root elements should have * * @return TreeViewNode[] @@ -232,12 +234,12 @@ class TreeViewGenerator throw new InvalidArgumentException('$parent must be of the type $class!'); } - /** @var StructuralDBElementRepository $repo */ + /** @var NamedDBElementRepository $repo */ $repo = $this->em->getRepository($class); //If we just want a part of a tree, don't cache it if ($parent instanceof AbstractStructuralDBElement) { - return $repo->getGenericNodeTree($parent); + return $repo->getGenericNodeTree($parent); //@phpstan-ignore-line PHPstan does not seem to recognize, that we have a StructuralDBElementRepository here, which have 1 argument } $secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class); @@ -246,7 +248,7 @@ class TreeViewGenerator return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) { // Invalidate when groups, an element with the class or the user changes $item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]); - return $repo->getGenericNodeTree($parent); + return $repo->getGenericNodeTree($parent); //@phpstan-ignore-line }); } }