From b74d944ae3121a4f00ed89a0f25bf2c9cb53e273 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 9 Sep 2021 01:08:45 +0100 Subject: [PATCH] [TOOLS][PHPStan] Raise PHPStan level to 3 and fix new errors --- phpstan.neon | 6 +++++- plugins/Embed/Entity/AttachmentEmbed.php | 2 +- src/Core/Form.php | 3 ++- src/Core/Security.php | 3 +++ src/Util/Common.php | 4 ++++ tests/Twig/ExtensionTest.php | 3 ++- 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index a190cadce8..dacb28b0e4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 2 + level: 3 bootstrapFiles: - config/bootstrap.php earlyTerminatingMethodCalls: @@ -10,6 +10,10 @@ parameters: message: '/Access to an undefined property App\\Util\\Bitmap::\$\w+/' paths: - * + # - + # message: '/has no return typehint specified/' + # paths: + # - tests/* services: - diff --git a/plugins/Embed/Entity/AttachmentEmbed.php b/plugins/Embed/Entity/AttachmentEmbed.php index 6dfb6e2688..899a39b5a7 100644 --- a/plugins/Embed/Entity/AttachmentEmbed.php +++ b/plugins/Embed/Entity/AttachmentEmbed.php @@ -182,7 +182,7 @@ class AttachmentEmbed extends Entity /** * Generate the Embed thumbnail HTML attributes * - * @return string[] ['class' => "string", 'has_attachment' => "bool", 'height' => "int|null", 'width' => "int|null"] + * @return mixed[] ['class' => string, 'has_attachment' => bool, 'height' => int|null, 'width' => int|null] */ public function getImageHTMLAttributes(): array { diff --git a/src/Core/Form.php b/src/Core/Form.php index cfeae980ce..b0b19f8568 100644 --- a/src/Core/Form.php +++ b/src/Core/Form.php @@ -36,6 +36,7 @@ use App\Util\Formatting; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Form as SymfForm; use Symfony\Component\Form\FormFactoryInterface; +use Symfony\Component\Form\FormInterface as SymfFormInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -86,7 +87,7 @@ abstract class Form ?object $target = null, array $extra_data = [], string $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', - array $form_options = []): SymfForm + array $form_options = []): SymfFormInterface { $name = $form[array_key_last($form)][0]; $fb = self::$form_factory->createNamedBuilder($name, $type, data: null, options: array_merge($form_options, ['translation_domain' => false])); diff --git a/src/Core/Security.php b/src/Core/Security.php index ad1cf9ce40..a063a00f4e 100644 --- a/src/Core/Security.php +++ b/src/Core/Security.php @@ -30,6 +30,7 @@ namespace App\Core; +use App\Entity\LocalUser; use HtmlSanitizer\SanitizerInterface; use Symfony\Component\Security\Core\Security as SymfonySecurity; @@ -40,6 +41,8 @@ use Symfony\Component\Security\Core\Security as SymfonySecurity; * @codeCoverageIgnore * @mixin SymfonySecurity * @mixin SanitizerInterface + * + * @method static LocalUser getUser() */ abstract class Security { diff --git a/src/Util/Common.php b/src/Util/Common.php index b9fb4f5342..dff3e48ebc 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -104,6 +104,10 @@ abstract class Common public static function user(): ?LocalUser { + // This returns the user stored in the session. We only use + // LocalUser, but this is more generic and returns + // UserInterface, so we need a type cast + /** @var LocalUser */ return Security::getUser(); } diff --git a/tests/Twig/ExtensionTest.php b/tests/Twig/ExtensionTest.php index c6d04919a7..c284be74c1 100644 --- a/tests/Twig/ExtensionTest.php +++ b/tests/Twig/ExtensionTest.php @@ -84,7 +84,8 @@ class ExtensionTest extends GNUsocialTestCase public function testIsCurrentRouteActive() { - $req = $this->createMock(Request::class); + $req = $this->createMock(Request::class); + // @phpstan-ignore-next-line $req->attributes = new class { public function get(string $arg) {