[TOOLS] Cleanup PHPStan warnings

This commit is contained in:
Hugo Sales 2021-12-01 12:07:43 +00:00 committed by Diogo Peralta Cordeiro
parent 0b57b20d38
commit d58483a6ca
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
5 changed files with 85 additions and 63 deletions

View File

@ -23,16 +23,22 @@ declare(strict_types = 1);
namespace Component\Left\Controller; namespace Component\Left\Controller;
use App\Core\Cache;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Router\Router;
use App\Entity\Feed; use App\Entity\Feed;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException;
use Functional as F;
use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class EditFeeds extends Controller class EditFeeds extends Controller
{ {
@ -70,7 +76,9 @@ class EditFeeds extends Controller
$data = $form->getData(); $data = $form->getData();
if ($form->get('update_exisiting')->isClicked()) { /** @var SubmitButton $update_existing */
$update_existing = $form->get('update_exisiting');
if ($update_existing->isClicked()) {
// Each feed has a URL, an order and a title // Each feed has a URL, an order and a title
$feeds_data = array_chunk($data, 3, preserve_keys: true); $feeds_data = array_chunk($data, 3, preserve_keys: true);
// The last three would be the new one // The last three would be the new one
@ -101,7 +109,10 @@ class EditFeeds extends Controller
foreach ($form_definitions as [$field, $type, $opts]) { foreach ($form_definitions as [$field, $type, $opts]) {
if (str_ends_with($field, '-url')) { if (str_ends_with($field, '-url')) {
$remove_id = str_replace('-url', '-remove', $field); $remove_id = str_replace('-url', '-remove', $field);
if ($form->get($remove_id)->isClicked()) { /** @var SubmitButton $remove_button */
$remove_button = $form->get($remove_id);
if ($remove_button->isClicked()) {
// @phpstan-ignore-next-line -- Doesn't quite understand that _this_ $opts for the current $form_definitions does have 'data'
DB::remove(DB::getReference('feed', ['actor_id' => $user->getId(), 'url' => $opts['data']])); DB::remove(DB::getReference('feed', ['actor_id' => $user->getId(), 'url' => $opts['data']]));
DB::flush(); DB::flush();
Cache::delete($key); Cache::delete($key);
@ -110,7 +121,9 @@ class EditFeeds extends Controller
} }
} }
if ($form->get('reset')->isClicked()) { /** @var SubmitButton $reset_button */
$reset_button = $form->get('reset');
if ($reset_button->isClicked()) {
F\map(DB::findBy('feed', ['actor_id' => $user->getId()]), fn ($f) => DB::remove($f)); F\map(DB::findBy('feed', ['actor_id' => $user->getId()]), fn ($f) => DB::remove($f));
DB::flush(); DB::flush();
Cache::delete($key); Cache::delete($key);
@ -134,6 +147,7 @@ class EditFeeds extends Controller
Cache::delete($key); Cache::delete($key);
throw new RedirectException(); throw new RedirectException();
} catch (ResourceNotFoundException) { } catch (ResourceNotFoundException) {
// TODO add error (flash?)
// throw new ClientException(_m('Invalid route')); // throw new ClientException(_m('Invalid route'));
// continue bellow // continue bellow
} }

View File

@ -11,6 +11,7 @@ use function App\Core\I18n\_m;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Security\EmailVerifier; use App\Security\EmailVerifier;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NotImplementedException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Form\FormFields; use App\Util\Form\FormFields;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
@ -36,20 +37,21 @@ class ResetPassword extends Controller
*/ */
public function requestPasswordReset(Request $request) public function requestPasswordReset(Request $request)
{ {
$form = Form::create([ throw new NotImplementedException;
['email', EmailType::class, ['label' => _m('Email'), 'constraints' => [new NotBlank(['message' => _m('Please enter an email')])]]], // $form = Form::create([
['password_reset_request', SubmitType::class, ['label' => _m('Submit request')]], // ['email', EmailType::class, ['label' => _m('Email'), 'constraints' => [new NotBlank(['message' => _m('Please enter an email')])]]],
]); // ['password_reset_request', SubmitType::class, ['label' => _m('Submit request')]],
// ]);
$form->handleRequest($request); // $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { // if ($form->isSubmitted() && $form->isValid()) {
return EmailVerifier::processSendingPasswordResetEmail($form->get('email')->getData(), $this); // return EmailVerifier::processSendingPasswordResetEmail($form->get('email')->getData(), $this);
} // }
return [ // return [
'_template' => 'reset_password/request.html.twig', // '_template' => 'reset_password/request.html.twig',
'password_reset_form' => $form->createView(), // 'password_reset_form' => $form->createView(),
]; // ];
} }
/** /**
@ -57,15 +59,17 @@ class ResetPassword extends Controller
*/ */
public function checkEmail() public function checkEmail()
{ {
// We prevent users from directly accessing this page throw new NotImplementedException;
if (null === ($resetToken = $this->getTokenObjectFromSession())) {
throw new RedirectException('request_reset_password');
}
return [ // // We prevent users from directly accessing this page
'_template' => 'reset_password/check_email.html.twig', // if (null === ($resetToken = $this->getTokenObjectFromSession())) {
'resetToken' => $resetToken, // throw new RedirectException('request_reset_password');
]; // }
// return [
// '_template' => 'reset_password/check_email.html.twig',
// 'resetToken' => $resetToken,
// ];
} }
/** /**
@ -73,53 +77,57 @@ class ResetPassword extends Controller
*/ */
public function reset(Request $request, ?string $token = null) public function reset(Request $request, ?string $token = null)
{ {
if ($token) { throw new NotImplementedException;
// We store the token in session and remove it from the URL, to avoid the URL being
// loaded in a browser and potentially leaking the token to 3rd party JavaScript.
$this->storeTokenInSession($token);
throw new RedirectException('reset_password');
}
$token = $this->getTokenFromSession(); // if ($token) {
if (null === $token) { // // We store the token in session and remove it from the URL, to avoid the URL being
throw new ClientException(_m('No reset password token found in the URL or in the session')); // // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
} // $this->storeTokenInSession($token);
// throw new RedirectException('reset_password');
// }
try { // $token = $this->getTokenFromSession();
$user = EmailVerifier::validateTokenAndFetchUser($token); // if (null === $token) {
} catch (ResetPasswordExceptionInterface $e) { // throw new ClientException(_m('No reset password token found in the URL or in the session'));
$this->addFlash('reset_password_error', _m('There was a problem validating your reset request - {reason}', ['reason' => $e->getReason()])); // }
throw new RedirectException('request_reset_password');
}
// The token is valid; allow the user to change their password. // try {
$form = Form::create([ // $user = EmailVerifier::validateTokenAndFetchUser($token);
FormFields::repeated_password(), // } catch (ResetPasswordExceptionInterface $e) {
['password_reset', SubmitType::class, ['label' => _m('Change password')]], // $this->addFlash('reset_password_error', _m('There was a problem validating your reset request - {reason}', ['reason' => $e->getReason()]));
]); // throw new RedirectException('request_reset_password');
// }
$form->handleRequest($request); // // The token is valid; allow the user to change their password.
if ($form->isSubmitted() && $form->isValid()) { // $form = Form::create([
// A password reset token should be used only once, remove it. // FormFields::repeated_password(),
EmailVerifier::removeResetRequest($token); // ['password_reset', SubmitType::class, ['label' => _m('Change password')]],
// ]);
$user->setPassword(LocalUser::hashPassword($form->get('password')->getData())); // $form->handleRequest($request);
DB::flush(); // if ($form->isSubmitted() && $form->isValid()) {
// // A password reset token should be used only once, remove it.
// EmailVerifier::removeResetRequest($token);
// The session is cleaned up after the password has been changed. // $user->setPassword(LocalUser::hashPassword($form->get('password')->getData()));
$this->cleanSessionAfterReset(); // DB::flush();
throw new RedirectException('main_all'); // // The session is cleaned up after the password has been changed.
} // $this->cleanSessionAfterReset();
return [ // throw new RedirectException('main_all');
'_template' => 'reset_password/reset.html.twig', // }
'resetForm' => $form->createView(),
]; // return [
// '_template' => 'reset_password/reset.html.twig',
// 'resetForm' => $form->createView(),
// ];
} }
public function setInSession(ResetPasswordToken $reset_token) public function setInSession(ResetPasswordToken $reset_token)
{ {
$this->setTokenObjectInSession($reset_token); throw new NotImplementedException;
// $this->setTokenObjectInSession($reset_token);
} }
} }

View File

@ -268,7 +268,7 @@ class Actor extends Entity
/** /**
* Tags attributed to self, shortcut function for increased legibility * Tags attributed to self, shortcut function for increased legibility
* *
* @return [ActorCircle[], ActorTag[]] * @return array<int, array> [ActorCircle[], ActorTag[]] resulting lists
*/ */
public function getSelfTags(bool $_test_force_recompute = false): array public function getSelfTags(bool $_test_force_recompute = false): array
{ {
@ -285,7 +285,7 @@ class Actor extends Entity
* @param null|int $offset Offset from latest * @param null|int $offset Offset from latest
* @param null|int $limit Max number to get * @param null|int $limit Max number to get
* *
* @return [ActorCircle[], ActorTag[]] resulting lists * @return array<int, array> [ActorCircle[], ActorTag[]] resulting lists
*/ */
public function getOtherTags(self|int|null $scoped = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array public function getOtherTags(self|int|null $scoped = null, ?int $offset = null, ?int $limit = null, bool $_test_force_recompute = false): array
{ {

View File

@ -97,7 +97,7 @@ class ActorLanguage extends Entity
} }
/** /**
* @return self[] * @return Language[]
*/ */
public static function getActorLanguages(LocalUser|Actor $actor, ?Actor $context): array public static function getActorLanguages(LocalUser|Actor $actor, ?Actor $context): array
{ {

View File

@ -68,7 +68,7 @@ class AttachmentTest extends GNUsocialTestCase
public function testAttachmentViewNotStored() public function testAttachmentViewNotStored()
{ {
$client = static::createClient(); $client = static::createClient();
$last_attachment = DB::findBy('attachment', [], orderBy: ['id' => 'DESC'], limit: 1)[0]; $last_attachment = DB::findBy('attachment', [], order_by: ['id' => 'DESC'], limit: 1)[0];
$id = $last_attachment->getId() + 1; $id = $last_attachment->getId() + 1;
$crawler = $client->request('GET', "/attachment/{$id}/view"); $crawler = $client->request('GET', "/attachment/{$id}/view");
$this->assertResponseStatusCodeSame(500); // TODO (exception page) 404 $this->assertResponseStatusCodeSame(500); // TODO (exception page) 404