From b01c94cbc1d7b5d0bea091c7174b8d5a812e4683 Mon Sep 17 00:00:00 2001
From: Camille Simiand <camille.simiand@tetras-libre.fr>
Date: Mon, 10 Jan 2022 12:54:32 +0100
Subject: [PATCH] Increase and fix errors phpstan level 8

---
 phpstan-tests.neon                            |  2 +-
 phpstan.neon                                  |  2 +-
 src/Controller/FallbackController.php         |  1 +
 src/DataFixtures/CapsuleFixtures.php          | 10 ++++++++++
 src/Entity/Capsule.php                        |  2 +-
 src/Entity/User.php                           |  4 ++--
 tests/functional/ProjectControllerTest.php    | 19 +++++++++++++++++-
 .../functional/RegistrationControllerTest.php | 20 ++++++++++++++-----
 .../ResetPasswordControllerTest.php           | 10 ++++++++--
 9 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/phpstan-tests.neon b/phpstan-tests.neon
index 64e8c88..296aee8 100644
--- a/phpstan-tests.neon
+++ b/phpstan-tests.neon
@@ -1,5 +1,5 @@
 parameters:
-    level: 7
+    level: 8
     paths:
         - tests/
     symfony:
diff --git a/phpstan.neon b/phpstan.neon
index a703b75..090087e 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -1,5 +1,5 @@
 parameters:
-    level: 7
+    level: 8
     paths:
         - src/
     symfony:
diff --git a/src/Controller/FallbackController.php b/src/Controller/FallbackController.php
index dfa9739..181aca6 100644
--- a/src/Controller/FallbackController.php
+++ b/src/Controller/FallbackController.php
@@ -103,6 +103,7 @@ class FallbackController extends AbstractController
             $url = preg_replace('(^https?:\/\/[^/]+(:\d+)?)', '', $url);
 
             $pattern = '/\/\//i';
+            /** @phpstan-ignore-next-line */
             $url = preg_replace($pattern, '/', $url);
 
             return $this->render("project/edit.html.twig", [
diff --git a/src/DataFixtures/CapsuleFixtures.php b/src/DataFixtures/CapsuleFixtures.php
index 43f6ea2..18f5460 100644
--- a/src/DataFixtures/CapsuleFixtures.php
+++ b/src/DataFixtures/CapsuleFixtures.php
@@ -18,7 +18,17 @@ class CapsuleFixtures extends Fixture implements DependentFixtureInterface
         $user_repository = $manager->getRepository(User::class);
 
         $verified_user1 = $user_repository->findOneBy(['email' => "defaultUser@localhost.com"]);
+
+        if (! $verified_user1 instanceof User) {
+            throw new \Exception("User does not exist.");
+        }
+
         $verified_user2 = $user_repository->findOneBy(['email' => "defaultUser2@localhost.com"]);
+
+        if (! $verified_user2 instanceof User) {
+            throw new \Exception("User does not exist.");
+        }
+
         $new_date_time = new DateTime();
         $uuid_first_capsule = Uuid::v4();
         $uuid_second_capsule = Uuid::v4();
diff --git a/src/Entity/Capsule.php b/src/Entity/Capsule.php
index b3dc11d..ef40356 100644
--- a/src/Entity/Capsule.php
+++ b/src/Entity/Capsule.php
@@ -136,7 +136,7 @@ class Capsule
         return $this;
     }
 
-    public function getUpdateAuthor(): User
+    public function getUpdateAuthor(): ?User
     {
         return $this->update_author;
     }
diff --git a/src/Entity/User.php b/src/Entity/User.php
index 1bbf9a3..d95519f 100644
--- a/src/Entity/User.php
+++ b/src/Entity/User.php
@@ -87,7 +87,7 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface
         return $this->id;
     }
 
-    public function getEmail(): ?string
+    public function getEmail(): string
     {
         return $this->email;
     }
