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

@ -35,21 +35,13 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class PasswordResetManager
{
protected MailerInterface $mailer;
protected EntityManagerInterface $em;
protected PasswordHasherInterface $passwordEncoder;
protected TranslatorInterface $translator;
protected UserPasswordHasherInterface $userPasswordEncoder;
public function __construct(MailerInterface $mailer, EntityManagerInterface $em,
TranslatorInterface $translator, UserPasswordHasherInterface $userPasswordEncoder,
public function __construct(protected MailerInterface $mailer, protected EntityManagerInterface $em,
protected TranslatorInterface $translator, protected UserPasswordHasherInterface $userPasswordEncoder,
PasswordHasherFactoryInterface $encoderFactory)
{
$this->em = $em;
$this->mailer = $mailer;
$this->passwordEncoder = $encoderFactory->getPasswordHasher(User::class);
$this->translator = $translator;
$this->userPasswordEncoder = $userPasswordEncoder;
}
public function request(string $name_or_email): void
@ -59,7 +51,7 @@ class PasswordResetManager
//Try to find a user by the given string
$user = $repo->findByEmailOrName($name_or_email);
//Do nothing if no user was found
if (null === $user) {
if (!$user instanceof \App\Entity\UserSystem\User) {
return;
}
@ -109,7 +101,7 @@ class PasswordResetManager
$user = $repo->findOneBy(['name' => $username]);
//If no user matching the name, show an error message
if (null === $user) {
if (!$user instanceof \App\Entity\UserSystem\User) {
return false;
}

View file

@ -40,19 +40,16 @@ use Symfony\Component\Yaml\Yaml;
class PermissionManager
{
protected $permission_structure;
protected bool $is_debug;
protected string $cache_file;
/**
* PermissionResolver constructor.
*/
public function __construct(bool $kernel_debug, string $kernel_cache_dir)
public function __construct(protected bool $is_debug, string $kernel_cache_dir)
{
$cache_dir = $kernel_cache_dir;
//Here the cached structure will be saved.
$this->cache_file = $cache_dir.'/permissions.php.cache';
$this->is_debug = $kernel_debug;
$this->permission_structure = $this->generatePermissionStructure();
}
@ -113,7 +110,7 @@ class PermissionManager
/** @var Group $parent */
$parent = $user->getGroup();
while (null !== $parent) { //The top group, has parent == null
while ($parent instanceof \App\Entity\Base\AbstractStructuralDBElement) { //The top group, has parent == null
//Check if our current element gives an info about disallow/allow
$allowed = $this->dontInherit($parent, $permission, $operation);
if (null !== $allowed) {
@ -196,8 +193,6 @@ class PermissionManager
/**
* This functions sets all operations mentioned in the alsoSet value of a permission, so that the structure is always valid.
* @param HasPermissionsInterface $user
* @return void
*/
public function ensureCorrectSetOperations(HasPermissionsInterface $user): void
{
@ -215,12 +210,7 @@ class PermissionManager
//Set every op listed in also Set
foreach ($op['alsoSet'] as $set_also) {
//If the alsoSet value contains a dot then we set the operation of another permission
if (str_contains($set_also, '.')) {
[$set_perm, $set_op] = explode('.', $set_also);
} else {
//Else we set the operation of the same permission
[$set_perm, $set_op] = [$perm_key, $set_also];
}
[$set_perm, $set_op] = str_contains((string) $set_also, '.') ? explode('.', (string) $set_also) : [$perm_key, $set_also];
//Check if we change the value of the permission
if ($this->dontInherit($user, $set_perm, $set_op) !== true) {
@ -237,9 +227,6 @@ class PermissionManager
/**
* Sets all possible operations of all possible permissions of the given entity to the given value.
* @param HasPermissionsInterface $perm_holder
* @param bool|null $new_value
* @return void
*/
public function setAllPermissions(HasPermissionsInterface $perm_holder, ?bool $new_value): void
{
@ -253,11 +240,6 @@ class PermissionManager
/**
* Sets all operations of the given permissions to the given value.
* Please note that you have to call ensureCorrectSetOperations() after this function, to ensure that all alsoSet values are set.
*
* @param HasPermissionsInterface $perm_holder
* @param string $permission
* @param bool|null $new_value
* @return void
*/
public function setAllOperationsOfPermission(HasPermissionsInterface $perm_holder, string $permission, ?bool $new_value): void
{
@ -272,11 +254,6 @@ class PermissionManager
/**
* This function sets all operations of the given permission to the given value, except the ones listed in the except array.
* @param HasPermissionsInterface $perm_holder
* @param string $permission
* @param bool|null $new_value
* @param array $except
* @return void
*/
public function setAllOperationsOfPermissionExcept(HasPermissionsInterface $perm_holder, string $permission, ?bool $new_value, array $except): void
{

View file

@ -25,18 +25,15 @@ use App\Security\Interfaces\HasPermissionsInterface;
class PermissionPresetsHelper
{
public const PRESET_ALL_INHERIT = 'all_inherit';
public const PRESET_ALL_FORBID = 'all_forbid';
public const PRESET_ALL_ALLOW = 'all_allow';
public const PRESET_READ_ONLY = 'read_only';
public const PRESET_EDITOR = 'editor';
public const PRESET_ADMIN = 'admin';
final public const PRESET_ALL_INHERIT = 'all_inherit';
final public const PRESET_ALL_FORBID = 'all_forbid';
final public const PRESET_ALL_ALLOW = 'all_allow';
final public const PRESET_READ_ONLY = 'read_only';
final public const PRESET_EDITOR = 'editor';
final public const PRESET_ADMIN = 'admin';
private PermissionManager $permissionResolver;
public function __construct(PermissionManager $permissionResolver)
public function __construct(private readonly PermissionManager $permissionResolver)
{
$this->permissionResolver = $permissionResolver;
}
/**
@ -44,7 +41,6 @@ class PermissionPresetsHelper
* The permission data will be reset during the process and then the preset will be applied.
*
* @param string $preset_name The name of the preset to use
* @return HasPermissionsInterface
*/
public function applyPreset(HasPermissionsInterface $perm_holder, string $preset_name): HasPermissionsInterface
{

View file

@ -29,7 +29,6 @@ class PermissionSchemaUpdater
{
/**
* Check if the given user/group needs an update of its permission schema.
* @param HasPermissionsInterface $holder
* @return bool True if the permission schema needs an update, false otherwise.
*/
public function isSchemaUpdateNeeded(HasPermissionsInterface $holder): bool
@ -42,12 +41,11 @@ class PermissionSchemaUpdater
/**
* Upgrades the permission schema of the given user/group to the chosen version.
* Please note that this function does not flush the changes to DB!
* @param HasPermissionsInterface $holder
* @param int $target_version
* @return bool True, if an upgrade was done, false if it was not needed.
*/
public function upgradeSchema(HasPermissionsInterface $holder, int $target_version = PermissionData::CURRENT_SCHEMA_VERSION): bool
{
$e = null;
if ($target_version > PermissionData::CURRENT_SCHEMA_VERSION) {
throw new \InvalidArgumentException('The target version is higher than the maximum possible schema version!');
}
@ -66,7 +64,7 @@ class PermissionSchemaUpdater
$method->setAccessible(true);
$method->invoke($this, $holder);
} catch (\ReflectionException $e) {
throw new \RuntimeException('Could not find update method for schema version '.($n + 1));
throw new \RuntimeException('Could not find update method for schema version '.($n + 1), $e->getCode(), $e);
}
//Bump the schema version
@ -80,8 +78,6 @@ class PermissionSchemaUpdater
/**
* Upgrades the permission schema of the given group and all of its parent groups to the chosen version.
* Please note that this function does not flush the changes to DB!
* @param Group $group
* @param int $target_version
* @return bool True if an upgrade was done, false if it was not needed.
*/
public function groupUpgradeSchemaRecursively(Group $group, int $target_version = PermissionData::CURRENT_SCHEMA_VERSION): bool
@ -101,14 +97,12 @@ class PermissionSchemaUpdater
/**
* Upgrades the permissions schema of the given users and its parent (including parent groups) to the chosen version.
* Please note that this function does not flush the changes to DB!
* @param User $user
* @param int $target_version
* @return bool True if an upgrade was done, false if it was not needed.
*/
public function userUpgradeSchemaRecursively(User $user, int $target_version = PermissionData::CURRENT_SCHEMA_VERSION): bool
{
$updated = $this->upgradeSchema($user, $target_version);
if ($user->getGroup()) {
if ($user->getGroup() instanceof \App\Entity\UserSystem\Group) {
$updated = $this->groupUpgradeSchemaRecursively($user->getGroup(), $target_version) || $updated;
}

View file

@ -31,7 +31,6 @@ use RuntimeException;
class BackupCodeGenerator
{
protected int $code_length;
protected int $code_count;
/**
* BackupCodeGenerator constructor.
@ -39,7 +38,7 @@ class BackupCodeGenerator
* @param int $code_length how many characters a single code should have
* @param int $code_count how many codes are generated for a whole backup set
*/
public function __construct(int $code_length, int $code_count)
public function __construct(int $code_length, protected int $code_count)
{
if ($code_length > 32) {
throw new RuntimeException('Backup code can have maximum 32 digits!');
@ -47,8 +46,6 @@ class BackupCodeGenerator
if ($code_length < 6) {
throw new RuntimeException('Code must have at least 6 digits to ensure security!');
}
$this->code_count = $code_count;
$this->code_length = $code_length;
}

View file

@ -29,11 +29,8 @@ use App\Entity\UserSystem\User;
*/
class BackupCodeManager
{
protected BackupCodeGenerator $backupCodeGenerator;
public function __construct(BackupCodeGenerator $backupCodeGenerator)
public function __construct(protected BackupCodeGenerator $backupCodeGenerator)
{
$this->backupCodeGenerator = $backupCodeGenerator;
}
/**

View file

@ -33,34 +33,18 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
class UserAvatarHelper
{
private bool $use_gravatar;
private Packages $packages;
private AttachmentURLGenerator $attachmentURLGenerator;
private FilterService $filterService;
private EntityManagerInterface $entityManager;
private AttachmentSubmitHandler $submitHandler;
public function __construct(bool $use_gravatar, Packages $packages, AttachmentURLGenerator $attachmentURLGenerator,
FilterService $filterService, EntityManagerInterface $entityManager, AttachmentSubmitHandler $attachmentSubmitHandler)
public function __construct(private readonly bool $use_gravatar, private readonly Packages $packages, private readonly AttachmentURLGenerator $attachmentURLGenerator, private readonly FilterService $filterService, private readonly EntityManagerInterface $entityManager, private readonly AttachmentSubmitHandler $submitHandler)
{
$this->use_gravatar = $use_gravatar;
$this->packages = $packages;
$this->attachmentURLGenerator = $attachmentURLGenerator;
$this->filterService = $filterService;
$this->entityManager = $entityManager;
$this->submitHandler = $attachmentSubmitHandler;
}
/**
* Returns the URL to the profile picture of the given user (in big size)
* @param User $user
* @return string
*/
public function getAvatarURL(User $user): string
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() !== null) {
if ($user->getMasterPictureAttachment() instanceof \App\Entity\Attachments\Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_md');
}
@ -76,7 +60,7 @@ class UserAvatarHelper
public function getAvatarSmURL(User $user): string
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() !== null) {
if ($user->getMasterPictureAttachment() instanceof \App\Entity\Attachments\Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_xs');
}
@ -88,7 +72,7 @@ class UserAvatarHelper
try {
//Otherwise we can serve the relative path via Asset component
return $this->filterService->getUrlOfFilteredImage('/img/default_avatar.png', 'thumbnail_xs');
} catch (\Imagine\Exception\RuntimeException $e) {
} catch (\Imagine\Exception\RuntimeException) {
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
return $this->packages->getUrl('/img/default_avatar.png');
}
@ -97,7 +81,7 @@ class UserAvatarHelper
public function getAvatarMdURL(User $user): string
{
//Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture)
if ($user->getMasterPictureAttachment() !== null) {
if ($user->getMasterPictureAttachment() instanceof \App\Entity\Attachments\Attachment) {
return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_sm');
}
@ -109,7 +93,7 @@ class UserAvatarHelper
try {
//Otherwise we can serve the relative path via Asset component
return $this->filterService->getUrlOfFilteredImage('/img/default_avatar.png', 'thumbnail_xs');
} catch (\Imagine\Exception\RuntimeException $e) {
} catch (\Imagine\Exception\RuntimeException) {
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
return $this->packages->getUrl('/img/default_avatar.png');
}
@ -136,22 +120,18 @@ class UserAvatarHelper
$url = 'https://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= "?s=${s}&d=${d}&r=${r}";
return $url;
return $url . "?s=${s}&d=${d}&r=${r}";
}
/**
* Handles the upload of the user avatar.
* @param User $user
* @param UploadedFile $file
* @return Attachment
*/
public function handleAvatarUpload(User $user, UploadedFile $file): Attachment
{
//Determine which attachment to user
//If the user already has a master attachment, we use this one
if ($user->getMasterPictureAttachment()) {
if ($user->getMasterPictureAttachment() instanceof \App\Entity\Attachments\Attachment) {
$attachment = $user->getMasterPictureAttachment();
} else { //Otherwise we have to create one
$attachment = new UserAttachment();

View file

@ -32,13 +32,8 @@ use Symfony\Component\Security\Core\Security;
*/
class UserCacheKeyGenerator
{
protected \Symfony\Bundle\SecurityBundle\Security $security;
protected RequestStack $requestStack;
public function __construct(\Symfony\Bundle\SecurityBundle\Security $security, RequestStack $requestStack)
public function __construct(protected \Symfony\Bundle\SecurityBundle\Security $security, protected RequestStack $requestStack)
{
$this->security = $security;
$this->requestStack = $requestStack;
}
/**
@ -51,10 +46,10 @@ class UserCacheKeyGenerator
{
$request = $this->requestStack->getCurrentRequest();
//Retrieve the locale from the request, if possible, otherwise use the default locale
$locale = $request ? $request->getLocale() : Locale::getDefault();
$locale = $request instanceof \Symfony\Component\HttpFoundation\Request ? $request->getLocale() : Locale::getDefault();
//If no user was specified, use the currently used one.
if (null === $user) {
if (!$user instanceof \App\Entity\UserSystem\User) {
$user = $this->security->getUser();
}