Applied code style rules to src/

This commit is contained in:
Jan Böhmer 2020-01-05 15:46:58 +01:00
parent 700c049d26
commit f861de791f
186 changed files with 1462 additions and 1059 deletions

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -49,7 +52,7 @@ class AttachmentManager
*/
public function attachmentToFile(Attachment $attachment): ?\SplFileInfo
{
if ($attachment->isExternal() || !$this->isFileExisting($attachment)) {
if ($attachment->isExternal() || ! $this->isFileExisting($attachment)) {
return null;
}
@ -117,7 +120,7 @@ class AttachmentManager
return null;
}
if (!$this->isFileExisting($attachment)) {
if (! $this->isFileExisting($attachment)) {
return null;
}
@ -138,7 +141,7 @@ class AttachmentManager
{
$bytes = $this->getFileSize($attachment);
if (null == $bytes) {
if (null === $bytes) {
return null;
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -114,23 +117,6 @@ class AttachmentPathResolver
return $tmp;
}
/**
* Create an array usable for preg_replace out of an array of placeholders or pathes.
* Slashes and other chars become escaped.
* For example: '%TEST%' becomes '/^%TEST%/'.
*/
protected function arrayToRegexArray(array $array): array
{
$ret = [];
foreach ($array as $item) {
$item = str_replace(['\\'], ['/'], $item);
$ret[] = '/'.preg_quote($item, '/').'/';
}
return $ret;
}
/**
* Converts an relative placeholder filepath (with %MEDIA% or older %BASE%) to an absolute filepath on disk.
* The directory separator is always /. Relative pathes are not realy possible (.. is striped).
@ -163,9 +149,7 @@ class AttachmentPathResolver
}
//Normalize path and remove .. (to prevent directory traversal attack)
$placeholder_path = str_replace(['\\'], ['/'], $placeholder_path);
return $placeholder_path;
return str_replace(['\\'], ['/'], $placeholder_path);
}
/**
@ -199,7 +183,7 @@ class AttachmentPathResolver
}
//If the new string does not begin with a placeholder, it is invalid
if (!preg_match('/^%\w+%/', $real_path)) {
if (! preg_match('/^%\w+%/', $real_path)) {
return null;
}
@ -246,4 +230,21 @@ class AttachmentPathResolver
{
return $this->models_path;
}
/**
* Create an array usable for preg_replace out of an array of placeholders or pathes.
* Slashes and other chars become escaped.
* For example: '%TEST%' becomes '/^%TEST%/'.
*/
protected function arrayToRegexArray(array $array): array
{
$ret = [];
foreach ($array as $item) {
$item = str_replace(['\\'], ['/'], $item);
$ret[] = '/'.preg_quote($item, '/').'/';
}
return $ret;
}
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -71,17 +74,6 @@ class AttachmentSubmitHandler
SupplierAttachment::class => 'supplier', UserAttachment::class => 'user', ];
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
//If no preview image was set yet, the new uploaded file will become the preview image
'become_preview_if_empty' => true,
//When an URL is given download the URL
'download_url' => false,
'secure_attachment' => false,
]);
}
/**
* Generates a filename for the given attachment and extension.
* The filename contains a random id, so every time this function is called you get an unique name.
@ -125,7 +117,7 @@ class AttachmentSubmitHandler
}
//Ensure the given attachment class is known to mapping
if (!isset($this->folder_mapping[\get_class($attachment)])) {
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));
}
//Ensure the attachment has an assigned element
@ -178,6 +170,17 @@ class AttachmentSubmitHandler
return $attachment;
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
//If no preview image was set yet, the new uploaded file will become the preview image
'become_preview_if_empty' => true,
//When an URL is given download the URL
'download_url' => false,
'secure_attachment' => false,
]);
}
/**
* Move the given attachment to secure location (or back to public folder) if needed.
*
@ -200,13 +203,13 @@ class AttachmentSubmitHandler
//Determine the old filepath
$old_path = $this->pathResolver->placeholderToRealPath($attachment->getPath());
if (!file_exists($old_path)) {
if (! file_exists($old_path)) {
return $attachment;
}
$filename = basename($old_path);
//If the basename is not one of the new unique on, we have to save the old filename
if (!preg_match('/\w+-\w{13}\./', $filename)) {
if (! preg_match('/\w+-\w{13}\./', $filename)) {
//Save filename to attachment field
$attachment->setFilename($attachment->getFilename());
}
@ -236,7 +239,7 @@ class AttachmentSubmitHandler
protected function downloadURL(Attachment $attachment, array $options): Attachment
{
//Check if we are allowed to download files
if (!$this->allow_attachments_downloads) {
if (! $this->allow_attachments_downloads) {
throw new \RuntimeException('Download of attachments is not allowed!');
}
@ -286,7 +289,7 @@ class AttachmentSubmitHandler
//Check if we have a extension given
$pathinfo = pathinfo($filename);
if (!empty($pathinfo['extension'])) {
if (! empty($pathinfo['extension'])) {
$new_ext = $pathinfo['extension'];
} else { //Otherwise we have to guess the extension for the new file, based on its content
$new_ext = $this->mimeTypes->getExtensions($this->mimeTypes->guessMimeType($tmp_path))[0] ?? 'tmp';

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -102,7 +105,7 @@ class AttachmentURLGenerator
*/
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): string
{
if (!$attachment->isPicture()) {
if (! $attachment->isPicture()) {
throw new \InvalidArgumentException('Thumbnail creation only works for picture attachments!');
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -40,16 +43,6 @@ class BuiltinAttachmentsFinder
$this->cache = $cache;
}
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'limit' => 15, //Given only 15 entries
//'allowed_extensions' => [], //Filter the filenames. For example ['jpg', 'jpeg'] to only get jpegs.
//'placeholders' => Attachment::BUILTIN_PLACEHOLDER, //By default use all builtin ressources,
'empty_returns_all' => false, //Return the whole list of ressources when empty keyword is given
]);
}
/**
* Returns a list of all builtin ressources.
* The array is a list of the relative filenames using the %PLACEHOLDERS%.
@ -138,4 +131,14 @@ class BuiltinAttachmentsFinder
return preg_grep($regex, $base_list);
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'limit' => 15, //Given only 15 entries
//'allowed_extensions' => [], //Filter the filenames. For example ['jpg', 'jpeg'] to only get jpegs.
//'placeholders' => Attachment::BUILTIN_PLACEHOLDER, //By default use all builtin ressources,
'empty_returns_all' => false, //Return the whole list of ressources when empty keyword is given
]);
}
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
@ -39,9 +42,9 @@ class FileTypeFilterTools
protected const IMAGE_EXTS = Attachment::PICTURE_EXTS;
protected const VIDEO_EXTS = ['mp4', 'ogv', 'ogg', 'webm'];
protected const AUDIO_EXTS = ['mp3', 'flac', 'ogg', 'oga', 'wav', 'm4a', 'opus'];
protected const ALLOWED_MIME_PLACEHOLDERS = ['image/*', 'audio/*', 'video/*'];
protected $mimeTypes;
protected const ALLOWED_MIME_PLACEHOLDERS = ['image/*', 'audio/*', 'video/*'];
protected $cache;
public function __construct(MimeTypesInterface $mimeTypes, CacheInterface $cache)
@ -69,9 +72,9 @@ class FileTypeFilterTools
//Check for each element if it is valid:
foreach ($elements as $element) {
$element = trim($element);
if (!preg_match('/^\.\w+$/', $element) // .ext is allowed
&& !preg_match('/^[-\w.]+\/[-\w.]+/', $element) //Explicit MIME type is allowed
&& !\in_array($element, static::ALLOWED_MIME_PLACEHOLDERS, false)) { //image/* is allowed
if (! preg_match('/^\.\w+$/', $element) // .ext is allowed
&& ! preg_match('/^[-\w.]+\/[-\w.]+/', $element) //Explicit MIME type is allowed
&& ! \in_array($element, static::ALLOWED_MIME_PLACEHOLDERS, false)) { //image/* is allowed
return false;
}
}
@ -117,7 +120,7 @@ class FileTypeFilterTools
$element = 'video/*';
} elseif ('audio' === $element || 'audio/' === $element) {
$element = 'audio/*';
} elseif (!preg_match('/^[-\w.]+\/[-\w.*]+/', $element) && 0 !== strpos($element, '.')) {
} elseif (! preg_match('/^[-\w.]+\/[-\w.*]+/', $element) && 0 !== strpos($element, '.')) {
//Convert jpg to .jpg
$element = '.'.$element;
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*