@@ -116,7 +116,7 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface
      */
     public function getUsername(): string
     {
-        return (string) $this->email;
+        return $this->email;
     }
 
     /**
diff --git a/tests/functional/ProjectControllerTest.php b/tests/functional/ProjectControllerTest.php
index c144d7f..efc7891 100644
--- a/tests/functional/ProjectControllerTest.php
+++ b/tests/functional/ProjectControllerTest.php
@@ -35,7 +35,10 @@ class ProjectControllerTest extends WebTestCase
             ->getRepository(User::class)
             ->findOneBy(['email' => 'defaultUser@localhost.com'])
         ;
-        self::assertInstanceOf(User::class, $verified_user);
+
+        if (! $verified_user instanceof User) {
+            throw new \Exception("User does not exist.");
+        }
 
         $this->client->loginUser($verified_user);
 
@@ -56,6 +59,11 @@ class ProjectControllerTest extends WebTestCase
 
         $capsule_repository = $this->object_manager->getRepository(Capsule::class);
         $last_capsule = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
+
+        if (! $last_capsule instanceof Capsule) {
+            throw new \Exception("Capsule does not exist.");
+        }
+
         $this->object_manager->remove($last_capsule);
         $this->object_manager->flush();
     }
@@ -89,12 +97,21 @@ class ProjectControllerTest extends WebTestCase
         $capsule_repository = $this->object_manager->getRepository(Capsule::class);
         $capsule_in_db = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
 
+        if (! $capsule_in_db instanceof Capsule) {
+            throw new \Exception("Capsule does not exist.");
+        }
+
         self::assertInstanceOf(Capsule::class, $capsule_in_db);
         $capsule_name_in_db = $capsule_in_db->getName();
 
         $dom_xml = $this->getDomDocument();
 
         $video_node = $dom_xml->getElementsByTagName('video')->item(0);
+
+        if ($video_node === null) {
+            throw new \Exception("Video node could not be found in XML project file");
+        }
+
         $video_url_in_xml_file = $video_node->getAttribute('url');
 
         $this->assertResponseIsSuccessful('/my_capsules');
diff --git a/tests/functional/RegistrationControllerTest.php b/tests/functional/RegistrationControllerTest.php
index 39eca6b..85fca77 100644
--- a/tests/functional/RegistrationControllerTest.php
+++ b/tests/functional/RegistrationControllerTest.php
@@ -2,6 +2,7 @@
 
 namespace App\Tests\functional;
 
+use App\Entity\Capsule;
 use App\Entity\User;
 use App\Repository\UserRepository;
 use Symfony\Bundle\FrameworkBundle\KernelBrowser;
@@ -83,8 +84,7 @@ class RegistrationControllerTest extends WebTestCase
 
     public function testAnAuthenticatedUserShouldNotBeAbleToAccessTheRegisterPage(): void
     {
-        $userRepository = static::getContainer()->get(UserRepository::class);
-        $testUser = $userRepository->findOneByEmail('defaultUser@localhost.com');
+        $testUser = $this->getUserByEmail('defaultUser@localhost.com');
 
         $this->client->loginUser($testUser);
         $this->client->request('GET', '/register');
@@ -94,8 +94,7 @@ class RegistrationControllerTest extends WebTestCase
 
     public function testAnAuthenticatedUserShouldNotBeAbleToAccessTheLoginPage(): void
     {
-        $userRepository = static::getContainer()->get(UserRepository::class);
-        $testUser = $userRepository->findOneByEmail('defaultUser@localhost.com');
+        $testUser = $this->getUserByEmail('defaultUser@localhost.com');
 
         $this->client->loginUser($testUser);
         $this->client->request('GET', '/login');
@@ -125,6 +124,11 @@ class RegistrationControllerTest extends WebTestCase
             'Once the user has been registered, the system should send an email to this user'
         );
         $emailMessage = $this->getMailerMessage(0);
+
+        if (null === $emailMessage) {
+            throw new \Exception("Email message could not be found");
+        }
+
         $this->assertEmailAddressContains(
             $emailMessage,
             'To',
@@ -180,6 +184,12 @@ class RegistrationControllerTest extends WebTestCase
     private function getUserByEmail(string $userEmail): User
     {
         $userRepository = static::getContainer()->get(UserRepository::class);
-        return $userRepository->findOneByEmail($userEmail);
+        $user = $userRepository->findOneByEmail($userEmail);
+
+        if (! $user instanceof User) {
+            throw new \Exception("User does not exist.");
+        }
+
+        return $user;
     }
 }
diff --git a/tests/functional/ResetPasswordControllerTest.php b/tests/functional/ResetPasswordControllerTest.php
index 9fc7b8d..0e7e57c 100644
--- a/tests/functional/ResetPasswordControllerTest.php
+++ b/tests/functional/ResetPasswordControllerTest.php
@@ -61,7 +61,7 @@ class ResetPasswordControllerTest extends WebTestCase
         $this->testRegisteredUserWithEnabledAccountIsRedirectedToCapsulesPageWhenSubmittingLoginForm();
     }
 
-    private function getEmailMessageWithResetPasswordLinkForVerifiedUser(): ?RawMessage
+    private function getEmailMessageWithResetPasswordLinkForVerifiedUser(): RawMessage
     {
         $crawler = $this->client->request('GET', '/reset-password');
         $this->assertResponseIsSuccessful();
@@ -72,7 +72,13 @@ class ResetPasswordControllerTest extends WebTestCase
         $form['reset_password_request_form[email]'] = self::VERIFIED_USER_EMAIL;
         $this->client->submit($form);
 
-        return $this->getMailerMessage(0);
+        $email_message = $this->getMailerMessage(0);
+
+        if (null === $email_message) {
+            throw new \Exception("Email message could not be found");
+        }
+
+        return $email_message;
     }
 
     private function submitResetPasswordFormWithAnUnverifiedEmailAddress(): void
-- 
GitLab