Applied rector with PHP8.1 migration rules

This commit is contained in:
Jan Böhmer 2023-06-11 14:15:46 +02:00
parent dc6a67c2f0
commit 7ee01d9a05
303 changed files with 1228 additions and 3465 deletions

View file

@ -70,30 +70,12 @@ class EventLoggerSubscriber implements EventSubscriber
];
protected const MAX_STRING_LENGTH = 2000;
protected EventLogger $logger;
protected SerializerInterface $serializer;
protected EventCommentHelper $eventCommentHelper;
protected EventUndoHelper $eventUndoHelper;
protected bool $save_changed_fields;
protected bool $save_changed_data;
protected bool $save_removed_data;
protected bool $save_new_data;
protected PropertyAccessorInterface $propertyAccessor;
public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper,
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, bool $save_new_data,
PropertyAccessorInterface $propertyAccessor, EventUndoHelper $eventUndoHelper)
public function __construct(protected EventLogger $logger, protected SerializerInterface $serializer, protected EventCommentHelper $eventCommentHelper,
protected bool $save_changed_fields, protected bool $save_changed_data, protected bool $save_removed_data, bool $save_new_data,
protected PropertyAccessorInterface $propertyAccessor, protected EventUndoHelper $eventUndoHelper)
{
$this->logger = $logger;
$this->serializer = $serializer;
$this->eventCommentHelper = $commentHelper;
$this->propertyAccessor = $propertyAccessor;
$this->eventUndoHelper = $eventUndoHelper;
$this->save_changed_fields = $save_changed_fields;
$this->save_changed_data = $save_changed_data;
$this->save_removed_data = $save_removed_data;
//This option only makes sense if save_changed_data is true
$this->save_new_data = $save_new_data && $save_changed_data;
}
@ -181,7 +163,7 @@ class EventLoggerSubscriber implements EventSubscriber
public function hasFieldRestrictions(AbstractDBElement $element): bool
{
foreach (array_keys(static::FIELD_BLACKLIST) as $class) {
if (is_a($element, $class)) {
if ($element instanceof $class) {
return true;
}
}
@ -195,7 +177,7 @@ class EventLoggerSubscriber implements EventSubscriber
public function shouldFieldBeSaved(AbstractDBElement $element, string $field_name): bool
{
foreach (static::FIELD_BLACKLIST as $class => $blacklist) {
if (is_a($element, $class) && in_array($field_name, $blacklist, true)) {
if ($element instanceof $class && in_array($field_name, $blacklist, true)) {
return false;
}
}
@ -231,11 +213,11 @@ class EventLoggerSubscriber implements EventSubscriber
//Check if we have to log CollectionElementDeleted entries
if ($this->save_changed_data) {
$metadata = $em->getClassMetadata(get_class($entity));
$metadata = $em->getClassMetadata($entity::class);
$mappings = $metadata->getAssociationMappings();
//Check if class is whitelisted for CollectionElementDeleted entry
foreach (static::TRIGGER_ASSOCIATION_LOG_WHITELIST as $class => $whitelist) {
if (is_a($entity, $class)) {
if ($entity instanceof $class) {
//Check names
foreach ($mappings as $field => $mapping) {
if (in_array($field, $whitelist, true)) {
@ -308,8 +290,6 @@ class EventLoggerSubscriber implements EventSubscriber
/**
* Restrict the length of every string in the given array to MAX_STRING_LENGTH, to save memory in the case of very
* long strings (e.g. images in notes)
* @param array $fields
* @return array
*/
protected function fieldLengthRestrict(array $fields): array
{
@ -325,6 +305,7 @@ class EventLoggerSubscriber implements EventSubscriber
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, bool $element_deleted = false): void
{
$new_data = null;
$uow = $em->getUnitOfWork();
if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) {
@ -348,7 +329,7 @@ class EventLoggerSubscriber implements EventSubscriber
$logEntry->setOldData($old_data);
if (!empty($new_data)) {
if ($new_data !== []) {
$new_data = $this->filterFieldRestrictions($entity, $new_data);
$new_data = $this->fieldLengthRestrict($new_data);

View file

@ -53,11 +53,8 @@ use Symfony\Component\Security\Core\Exception\AccessDeniedException;
*/
class LogAccessDeniedSubscriber implements EventSubscriberInterface
{
private EventLogger $logger;
public function __construct(EventLogger $logger)
public function __construct(private readonly EventLogger $logger)
{
$this->logger = $logger;
}
public function onKernelException(ExceptionEvent $event): void

View file

@ -37,13 +37,8 @@ class LogDBMigrationSubscriber implements EventSubscriber
protected ?string $old_version = null;
protected ?string $new_version = null;
protected EventLogger $eventLogger;
protected DependencyFactory $dependencyFactory;
public function __construct(EventLogger $eventLogger, DependencyFactory $dependencyFactory)
public function __construct(protected EventLogger $eventLogger, protected DependencyFactory $dependencyFactory)
{
$this->eventLogger = $eventLogger;
$this->dependencyFactory = $dependencyFactory;
}
public function onMigrationsMigrated(MigrationsEventArgs $args): void
@ -66,7 +61,7 @@ class LogDBMigrationSubscriber implements EventSubscriber
try {
$log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version);
$this->eventLogger->logAndFlush($log);
} catch (\Throwable $exception) {
} catch (\Throwable) {
//Ignore any exception occuring here...
}
}

View file

@ -32,15 +32,10 @@ use Symfony\Component\Security\Http\Event\LogoutEvent;
* This handler logs to event log, if a user logs out.
*/
#[AsEventListener]
final class LogLogoutEventListener
final class LogLogoutEventEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
protected EventLogger $logger;
protected bool $gpdr_compliance;
public function __construct(EventLogger $logger, bool $gpdr_compliance)
public function __construct(protected EventLogger $logger, protected bool $gpdr_compliance)
{
$this->logger = $logger;
$this->gpdr_compliance = $gpdr_compliance;
}
public function __invoke(LogoutEvent $event): void
@ -48,7 +43,7 @@ final class LogLogoutEventListener
$request = $event->getRequest();
$token = $event->getToken();
if (null === $token) {
if (!$token instanceof \Symfony\Component\Security\Core\Authentication\Token\TokenInterface) {
return;
}
@ -60,4 +55,11 @@ final class LogLogoutEventListener
$this->logger->logAndFlush($log);
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['' => ''];
}
}

View file

@ -53,15 +53,8 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
private bool $gpdr_compliant;
private EventLogger $eventLogger;
public function __construct(RequestStack $requestStack, EventLogger $eventLogger, bool $gpdr_compliance)
public function __construct(private readonly RequestStack $requestStack, private readonly EventLogger $eventLogger, private readonly bool $gpdr_compliant)
{
$this->requestStack = $requestStack;
$this->gpdr_compliant = $gpdr_compliance;
$this->eventLogger = $eventLogger;
}
public static function getSubscribedEvents(): array
@ -129,7 +122,7 @@ final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
$anonymize = $this->gpdr_compliant;
$request = $this->requestStack->getCurrentRequest();
if (null !== $request) {
if ($request instanceof \Symfony\Component\HttpFoundation\Request) {
$ip = $request->getClientIp() ?? 'unknown';
} else {
$ip = 'Console';

View file

@ -32,13 +32,8 @@ use Symfony\Component\Mime\Email;
*/
final class SetMailFromSubscriber implements EventSubscriberInterface
{
private string $email;
private string $name;
public function __construct(string $email, string $name)
public function __construct(private readonly string $email, private readonly string $name)
{
$this->email = $email;
$this->name = $name;
}
public function onMessage(MessageEvent $event): void

View file

@ -30,11 +30,8 @@ use Symfony\Component\HttpKernel\Event\ResponseEvent;
*/
final class SymfonyDebugToolbarSubscriber implements EventSubscriberInterface
{
private bool $kernel_debug;
public function __construct(bool $kernel_debug)
public function __construct(private readonly bool $kernel_debug)
{
$this->kernel_debug = $kernel_debug;
}
/**

View file

@ -37,17 +37,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
*/
final class LoginSuccessSubscriber implements EventSubscriberInterface
{
private TranslatorInterface $translator;
private RequestStack $requestStack;
private EventLogger $eventLogger;
private bool $gpdr_compliance;
public function __construct(TranslatorInterface $translator, RequestStack $requestStack, EventLogger $eventLogger, bool $gpdr_compliance)
public function __construct(private readonly TranslatorInterface $translator, private readonly RequestStack $requestStack, private readonly EventLogger $eventLogger, private readonly bool $gpdr_compliance)
{
$this->translator = $translator;
$this->requestStack = $requestStack;
$this->eventLogger = $eventLogger;
$this->gpdr_compliance = $gpdr_compliance;
}
public function onLogin(InteractiveLoginEvent $event): void

View file

@ -36,14 +36,8 @@ use Symfony\Component\Security\Core\Security;
*/
final class LogoutDisabledUserSubscriber implements EventSubscriberInterface
{
private \Symfony\Bundle\SecurityBundle\Security $security;
private UrlGeneratorInterface $urlGenerator;
public function __construct(\Symfony\Bundle\SecurityBundle\Security $security, UrlGeneratorInterface $urlGenerator)
public function __construct(private readonly \Symfony\Bundle\SecurityBundle\Security $security, private readonly UrlGeneratorInterface $urlGenerator)
{
$this->security = $security;
$this->urlGenerator = $urlGenerator;
}
public function onRequest(RequestEvent $event): void

View file

@ -55,13 +55,9 @@ final class PasswordChangeNeededSubscriber implements EventSubscriberInterface
* @var string The route the user will redirected to, if he needs to change this password
*/
public const REDIRECT_TARGET = 'user_settings';
private \Symfony\Bundle\SecurityBundle\Security $security;
private HttpUtils $httpUtils;
public function __construct(\Symfony\Bundle\SecurityBundle\Security $security, HttpUtils $httpUtils)
public function __construct(private readonly \Symfony\Bundle\SecurityBundle\Security $security, private readonly HttpUtils $httpUtils)
{
$this->security = $security;
$this->httpUtils = $httpUtils;
}
/**
@ -129,7 +125,7 @@ final class PasswordChangeNeededSubscriber implements EventSubscriberInterface
{
$tfa_enabled = $user->isWebAuthnAuthenticatorEnabled() || $user->isGoogleAuthenticatorEnabled();
return null !== $user->getGroup() && $user->getGroup()->isEnforce2FA() && !$tfa_enabled;
return $user->getGroup() instanceof \App\Entity\UserSystem\Group && $user->getGroup()->isEnforce2FA() && !$tfa_enabled;
}
public static function getSubscribedEvents(): array

View file

@ -33,13 +33,8 @@ use Symfony\Component\Security\Core\Security;
*/
final class SetUserTimezoneSubscriber implements EventSubscriberInterface
{
private string $default_timezone;
private \Symfony\Bundle\SecurityBundle\Security $security;
public function __construct(string $timezone, \Symfony\Bundle\SecurityBundle\Security $security)
public function __construct(private readonly string $default_timezone, private readonly \Symfony\Bundle\SecurityBundle\Security $security)
{
$this->default_timezone = $timezone;
$this->security = $security;
}
public function setTimeZone(ControllerEvent $event): void

View file

@ -35,17 +35,8 @@ use Symfony\Component\Security\Core\Security;
*/
class UpgradePermissionsSchemaSubscriber implements EventSubscriberInterface
{
private \Symfony\Bundle\SecurityBundle\Security $security;
private PermissionSchemaUpdater $permissionSchemaUpdater;
private EntityManagerInterface $entityManager;
private EventCommentHelper $eventCommentHelper;
public function __construct(\Symfony\Bundle\SecurityBundle\Security $security, PermissionSchemaUpdater $permissionSchemaUpdater, EntityManagerInterface $entityManager, EventCommentHelper $eventCommentHelper)
public function __construct(private readonly \Symfony\Bundle\SecurityBundle\Security $security, private readonly PermissionSchemaUpdater $permissionSchemaUpdater, private readonly EntityManagerInterface $entityManager, private readonly EventCommentHelper $eventCommentHelper)
{
$this->security = $security;
$this->permissionSchemaUpdater = $permissionSchemaUpdater;
$this->entityManager = $entityManager;
$this->eventCommentHelper = $eventCommentHelper;
}
public function onRequest(RequestEvent $event): void
@ -55,7 +46,7 @@ class UpgradePermissionsSchemaSubscriber implements EventSubscriberInterface
}
$user = $this->security->getUser();
if (null === $user) {
if (!$user instanceof \Symfony\Component\Security\Core\User\UserInterface) {
//Retrieve anonymous user
$user = $this->entityManager->getRepository(User::class)->getAnonymousUser();
}