Fixed phpstan issues

This commit is contained in:
Jan Böhmer 2023-07-17 00:43:35 +02:00
parent afcbbe0f43
commit 3a8c5a788f
11 changed files with 25 additions and 124 deletions

View file

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

View file

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

View file

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

View file

@ -231,7 +231,7 @@ final class DTOtoEntityConverter
* @phpstan-param class-string<T> $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);
}

View file

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

View file

@ -49,7 +49,7 @@ final class ProviderRegistry
/**
* @param iterable<InfoProviderInterface> $providers
*/
public function __construct(private readonly iterable $providers)
public function __construct(iterable $providers)
{
foreach ($providers as $provider) {
$key = $provider->getProviderKey();

View file

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

View file

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

View file

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

View file

@ -1,108 +0,0 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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,
]);
}
}

View file

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