1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00

PHPStan level 5

This commit is contained in:
Yassine Guedidi 2025-04-21 16:27:44 +02:00
parent 3ef7064ada
commit 36eb513e1b
22 changed files with 60 additions and 47 deletions

View file

@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon - phpstan-baseline.neon
parameters: parameters:
level: 4 level: 5
paths: paths:
- src - src
- tests - tests

View file

@ -96,6 +96,7 @@ class ImportCommand extends Command
$this->tokenStorage->setToken($token); $this->tokenStorage->setToken($token);
$user = $this->tokenStorage->getToken()->getUser(); $user = $this->tokenStorage->getToken()->getUser();
\assert($user instanceof User);
$import = match ($input->getOption('importer')) { $import = match ($input->getOption('importer')) {
'v2' => $this->wallabagV2Import, 'v2' => $this->wallabagV2Import,

View file

@ -308,10 +308,10 @@ class EntryRestController extends WallabagRestController
$isNotParsed = (null === $request->query->get('notParsed')) ? null : (bool) $request->query->get('notParsed'); $isNotParsed = (null === $request->query->get('notParsed')) ? null : (bool) $request->query->get('notParsed');
$sort = strtolower($request->query->get('sort', 'created')); $sort = strtolower($request->query->get('sort', 'created'));
$order = strtolower($request->query->get('order', 'desc')); $order = strtolower($request->query->get('order', 'desc'));
$page = (int) $request->query->get('page', 1); $page = $request->query->getInt('page', 1);
$perPage = (int) $request->query->get('perPage', 30); $perPage = $request->query->getInt('perPage', 30);
$tags = \is_array($request->query->all()['tags'] ?? '') ? '' : (string) $request->query->get('tags', ''); $tags = \is_array($request->query->all()['tags'] ?? '') ? '' : (string) $request->query->get('tags', '');
$since = $request->query->get('since', 0); $since = $request->query->getInt('since');
$detail = strtolower($request->query->get('detail', 'full')); $detail = strtolower($request->query->get('detail', 'full'));
$domainName = (null === $request->query->get('domain_name')) ? '' : (string) $request->query->get('domain_name'); $domainName = (null === $request->query->get('domain_name')) ? '' : (string) $request->query->get('domain_name');
$httpStatus = (!\array_key_exists((int) $request->query->get('http_status'), Response::$statusTexts)) ? null : (int) $request->query->get('http_status'); $httpStatus = (!\array_key_exists((int) $request->query->get('http_status'), Response::$statusTexts)) ? null : (int) $request->query->get('http_status');

View file

@ -62,8 +62,8 @@ class SearchRestController extends WallabagRestController
public function getSearchAction(Request $request, EntryRepository $entryRepository) public function getSearchAction(Request $request, EntryRepository $entryRepository)
{ {
$term = $request->query->get('term'); $term = $request->query->get('term');
$page = (int) $request->query->get('page', 1); $page = $request->query->getInt('page', 1);
$perPage = (int) $request->query->get('perPage', 30); $perPage = $request->query->getInt('perPage', 30);
$qb = $entryRepository->getBuilderForSearchByUser( $qb = $entryRepository->getBuilderForSearchByUser(
$this->getUser()->getId(), $this->getUser()->getId(),

View file

@ -647,7 +647,7 @@ class ConfigController extends AbstractController
} }
$user = $this->getUser(); $user = $this->getUser();
$user->getConfig()->setListMode(!$user->getConfig()->getListMode()); $user->getConfig()->setListMode((int) !$user->getConfig()->getListMode());
$this->entityManager->persist($user); $this->entityManager->persist($user);
$this->entityManager->flush(); $this->entityManager->flush();

View file

@ -102,7 +102,7 @@ class EntryController extends AbstractController
if (isset($values['entry-checkbox'])) { if (isset($values['entry-checkbox'])) {
foreach ($values['entry-checkbox'] as $id) { foreach ($values['entry-checkbox'] as $id) {
/** @var Entry * */ /** @var Entry * */
$entry = $this->entryRepository->findById((int) $id)[0]; $entry = $this->entryRepository->findById([(int) $id])[0];
if (!$this->security->isGranted('EDIT', $entry)) { if (!$this->security->isGranted('EDIT', $entry)) {
throw $this->createAccessDeniedException('You can not access this entry.'); throw $this->createAccessDeniedException('You can not access this entry.');

View file

@ -54,7 +54,7 @@ class ExportController extends AbstractController
if ('same_domain' === $category) { if ('same_domain' === $category) {
$entries = $entryRepository->getBuilderForSameDomainByUser( $entries = $entryRepository->getBuilderForSameDomainByUser(
$this->getUser()->getId(), $this->getUser()->getId(),
$request->query->get('entry') $request->query->getInt('entry')
)->getQuery() )->getQuery()
->getResult(); ->getResult();

View file

@ -238,7 +238,7 @@ class Config
/** /**
* Set feed Token. * Set feed Token.
* *
* @param string $feedToken * @param string|null $feedToken
* *
* @return Config * @return Config
*/ */
@ -252,7 +252,7 @@ class Config
/** /**
* Get feedToken. * Get feedToken.
* *
* @return string * @return string|null
*/ */
public function getFeedToken() public function getFeedToken()
{ {

View file

@ -383,7 +383,7 @@ class Entry
public function toggleArchive() public function toggleArchive()
{ {
$this->updateArchived($this->isArchived() ^ 1); $this->updateArchived((bool) ($this->isArchived() ^ 1));
return $this; return $this;
} }

View file

@ -8,6 +8,7 @@ use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Wallabag\Entity\Config; use Wallabag\Entity\Config;
use Wallabag\Entity\User;
/** /**
* This listener will create the associated configuration when a user register. * This listener will create the associated configuration when a user register.
@ -48,7 +49,10 @@ class CreateConfigListener implements EventSubscriberInterface
$language = $session->get('_locale', $this->language); $language = $session->get('_locale', $this->language);
} }
$config = new Config($event->getUser()); $user = $event->getUser();
\assert($user instanceof User);
$config = new Config($user);
$config->setItemsPerPage($this->itemsOnPage); $config->setItemsPerPage($this->itemsOnPage);
$config->setFeedLimit($this->feedLimit); $config->setFeedLimit($this->feedLimit);
$config->setLanguage($language); $config->setLanguage($language);

View file

@ -208,7 +208,7 @@ class DownloadImages
case 'png': case 'png':
imagealphablending($im, false); imagealphablending($im, false);
imagesavealpha($im, true); imagesavealpha($im, true);
imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9)); imagepng($im, $localPath, (int) ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9));
$this->logger->debug('DownloadImages: Re-creating png'); $this->logger->debug('DownloadImages: Re-creating png');
break; break;
case 'webp': case 'webp':
@ -254,7 +254,7 @@ class DownloadImages
*/ */
public function getRelativePath($entryId, $createFolder = true) public function getRelativePath($entryId, $createFolder = true)
{ {
$hashId = hash('crc32', $entryId); $hashId = hash('crc32', (string) $entryId);
$relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId; $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
$folderPath = $this->baseFolder . '/' . $relativePath; $folderPath = $this->baseFolder . '/' . $relativePath;

View file

@ -285,13 +285,13 @@ class EntriesExport
'<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' . '<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' .
'<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' . '<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' .
'</dl>'; '</dl>';
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $pdf->writeHTMLCell(0, 0, null, null, $html, 0, 1);
$pdf->AddPage(); $pdf->AddPage();
$html = '<h1>' . $entry->getTitle() . '</h1>'; $html = '<h1>' . $entry->getTitle() . '</h1>';
$html .= $entry->getContent(); $html .= $entry->getContent();
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $pdf->writeHTMLCell(0, 0, null, null, $html, 0, 1);
} }
/* /*
@ -300,7 +300,7 @@ class EntriesExport
$pdf->AddPage(); $pdf->AddPage();
$html = $this->getExportInformation('tcpdf'); $html = $this->getExportInformation('tcpdf');
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $pdf->writeHTMLCell(0, 0, null, null, $html, 0, 1);
// set image scale factor // set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

View file

@ -5,9 +5,10 @@ namespace Wallabag\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Wallabag\Entity\Config; use Wallabag\Entity\Config;
use Wallabag\Entity\User;
/** /**
* @method Config|null findOneByUser(int $userId) * @method Config|null findOneByUser(User $user)
*/ */
class ConfigRepository extends ServiceEntityRepository class ConfigRepository extends ServiceEntityRepository
{ {

View file

@ -13,7 +13,7 @@ use Wallabag\Entity\Tag;
use Wallabag\Helper\UrlHasher; use Wallabag\Helper\UrlHasher;
/** /**
* @method Entry[] findById(int $id) * @method Entry[] findById(int[] $id)
* @method Entry|null findOneByUser(int $userId) * @method Entry|null findOneByUser(int $userId)
*/ */
class EntryRepository extends ServiceEntityRepository class EntryRepository extends ServiceEntityRepository
@ -171,9 +171,9 @@ class EntryRepository extends ServiceEntityRepository
/** /**
* Retrieves entries filtered with a search term for a user. * Retrieves entries filtered with a search term for a user.
* *
* @param int $userId * @param int $userId
* @param string $term * @param string $term
* @param string $currentRoute * @param 'starred'|'unread'|'homepage'|'archive'|null $currentRoute
* *
* @return QueryBuilder * @return QueryBuilder
*/ */

View file

@ -5,12 +5,13 @@ namespace Wallabag\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Wallabag\Entity\SiteCredential; use Wallabag\Entity\SiteCredential;
use Wallabag\Entity\User;
use Wallabag\Helper\CryptoProxy; use Wallabag\Helper\CryptoProxy;
/** /**
* SiteCredentialRepository. * SiteCredentialRepository.
* *
* @method SiteCredential[] findByUser(int $userId) * @method SiteCredential[] findByUser(User $user)
*/ */
class SiteCredentialRepository extends ServiceEntityRepository class SiteCredentialRepository extends ServiceEntityRepository
{ {

View file

@ -5,6 +5,7 @@ namespace Tests\Wallabag\Controller\Api;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Tests\Wallabag\WallabagTestCase; use Tests\Wallabag\WallabagTestCase;
use Wallabag\Entity\Api\Client; use Wallabag\Entity\Api\Client;
use Wallabag\Entity\User;
class DeveloperControllerTest extends WallabagTestCase class DeveloperControllerTest extends WallabagTestCase
{ {
@ -133,7 +134,10 @@ class DeveloperControllerTest extends WallabagTestCase
$client = $this->getTestClient(); $client = $this->getTestClient();
$em = $client->getContainer()->get(EntityManagerInterface::class); $em = $client->getContainer()->get(EntityManagerInterface::class);
$userManager = static::getContainer()->get('fos_user.user_manager'); $userManager = static::getContainer()->get('fos_user.user_manager');
$user = $userManager->findUserBy(['username' => $username]); $user = $userManager->findUserBy(['username' => $username]);
\assert($user instanceof User);
$apiClient = new Client($user); $apiClient = new Client($user);
$apiClient->setName('My app'); $apiClient->setName('My app');
$apiClient->setAllowedGrantTypes($grantTypes); $apiClient->setAllowedGrantTypes($grantTypes);

View file

@ -45,7 +45,7 @@ class UserRestControllerTest extends WallabagApiTestCase
public function testCreateNewUser() public function testCreateNewUser()
{ {
$this->client->getContainer()->get(Config::class)->set('api_user_registration', 1); $this->client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$this->client->request('PUT', '/api/user.json', [ $this->client->request('PUT', '/api/user.json', [
'username' => 'google', 'username' => 'google',
'password' => 'googlegoogle', 'password' => 'googlegoogle',
@ -73,14 +73,14 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
$this->client->getContainer()->get(Config::class)->set('api_user_registration', 0); $this->client->getContainer()->get(Config::class)->set('api_user_registration', '0');
} }
public function testCreateNewUserWithoutAuthentication() public function testCreateNewUserWithoutAuthentication()
{ {
// create a new client instead of using $this->client to be sure client isn't authenticated // create a new client instead of using $this->client to be sure client isn't authenticated
$client = $this->createUnauthorizedClient(); $client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', 1); $client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [ $client->request('PUT', '/api/user.json', [
'username' => 'google', 'username' => 'google',
'password' => 'googlegoogle', 'password' => 'googlegoogle',
@ -109,13 +109,13 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', 0); $client->getContainer()->get(Config::class)->set('api_user_registration', '0');
} }
public function testCreateNewUserWithExistingEmail() public function testCreateNewUserWithExistingEmail()
{ {
$client = $this->createUnauthorizedClient(); $client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', 1); $client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [ $client->request('PUT', '/api/user.json', [
'username' => 'admin', 'username' => 'admin',
'password' => 'googlegoogle', 'password' => 'googlegoogle',
@ -138,13 +138,13 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', 0); $client->getContainer()->get(Config::class)->set('api_user_registration', '0');
} }
public function testCreateNewUserWithTooShortPassword() public function testCreateNewUserWithTooShortPassword()
{ {
$client = $this->createUnauthorizedClient(); $client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', 1); $client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [ $client->request('PUT', '/api/user.json', [
'username' => 'facebook', 'username' => 'facebook',
'password' => 'face', 'password' => 'face',
@ -162,7 +162,7 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', 0); $client->getContainer()->get(Config::class)->set('api_user_registration', '0');
} }
public function testCreateNewUserWhenRegistrationIsDisabled() public function testCreateNewUserWhenRegistrationIsDisabled()

View file

@ -3,7 +3,6 @@
namespace Tests\Wallabag\Controller\Api; namespace Tests\Wallabag\Controller\Api;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManager; use FOS\UserBundle\Model\UserManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -17,7 +16,7 @@ abstract class WallabagApiTestCase extends WebTestCase
protected $client; protected $client;
/** /**
* @var UserInterface * @var User
*/ */
protected $user; protected $user;
@ -49,9 +48,12 @@ abstract class WallabagApiTestCase extends WebTestCase
$userManager = $container->get('fos_user.user_manager'); $userManager = $container->get('fos_user.user_manager');
$firewallName = $container->getParameter('fos_user.firewall_name'); $firewallName = $container->getParameter('fos_user.firewall_name');
$this->user = $userManager->findUserBy(['username' => 'admin']); $adminUser = $userManager->findUserBy(['username' => 'admin']);
\assert($adminUser instanceof User);
$client->loginUser($this->user, $firewallName); $this->user = $adminUser;
$client->loginUser($adminUser, $firewallName);
return $client; return $client;
} }

View file

@ -45,7 +45,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase
$this->markTestSkipped('fosuser_registration is not enabled.'); $this->markTestSkipped('fosuser_registration is not enabled.');
} }
$client->getContainer()->get(Config::class)->set('api_user_registration', 1); $client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('GET', '/api/info'); $client->request('GET', '/api/info');
@ -53,7 +53,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase
$this->assertTrue($content['allowed_registration']); $this->assertTrue($content['allowed_registration']);
$client->getContainer()->get(Config::class)->set('api_user_registration', 0); $client->getContainer()->get(Config::class)->set('api_user_registration', '0');
$client->request('GET', '/api/info'); $client->request('GET', '/api/info');

View file

@ -32,7 +32,7 @@ class EntryControllerTest extends WallabagTestCase
{ {
if ($this->downloadImagesEnabled) { if ($this->downloadImagesEnabled) {
$client = static::createClient(); $client = static::createClient();
$client->getContainer()->get(Config::class)->set('download_images_enabled', 0); $client->getContainer()->get(Config::class)->set('download_images_enabled', '0');
$this->downloadImagesEnabled = false; $this->downloadImagesEnabled = false;
} }
@ -689,7 +689,7 @@ class EntryControllerTest extends WallabagTestCase
->getRepository(Entry::class) ->getRepository(Entry::class)
->find($entry->getId()); ->find($entry->getId());
$this->assertSame(1, $res->isArchived()); $this->assertTrue($res->isArchived());
} }
public function testToggleStar() public function testToggleStar()
@ -1400,7 +1400,7 @@ class EntryControllerTest extends WallabagTestCase
$entry = new Entry($this->getLoggedInUser()); $entry = new Entry($this->getLoggedInUser());
$entry->setUrl('https://www.lemonde.fr/incorrect-url/'); $entry->setUrl('https://www.lemonde.fr/incorrect-url/');
$entry->setHttpStatus(404); $entry->setHttpStatus('404');
$this->getEntityManager()->persist($entry); $this->getEntityManager()->persist($entry);
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
@ -1418,12 +1418,12 @@ class EntryControllerTest extends WallabagTestCase
$entry = new Entry($this->getLoggedInUser()); $entry = new Entry($this->getLoggedInUser());
$entry->setUrl($this->url); $entry->setUrl($this->url);
$entry->setHttpStatus(200); $entry->setHttpStatus('200');
$this->getEntityManager()->persist($entry); $this->getEntityManager()->persist($entry);
$entry = new Entry($this->getLoggedInUser()); $entry = new Entry($this->getLoggedInUser());
$entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm'); $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
$entry->setHttpStatus(200); $entry->setHttpStatus('200');
$this->getEntityManager()->persist($entry); $this->getEntityManager()->persist($entry);
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
@ -1868,14 +1868,14 @@ class EntryControllerTest extends WallabagTestCase
->getRepository(Entry::class) ->getRepository(Entry::class)
->find($entry1->getId()); ->find($entry1->getId());
$this->assertSame(1, $res->isArchived()); $this->assertTrue($res->isArchived());
$res = $client->getContainer() $res = $client->getContainer()
->get(EntityManagerInterface::class) ->get(EntityManagerInterface::class)
->getRepository(Entry::class) ->getRepository(Entry::class)
->find($entry2->getId()); ->find($entry2->getId());
$this->assertSame(1, $res->isArchived()); $this->assertTrue($res->isArchived());
$crawler = $client->request('GET', '/all/list'); $crawler = $client->request('GET', '/all/list');
$token = $crawler->filter('#form_mass_action input[name=token]')->attr('value'); $token = $crawler->filter('#form_mass_action input[name=token]')->attr('value');

View file

@ -127,7 +127,7 @@ class FeedControllerTest extends WallabagTestCase
$client = $this->getTestClient(); $client = $this->getTestClient();
$client->request('GET', '/feed/admin/SUPERTOKEN/starred'); $client->request('GET', '/feed/admin/SUPERTOKEN/starred');
$this->assertSame(200, $client->getResponse()->getStatusCode(), 1); $this->assertSame(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'starred'); $this->validateDom($client->getResponse()->getContent(), 'starred');
} }

View file

@ -54,7 +54,7 @@ TWIG;
$user = new User(); $user = new User();
$user->setEmailTwoFactor(true); $user->setEmailTwoFactor(true);
$user->setEmailAuthCode(666666); $user->setEmailAuthCode('666666');
$user->setEmail('test@wallabag.io'); $user->setEmail('test@wallabag.io');
$user->setName('Bob'); $user->setName('Bob');