From ba52dcaa8bee6fa992da89956282f413f66b97eb Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 26 Jun 2021 14:38:08 +0200 Subject: [PATCH] fix phpstan/phpcs --- Admin/Installer.php | 6 +- Controller/ApiController.php | 15 ++++- Models/Collection.php | 9 +++ Models/CollectionMapper.php | 6 +- Models/MediaMapper.php | 8 +-- Models/UploadFile.php | 6 +- .../Backend/Components/Media/ElementView.php | 8 --- .../Components/Media/spreadsheet.tpl.php | 60 +++++++++---------- Theme/Backend/Components/Media/word.tpl.php | 2 +- composer.json | 2 +- 10 files changed, 69 insertions(+), 53 deletions(-) diff --git a/Admin/Installer.php b/Admin/Installer.php index 675a007..1074240 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -42,7 +42,7 @@ final class Installer extends InstallerAbstract * Install data from providing modules. * * @param ApplicationAbstract $app Application - * @param array $data Module info + * @param array $data Additional data * * @return array * @@ -116,7 +116,7 @@ final class Installer extends InstallerAbstract $path = '/Modules/Media/Files' . ($data['virtualPath'] ?? '') . '/' . ($data['name'] ?? ''); } else { $dirPath = $data['path'] . '/' . ($data['name'] ?? ''); - $path = $data['path'] ?? '/Modules/Media/Files' . '/' . ($data['name'] ?? ''); + $path = $data['path'] ?? '/Modules/Media/Files/' . ($data['name'] ?? ''); } $collection = new Collection(); @@ -193,7 +193,7 @@ final class Installer extends InstallerAbstract $media = new Media(); $media->setPath(ApiController::normalizeDbPath($data['path']) . '/' . $uFile['filename']); - $media->name = $uFile['name']; + $media->name = $uFile['filename']; $media->size = $uFile['size']; $media->createdBy = new NullAccount((int) $data['user'] ?? 1); $media->extension = $uFile['extension']; diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 9e740a5..0154cfd 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -172,6 +172,17 @@ final class ApiController extends Controller return $this->createDbEntries($status, $account, $virtualPath, $type); } + /** + * Uploads a file to a destination + * + * @param array $files Files to upload + * @param string $name Name of the file (only if a single file is provided) + * @param string $path Upload path + * + * @return array + * + * @since 1.0.0 + */ public static function uploadFilesToDestination( array $files, string $name = '', @@ -266,7 +277,7 @@ final class ApiController extends Controller $media = new Media(); $media->setPath(self::normalizeDbPath($status['path']) . '/' . $status['filename']); - $media->name = $status['name']; + $media->name = empty($status['name']) ? $status['filename'] : $status['name']; $media->size = $status['size']; $media->createdBy = new NullAccount($account); $media->extension = $status['extension']; @@ -579,7 +590,7 @@ final class ApiController extends Controller public function apiMediaExport(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { /** @var Media $media */ - $media = MediaMapper::get((int) $request->getData('id')); + $media = MediaMapper::get((int) $request->getData('id')); $view = $this->createView($media, $request, $response); $this->setMediaResponseHeader($view, $media, $request, $response); diff --git a/Models/Collection.php b/Models/Collection.php index 9aa4e1b..ae703fe 100755 --- a/Models/Collection.php +++ b/Models/Collection.php @@ -88,6 +88,15 @@ class Collection extends Media implements \Iterator return $this->sources; } + /** + * Get media element by its name. + * + * @param string $name Name of the media element + * + * @return Media + * + * @since 1.0.0 + */ public function getSourceByName(string $name) : Media { foreach ($this->sources as $source) { diff --git a/Models/CollectionMapper.php b/Models/CollectionMapper.php index 8731f0b..60bb6ec 100755 --- a/Models/CollectionMapper.php +++ b/Models/CollectionMapper.php @@ -100,11 +100,11 @@ final class CollectionMapper extends MediaMapper { $depth = 3; $query = self::getQuery(); - $query->where(self::$table . '_' . $depth . '.media_virtual', '=', $virtualPath); - $query->where(self::$table . '_' . $depth . '.media_collection', '=', 1); + $query->where(self::$table . '_d' . $depth . '.media_virtual', '=', $virtualPath); + $query->where(self::$table . '_d' . $depth . '.media_collection', '=', 1); if ($hidden === false) { - $query->andWhere(self::$table . '_' . $depth . '.media_hidden', '=', (int) $hidden); + $query->andWhere(self::$table . '_d' . $depth . '.media_hidden', '=', (int) $hidden); } return self::getAllByQuery($query, RelationType::ALL, $depth); diff --git a/Models/MediaMapper.php b/Models/MediaMapper.php index 906a399..79db48b 100755 --- a/Models/MediaMapper.php +++ b/Models/MediaMapper.php @@ -141,10 +141,10 @@ class MediaMapper extends DataMapperAbstract { $depth = 3; $query = self::getQuery(); - $query->where(self::$table . '_' . $depth . '.media_virtual', '=', $virtualPath); + $query->where(self::$table . '_d' . $depth . '.media_virtual', '=', $virtualPath); if ($hidden === false) { - $query->andWhere(self::$table . '_' . $depth . '.media_hidden', '=', (int) $hidden); + $query->andWhere(self::$table . '_d' . $depth . '.media_hidden', '=', (int) $hidden); } return self::getAllByQuery($query, RelationType::ALL, $depth); @@ -166,8 +166,8 @@ class MediaMapper extends DataMapperAbstract $depth = 3; $query = self::getQuery(); - $query->where(self::$table. '_' . $depth . '.media_virtual', '=', $virtualPath) - ->andWhere(self::$table. '_' . $depth . '.media_name', '=', $name); + $query->where(self::$table. '_d' . $depth . '.media_virtual', '=', $virtualPath) + ->andWhere(self::$table. '_d' . $depth . '.media_name', '=', $name); $objs = self::getAllByQuery($query, RelationType::ALL, $depth); diff --git a/Models/UploadFile.php b/Models/UploadFile.php index 3e3c0f6..75c3181 100755 --- a/Models/UploadFile.php +++ b/Models/UploadFile.php @@ -107,6 +107,8 @@ class UploadFile $files = [$files]; } + $fileCount = \count($files); + if (!$absolute && \count($files) > 1) { $this->outputDir = $this->findOutputDir(); } @@ -158,7 +160,7 @@ class UploadFile $result[$key]['filename'] = $name; } - if (empty($name) || \is_file($path . '/' . $name)) { + if (!$this->preserveFileName || $fileCount !== 1 || empty($name) || \is_file($path . '/' . $name)) { try { $name = $this->createFileName($path, $f['tmp_name'], $extension); $result[$key]['filename'] = $name; @@ -170,6 +172,8 @@ class UploadFile } } + $result[$key]['name'] = empty($name) ? $result[$key]['filename'] : $name; + if (!\is_dir($path)) { $created = Directory::create($path, 0755, true); diff --git a/Theme/Backend/Components/Media/ElementView.php b/Theme/Backend/Components/Media/ElementView.php index 0d84f99..5996d74 100755 --- a/Theme/Backend/Components/Media/ElementView.php +++ b/Theme/Backend/Components/Media/ElementView.php @@ -39,14 +39,6 @@ class ElementView extends MediaView */ protected Media $media; - /** - * {@inheritdoc} - */ - public function __construct(L11nManager $l11n = null, RequestAbstract $request, ResponseAbstract $response) - { - parent::__construct($l11n, $request, $response); - } - /** * {@inheritdoc} */ diff --git a/Theme/Backend/Components/Media/spreadsheet.tpl.php b/Theme/Backend/Components/Media/spreadsheet.tpl.php index 2ac5d74..efb9fe9 100755 --- a/Theme/Backend/Components/Media/spreadsheet.tpl.php +++ b/Theme/Backend/Components/Media/spreadsheet.tpl.php @@ -17,45 +17,45 @@ Autoloader::addPath(__DIR__ . '/../../../../../../Resources/');
- +
media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath()); - $reader->setReadDataOnly(true); - $spreadsheet = $reader->load(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath()); + $reader->setReadDataOnly(true); + $spreadsheet = $reader->load(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath()); - $writer = new Csv($spreadsheet); - $writer->setDelimiter(';'); - $writer->setEnclosure('"'); - $writer->setLineEnding("\n"); - $writer->setUseBOM(true); + $writer = new Csv($spreadsheet); + $writer->setDelimiter(';'); + $writer->setEnclosure('"'); + $writer->setLineEnding("\n"); + $writer->setUseBOM(true); - \ob_start(); - $writer->save('php://output'); - $data = \ob_get_clean(); - $csv = \explode("\n", \trim($data, "\n")); - ?> - - save('php://output'); + $data = \ob_get_clean(); + $csv = \explode("\n", \trim($data, "\n")); + ?> +
+ '; - foreach ($lineCsv as $cell) { - if ($cell === null) { - break; - } + echo ''; + foreach ($lineCsv as $cell) { + if ($cell === null) { + break; + } - echo '
' . \htmlspecialchars($cell); - } - } - ?> -
+ echo '' . \htmlspecialchars($cell); + } + } + ?> +
diff --git a/Theme/Backend/Components/Media/word.tpl.php b/Theme/Backend/Components/Media/word.tpl.php index 86cff28..fc43fc5 100755 --- a/Theme/Backend/Components/Media/word.tpl.php +++ b/Theme/Backend/Components/Media/word.tpl.php @@ -5,6 +5,6 @@ use phpOMS\Uri\UriFactory; ?>
- +
diff --git a/composer.json b/composer.json index 8f2fb6d..7ad134b 100755 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ ], "require-dev": { "phpunit/phpunit": ">=9.4", - "friendsofphp/php-cs-fixer": ">=2.18", + "friendsofphp/php-cs-fixer": ">=3.0", "squizlabs/php_codesniffer": ">=3.5", "phpmd/phpmd": ">=2.9", "phpstan/phpstan": ">=0.12.58",