Skip to content

Commit

Permalink
Refactored phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evertharmeling committed Nov 29, 2023
1 parent ab844e1 commit 5eb1752
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 121 deletions.
3 changes: 1 addition & 2 deletions src/Controller/AbstractChunkedController.php
Expand Up @@ -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);
}
Expand Down
12 changes: 3 additions & 9 deletions src/DependencyInjection/OneupUploaderExtension.php
Expand Up @@ -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'));
Expand All @@ -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;
}

Expand Down
12 changes: 2 additions & 10 deletions src/Routing/RouteLoader.php
Expand Up @@ -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 = '::';
Expand Down
5 changes: 1 addition & 4 deletions src/Templating/Helper/UploaderHelper.php
Expand Up @@ -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.');
Expand Down
5 changes: 1 addition & 4 deletions src/Twig/Extension/UploaderExtension.php
Expand Up @@ -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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Uploader/Chunk/ChunkManager.php
Expand Up @@ -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);
}
Expand Down
13 changes: 3 additions & 10 deletions src/Uploader/Chunk/ChunkManagerInterface.php
Expand Up @@ -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.
Expand Down
16 changes: 3 additions & 13 deletions src/Uploader/Chunk/Storage/ChunkStorageInterface.php
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion src/Uploader/Chunk/Storage/FlysystemStorage.php
Expand Up @@ -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);
Expand All @@ -30,6 +30,8 @@ public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $
'chunk' => $chunk,
'original' => $original,
];

return null;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Uploader/Chunk/Storage/GaufretteStorage.php
Expand Up @@ -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);
Expand All @@ -104,6 +104,8 @@ public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $
'chunk' => $chunk,
'original' => $original,
];

return null;
}

/**
Expand Down
25 changes: 6 additions & 19 deletions src/Uploader/File/FileInterface.php
Expand Up @@ -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;
}
5 changes: 1 addition & 4 deletions src/Uploader/File/FilesystemFile.php
Expand Up @@ -23,10 +23,7 @@ public function getExtension(): string
return $this->getClientOriginalExtension();
}

/**
* @return null
*/
public function getFileSystem()
public function getFileSystem(): mixed
{
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Uploader/File/FlysystemFile.php
Expand Up @@ -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);
}
Expand All @@ -34,7 +34,7 @@ public function getPath(): string
/**
* @throws FilesystemException
*/
public function getMimeType(): string
public function getMimeType(): ?string
{
return $this->filesystem->mimeType($this->pathname);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uploader/File/GaufretteFile.php
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Uploader/Response/AbstractResponse.php
Expand Up @@ -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) {
Expand Down
38 changes: 7 additions & 31 deletions src/Uploader/Response/MooUploadResponse.php
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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;

Expand Down

0 comments on commit 5eb1752

Please sign in to comment.