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,11 +35,8 @@ use function strlen;
*/
class AttachmentManager
{
protected AttachmentPathResolver $pathResolver;
public function __construct(AttachmentPathResolver $pathResolver)
public function __construct(protected AttachmentPathResolver $pathResolver)
{
$this->pathResolver = $pathResolver;
}
/**

View file

@ -32,14 +32,12 @@ use Symfony\Component\Filesystem\Filesystem;
*/
class AttachmentPathResolver
{
protected string $project_dir;
protected ?string $media_path;
protected ?string $footprints_path;
protected ?string $models_path;
protected ?string $secure_path;
protected array $placeholders;
protected array $placeholders = ['%MEDIA%', '%BASE%/data/media', '%FOOTPRINTS%', '%FOOTPRINTS_3D%', '%SECURE%'];
protected array $pathes;
protected array $placeholders_regex;
protected array $pathes_regex;
@ -53,18 +51,13 @@ class AttachmentPathResolver
* Set to null if this ressource should be disabled.
* @param string|null $models_path set to null if this ressource should be disabled
*/
public function __construct(string $project_dir, string $media_path, string $secure_path, ?string $footprints_path, ?string $models_path)
public function __construct(protected string $project_dir, string $media_path, string $secure_path, ?string $footprints_path, ?string $models_path)
{
$this->project_dir = $project_dir;
//Determine the path for our ressources
$this->media_path = $this->parameterToAbsolutePath($media_path);
$this->footprints_path = $this->parameterToAbsolutePath($footprints_path);
$this->models_path = $this->parameterToAbsolutePath($models_path);
$this->secure_path = $this->parameterToAbsolutePath($secure_path);
//Here we define the valid placeholders and their replacement values
$this->placeholders = ['%MEDIA%', '%BASE%/data/media', '%FOOTPRINTS%', '%FOOTPRINTS_3D%', '%SECURE%'];
$this->pathes = [$this->media_path, $this->media_path, $this->footprints_path, $this->models_path, $this->secure_path];
//Remove all disabled placeholders
@ -248,7 +241,7 @@ class AttachmentPathResolver
$ret = [];
foreach ($array as $item) {
$item = str_replace(['\\'], ['/'], $item);
$item = str_replace(['\\'], ['/'], (string) $item);
$ret[] = '/'.preg_quote($item, '/').'/';
}

View file

@ -34,18 +34,8 @@ use Symfony\Component\Filesystem\Filesystem;
*/
class AttachmentReverseSearch
{
protected EntityManagerInterface $em;
protected AttachmentPathResolver $pathResolver;
protected CacheManager $cacheManager;
protected AttachmentURLGenerator $attachmentURLGenerator;
public function __construct(EntityManagerInterface $em, AttachmentPathResolver $pathResolver,
CacheManager $cacheManager, AttachmentURLGenerator $attachmentURLGenerator)
public function __construct(protected EntityManagerInterface $em, protected AttachmentPathResolver $pathResolver, protected CacheManager $cacheManager, protected AttachmentURLGenerator $attachmentURLGenerator)
{
$this->em = $em;
$this->pathResolver = $pathResolver;
$this->cacheManager = $cacheManager;
$this->attachmentURLGenerator = $attachmentURLGenerator;
}
/**

View file

@ -55,16 +55,7 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
*/
class AttachmentSubmitHandler
{
protected AttachmentPathResolver $pathResolver;
protected array $folder_mapping;
protected bool $allow_attachments_downloads;
protected HttpClientInterface $httpClient;
protected MimeTypesInterface $mimeTypes;
protected FileTypeFilterTools $filterTools;
/**
* @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to
*/
protected string $max_upload_size;
private ?int $max_upload_size_bytes = null;
@ -72,18 +63,13 @@ class AttachmentSubmitHandler
'asp', 'cgi', 'py', 'pl', 'exe', 'aspx', 'js', 'mjs', 'jsp', 'css', 'jar', 'html', 'htm', 'shtm', 'shtml', 'htaccess',
'htpasswd', ''];
public function __construct(AttachmentPathResolver $pathResolver, bool $allow_attachments_downloads,
HttpClientInterface $httpClient, MimeTypesInterface $mimeTypes,
FileTypeFilterTools $filterTools, string $max_upload_size)
public function __construct(protected AttachmentPathResolver $pathResolver, protected bool $allow_attachments_downloads,
protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes,
protected FileTypeFilterTools $filterTools, /**
* @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to
*/
protected string $max_upload_size)
{
$this->pathResolver = $pathResolver;
$this->allow_attachments_downloads = $allow_attachments_downloads;
$this->httpClient = $httpClient;
$this->mimeTypes = $mimeTypes;
$this->max_upload_size = $max_upload_size;
$this->filterTools = $filterTools;
//The mapping used to determine which folder will be used for an attachment type
$this->folder_mapping = [
PartAttachment::class => 'part',
@ -155,25 +141,21 @@ class AttachmentSubmitHandler
*/
public function generateAttachmentPath(Attachment $attachment, bool $secure_upload = false): string
{
if ($secure_upload) {
$base_path = $this->pathResolver->getSecurePath();
} else {
$base_path = $this->pathResolver->getMediaPath();
}
$base_path = $secure_upload ? $this->pathResolver->getSecurePath() : $this->pathResolver->getMediaPath();
//Ensure the given attachment class is known to mapping
if (!isset($this->folder_mapping[get_class($attachment)])) {
throw new InvalidArgumentException('The given attachment class is not known! The passed class was: '.get_class($attachment));
if (!isset($this->folder_mapping[$attachment::class])) {
throw new InvalidArgumentException('The given attachment class is not known! The passed class was: '.$attachment::class);
}
//Ensure the attachment has an assigned element
if (null === $attachment->getElement()) {
if (!$attachment->getElement() instanceof \App\Entity\Attachments\AttachmentContainingDBElement) {
throw new InvalidArgumentException('The given attachment is not assigned to an element! An element is needed to generate a path!');
}
//Build path
return
$base_path.DIRECTORY_SEPARATOR //Base path
.$this->folder_mapping[get_class($attachment)].DIRECTORY_SEPARATOR.$attachment->getElement()->getID();
.$this->folder_mapping[$attachment::class].DIRECTORY_SEPARATOR.$attachment->getElement()->getID();
}
/**
@ -194,7 +176,7 @@ class AttachmentSubmitHandler
$options = $resolver->resolve($options);
//When a file is given then upload it, otherwise check if we need to download the URL
if ($file) {
if ($file instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) {
$this->upload($attachment, $file, $options);
} elseif ($options['download_url'] && $attachment->isExternal()) {
$this->downloadURL($attachment, $options);
@ -210,7 +192,7 @@ class AttachmentSubmitHandler
//this is only possible if the attachment is new (not yet persisted to DB)
if ($options['become_preview_if_empty'] && null === $attachment->getID() && $attachment->isPicture()) {
$element = $attachment->getElement();
if ($element instanceof AttachmentContainingDBElement && null === $element->getMasterPictureAttachment()) {
if ($element instanceof AttachmentContainingDBElement && !$element->getMasterPictureAttachment() instanceof \App\Entity\Attachments\Attachment) {
$element->setMasterPictureAttachment($attachment);
}
}
@ -220,8 +202,6 @@ class AttachmentSubmitHandler
/**
* Rename attachments with an unsafe extension (meaning files which would be run by a to a safe one).
* @param Attachment $attachment
* @return Attachment
*/
protected function renameBlacklistedExtensions(Attachment $attachment): Attachment
{
@ -391,7 +371,7 @@ class AttachmentSubmitHandler
$new_path = $this->pathResolver->realPathToPlaceholder($new_path);
//Save the path to the attachment
$attachment->setPath($new_path);
} catch (TransportExceptionInterface $transportExceptionInterface) {
} catch (TransportExceptionInterface) {
throw new AttachmentDownloadException('Transport error!');
}
@ -428,8 +408,6 @@ class AttachmentSubmitHandler
/**
* Parses the given file size string and returns the size in bytes.
* Taken from https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Validator/Constraints/File.php
* @param string $maxSize
* @return int
*/
private function parseFileSizeString(string $maxSize): int
{

View file

@ -32,26 +32,12 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AttachmentURLGenerator
{
protected Packages $assets;
protected string $public_path;
protected AttachmentPathResolver $pathResolver;
protected UrlGeneratorInterface $urlGenerator;
protected AttachmentManager $attachmentHelper;
protected CacheManager $thumbnailManager;
protected LoggerInterface $logger;
public function __construct(Packages $assets, AttachmentPathResolver $pathResolver,
UrlGeneratorInterface $urlGenerator, AttachmentManager $attachmentHelper,
CacheManager $thumbnailManager, LoggerInterface $logger)
public function __construct(protected Packages $assets, protected AttachmentPathResolver $pathResolver,
protected UrlGeneratorInterface $urlGenerator, protected AttachmentManager $attachmentHelper,
protected CacheManager $thumbnailManager, protected LoggerInterface $logger)
{
$this->assets = $assets;
$this->pathResolver = $pathResolver;
$this->urlGenerator = $urlGenerator;
$this->attachmentHelper = $attachmentHelper;
$this->thumbnailManager = $thumbnailManager;
$this->logger = $logger;
//Determine a normalized path to the public folder (assets are relative to this folder)
$this->public_path = $this->pathResolver->parameterToAbsolutePath('public');
}

View file

@ -33,13 +33,8 @@ use Symfony\Contracts\Cache\CacheInterface;
*/
class BuiltinAttachmentsFinder
{
protected AttachmentPathResolver $pathResolver;
protected CacheInterface $cache;
public function __construct(CacheInterface $cache, AttachmentPathResolver $pathResolver)
public function __construct(protected CacheInterface $cache, protected AttachmentPathResolver $pathResolver)
{
$this->pathResolver = $pathResolver;
$this->cache = $cache;
}
/**
@ -49,7 +44,6 @@ class BuiltinAttachmentsFinder
* '%FOOTPRINTS%/path/to/folder/file1.png',
* '%FOOTPRINTS%/path/to/folder/file2.png',
* ]
* @return array
*/
public function getListOfFootprintsGroupedByFolder(): array
{
@ -109,7 +103,7 @@ class BuiltinAttachmentsFinder
return $results;
});
} catch (InvalidArgumentException $invalidArgumentException) {
} catch (InvalidArgumentException) {
return [];
}
}

View file

@ -43,13 +43,8 @@ class FileTypeFilterTools
protected const AUDIO_EXTS = ['mp3', 'flac', 'ogg', 'oga', 'wav', 'm4a', 'opus'];
protected const ALLOWED_MIME_PLACEHOLDERS = ['image/*', 'audio/*', 'video/*'];
protected MimeTypesInterface $mimeTypes;
protected CacheInterface $cache;
public function __construct(MimeTypesInterface $mimeTypes, CacheInterface $cache)
public function __construct(protected MimeTypesInterface $mimeTypes, protected CacheInterface $cache)
{
$this->mimeTypes = $mimeTypes;
$this->cache = $cache;
}
/**

View file

@ -27,11 +27,8 @@ use App\Entity\Parts\Part;
class PartPreviewGenerator
{
protected AttachmentManager $attachmentHelper;
public function __construct(AttachmentManager $attachmentHelper)
public function __construct(protected AttachmentManager $attachmentHelper)
{
$this->attachmentHelper = $attachmentHelper;
}
/**
@ -55,21 +52,21 @@ class PartPreviewGenerator
$list[] = $attachment;
}
if (null !== $part->getFootprint()) {
if ($part->getFootprint() instanceof \App\Entity\Parts\Footprint) {
$attachment = $part->getFootprint()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
}
}
if (null !== $part->getBuiltProject()) {
if ($part->getBuiltProject() instanceof \App\Entity\ProjectSystem\Project) {
$attachment = $part->getBuiltProject()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
}
}
if (null !== $part->getCategory()) {
if ($part->getCategory() instanceof \App\Entity\Parts\Category) {
$attachment = $part->getCategory()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
@ -77,7 +74,7 @@ class PartPreviewGenerator
}
foreach ($part->getPartLots() as $lot) {
if (null !== $lot->getStorageLocation()) {
if ($lot->getStorageLocation() instanceof \App\Entity\Parts\Storelocation) {
$attachment = $lot->getStorageLocation()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
@ -85,14 +82,14 @@ class PartPreviewGenerator
}
}
if (null !== $part->getPartUnit()) {
if ($part->getPartUnit() instanceof \App\Entity\Parts\MeasurementUnit) {
$attachment = $part->getPartUnit()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
}
}
if (null !== $part->getManufacturer()) {
if ($part->getManufacturer() instanceof \App\Entity\Parts\Manufacturer) {
$attachment = $part->getManufacturer()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
$list[] = $attachment;
@ -117,7 +114,7 @@ class PartPreviewGenerator
}
//Otherwise check if the part has a footprint with a valid master attachment
if (null !== $part->getFootprint()) {
if ($part->getFootprint() instanceof \App\Entity\Parts\Footprint) {
$attachment = $part->getFootprint()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
return $attachment;
@ -125,7 +122,7 @@ class PartPreviewGenerator
}
//With lowest priority use the master attachment of the project this part represents (when existing)
if (null !== $part->getBuiltProject()) {
if ($part->getBuiltProject() instanceof \App\Entity\ProjectSystem\Project) {
$attachment = $part->getBuiltProject()->getMasterPictureAttachment();
if ($this->isAttachmentValidPicture($attachment)) {
return $attachment;
@ -145,7 +142,7 @@ class PartPreviewGenerator
*/
protected function isAttachmentValidPicture(?Attachment $attachment): bool
{
return null !== $attachment
return $attachment instanceof \App\Entity\Attachments\Attachment
&& $attachment->isPicture()
&& $this->attachmentHelper->isFileExisting($attachment);
}