From c063ae1a29b18a2f82affa50ba80a6026923e484 Mon Sep 17 00:00:00 2001 From: seb-jean Date: Mon, 6 May 2024 10:46:57 +0200 Subject: [PATCH 1/2] Add types for PHPStan --- src/Maker/MakeWebhook.php | 2 +- .../crud/controller/Controller.tpl.php | 2 +- .../skeleton/message/MessageHandler.tpl.php | 2 +- .../RegistrationController.tpl.php | 23 +++++++++++-------- .../ResetPasswordController.tpl.php | 21 ++++++++--------- .../skeleton/validator/Constraint.tpl.php | 2 +- .../skeleton/validator/Validator.tpl.php | 2 +- .../verifyEmail/EmailVerifier.tpl.php | 8 +++---- .../skeleton/webhook/RequestParser.tpl.php | 8 +++---- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Maker/MakeWebhook.php b/src/Maker/MakeWebhook.php index 3fe1d1931..a0c77a074 100644 --- a/src/Maker/MakeWebhook.php +++ b/src/Maker/MakeWebhook.php @@ -300,7 +300,7 @@ private function getRequestMatcherArguments(string $requestMatcherClass): string IsJsonRequestMatcher::class => '', MethodRequestMatcher::class => '\'POST\'', PortRequestMatcher::class => '443', - SchemeRequestMatcher::class => 'https', + SchemeRequestMatcher::class => '\'https\'', default => '[]', }; } diff --git a/src/Resources/skeleton/crud/controller/Controller.tpl.php b/src/Resources/skeleton/crud/controller/Controller.tpl.php index 69a15df3a..012ce710f 100644 --- a/src/Resources/skeleton/crud/controller/Controller.tpl.php +++ b/src/Resources/skeleton/crud/controller/Controller.tpl.php @@ -77,7 +77,7 @@ public function edit(Request $request, $generateRouteForControllerMethod(sprintf('/{%s}', $entity_identifier), sprintf('%s_delete', $route_name), ['POST']) ?> public function delete(Request $request, $, EntityManagerInterface $entityManager): Response { - if ($this->isCsrfTokenValid('delete'.$->get(), $request->getPayload()->get('_token'))) { + if ($this->isCsrfTokenValid('delete'.$->get(), $request->getPayload()->getString('_token'))) { $entityManager->remove($); $entityManager->flush(); } diff --git a/src/Resources/skeleton/message/MessageHandler.tpl.php b/src/Resources/skeleton/message/MessageHandler.tpl.php index 1164c960c..1c55c72aa 100644 --- a/src/Resources/skeleton/message/MessageHandler.tpl.php +++ b/src/Resources/skeleton/message/MessageHandler.tpl.php @@ -7,7 +7,7 @@ #[AsMessageHandler] final class { - public function __invoke( $message) + public function __invoke( $message): void { // do something with your message } diff --git a/src/Resources/skeleton/registration/RegistrationController.tpl.php b/src/Resources/skeleton/registration/RegistrationController.tpl.php index 77969a1cf..a95c0d830 100644 --- a/src/Resources/skeleton/registration/RegistrationController.tpl.php +++ b/src/Resources/skeleton/registration/RegistrationController.tpl.php @@ -7,8 +7,9 @@ class extends AbstractController { - public function __construct(private getPropertyType($email_verifier_class_details) ?>$emailVerifier) - { + public function __construct( + private readonly getPropertyType($email_verifier_class_details) ?>$emailVerifier + ) { } @@ -20,13 +21,11 @@ public function register(Request $request, UserPasswordHasherInterface $userPass $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + /** @var string $plainPassword */ + $plainPassword = $form->get('plainPassword')->getData(); + // encode the plain password - $user->set( - $userPasswordHasher->hashPassword( - $user, - $form->get('plainPassword')->getData() - ) - ); + $user->set($userPasswordHasher->hashPassword($user, $plainPassword)); $entityManager->persist($user); $entityManager->flush(); @@ -36,7 +35,7 @@ public function register(Request $request, UserPasswordHasherInterface $userPass $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user, (new TemplatedEmail()) ->from(new Address('', '')) - ->to($user->()) + ->to((string) $user->()) ->subject('Please Confirm your Email') ->htmlTemplate('registration/confirmation_email.html.twig') ); @@ -84,7 +83,11 @@ public function verifyUserEmail(Request $requestemailVerifier->handleEmailConfirmation($request, getUser()' ?>); + + /** @var $user */ + $user = $this->getUser(); + + $this->emailVerifier->handleEmailConfirmation($request, $user); } catch (VerifyEmailExceptionInterface $exception) { $this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle')$exception->getReason()); diff --git a/src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php b/src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php index 6af5c3591..7ce8c55c2 100644 --- a/src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php +++ b/src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php @@ -25,11 +25,10 @@ public function request(Request $request, MailerInterface $mailerhandleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - return $this->processSendingPasswordResetEmail( - $form->get('')->getData(), - $mailer, - $translator - ); + /** @var string $email */ + $email = $form->get('')->getData(); + + return $this->processSendingPasswordResetEmail($email, $mailer, $translator); } return $this->render('reset_password/request.html.twig', [ @@ -94,13 +93,11 @@ public function reset(Request $request, UserPasswordHasherInterface $passwordHas // A password reset token should be used only once, remove it. $this->resetPasswordHelper->removeResetRequest($token); - // Encode(hash) the plain password, and set it. - $encodedPassword = $passwordHasher->hashPassword( - $user, - $form->get('plainPassword')->getData() - ); + /** @var string $plainPassword */ + $plainPassword = $form->get('plainPassword')->getData(); - $user->($encodedPassword); + // Encode(hash) the plain password, and set it. + $user->($passwordHasher->hashPassword($user, $plainPassword)); $this->entityManager->flush(); // The session is cleaned up after the password has been changed. @@ -143,7 +140,7 @@ private function processSendingPasswordResetEmail(string $emailFormData, MailerI $email = (new TemplatedEmail()) ->from(new Address('', '')) - ->to($user->()) + ->to((string) $user->()) ->subject('Your password reset request') ->htmlTemplate('reset_password/email.html.twig') ->context([ diff --git a/src/Resources/skeleton/validator/Constraint.tpl.php b/src/Resources/skeleton/validator/Constraint.tpl.php index 991ad9424..7baafa472 100644 --- a/src/Resources/skeleton/validator/Constraint.tpl.php +++ b/src/Resources/skeleton/validator/Constraint.tpl.php @@ -16,5 +16,5 @@ class extends Constraint * Any public properties become valid options for the annotation. * Then, use these in your validator class. */ - public $message = 'The value "{{ value }}" is not valid.'; + public string $message = 'The value "{{ value }}" is not valid.'; } diff --git a/src/Resources/skeleton/validator/Validator.tpl.php b/src/Resources/skeleton/validator/Validator.tpl.php index 0656d4cb4..ccc1a00f9 100644 --- a/src/Resources/skeleton/validator/Validator.tpl.php +++ b/src/Resources/skeleton/validator/Validator.tpl.php @@ -7,7 +7,7 @@ class extends ConstraintValidator { - public function validate($value, Constraint $constraint) + public function validate(mixed $value, Constraint $constraint): void { /** @var $constraint */ diff --git a/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php b/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php index 5c0864efd..a75aaf0c2 100644 --- a/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php +++ b/src/Resources/skeleton/verifyEmail/EmailVerifier.tpl.php @@ -4,7 +4,7 @@ -class +readonly class { public function __construct( private VerifyEmailHelperInterface $verifyEmailHelper, @@ -19,10 +19,10 @@ public function sendEmailConfirmation(string $verifyEmailRouteName, (), - $user->(), + (string) $user->(), ['id' => $user->()] - $user->() + (string) $user->() ); @@ -41,7 +41,7 @@ public function sendEmailConfirmation(string $verifyEmailRouteName, $user): void { - $this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, (string) $user->(), $user->()); + $this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, (string) $user->(), (string) $user->()); $user->setVerified(true); diff --git a/src/Resources/skeleton/webhook/RequestParser.tpl.php b/src/Resources/skeleton/webhook/RequestParser.tpl.php index 4102ab7a2..292b87e28 100644 --- a/src/Resources/skeleton/webhook/RequestParser.tpl.php +++ b/src/Resources/skeleton/webhook/RequestParser.tpl.php @@ -41,12 +41,12 @@ protected function doParse(Request $request, #[\SensitiveParameter] string $secr } // Parse the request payload and return a RemoteEvent object. - $payload = $request->getPayload()->all(); + $payload = $request->getPayload(); return new RemoteEvent( - $payload['name'], - $payload['id'], - $payload, + $payload->getString('name'), + $payload->getString('id'), + $payload->all(), ); } } From f6956d1ab1bd01f3c53539a2e7089c8e45df6b1e Mon Sep 17 00:00:00 2001 From: seb-jean Date: Mon, 6 May 2024 11:09:57 +0200 Subject: [PATCH 2/2] Add ? for User::getPassword() --- src/Security/UserClassBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Security/UserClassBuilder.php b/src/Security/UserClassBuilder.php index 261780368..14681e4ae 100644 --- a/src/Security/UserClassBuilder.php +++ b/src/Security/UserClassBuilder.php @@ -179,7 +179,7 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo // add an empty method only $builder = $manipulator->createMethodBuilder( 'getPassword', - 'string', + '?string', true, [ 'This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords.', @@ -216,7 +216,7 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo $manipulator->addGetter( 'password', - 'string', + '?string', true ); @@ -231,7 +231,7 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo $manipulator->addAccessorMethod( 'password', 'getPassword', - 'string', + '?string', false, [ '@see PasswordAuthenticatedUserInterface',