From 3a8c5a788fc9250d59a10c63a608f5969617bc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 17 Jul 2023 00:43:35 +0200 Subject: [PATCH] Fixed phpstan issues --- src/Controller/OAuthClientController.php | 2 +- src/Controller/PartController.php | 5 +- .../PartTraits/AdvancedPropertyTrait.php | 4 +- .../DTOtoEntityConverter.php | 6 +- .../InfoProviderSystem/PartInfoRetriever.php | 2 +- .../InfoProviderSystem/ProviderRegistry.php | 2 +- .../Providers/Element14Provider.php | 6 +- .../Providers/TMEProvider.php | 2 +- src/Services/OAuth/OAuthTokenManager.php | 6 +- src/Services/Parts/PartFormHelper.php | 108 ------------------ src/Twig/UserExtension.php | 6 +- 11 files changed, 25 insertions(+), 124 deletions(-) delete mode 100644 src/Services/Parts/PartFormHelper.php diff --git a/src/Controller/OAuthClientController.php b/src/Controller/OAuthClientController.php index 71d8ec1d..ff2aab0e 100644 --- a/src/Controller/OAuthClientController.php +++ b/src/Controller/OAuthClientController.php @@ -47,7 +47,7 @@ class OAuthClientController extends AbstractController return $this->clientRegistry ->getClient($name) // key used in config/packages/knpu_oauth2_client.yaml - ->redirect(); + ->redirect([], []); } #[Route('/{name}/check', name: 'oauth_client_check')] diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 1a96735c..5b80a5cb 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -236,14 +236,14 @@ class PartController extends AbstractController /** * This function provides a common implementation for methods, which use the part form. * @param Request $request - * @param Part $new_part + * @param Part $data * @param array $form_options * @return Response */ private function renderPartForm(string $mode, Request $request, Part $data, array $form_options = []): Response { //Ensure that mode is either 'new' or 'edit - if (!in_array($mode, ['new', 'edit'])) { + if (!in_array($mode, ['new', 'edit'], true)) { throw new \InvalidArgumentException('Invalid mode given'); } @@ -305,6 +305,7 @@ class PartController extends AbstractController $this->addFlash('error', 'part.created_flash.invalid'); } + $template = ''; if ($mode === 'new') { $template = 'parts/edit/new_part.html.twig'; } else if ($mode === 'edit') { diff --git a/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php b/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php index 51bce445..648cf2a5 100644 --- a/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php +++ b/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php @@ -169,9 +169,9 @@ trait AdvancedPropertyTrait /** * Sets the reference to the info provider, that provided the information about this part. * @param InfoProviderReference $providerReference - * @return AdvancedPropertyTrait + * @return Part */ - public function setProviderReference(InfoProviderReference $providerReference): self + public function setProviderReference(InfoProviderReference $providerReference): Part { $this->providerReference = $providerReference; return $this; diff --git a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php index a12628ac..4a359a03 100644 --- a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php +++ b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php @@ -231,7 +231,7 @@ final class DTOtoEntityConverter * @phpstan-param class-string $class * @param string $name The name of the entity to create * @return AbstractStructuralDBElement - * @phpstan-return T|null + * @phpstan-return T */ private function getOrCreateEntityNonNull(string $class, string $name): AbstractStructuralDBElement { @@ -263,7 +263,7 @@ final class DTOtoEntityConverter $tmp = $this->em->getRepository(AttachmentType::class)->findOrCreateForInfoProvider(self::TYPE_DATASHEETS_NAME); //If the entity was newly created, set the file filter - if ($tmp->getId() === null) { + if ($tmp->getID() === null) { $tmp->setFiletypeFilter('application/pdf'); $tmp->setAlternativeNames(self::TYPE_DATASHEETS_NAME); } @@ -281,7 +281,7 @@ final class DTOtoEntityConverter $tmp = $this->em->getRepository(AttachmentType::class)->findOrCreateForInfoProvider(self::TYPE_IMAGE_NAME); //If the entity was newly created, set the file filter - if ($tmp->getId() === null) { + if ($tmp->getID() === null) { $tmp->setFiletypeFilter('image/*'); $tmp->setAlternativeNames(self::TYPE_DATASHEETS_NAME); } diff --git a/src/Services/InfoProviderSystem/PartInfoRetriever.php b/src/Services/InfoProviderSystem/PartInfoRetriever.php index 17f4acbc..f9bf4d84 100644 --- a/src/Services/InfoProviderSystem/PartInfoRetriever.php +++ b/src/Services/InfoProviderSystem/PartInfoRetriever.php @@ -86,7 +86,7 @@ final class PartInfoRetriever * The result is cached for 4 days. * @param string $provider_key * @param string $part_id - * @return + * @return PartDetailDTO */ public function getDetails(string $provider_key, string $part_id): PartDetailDTO { diff --git a/src/Services/InfoProviderSystem/ProviderRegistry.php b/src/Services/InfoProviderSystem/ProviderRegistry.php index 921430e0..46f2484b 100644 --- a/src/Services/InfoProviderSystem/ProviderRegistry.php +++ b/src/Services/InfoProviderSystem/ProviderRegistry.php @@ -49,7 +49,7 @@ final class ProviderRegistry /** * @param iterable $providers */ - public function __construct(private readonly iterable $providers) + public function __construct(iterable $providers) { foreach ($providers as $provider) { $key = $provider->getProviderKey(); diff --git a/src/Services/InfoProviderSystem/Providers/Element14Provider.php b/src/Services/InfoProviderSystem/Providers/Element14Provider.php index d4383680..7cc6693b 100644 --- a/src/Services/InfoProviderSystem/Providers/Element14Provider.php +++ b/src/Services/InfoProviderSystem/Providers/Element14Provider.php @@ -243,9 +243,9 @@ class Element14Provider implements InfoProviderInterface /** * @param array|null $attributes - * @return ParameterDTO[]|null + * @return ParameterDTO[] */ - private function attributesToParameters(?array $attributes): ?array + private function attributesToParameters(?array $attributes): array { $result = []; @@ -258,7 +258,7 @@ class Element14Provider implements InfoProviderInterface } //tariffCode is a special case, we prepend a # to prevent conversion to float - if (in_array($attribute['attributeLabel'], ['tariffCode', 'hazardCode'])) { + if (in_array($attribute['attributeLabel'], ['tariffCode', 'hazardCode'], true)) { $attribute['attributeValue'] = '#' . $attribute['attributeValue']; } diff --git a/src/Services/InfoProviderSystem/Providers/TMEProvider.php b/src/Services/InfoProviderSystem/Providers/TMEProvider.php index 213f1bbc..2d12b222 100644 --- a/src/Services/InfoProviderSystem/Providers/TMEProvider.php +++ b/src/Services/InfoProviderSystem/Providers/TMEProvider.php @@ -41,7 +41,7 @@ class TMEProvider implements InfoProviderInterface public function __construct(private readonly TMEClient $tmeClient, private readonly string $country, private readonly string $language, private readonly string $currency, /** @var bool If true, the prices are gross prices. If false, the prices are net prices. */ - private readonly string $get_gross_prices) + private readonly bool $get_gross_prices) { } diff --git a/src/Services/OAuth/OAuthTokenManager.php b/src/Services/OAuth/OAuthTokenManager.php index bf4dcaa1..020eead7 100644 --- a/src/Services/OAuth/OAuthTokenManager.php +++ b/src/Services/OAuth/OAuthTokenManager.php @@ -50,6 +50,7 @@ final class OAuthTokenManager if ($tokenEntity) { $tokenEntity->replaceWithNewToken($token); + //@phpstan-ignore-next-line $this->entityManager->flush($tokenEntity); //We are done @@ -59,6 +60,7 @@ final class OAuthTokenManager //If the token was not existing, we create a new one $tokenEntity = OAuthToken::fromAccessToken($token, $app_name); $this->entityManager->persist($tokenEntity); + //@phpstan-ignore-next-line $this->entityManager->flush($tokenEntity); return; @@ -104,6 +106,8 @@ final class OAuthTokenManager //Persist the token $token->replaceWithNewToken($new_token); + + //@phpstan-ignore-next-line $this->entityManager->flush($token); return $token; @@ -112,7 +116,7 @@ final class OAuthTokenManager /** * This function returns the token of the given app name * @param string $app_name - * @return OAuthToken|null + * @return string|null */ public function getAlwaysValidTokenString(string $app_name): ?string { diff --git a/src/Services/Parts/PartFormHelper.php b/src/Services/Parts/PartFormHelper.php deleted file mode 100644 index 57735a39..00000000 --- a/src/Services/Parts/PartFormHelper.php +++ /dev/null @@ -1,108 +0,0 @@ -. - */ - -declare(strict_types=1); - - -namespace App\Services\Parts; - -use App\Entity\Parts\Part; -use App\Exceptions\AttachmentDownloadException; -use App\Form\Part\PartBaseType; -use App\Services\Attachments\AttachmentSubmitHandler; -use App\Services\LogSystem\EventCommentHelper; -use Doctrine\ORM\EntityManagerInterface; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\Form\FormFactoryInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Contracts\Translation\TranslatorInterface; - -final class PartFormHelper -{ - private function __construct(private readonly TranslatorInterface $translator, private readonly EventCommentHelper $commentHelper, - private readonly AttachmentSubmitHandler $attachmentSubmitHandler, private readonly EntityManagerInterface $em, - private readonly FormFactoryInterface $formFactory) - { - - } - - public function renderCreateForm(Request $request, Part $new_part = null, array $form_options = []): Response - { - $form = $this->formFactory->create(PartBaseType::class, $new_part, $form_options); - - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - //Upload passed files - $attachments = $form['attachments']; - foreach ($attachments as $attachment) { - /** @var FormInterface $attachment */ - $options = [ - 'secure_attachment' => $attachment['secureFile']->getData(), - 'download_url' => $attachment['downloadURL']->getData(), - ]; - - try { - $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); - } catch (AttachmentDownloadException $attachmentDownloadException) { - $this->addFlash( - 'error', - $this->translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage() - ); - } - } - - $this->commentHelper->setMessage($form['log_comment']->getData()); - - $this->em->persist($new_part); - $this->em->flush(); - $this->addFlash('success', 'part.created_flash'); - - //If a redirect URL was given, redirect there - if ($request->query->get('_redirect')) { - return $this->redirect($request->query->get('_redirect')); - } - - //Redirect to clone page if user wished that... - //@phpstan-ignore-next-line - if ('save_and_clone' === $form->getClickedButton()->getName()) { - return $this->redirectToRoute('part_clone', ['id' => $new_part->getID()]); - } - //@phpstan-ignore-next-line - if ('save_and_new' === $form->getClickedButton()->getName()) { - return $this->redirectToRoute('part_new'); - } - - return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]); - } - - if ($form->isSubmitted() && !$form->isValid()) { - $this->addFlash('error', 'part.created_flash.invalid'); - } - - return $this->render('parts/edit/new_part.html.twig', - [ - 'part' => $new_part, - 'form' => $form, - ]); - } -} \ No newline at end of file diff --git a/src/Twig/UserExtension.php b/src/Twig/UserExtension.php index 0a06ef2d..93ea57be 100644 --- a/src/Twig/UserExtension.php +++ b/src/Twig/UserExtension.php @@ -97,7 +97,11 @@ final class UserExtension extends AbstractExtension { $token = $this->security->getToken(); if ($token instanceof SwitchUserToken) { - return $token->getOriginalToken()->getUser(); + $tmp = $token->getOriginalToken()->getUser(); + + if ($tmp instanceof User) { + return $tmp; + } } return null;