From 5eb1752839d9a9e5ae7095a9842c0d22484f7e38 Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 29 Nov 2023 09:40:52 +0100 Subject: [PATCH] Refactored phpstan fixes --- src/Controller/AbstractChunkedController.php | 3 +- .../OneupUploaderExtension.php | 12 ++---- src/Routing/RouteLoader.php | 12 +----- src/Templating/Helper/UploaderHelper.php | 5 +-- src/Twig/Extension/UploaderExtension.php | 5 +-- src/Uploader/Chunk/ChunkManager.php | 7 +--- src/Uploader/Chunk/ChunkManagerInterface.php | 13 ++----- .../Chunk/Storage/ChunkStorageInterface.php | 16 ++------ .../Chunk/Storage/FlysystemStorage.php | 4 +- .../Chunk/Storage/GaufretteStorage.php | 4 +- src/Uploader/File/FileInterface.php | 25 +++--------- src/Uploader/File/FilesystemFile.php | 5 +-- src/Uploader/File/FlysystemFile.php | 4 +- src/Uploader/File/GaufretteFile.php | 2 +- src/Uploader/Response/AbstractResponse.php | 6 +-- src/Uploader/Response/MooUploadResponse.php | 38 ++++--------------- 16 files changed, 40 insertions(+), 121 deletions(-) diff --git a/src/Controller/AbstractChunkedController.php b/src/Controller/AbstractChunkedController.php index 959d72a..c169915 100644 --- a/src/Controller/AbstractChunkedController.php +++ b/src/Controller/AbstractChunkedController.php @@ -73,9 +73,8 @@ protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $re $path = $assembled->getPath(); if ($assembled instanceof File) { $this->handleUpload($assembled, $response, $request); - } else { - // @todo $assembled is of type mixed, so would error without check } + // @todo $assembled is of type mixed, so would error without check $chunkManager->cleanup($path); } diff --git a/src/DependencyInjection/OneupUploaderExtension.php b/src/DependencyInjection/OneupUploaderExtension.php index 3124544..af23747 100644 --- a/src/DependencyInjection/OneupUploaderExtension.php +++ b/src/DependencyInjection/OneupUploaderExtension.php @@ -285,10 +285,7 @@ protected function registerFilesystem(string $type, string $key, string $class, ->addArgument($prefix); } - /** - * @param mixed $input - */ - protected function getMaxUploadSize($input): int + protected function getMaxUploadSize(mixed $input): int { $input = $this->getValueInBytes($input); $maxPost = $this->getValueInBytes(\ini_get('upload_max_filesize')); @@ -301,13 +298,10 @@ protected function getMaxUploadSize($input): int return min(min($input, $maxPost), $maxFile); } - /** - * @param mixed $input - */ - protected function getValueInBytes($input): int + protected function getValueInBytes(mixed $input): int { // see: http://www.php.net/manual/en/function.ini-get.php - if (!is_scalar($input)) { + if (!\is_scalar($input)) { return -1; } diff --git a/src/Routing/RouteLoader.php b/src/Routing/RouteLoader.php index 4708b5d..39279c0 100644 --- a/src/Routing/RouteLoader.php +++ b/src/Routing/RouteLoader.php @@ -15,20 +15,12 @@ public function __construct(protected array $controllers) parent::__construct(); } - /** - * @param mixed $resource - * @param string|null $type - */ - public function supports($resource, $type = null): bool + public function supports(mixed $resource, string $type = null): bool { return 'uploader' === $type; } - /** - * @param mixed $resource - * @param string|null $type - */ - public function load($resource, $type = null): RouteCollection + public function load(mixed $resource, string $type = null): RouteCollection { $routes = new RouteCollection(); $separator = '::'; diff --git a/src/Templating/Helper/UploaderHelper.php b/src/Templating/Helper/UploaderHelper.php index 0bedf4a..70e89ae 100644 --- a/src/Templating/Helper/UploaderHelper.php +++ b/src/Templating/Helper/UploaderHelper.php @@ -38,10 +38,7 @@ public function uploadKey(): string return (string) \ini_get('session.upload_progress.name'); } - /** - * @return int - */ - public function maxSize(string $key) + public function maxSize(string $key): int { if (!\array_key_exists($key, $this->maxsize)) { throw new \InvalidArgumentException('No such mapping found to get maxsize for.'); diff --git a/src/Twig/Extension/UploaderExtension.php b/src/Twig/Extension/UploaderExtension.php index 5324321..ec62cc1 100644 --- a/src/Twig/Extension/UploaderExtension.php +++ b/src/Twig/Extension/UploaderExtension.php @@ -50,10 +50,7 @@ public function uploadKey(): string return $this->helper->uploadKey(); } - /** - * @return int - */ - public function maxSize(string $key) + public function maxSize(string $key): int { return $this->helper->maxSize($key); } diff --git a/src/Uploader/Chunk/ChunkManager.php b/src/Uploader/Chunk/ChunkManager.php index 340cbbf..989f8a0 100644 --- a/src/Uploader/Chunk/ChunkManager.php +++ b/src/Uploader/Chunk/ChunkManager.php @@ -23,12 +23,7 @@ public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $ return $this->storage->addChunk($uuid, $index, $chunk, $original); } - /** - * @param mixed $chunks - * @param bool $removeChunk - * @param bool $renameChunk - */ - public function assembleChunks($chunks, $removeChunk = true, $renameChunk = false): mixed + public function assembleChunks(mixed $chunks, bool $removeChunk = true, bool $renameChunk = false): mixed { return $this->storage->assembleChunks($chunks, $removeChunk, $renameChunk); } diff --git a/src/Uploader/Chunk/ChunkManagerInterface.php b/src/Uploader/Chunk/ChunkManagerInterface.php index fe3205c..f4aac04 100644 --- a/src/Uploader/Chunk/ChunkManagerInterface.php +++ b/src/Uploader/Chunk/ChunkManagerInterface.php @@ -10,28 +10,21 @@ interface ChunkManagerInterface { /** * Adds a new Chunk to a given uuid. - * - * @return mixed */ - public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original); + public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): mixed; /** * Assembles the given chunks and return the resulting file. * - * @param mixed $chunks * @param bool $removeChunk remove the chunk file once its assembled * @param bool $renameChunk rename the chunk file once its assembled - * - * @return mixed */ - public function assembleChunks($chunks, $removeChunk = true, $renameChunk = false); + public function assembleChunks(mixed $chunks, bool $removeChunk = true, bool $renameChunk = false): mixed; /** * Get chunks associated with the given uuid. - * - * @return mixed */ - public function getChunks(string $uuid); + public function getChunks(string $uuid): mixed; /** * Clean a given path. diff --git a/src/Uploader/Chunk/Storage/ChunkStorageInterface.php b/src/Uploader/Chunk/Storage/ChunkStorageInterface.php index e1b419f..11f8225 100644 --- a/src/Uploader/Chunk/Storage/ChunkStorageInterface.php +++ b/src/Uploader/Chunk/Storage/ChunkStorageInterface.php @@ -10,21 +10,11 @@ interface ChunkStorageInterface { public function clear(int $maxAge): void; - /** - * @return mixed - */ - public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original); + public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): mixed; - /** - * @param mixed $chunks - * @return mixed - */ - public function assembleChunks($chunks, bool $removeChunk, bool $renameChunk); + public function assembleChunks(mixed $chunks, bool $removeChunk, bool $renameChunk): mixed; public function cleanup(string $path): void; - /** - * @return mixed - */ - public function getChunks(string $uuid); + public function getChunks(string $uuid): mixed; } diff --git a/src/Uploader/Chunk/Storage/FlysystemStorage.php b/src/Uploader/Chunk/Storage/FlysystemStorage.php index b30793f..f28bd0c 100644 --- a/src/Uploader/Chunk/Storage/FlysystemStorage.php +++ b/src/Uploader/Chunk/Storage/FlysystemStorage.php @@ -19,7 +19,7 @@ public function __construct(protected Filesystem $filesystem, public int $buffer { } - public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): void + public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): mixed { // Prevent path traversal attacks $uuid = basename($uuid); @@ -30,6 +30,8 @@ public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $ 'chunk' => $chunk, 'original' => $original, ]; + + return null; } /** diff --git a/src/Uploader/Chunk/Storage/GaufretteStorage.php b/src/Uploader/Chunk/Storage/GaufretteStorage.php index e622cd2..345f028 100644 --- a/src/Uploader/Chunk/Storage/GaufretteStorage.php +++ b/src/Uploader/Chunk/Storage/GaufretteStorage.php @@ -93,7 +93,7 @@ public function clear(int $maxAge, string $prefix = null): void * for gaufrette based chunk storage therefore assembleChunks will * be called in the same request. */ - public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): void + public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original): mixed { // Prevent path traversal attacks $uuid = basename($uuid); @@ -104,6 +104,8 @@ public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $ 'chunk' => $chunk, 'original' => $original, ]; + + return null; } /** diff --git a/src/Uploader/File/FileInterface.php b/src/Uploader/File/FileInterface.php index a58ea10..46dd194 100644 --- a/src/Uploader/File/FileInterface.php +++ b/src/Uploader/File/FileInterface.php @@ -15,46 +15,33 @@ interface FileInterface { /** * Returns the size of the file. - * - * @return int */ - public function getSize(); + public function getSize(): int|false; /** * Returns the path of the file. - * - * @return string */ - public function getPathname(); + public function getPathname(): string; /** * Return the path of the file without the filename. - * - * @return string|null */ - public function getPath(); + public function getPath(): ?string; /** * Returns the guessed mime type of the file. - * - * @return string */ - public function getMimeType(); + public function getMimeType(): ?string; /** * Returns the basename of the file. - * - * @return string */ - public function getBasename(); + public function getBasename(): string; /** * Returns the guessed extension of the file. */ public function getExtension(): string; - /** - * @return mixed - */ - public function getFileSystem(); + public function getFileSystem(): mixed; } diff --git a/src/Uploader/File/FilesystemFile.php b/src/Uploader/File/FilesystemFile.php index b0094f7..e63f00c 100644 --- a/src/Uploader/File/FilesystemFile.php +++ b/src/Uploader/File/FilesystemFile.php @@ -23,10 +23,7 @@ public function getExtension(): string return $this->getClientOriginalExtension(); } - /** - * @return null - */ - public function getFileSystem() + public function getFileSystem(): mixed { return null; } diff --git a/src/Uploader/File/FlysystemFile.php b/src/Uploader/File/FlysystemFile.php index 6e51a1c..e131daa 100644 --- a/src/Uploader/File/FlysystemFile.php +++ b/src/Uploader/File/FlysystemFile.php @@ -16,7 +16,7 @@ public function __construct(private string $pathname, private FilesystemOperator /** * @throws FilesystemException */ - public function getSize(): int + public function getSize(): int|false { return $this->filesystem->fileSize($this->pathname); } @@ -34,7 +34,7 @@ public function getPath(): string /** * @throws FilesystemException */ - public function getMimeType(): string + public function getMimeType(): ?string { return $this->filesystem->mimeType($this->pathname); } diff --git a/src/Uploader/File/GaufretteFile.php b/src/Uploader/File/GaufretteFile.php index b8eda29..0019a1a 100644 --- a/src/Uploader/File/GaufretteFile.php +++ b/src/Uploader/File/GaufretteFile.php @@ -31,7 +31,7 @@ public function __construct(File $file, FilesystemInterface $filesystem, protect * and will have heavy performance footprint. * !! ------- !! */ - public function getSize(): int + public function getSize(): int|false { // This can only work on streamable files, so basically local files, // still only perform it once even on local files to avoid bothering the filesystem.php g diff --git a/src/Uploader/Response/AbstractResponse.php b/src/Uploader/Response/AbstractResponse.php index 2e82eba..38e6628 100644 --- a/src/Uploader/Response/AbstractResponse.php +++ b/src/Uploader/Response/AbstractResponse.php @@ -35,14 +35,12 @@ public function offsetGet($offset) } /** - * The \ArrayAccess interface does not support multi-dimensional array syntax such as $array["foo"][] = bar + * The \ArrayAccess interface does not support multidimensional array syntax such as $array["foo"][] = bar * This function will take a path of arrays and add a new element to it, creating the path if needed. * - * @param array $value - * * @throws \InvalidArgumentException if the path contains non-array items */ - public function addToOffset($value, array $offsets): void + public function addToOffset(array $value, array $offsets): void { $element = &$this->data; foreach ($offsets as $offset) { diff --git a/src/Uploader/Response/MooUploadResponse.php b/src/Uploader/Response/MooUploadResponse.php index a1af863..3a5cad9 100644 --- a/src/Uploader/Response/MooUploadResponse.php +++ b/src/Uploader/Response/MooUploadResponse.php @@ -6,25 +6,10 @@ class MooUploadResponse extends AbstractResponse { - /** - * @var int|string|null - */ - protected $id; - - /** - * @var string|null - */ - protected $name; - - /** - * @var int - */ - protected $size; - - /** - * @var string - */ - protected $uploadedName; + protected int|string|null $id; + protected ?string $name; + protected int $size; + protected string $uploadedName; public function __construct(protected bool $finish = true, protected int $error = 0) { @@ -45,20 +30,14 @@ public function assemble(): array return $data; } - /** - * @param int|string|null $id - */ - public function setId($id): self + public function setId(int|string|null $id): self { $this->id = $id; return $this; } - /** - * @return int|string|null - */ - public function getId() + public function getId(): int|string|null { return $this->id; } @@ -75,10 +54,7 @@ public function getName(): ?string return $this->name; } - /** - * @param mixed $size - */ - public function setSize($size): self + public function setSize(mixed $size): self { $this->size = (int) $size;