From f69b0889ebc2381299022a46a38c90e2c3996516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 14 Feb 2026 23:53:31 +0100 Subject: [PATCH] Ran rector to convert some our twig extensions to use #[AsTwigXX] attributes --- src/Controller/SecurityController.php | 5 +---- src/Controller/UserSettingsController.php | 5 +---- src/Form/AttachmentFormType.php | 4 +--- .../FieldToProviderMappingType.php | 2 +- src/Form/UserAdminForm.php | 5 +---- src/Form/UserSettingsType.php | 4 +--- src/Twig/InfoProviderExtension.php | 17 ++++++---------- src/Twig/MiscExtension.php | 20 +++++++------------ src/Twig/TwigCoreExtension.php | 15 +++++--------- src/Twig/UpdateExtension.php | 15 +++++--------- src/Twig/UserExtension.php | 9 +++++---- 11 files changed, 34 insertions(+), 67 deletions(-) diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 4e2077c4..ad4d272f 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -147,10 +147,7 @@ class SecurityController extends AbstractController 'label' => 'user.settings.pw_confirm.label', ], 'invalid_message' => 'password_must_match', - 'constraints' => [new Length([ - 'min' => 6, - 'max' => 128, - ])], + 'constraints' => [new Length(min: 6, max: 128)], ]); $builder->add('submit', SubmitType::class, [ diff --git a/src/Controller/UserSettingsController.php b/src/Controller/UserSettingsController.php index 4e56015a..3f54e99c 100644 --- a/src/Controller/UserSettingsController.php +++ b/src/Controller/UserSettingsController.php @@ -295,10 +295,7 @@ class UserSettingsController extends AbstractController 'autocomplete' => 'new-password', ], ], - 'constraints' => [new Length([ - 'min' => 6, - 'max' => 128, - ])], + 'constraints' => [new Length(min: 6, max: 128)], ]) ->add('submit', SubmitType::class, [ 'label' => 'save', diff --git a/src/Form/AttachmentFormType.php b/src/Form/AttachmentFormType.php index 5cbde178..d9fe2cd2 100644 --- a/src/Form/AttachmentFormType.php +++ b/src/Form/AttachmentFormType.php @@ -122,9 +122,7 @@ class AttachmentFormType extends AbstractType ], 'constraints' => [ //new AllowedFileExtension(), - new File([ - 'maxSize' => $options['max_file_size'], - ]), + new File(maxSize: $options['max_file_size']), ], ]); diff --git a/src/Form/InfoProviderSystem/FieldToProviderMappingType.php b/src/Form/InfoProviderSystem/FieldToProviderMappingType.php index 1a094f7e..7df8985e 100644 --- a/src/Form/InfoProviderSystem/FieldToProviderMappingType.php +++ b/src/Form/InfoProviderSystem/FieldToProviderMappingType.php @@ -62,7 +62,7 @@ class FieldToProviderMappingType extends AbstractType 'style' => 'width: 80px;' ], 'constraints' => [ - new Range(['min' => 1, 'max' => 10]), + new Range(min: 1, max: 10), ], ]); } diff --git a/src/Form/UserAdminForm.php b/src/Form/UserAdminForm.php index 69be181f..457a6e0b 100644 --- a/src/Form/UserAdminForm.php +++ b/src/Form/UserAdminForm.php @@ -177,10 +177,7 @@ class UserAdminForm extends AbstractType 'required' => false, 'mapped' => false, 'disabled' => !$this->security->isGranted('set_password', $entity) || $entity->isSamlUser(), - 'constraints' => [new Length([ - 'min' => 6, - 'max' => 128, - ])], + 'constraints' => [new Length(min: 6, max: 128)], ]) ->add('need_pw_change', CheckboxType::class, [ diff --git a/src/Form/UserSettingsType.php b/src/Form/UserSettingsType.php index 0c7cb169..968d8063 100644 --- a/src/Form/UserSettingsType.php +++ b/src/Form/UserSettingsType.php @@ -92,9 +92,7 @@ class UserSettingsType extends AbstractType 'accept' => 'image/*', ], 'constraints' => [ - new File([ - 'maxSize' => '5M', - ]), + new File(maxSize: '5M'), ], ]) ->add('aboutMe', RichTextEditorType::class, [ diff --git a/src/Twig/InfoProviderExtension.php b/src/Twig/InfoProviderExtension.php index a963b778..dc64a80a 100644 --- a/src/Twig/InfoProviderExtension.php +++ b/src/Twig/InfoProviderExtension.php @@ -23,31 +23,25 @@ declare(strict_types=1); namespace App\Twig; +use Twig\Attribute\AsTwigFunction; use App\Services\InfoProviderSystem\ProviderRegistry; use App\Services\InfoProviderSystem\Providers\InfoProviderInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; -class InfoProviderExtension extends AbstractExtension +class InfoProviderExtension { public function __construct( private readonly ProviderRegistry $providerRegistry ) {} - public function getFunctions(): array - { - return [ - new TwigFunction('info_provider', $this->getInfoProvider(...)), - new TwigFunction('info_provider_label', $this->getInfoProviderName(...)) - ]; - } - /** * Gets the info provider with the given key. Returns null, if the provider does not exist. * @param string $key * @return InfoProviderInterface|null */ - private function getInfoProvider(string $key): ?InfoProviderInterface + #[AsTwigFunction(name: 'info_provider')] + public function getInfoProvider(string $key): ?InfoProviderInterface { try { return $this->providerRegistry->getProviderByKey($key); @@ -61,7 +55,8 @@ class InfoProviderExtension extends AbstractExtension * @param string $key * @return string|null */ - private function getInfoProviderName(string $key): ?string + #[AsTwigFunction(name: 'info_provider_label')] + public function getInfoProviderName(string $key): ?string { try { return $this->providerRegistry->getProviderByKey($key)->getProviderInfo()['name']; diff --git a/src/Twig/MiscExtension.php b/src/Twig/MiscExtension.php index 8b6ebc68..9691abc0 100644 --- a/src/Twig/MiscExtension.php +++ b/src/Twig/MiscExtension.php @@ -22,6 +22,7 @@ declare(strict_types=1); */ namespace App\Twig; +use Twig\Attribute\AsTwigFunction; use App\Settings\SettingsIcon; use Symfony\Component\HttpFoundation\Request; use App\Services\LogSystem\EventCommentType; @@ -31,23 +32,14 @@ use Twig\TwigFunction; use App\Services\LogSystem\EventCommentNeededHelper; use Twig\Extension\AbstractExtension; -final class MiscExtension extends AbstractExtension +final class MiscExtension { public function __construct(private readonly EventCommentNeededHelper $eventCommentNeededHelper) { } - public function getFunctions(): array - { - return [ - new TwigFunction('event_comment_needed', $this->evenCommentNeeded(...)), - - new TwigFunction('settings_icon', $this->settingsIcon(...)), - new TwigFunction('uri_without_host', $this->uri_without_host(...)) - ]; - } - - private function evenCommentNeeded(string|EventCommentType $operation_type): bool + #[AsTwigFunction(name: 'event_comment_needed')] + public function evenCommentNeeded(string|EventCommentType $operation_type): bool { if (is_string($operation_type)) { $operation_type = EventCommentType::from($operation_type); @@ -63,7 +55,8 @@ final class MiscExtension extends AbstractExtension * @return string|null * @throws \ReflectionException */ - private function settingsIcon(string|object $objectOrClass): ?string + #[AsTwigFunction(name: 'settings_icon')] + public function settingsIcon(string|object $objectOrClass): ?string { //If the given object is a proxy, then get the real object if (is_a($objectOrClass, SettingsProxyInterface::class)) { @@ -82,6 +75,7 @@ final class MiscExtension extends AbstractExtension * @param Request $request * @return string */ + #[AsTwigFunction(name: 'uri_without_host')] public function uri_without_host(Request $request): string { if (null !== $qs = $request->getQueryString()) { diff --git a/src/Twig/TwigCoreExtension.php b/src/Twig/TwigCoreExtension.php index 7b2b58f8..58f991b4 100644 --- a/src/Twig/TwigCoreExtension.php +++ b/src/Twig/TwigCoreExtension.php @@ -22,6 +22,8 @@ declare(strict_types=1); */ namespace App\Twig; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Twig\Attribute\AsTwigFunction; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -32,23 +34,15 @@ use Twig\TwigTest; * The functionalities here extend the Twig with some core functions, which are independently of Part-DB. * @see \App\Tests\Twig\TwigCoreExtensionTest */ -final class TwigCoreExtension extends AbstractExtension +final class TwigCoreExtension { - private readonly ObjectNormalizer $objectNormalizer; + private readonly NormalizerInterface $objectNormalizer; public function __construct() { $this->objectNormalizer = new ObjectNormalizer(); } - public function getFunctions(): array - { - return [ - /* Returns the enum cases as values */ - new TwigFunction('enum_cases', $this->getEnumCases(...)), - ]; - } - public function getTests(): array { return [ @@ -66,6 +60,7 @@ final class TwigCoreExtension extends AbstractExtension * @param string $enum_class * @phpstan-param class-string $enum_class */ + #[AsTwigFunction(name: 'enum_cases')] public function getEnumCases(string $enum_class): array { if (!enum_exists($enum_class)) { diff --git a/src/Twig/UpdateExtension.php b/src/Twig/UpdateExtension.php index ee3bb16c..552335ae 100644 --- a/src/Twig/UpdateExtension.php +++ b/src/Twig/UpdateExtension.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace App\Twig; +use Twig\Attribute\AsTwigFunction; use App\Services\System\UpdateAvailableFacade; use Symfony\Bundle\SecurityBundle\Security; use Twig\Extension\AbstractExtension; @@ -31,7 +32,7 @@ use Twig\TwigFunction; /** * Twig extension for update-related functions. */ -final class UpdateExtension extends AbstractExtension +final class UpdateExtension { public function __construct(private readonly UpdateAvailableFacade $updateAvailableManager, private readonly Security $security) @@ -39,18 +40,10 @@ final class UpdateExtension extends AbstractExtension } - public function getFunctions(): array - { - return [ - new TwigFunction('is_update_available', $this->isUpdateAvailable(...)), - new TwigFunction('get_latest_version', $this->getLatestVersion(...)), - new TwigFunction('get_latest_version_url', $this->getLatestVersionUrl(...)), - ]; - } - /** * Check if an update is available and the user has permission to see it. */ + #[AsTwigFunction(name: 'is_update_available')] public function isUpdateAvailable(): bool { // Only show to users with the show_updates permission @@ -64,6 +57,7 @@ final class UpdateExtension extends AbstractExtension /** * Get the latest available version string. */ + #[AsTwigFunction(name: 'get_latest_version')] public function getLatestVersion(): string { return $this->updateAvailableManager->getLatestVersionString(); @@ -72,6 +66,7 @@ final class UpdateExtension extends AbstractExtension /** * Get the URL to the latest version release page. */ + #[AsTwigFunction(name: 'get_latest_version_url')] public function getLatestVersionUrl(): string { return $this->updateAvailableManager->getLatestVersionUrl(); diff --git a/src/Twig/UserExtension.php b/src/Twig/UserExtension.php index 5045257a..687f4e84 100644 --- a/src/Twig/UserExtension.php +++ b/src/Twig/UserExtension.php @@ -41,6 +41,7 @@ declare(strict_types=1); namespace App\Twig; +use Twig\Attribute\AsTwigFunction; use App\Entity\Base\AbstractDBElement; use App\Entity\UserSystem\User; use App\Entity\LogSystem\AbstractLogEntry; @@ -57,7 +58,7 @@ use Twig\TwigFunction; /** * @see \App\Tests\Twig\UserExtensionTest */ -final class UserExtension extends AbstractExtension +final class UserExtension { private readonly LogEntryRepository $repo; @@ -82,9 +83,6 @@ final class UserExtension extends AbstractExtension new TwigFunction('last_editing_user', fn(AbstractDBElement $element): ?User => $this->repo->getLastEditingUser($element)), /* Returns the user which has created the given entity. */ new TwigFunction('creating_user', fn(AbstractDBElement $element): ?User => $this->repo->getCreatingUser($element)), - new TwigFunction('impersonator_user', $this->getImpersonatorUser(...)), - new TwigFunction('impersonation_active', $this->isImpersonationActive(...)), - new TwigFunction('impersonation_path', $this->getImpersonationPath(...)), ]; } @@ -93,6 +91,7 @@ final class UserExtension extends AbstractExtension * If the current user is not impersonated, null is returned. * @return User|null */ + #[AsTwigFunction(name: 'impersonator_user')] public function getImpersonatorUser(): ?User { $token = $this->security->getToken(); @@ -107,11 +106,13 @@ final class UserExtension extends AbstractExtension return null; } + #[AsTwigFunction(name: 'impersonation_active')] public function isImpersonationActive(): bool { return $this->security->isGranted('IS_IMPERSONATOR'); } + #[AsTwigFunction(name: 'impersonation_path')] public function getImpersonationPath(User $user, string $route_name = 'homepage'): string { if (! $this->security->isGranted('CAN_SWITCH_USER', $user)) {