mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-30 13:34:28 +02:00
Applied rector with PHP8.1 migration rules
This commit is contained in:
parent
dc6a67c2f0
commit
7ee01d9a05
303 changed files with 1228 additions and 3465 deletions
|
@ -27,22 +27,17 @@ use Twig\TwigFunction;
|
|||
|
||||
final class AttachmentExtension extends AbstractExtension
|
||||
{
|
||||
protected AttachmentURLGenerator $attachmentURLGenerator;
|
||||
protected FAIconGenerator $FAIconGenerator;
|
||||
|
||||
public function __construct(AttachmentURLGenerator $attachmentURLGenerator, FAIconGenerator $FAIconGenerator)
|
||||
public function __construct(protected AttachmentURLGenerator $attachmentURLGenerator, protected FAIconGenerator $FAIconGenerator)
|
||||
{
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
$this->FAIconGenerator = $FAIconGenerator;
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
/* Returns the URL to a thumbnail of the given attachment */
|
||||
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']),
|
||||
new TwigFunction('attachment_thumbnail', fn(\App\Entity\Attachments\Attachment $attachment, string $filter_name = 'thumbnail_sm'): ?string => $this->attachmentURLGenerator->getThumbnailURL($attachment, $filter_name)),
|
||||
/* Returns the font awesome icon class which is representing the given file extension */
|
||||
new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']),
|
||||
new TwigFunction('ext_to_fa_icon', fn(string $extension): string => $this->FAIconGenerator->fileExtensionToFAType($extension)),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -30,7 +30,7 @@ final class BarcodeExtension extends AbstractExtension
|
|||
{
|
||||
return [
|
||||
/* Generates a barcode with the given Type and Data and returns it as an SVG represenation */
|
||||
new TwigFunction('barcode_svg', [$this, 'barcodeSVG']),
|
||||
new TwigFunction('barcode_svg', fn(string $content, string $type = 'QRCODE'): string => $this->barcodeSVG($content, $type)),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -44,24 +44,15 @@ use Twig\TwigTest;
|
|||
|
||||
final class EntityExtension extends AbstractExtension
|
||||
{
|
||||
protected EntityURLGenerator $entityURLGenerator;
|
||||
protected TreeViewGenerator $treeBuilder;
|
||||
private ElementTypeNameGenerator $nameGenerator;
|
||||
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator, TreeViewGenerator $treeBuilder, ElementTypeNameGenerator $elementTypeNameGenerator)
|
||||
public function __construct(protected EntityURLGenerator $entityURLGenerator, protected TreeViewGenerator $treeBuilder, private readonly ElementTypeNameGenerator $nameGenerator)
|
||||
{
|
||||
$this->entityURLGenerator = $entityURLGenerator;
|
||||
$this->treeBuilder = $treeBuilder;
|
||||
$this->nameGenerator = $elementTypeNameGenerator;
|
||||
}
|
||||
|
||||
public function getTests(): array
|
||||
{
|
||||
return [
|
||||
/* Checks if the given variable is an entitity (instance of AbstractDBElement) */
|
||||
new TwigTest('entity', static function ($var) {
|
||||
return $var instanceof AbstractDBElement;
|
||||
}),
|
||||
new TwigTest('entity', static fn($var) => $var instanceof AbstractDBElement),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -69,16 +60,16 @@ final class EntityExtension extends AbstractExtension
|
|||
{
|
||||
return [
|
||||
/* Returns a string representation of the given entity */
|
||||
new TwigFunction('entity_type', [$this, 'getEntityType']),
|
||||
new TwigFunction('entity_type', fn(object $entity): ?string => $this->getEntityType($entity)),
|
||||
/* Returns the URL to the given entity */
|
||||
new TwigFunction('entity_url', [$this, 'generateEntityURL']),
|
||||
new TwigFunction('entity_url', fn(\App\Entity\Base\AbstractDBElement $entity, string $method = 'info'): string => $this->generateEntityURL($entity, $method)),
|
||||
/* Returns the URL to the given entity in timetravel mode */
|
||||
new TwigFunction('timetravel_url', [$this, 'timeTravelURL']),
|
||||
new TwigFunction('timetravel_url', fn(\App\Entity\Base\AbstractDBElement $element, \DateTimeInterface $dateTime): ?string => $this->timeTravelURL($element, $dateTime)),
|
||||
/* Generates a JSON array of the given tree */
|
||||
new TwigFunction('tree_data', [$this, 'treeData']),
|
||||
new TwigFunction('tree_data', fn(\App\Entity\Base\AbstractDBElement $element, string $type = 'newEdit'): string => $this->treeData($element, $type)),
|
||||
|
||||
/* Gets a human readable label for the type of the given entity */
|
||||
new TwigFunction('entity_type_label', [$this->nameGenerator, 'getLocalizedTypeLabel']),
|
||||
new TwigFunction('entity_type_label', fn(object|string $entity): string => $this->nameGenerator->getLocalizedTypeLabel($entity)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -86,14 +77,14 @@ final class EntityExtension extends AbstractExtension
|
|||
{
|
||||
try {
|
||||
return $this->entityURLGenerator->timeTravelURL($element, $dateTime);
|
||||
} catch (EntityNotSupportedException $e) {
|
||||
} catch (EntityNotSupportedException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
|
||||
{
|
||||
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element);
|
||||
$tree = $this->treeBuilder->getTreeView($element::class, null, $type, $element);
|
||||
|
||||
return json_encode($tree, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
|
|
@ -34,37 +34,26 @@ use Twig\TwigFilter;
|
|||
|
||||
final class FormatExtension extends AbstractExtension
|
||||
{
|
||||
protected MarkdownParser $markdownParser;
|
||||
protected MoneyFormatter $moneyFormatter;
|
||||
protected SIFormatter $siformatter;
|
||||
protected AmountFormatter $amountFormatter;
|
||||
|
||||
|
||||
public function __construct(MarkdownParser $markdownParser, MoneyFormatter $moneyFormatter,
|
||||
SIFormatter $SIFormatter, AmountFormatter $amountFormatter)
|
||||
public function __construct(protected MarkdownParser $markdownParser, protected MoneyFormatter $moneyFormatter, protected SIFormatter $siformatter, protected AmountFormatter $amountFormatter)
|
||||
{
|
||||
$this->markdownParser = $markdownParser;
|
||||
$this->moneyFormatter = $moneyFormatter;
|
||||
$this->siformatter = $SIFormatter;
|
||||
$this->amountFormatter = $amountFormatter;
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
/* Mark the given text as markdown, which will be rendered in the browser */
|
||||
new TwigFilter('format_markdown', [$this->markdownParser, 'markForRendering'], [
|
||||
new TwigFilter('format_markdown', fn(string $markdown, bool $inline_mode = false): string => $this->markdownParser->markForRendering($markdown, $inline_mode), [
|
||||
'pre_escape' => 'html',
|
||||
'is_safe' => ['html'],
|
||||
]),
|
||||
/* Format the given amount as money, using a given currency */
|
||||
new TwigFilter('format_money', [$this, 'formatCurrency']),
|
||||
new TwigFilter('format_money', fn($amount, ?\App\Entity\PriceInformations\Currency $currency = null, int $decimals = 5): string => $this->formatCurrency($amount, $currency, $decimals)),
|
||||
/* Format the given number using SI prefixes and the given unit (string) */
|
||||
new TwigFilter('format_si', [$this, 'siFormat']),
|
||||
new TwigFilter('format_si', fn($value, $unit, $decimals = 2, bool $show_all_digits = false): string => $this->siFormat($value, $unit, $decimals, $show_all_digits)),
|
||||
/** Format the given amount using the given MeasurementUnit */
|
||||
new TwigFilter('format_amount', [$this, 'amountFormat']),
|
||||
new TwigFilter('format_amount', fn($value, ?\App\Entity\Parts\MeasurementUnit $unit, array $options = []): string => $this->amountFormat($value, $unit, $options)),
|
||||
/** Format the given number of bytes as human-readable number */
|
||||
new TwigFilter('format_bytes', [$this, 'formatBytes']),
|
||||
new TwigFilter('format_bytes', fn(int $bytes, int $precision = 2): string => $this->formatBytes($bytes, $precision)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -89,8 +78,6 @@ final class FormatExtension extends AbstractExtension
|
|||
|
||||
/**
|
||||
* @param $bytes
|
||||
* @param int $precision
|
||||
* @return string
|
||||
*/
|
||||
public function formatBytes(int $bytes, int $precision = 2): string
|
||||
{
|
||||
|
|
|
@ -28,20 +28,15 @@ use Twig\TwigFunction;
|
|||
final class LogExtension extends AbstractExtension
|
||||
{
|
||||
|
||||
private LogDataFormatter $logDataFormatter;
|
||||
private LogDiffFormatter $logDiffFormatter;
|
||||
|
||||
public function __construct(LogDataFormatter $logDataFormatter, LogDiffFormatter $logDiffFormatter)
|
||||
public function __construct(private readonly LogDataFormatter $logDataFormatter, private readonly LogDiffFormatter $logDiffFormatter)
|
||||
{
|
||||
$this->logDataFormatter = $logDataFormatter;
|
||||
$this->logDiffFormatter = $logDiffFormatter;
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('format_log_data', [$this->logDataFormatter, 'formatData'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('format_log_diff', [$this->logDiffFormatter, 'formatDiff'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('format_log_data', fn($data, \App\Entity\LogSystem\AbstractLogEntry $logEntry, string $fieldName): string => $this->logDataFormatter->formatData($data, $logEntry, $fieldName), ['is_safe' => ['html']]),
|
||||
new TwigFunction('format_log_diff', fn($old_data, $new_data): string => $this->logDiffFormatter->formatDiff($old_data, $new_data), ['is_safe' => ['html']]),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -25,11 +25,8 @@ use Twig\Extension\AbstractExtension;
|
|||
|
||||
final class MiscExtension extends AbstractExtension
|
||||
{
|
||||
private EventCommentNeededHelper $eventCommentNeededHelper;
|
||||
|
||||
public function __construct(EventCommentNeededHelper $eventCommentNeededHelper)
|
||||
public function __construct(private readonly EventCommentNeededHelper $eventCommentNeededHelper)
|
||||
{
|
||||
$this->eventCommentNeededHelper = $eventCommentNeededHelper;
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
|
|
|
@ -35,19 +35,11 @@ use function is_array;
|
|||
*/
|
||||
final class InheritanceSecurityPolicy implements SecurityPolicyInterface
|
||||
{
|
||||
private array $allowedTags;
|
||||
private array $allowedFilters;
|
||||
private array $allowedMethods;
|
||||
private array $allowedProperties;
|
||||
private array $allowedFunctions;
|
||||
|
||||
public function __construct(array $allowedTags = [], array $allowedFilters = [], array $allowedMethods = [], array $allowedProperties = [], array $allowedFunctions = [])
|
||||
public function __construct(private array $allowedTags = [], private array $allowedFilters = [], array $allowedMethods = [], private array $allowedProperties = [], private array $allowedFunctions = [])
|
||||
{
|
||||
$this->allowedTags = $allowedTags;
|
||||
$this->allowedFilters = $allowedFilters;
|
||||
$this->setAllowedMethods($allowedMethods);
|
||||
$this->allowedProperties = $allowedProperties;
|
||||
$this->allowedFunctions = $allowedFunctions;
|
||||
}
|
||||
|
||||
public function setAllowedTags(array $tags): void
|
||||
|
@ -65,7 +57,7 @@ final class InheritanceSecurityPolicy implements SecurityPolicyInterface
|
|||
$this->allowedMethods = [];
|
||||
foreach ($methods as $class => $m) {
|
||||
$this->allowedMethods[$class] = array_map(
|
||||
static function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, is_array($m) ? $m : [$m]);
|
||||
static fn($value): string => strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), is_array($m) ? $m : [$m]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,7 +112,7 @@ final class InheritanceSecurityPolicy implements SecurityPolicyInterface
|
|||
}
|
||||
|
||||
if (!$allowed) {
|
||||
$class = get_class($obj);
|
||||
$class = $obj::class;
|
||||
|
||||
throw new SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method);
|
||||
}
|
||||
|
@ -141,7 +133,7 @@ final class InheritanceSecurityPolicy implements SecurityPolicyInterface
|
|||
}
|
||||
|
||||
if (!$allowed) {
|
||||
$class = get_class($obj);
|
||||
$class = $obj::class;
|
||||
|
||||
throw new SecurityNotAllowedPropertyError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property);
|
||||
}
|
||||
|
|
|
@ -30,11 +30,8 @@ use Twig\TwigTest;
|
|||
*/
|
||||
final class TwigCoreExtension extends AbstractExtension
|
||||
{
|
||||
protected ObjectNormalizer $objectNormalizer;
|
||||
|
||||
public function __construct(ObjectNormalizer $objectNormalizer)
|
||||
public function __construct(protected ObjectNormalizer $objectNormalizer)
|
||||
{
|
||||
$this->objectNormalizer = $objectNormalizer;
|
||||
}
|
||||
|
||||
public function getTests(): array
|
||||
|
@ -43,13 +40,9 @@ final class TwigCoreExtension extends AbstractExtension
|
|||
/*
|
||||
* Checks if a given variable is an instance of a given class. E.g. ` x is instanceof('App\Entity\Parts\Part')`
|
||||
*/
|
||||
new TwigTest('instanceof', static function ($var, $instance) {
|
||||
return $var instanceof $instance;
|
||||
}),
|
||||
new TwigTest('instanceof', static fn($var, $instance) => $var instanceof $instance),
|
||||
/* Checks if a given variable is an object. E.g. `x is object` */
|
||||
new TwigTest('object', static function ($var) {
|
||||
return is_object($var);
|
||||
}),
|
||||
new TwigTest('object', static fn($var): object => is_object($var)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -57,7 +50,7 @@ final class TwigCoreExtension extends AbstractExtension
|
|||
{
|
||||
return [
|
||||
/* Converts the given object to an array representation of the public/accessible properties */
|
||||
new TwigFilter('to_array', [$this, 'toArray']),
|
||||
new TwigFilter('to_array', fn($object) => $this->toArray($object)),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ use Twig\TwigFunction;
|
|||
|
||||
final class UserExtension extends AbstractExtension
|
||||
{
|
||||
private LogEntryRepository $repo;
|
||||
private readonly LogEntryRepository $repo;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ final class UserExtension extends AbstractExtension
|
|||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('remove_locale_from_path', [$this, 'removeLocaleFromPath']),
|
||||
new TwigFilter('remove_locale_from_path', fn(string $path): string => $this->removeLocaleFromPath($path)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -68,9 +68,9 @@ final class UserExtension extends AbstractExtension
|
|||
{
|
||||
return [
|
||||
/* Returns the user which has edited the given entity the last time. */
|
||||
new TwigFunction('last_editing_user', [$this->repo, 'getLastEditingUser']),
|
||||
new TwigFunction('last_editing_user', fn(\App\Entity\Base\AbstractDBElement $element): ?\App\Entity\UserSystem\User => $this->repo->getLastEditingUser($element)),
|
||||
/* Returns the user which has created the given entity. */
|
||||
new TwigFunction('creating_user', [$this->repo, 'getCreatingUser']),
|
||||
new TwigFunction('creating_user', fn(\App\Entity\Base\AbstractDBElement $element): ?\App\Entity\UserSystem\User => $this->repo->getCreatingUser($element)),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue