diff --git a/UPGRADING-3.md b/UPGRADING-3.md index d09871b2b..410d2549e 100644 --- a/UPGRADING-3.md +++ b/UPGRADING-3.md @@ -31,5 +31,7 @@ Do not use method chaining: all fluent interface, especially in Entities, are no - Removed `User::isExportDecimal()` - Use duration format `HH:mm` in default PDF exports - Replace Twig `AppVariable` with custom implementation - - Replace `app.request.locale` with `app.locale` - - Replace `app.request.attributes.get('_route')` with `app.current_route` + - You need to adjust your templates if you access anything else then `app.locale`, `app.user`. `app.current_route`. + - Most often used: + - Replace `app.request.locale` with `app.locale` + - Replace `app.request.attributes.get('_route')` with `app.current_route` diff --git a/phpstan.neon b/phpstan.neon index 7f3912ea4..6fc4e9e6b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3345,34 +3345,24 @@ parameters: path: src/Twig/Context.php - - message: "#^Method App\\\\Twig\\\\DatatableExtensions\\:\\:checkInColumDefinition\\(\\) has parameter \\$columns with no value type specified in iterable type array\\.$#" + message: "#^Method App\\\\Twig\\\\Runtime\\\\DatatableExtensions\\:\\:checkInColumDefinition\\(\\) has parameter \\$columns with no value type specified in iterable type array\\.$#" count: 1 - path: src/Twig/DatatableExtensions.php + path: src/Twig/Runtime/DatatableExtensions.php - - message: "#^Method App\\\\Twig\\\\DatatableExtensions\\:\\:getClass\\(\\) has parameter \\$class with no type specified\\.$#" + message: "#^Method App\\\\Twig\\\\Runtime\\\\DatatableExtensions\\:\\:getDatatableColumnClass\\(\\) should return string but returns bool\\|string\\.$#" count: 1 - path: src/Twig/DatatableExtensions.php + path: src/Twig/Runtime/DatatableExtensions.php - - message: "#^Method App\\\\Twig\\\\DatatableExtensions\\:\\:getDatatableColumnClass\\(\\) should return string but returns bool\\|string\\.$#" + message: "#^Method App\\\\Twig\\\\Runtime\\\\DatatableExtensions\\:\\:initializeDatatable\\(\\) has parameter \\$defaultColumns with no value type specified in iterable type array\\.$#" count: 1 - path: src/Twig/DatatableExtensions.php + path: src/Twig/Runtime/DatatableExtensions.php - - message: "#^Method App\\\\Twig\\\\DatatableExtensions\\:\\:initializeDatatable\\(\\) has parameter \\$defaultColumns with no value type specified in iterable type array\\.$#" + message: "#^Method App\\\\Twig\\\\Runtime\\\\DatatableExtensions\\:\\:initializeDatatable\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 - path: src/Twig/DatatableExtensions.php - - - - message: "#^Method App\\\\Twig\\\\DatatableExtensions\\:\\:initializeDatatable\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Twig/DatatableExtensions.php - - - - message: "#^Property App\\\\Twig\\\\DatatableExtensions\\:\\:\\$tableNames type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Twig/DatatableExtensions.php + path: src/Twig/Runtime/DatatableExtensions.php - message: "#^Method App\\\\Twig\\\\Extensions\\:\\:replaceNewline\\(\\) has no return type specified\\.$#" diff --git a/src/Twig/AppVariable.php b/src/Twig/AppVariable.php index c7ce782bf..176185c1d 100644 --- a/src/Twig/AppVariable.php +++ b/src/Twig/AppVariable.php @@ -9,6 +9,7 @@ namespace App\Twig; +use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -20,7 +21,10 @@ use Symfony\Component\Security\Core\User\UserInterface; */ final class AppVariable { - public function __construct(private readonly RequestStack $requestStack, private readonly TokenStorageInterface $tokenStorage) + public function __construct( + private readonly RequestStack $requestStack, + private readonly TokenStorageInterface $tokenStorage + ) { } @@ -36,12 +40,20 @@ final class AppVariable public function getCurrent_route(): ?string { - return $this->requestStack->getCurrentRequest()->attributes->get('_route'); + $route = $this->requestStack->getCurrentRequest()?->attributes->get('_route'); + if (!\is_string($route)) { + return null; + } + + return $route; } + /** + * @return array + */ public function getFlashes(): array { - $session = $this->getSession2(); + $session = $this->getSession(); if (!$session instanceof FlashBagAwareSessionInterface) { return []; @@ -50,13 +62,11 @@ final class AppVariable return $session->getFlashBag()->all(); } - private function getSession2(): ?SessionInterface + private function getSession(): ?SessionInterface { try { - if (null !== $session = $this->requestStack->getSession()) { - return $session; - } - } catch (\RuntimeException) { + return $this->requestStack->getSession(); + } catch (SessionNotFoundException) { } return null; @@ -65,6 +75,8 @@ final class AppVariable /** * The request should not be exposed under any circumstance to the frontend. * This here is added as fallback for old customer templates still using this object. + * + * @return array{locale: string} */ public function getRequest(): array { diff --git a/src/Twig/Runtime/DatatableExtensions.php b/src/Twig/Runtime/DatatableExtensions.php index d7fb91946..ddc6ced22 100644 --- a/src/Twig/Runtime/DatatableExtensions.php +++ b/src/Twig/Runtime/DatatableExtensions.php @@ -22,6 +22,7 @@ final class DatatableExtensions implements RuntimeExtensionInterface * @var array>> */ private array $dataTables = []; + /** @var array */ private array $tableNames = []; private ?string $prefix = null; @@ -167,7 +168,7 @@ final class DatatableExtensions implements RuntimeExtensionInterface return $newClass; } - private function getClass($class): string + private function getClass(mixed $class): string { if (\is_array($class)) { if (!\array_key_exists('class', $class)) { diff --git a/src/Utils/ProfileManager.php b/src/Utils/ProfileManager.php index fbd256ba4..944f7a441 100644 --- a/src/Utils/ProfileManager.php +++ b/src/Utils/ProfileManager.php @@ -10,7 +10,6 @@ namespace App\Utils; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\SessionInterface; final class ProfileManager @@ -72,7 +71,7 @@ final class ProfileManager /** * Always returns a valid profile name (default: desktop). */ - public function getProfileFromSession(Session $session): string + public function getProfileFromSession(SessionInterface $session): string { $profile = $session->get(self::SESSION_PROFILE, self::PROFILE_DESKTOP); diff --git a/tests/Twig/DatatableExtensionsTest.php b/tests/Twig/Runtime/DatatableExtensionsTest.php similarity index 89% rename from tests/Twig/DatatableExtensionsTest.php rename to tests/Twig/Runtime/DatatableExtensionsTest.php index 016b29a63..30e3aabab 100644 --- a/tests/Twig/DatatableExtensionsTest.php +++ b/tests/Twig/Runtime/DatatableExtensionsTest.php @@ -7,14 +7,14 @@ * file that was distributed with this source code. */ -namespace App\Tests\Twig; +namespace App\Tests\Twig\Runtime; use App\Repository\BookmarkRepository; use App\Twig\Runtime\DatatableExtensions; use App\Utils\ProfileManager; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\RequestStack; #[CoversClass(DatatableExtensions::class)] class DatatableExtensionsTest extends TestCase @@ -23,7 +23,7 @@ class DatatableExtensionsTest extends TestCase { $repository = $this->createMock(BookmarkRepository::class); - return new DatatableExtensions($repository, new ProfileManager(), new Session()); + return new DatatableExtensions($repository, new ProfileManager(), new RequestStack()); } public function testGetFunctions(): void diff --git a/tests/Twig/RuntimeExtensionsTest.php b/tests/Twig/RuntimeExtensionsTest.php index 949e2d715..2e74e6976 100644 --- a/tests/Twig/RuntimeExtensionsTest.php +++ b/tests/Twig/RuntimeExtensionsTest.php @@ -50,6 +50,8 @@ class RuntimeExtensionsTest extends TestCase 'icon', 'qr_code_data_uri', 'user_shortcuts', + 'initialize_datatable', + 'datatable_column_class' ]; $i = 0;