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

@ -45,11 +45,10 @@ class EventCommentHelper
{
protected const MAX_MESSAGE_LENGTH = 255;
protected ?string $message;
protected ?string $message = null;
public function __construct()
{
$this->message = null;
}
/**
@ -60,11 +59,7 @@ class EventCommentHelper
public function setMessage(?string $message): void
{
//Restrict the length of the string
if ($message) {
$this->message = mb_strimwidth($message, 0, self::MAX_MESSAGE_LENGTH, '...');
} else {
$this->message = null;
}
$this->message = $message ? mb_strimwidth($message, 0, self::MAX_MESSAGE_LENGTH, '...') : null;
}
/**

View file

@ -26,9 +26,7 @@ namespace App\Services\LogSystem;
*/
class EventCommentNeededHelper
{
protected array $enforce_change_comments_for;
public const VALID_OPERATION_TYPES = [
final public const VALID_OPERATION_TYPES = [
'part_edit',
'part_create',
'part_delete',
@ -38,15 +36,12 @@ class EventCommentNeededHelper
'datastructure_delete',
];
public function __construct(array $enforce_change_comments_for)
public function __construct(protected array $enforce_change_comments_for)
{
$this->enforce_change_comments_for = $enforce_change_comments_for;
}
/**
* Checks if a log change comment is needed for the given operation type
* @param string $comment_type
* @return bool
*/
public function isCommentNeeded(string $comment_type): bool
{

View file

@ -30,22 +30,8 @@ use Symfony\Component\Security\Core\Security;
class EventLogger
{
protected int $minimum_log_level;
protected array $blacklist;
protected array $whitelist;
protected EntityManagerInterface $em;
protected \Symfony\Bundle\SecurityBundle\Security $security;
protected ConsoleInfoHelper $console_info_helper;
public function __construct(int $minimum_log_level, array $blacklist, array $whitelist, EntityManagerInterface $em,
\Symfony\Bundle\SecurityBundle\Security $security, ConsoleInfoHelper $console_info_helper)
public function __construct(protected int $minimum_log_level, protected array $blacklist, protected array $whitelist, protected EntityManagerInterface $em, protected \Symfony\Bundle\SecurityBundle\Security $security, protected ConsoleInfoHelper $console_info_helper)
{
$this->minimum_log_level = $minimum_log_level;
$this->blacklist = $blacklist;
$this->whitelist = $whitelist;
$this->em = $em;
$this->security = $security;
$this->console_info_helper = $console_info_helper;
}
/**
@ -58,14 +44,14 @@ class EventLogger
{
$user = $this->security->getUser();
//If the user is not specified explicitly, set it to the current user
if ((null === $user || $user instanceof User) && null === $logEntry->getUser()) {
if (null === $user) {
if ((!$user instanceof \Symfony\Component\Security\Core\User\UserInterface || $user instanceof User) && !$logEntry->getUser() instanceof \App\Entity\UserSystem\User) {
if (!$user instanceof \App\Entity\UserSystem\User) {
$repo = $this->em->getRepository(User::class);
$user = $repo->getAnonymousUser();
}
//If no anonymous user is available skip the log (needed for data fixtures)
if (null === $user) {
if (!$user instanceof \App\Entity\UserSystem\User) {
return false;
}
$logEntry->setUser($user);
@ -88,15 +74,13 @@ class EventLogger
/**
* Same as log(), but this function can be safely called from within the onFlush() doctrine event, as it
* updated the changesets of the unit of work.
* @param AbstractLogEntry $logEntry
* @return bool
*/
public function logFromOnFlush(AbstractLogEntry $logEntry): bool
{
if ($this->log($logEntry)) {
$uow = $this->em->getUnitOfWork();
//As we call it from onFlush, we have to recompute the changeset here, according to https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/events.html#reference-events-on-flush
$uow->computeChangeSet($this->em->getClassMetadata(get_class($logEntry)), $logEntry);
$uow->computeChangeSet($this->em->getClassMetadata($logEntry::class), $logEntry);
return true;
}
@ -125,9 +109,9 @@ class EventLogger
?array $whitelist = null
): bool {
//Apply the global settings, if nothing was specified
$minimum_log_level = $minimum_log_level ?? $this->minimum_log_level;
$blacklist = $blacklist ?? $this->blacklist;
$whitelist = $whitelist ?? $this->whitelist;
$minimum_log_level ??= $this->minimum_log_level;
$blacklist ??= $this->blacklist;
$whitelist ??= $this->whitelist;
//Don't add the entry if it does not reach the minimum level
if ($logEntry->getLevel() > $minimum_log_level) {
@ -135,17 +119,12 @@ class EventLogger
}
//Check if the event type is blacklisted
if (!empty($blacklist) && $this->isObjectClassInArray($logEntry, $blacklist)) {
if ($blacklist !== [] && $this->isObjectClassInArray($logEntry, $blacklist)) {
return false;
}
//Check for whitelisting
if (!empty($whitelist) && !$this->isObjectClassInArray($logEntry, $whitelist)) {
return false;
}
// By default, all things should be added
return true;
return !($whitelist !== [] && !$this->isObjectClassInArray($logEntry, $whitelist));
}
/**
@ -157,13 +136,13 @@ class EventLogger
protected function isObjectClassInArray(object $object, array $classes): bool
{
//Check if the class is directly in the classes array
if (in_array(get_class($object), $classes, true)) {
if (in_array($object::class, $classes, true)) {
return true;
}
//Iterate over all classes and check for inheritance
foreach ($classes as $class) {
if (is_a($object, $class)) {
if ($object instanceof $class) {
return true;
}
}

View file

@ -46,18 +46,16 @@ use InvalidArgumentException;
class EventUndoHelper
{
public const MODE_UNDO = 'undo';
public const MODE_REVERT = 'revert';
final public const MODE_UNDO = 'undo';
final public const MODE_REVERT = 'revert';
protected const ALLOWED_MODES = [self::MODE_REVERT, self::MODE_UNDO];
protected ?AbstractLogEntry $undone_event;
protected string $mode;
protected ?AbstractLogEntry $undone_event = null;
protected string $mode = self::MODE_UNDO;
public function __construct()
{
$this->undone_event = null;
$this->mode = self::MODE_UNDO;
}
public function setMode(string $mode): void

View file

@ -29,25 +29,14 @@ class LogDataFormatter
{
private const STRING_MAX_LENGTH = 1024;
private TranslatorInterface $translator;
private EntityManagerInterface $entityManager;
private ElementTypeNameGenerator $elementTypeNameGenerator;
public function __construct(TranslatorInterface $translator, EntityManagerInterface $entityManager, ElementTypeNameGenerator $elementTypeNameGenerator)
public function __construct(private readonly TranslatorInterface $translator, private readonly EntityManagerInterface $entityManager, private readonly ElementTypeNameGenerator $elementTypeNameGenerator)
{
$this->translator = $translator;
$this->entityManager = $entityManager;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
}
/**
* Formats the given data of a log entry as HTML
* @param mixed $data
* @param AbstractLogEntry $logEntry
* @param string $fieldName
* @return string
*/
public function formatData($data, AbstractLogEntry $logEntry, string $fieldName): string
public function formatData(mixed $data, AbstractLogEntry $logEntry, string $fieldName): string
{
if (is_string($data)) {
$tmp = '<span class="text-muted user-select-none">"</span>' . mb_strimwidth(htmlspecialchars($data), 0, self::STRING_MAX_LENGTH, ) . '<span class="text-muted user-select-none">"</span>';
@ -55,9 +44,8 @@ class LogDataFormatter
//Show special characters and line breaks
$tmp = preg_replace('/\n/', '<span class="text-muted user-select-none">\\n</span><br>', $tmp);
$tmp = preg_replace('/\r/', '<span class="text-muted user-select-none">\\r</span>', $tmp);
$tmp = preg_replace('/\t/', '<span class="text-muted user-select-none">\\t</span>', $tmp);
return $tmp;
return preg_replace('/\t/', '<span class="text-muted user-select-none">\\t</span>', $tmp);
}
if (is_bool($data)) {
@ -126,7 +114,7 @@ class LogDataFormatter
}
} catch (\InvalidArgumentException|\ReflectionException $exception) {
} catch (\InvalidArgumentException|\ReflectionException) {
return '<i>unknown target class</i>: ' . $id;
}
}
@ -147,7 +135,7 @@ class LogDataFormatter
try {
$dateTime = new \DateTime($date, new \DateTimeZone($timezone));
} catch (\Exception $exception) {
} catch (\Exception) {
return '<i>unknown DateTime format</i>';
}

View file

@ -29,7 +29,6 @@ class LogDiffFormatter
* If the diff is not possible, an empty string is returned.
* @param $old_data
* @param $new_data
* @return string
*/
public function formatDiff($old_data, $new_data): string
{
@ -67,7 +66,7 @@ class LogDiffFormatter
//Positive difference
if ($difference > 0) {
return sprintf('<span class="text-success">+%s</span>', $difference);
} else if ($difference < 0) {
} elseif ($difference < 0) {
return sprintf('<span class="text-danger">%s</span>', $difference);
} else {
return sprintf('<span class="text-muted">%s</span>', $difference);

View file

@ -48,13 +48,9 @@ class LogEntryExtraFormatter
{
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>'];
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>'];
protected TranslatorInterface $translator;
protected ElementTypeNameGenerator $elementTypeNameGenerator;
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator)
public function __construct(protected TranslatorInterface $translator, protected ElementTypeNameGenerator $elementTypeNameGenerator)
{
$this->translator = $translator;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
}
/**
@ -163,7 +159,7 @@ class LogEntryExtraFormatter
'%s <i class="fas fa-long-arrow-alt-right"></i> %s (%s)',
$context->getOldInstock(),
$context->getNewInstock(),
(!$context->isWithdrawal() ? '+' : '-').$context->getDifference(true)
($context->isWithdrawal() ? '-' : '+').$context->getDifference(true)
);
$array['log.instock_changed.comment'] = htmlspecialchars($context->getComment());
}

View file

@ -29,30 +29,20 @@ class LogLevelHelper
* Returns the FontAwesome icon class for the given log level.
* This returns just the specific icon class (so 'fa-info' for example).
* @param string $logLevel The string representation of the log level (one of the LogLevel::* constants)
* @return string
*/
public function logLevelToIconClass(string $logLevel): string
{
switch ($logLevel) {
case LogLevel::DEBUG:
return 'fa-bug';
case LogLevel::INFO:
return 'fa-info';
case LogLevel::NOTICE:
return 'fa-flag';
case LogLevel::WARNING:
return 'fa-exclamation-circle';
case LogLevel::ERROR:
return 'fa-exclamation-triangle';
case LogLevel::CRITICAL:
return 'fa-bolt';
case LogLevel::ALERT:
return 'fa-radiation';
case LogLevel::EMERGENCY:
return 'fa-skull-crossbones';
default:
return 'fa-question-circle';
}
return match ($logLevel) {
LogLevel::DEBUG => 'fa-bug',
LogLevel::INFO => 'fa-info',
LogLevel::NOTICE => 'fa-flag',
LogLevel::WARNING => 'fa-exclamation-circle',
LogLevel::ERROR => 'fa-exclamation-triangle',
LogLevel::CRITICAL => 'fa-bolt',
LogLevel::ALERT => 'fa-radiation',
LogLevel::EMERGENCY => 'fa-skull-crossbones',
default => 'fa-question-circle',
};
}
/**
@ -63,18 +53,11 @@ class LogLevelHelper
public function logLevelToTableColorClass(string $logLevel): string
{
switch ($logLevel) {
case LogLevel::EMERGENCY:
case LogLevel::ALERT:
case LogLevel::CRITICAL:
case LogLevel::ERROR:
return 'table-danger';
case LogLevel::WARNING:
return 'table-warning';
case LogLevel::NOTICE:
return 'table-info';
default:
return '';
}
return match ($logLevel) {
LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR => 'table-danger',
LogLevel::WARNING => 'table-warning',
LogLevel::NOTICE => 'table-info',
default => '',
};
}
}

View file

@ -40,21 +40,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class LogTargetHelper
{
protected EntityManagerInterface $em;
protected LogEntryRepository $entryRepository;
protected EntityURLGenerator $entityURLGenerator;
protected ElementTypeNameGenerator $elementTypeNameGenerator;
protected TranslatorInterface $translator;
public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator,
ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
public function __construct(protected EntityManagerInterface $em, protected EntityURLGenerator $entityURLGenerator,
protected ElementTypeNameGenerator $elementTypeNameGenerator, protected TranslatorInterface $translator)
{
$this->em = $entityManager;
$this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class);
$this->entityURLGenerator = $entityURLGenerator;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->translator = $translator;
$this->entryRepository = $em->getRepository(AbstractLogEntry::class);
}
private function configureOptions(OptionsResolver $resolver): self
@ -79,7 +70,7 @@ class LogTargetHelper
$target = $this->entryRepository->getTargetElement($context);
//If the target is null and the context has a target, that means that the target was deleted. Show it that way.
if ($target === null) {
if (!$target instanceof \App\Entity\Base\AbstractDBElement) {
if ($context->hasTarget()) {
return $this->elementTypeNameGenerator->formatElementDeletedHTML($context->getTargetClass(),
$context->getTargetId());

View file

@ -43,12 +43,10 @@ use ReflectionClass;
class TimeTravel
{
protected EntityManagerInterface $em;
protected LogEntryRepository $repo;
public function __construct(EntityManagerInterface $em)
public function __construct(protected EntityManagerInterface $em)
{
$this->em = $em;
$this->repo = $em->getRepository(AbstractLogEntry::class);
}
@ -126,7 +124,7 @@ class TimeTravel
}
// Revert any of the associated elements
$metadata = $this->em->getClassMetadata(get_class($element));
$metadata = $this->em->getClassMetadata($element::class);
$associations = $metadata->getAssociationMappings();
foreach ($associations as $field => $mapping) {
if (
@ -148,10 +146,10 @@ class TimeTravel
} elseif ( //Revert *_TO_MANY associations (collection properties)
(ClassMetadataInfo::MANY_TO_MANY === $mapping['type']
|| ClassMetadataInfo::ONE_TO_MANY === $mapping['type'])
&& false === $mapping['isOwningSide']
&& !$mapping['isOwningSide']
) {
$target_elements = $this->getField($element, $field);
if (null === $target_elements || count($target_elements) > 10) {
if (null === $target_elements || (is_countable($target_elements) ? count($target_elements) : 0) > 10) {
continue;
}
foreach ($target_elements as $target_element) {
@ -200,7 +198,7 @@ class TimeTravel
if (!$element instanceof TimeStampableInterface) {
return;
}
$metadata = $this->em->getClassMetadata(get_class($element));
$metadata = $this->em->getClassMetadata($element::class);
$old_data = $logEntry->getOldData();
foreach ($old_data as $field => $data) {
@ -232,19 +230,16 @@ class TimeTravel
protected function getField(AbstractDBElement $element, string $field)
{
$reflection = new ReflectionClass(get_class($element));
$reflection = new ReflectionClass($element::class);
$property = $reflection->getProperty($field);
$property->setAccessible(true);
return $property->getValue($element);
}
/**
* @param DateTime|int|null $new_value
*/
protected function setField(AbstractDBElement $element, string $field, $new_value): void
protected function setField(AbstractDBElement $element, string $field, \DateTime|int|null $new_value): void
{
$reflection = new ReflectionClass(get_class($element));
$reflection = new ReflectionClass($element::class);
$property = $reflection->getProperty($field);
$property->setAccessible(true);