Skip to content

Commit

Permalink
Fixes phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
evertharmeling committed Nov 28, 2023
1 parent de49885 commit ab844e1
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 17 deletions.
17 changes: 16 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ parameters:
- '#Cannot call method getRealPath\(\) on mixed\.#'
- '#Cannot call method getBasename\(\) on mixed\.#'
- '#Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required\.#'
-
message: '#Cannot use array destructuring on array<int, string>\|false\.#'
path: '%currentWorkingDirectory%/src/Controller/BlueimpController.php'
-
message: '#Binary operation "/" between string and int results in an error\.#'
path: '%currentWorkingDirectory%/src/Controller/BlueimpController.php'
-
message: '#Dead catch - Gaufrette\\Exception\\FileNotFound is never thrown in the try block\.#'
path: '%currentWorkingDirectory%/src/Uploader/Chunk/Storage/GaufretteStorage.php'
-
message: '#Parameter \#1 \$finfo of function finfo_file|finfo_close expects finfo, resource given\.#'
path: '%currentWorkingDirectory%/src/Uploader/File/GaufretteFile.php'
-
message: '#Parameter \#2 ...\$values of function sprintf expects bool\|float\|int\|string\|null, mixed given\.#'
path: '%currentWorkingDirectory%/src/Controller/PluploadController.php'
-
message: '#Parameter \#1 \$parameters of class Symfony\\Component\\HttpFoundation\\FileBag constructor expects array<Symfony\\Component\\HttpFoundation\\File\\UploadedFile>, array<int, Symfony\\Component\\HttpFoundation\\File\\UploadedFile\|null> given\.#'
path: '%currentWorkingDirectory%/tests/Controller/FileBagExtractorTest.php'
path: '%currentWorkingDirectory%/tests/Controller/FileBagExtractorTest.php'
7 changes: 6 additions & 1 deletion src/Controller/AbstractChunkedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Oneup\UploaderBundle\Event\PostChunkUploadEvent;
use Oneup\UploaderBundle\Uploader\Chunk\ChunkManagerInterface;
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -70,7 +71,11 @@ protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $re
// with reassembling the parts
if ($last) {
$path = $assembled->getPath();
$this->handleUpload($assembled, $response, $request);
if ($assembled instanceof File) {
$this->handleUpload($assembled, $response, $request);
} else {
// @todo $assembled is of type mixed, so would error without check
}

$chunkManager->cleanup($path);
}
Expand Down
1 change: 1 addition & 0 deletions src/Controller/BlueimpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function progress(): JsonResponse

// ref: https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-Session-Upload-Progress
$key = sprintf('%s.%s', $prefix, $value);
/** @var array<string, mixed> $value */
$value = $session->get($key);

$progress = [
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/DropzoneController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ protected function parseChunkedRequest(Request $request): array
$uuid = $request->get('dzuuid');

/**
* @var UploadedFile
* @var UploadedFile $file
*/
$file = $request->files->get('file')->getClientOriginalName();
$orig = $file;
$file = $request->files->get('file');
$fileName = $file->getClientOriginalName();
$orig = $fileName;

return [$last, $uuid, $index, $orig];
}
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/MooUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ protected function parseChunkedRequest(Request $request): array

try {
// loop through every file that has been uploaded before
foreach ($chunkManager->getChunks((string) $uuid) as $file) {
/** @var iterable $chunks */
$chunks = $chunkManager->getChunks((string) $uuid);
foreach ($chunks as $file) {
$size += $file->getSize();
}
} catch (\InvalidArgumentException $e) {
Expand Down
18 changes: 14 additions & 4 deletions src/DependencyInjection/OneupUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ protected function createController(string $key, array $config): string
} else {
$customFrontend = $config['custom_frontend'];

$controllerName = sprintf('oneup_uploader.controller.%s', $customFrontend['name']);
$controllerType = $customFrontend['class'];

if (empty($controllerName) || empty($controllerType)) {
if (empty($customFrontend['name']) || empty($customFrontend['class'])) {
throw new ServiceNotFoundException('Empty controller class or name. If you really want to use a custom frontend implementation, be sure to provide a class and a name.');
}

$controllerName = sprintf('oneup_uploader.controller.%s', $customFrontend['name']);
$controllerType = $customFrontend['class'];
}

$errorHandler = $this->createErrorHandler($config);
Expand Down Expand Up @@ -285,6 +285,9 @@ protected function registerFilesystem(string $type, string $key, string $class,
->addArgument($prefix);
}

/**
* @param mixed $input
*/
protected function getMaxUploadSize($input): int
{
$input = $this->getValueInBytes($input);
Expand All @@ -298,9 +301,16 @@ protected function getMaxUploadSize($input): int
return min(min($input, $maxPost), $maxFile);
}

/**
* @param mixed $input
*/
protected function getValueInBytes($input): int
{
// see: http://www.php.net/manual/en/function.ini-get.php
if (!is_scalar($input)) {
return -1;
}

$input = trim((string) $input);
$last = strtolower($input[\strlen($input) - 1]);
$numericInput = (float) substr($input, 0, -1);
Expand Down
2 changes: 2 additions & 0 deletions src/Routing/RouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(protected array $controllers)
}

/**
* @param mixed $resource
* @param string|null $type
*/
public function supports($resource, $type = null): bool
Expand All @@ -24,6 +25,7 @@ public function supports($resource, $type = null): bool
}

/**
* @param mixed $resource
* @param string|null $type
*/
public function load($resource, $type = null): RouteCollection
Expand Down
3 changes: 3 additions & 0 deletions src/Templating/Helper/UploaderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function uploadKey(): string
return (string) \ini_get('session.upload_progress.name');
}

/**
* @return int
*/
public function maxSize(string $key)
{
if (!\array_key_exists($key, $this->maxsize)) {
Expand Down
3 changes: 3 additions & 0 deletions src/Twig/Extension/UploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function uploadKey(): string
return $this->helper->uploadKey();
}

/**
* @return int
*/
public function maxSize(string $key)
{
return $this->helper->maxSize($key);
Expand Down
5 changes: 5 additions & 0 deletions src/Uploader/Chunk/ChunkManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ 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
{
return $this->storage->assembleChunks($chunks, $removeChunk, $renameChunk);
Expand Down
8 changes: 7 additions & 1 deletion src/Uploader/Chunk/ChunkManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@

namespace Oneup\UploaderBundle\Uploader\Chunk;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

interface ChunkManagerInterface
{
/**
* Adds a new Chunk to a given uuid.
*
* @return mixed
*/
public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original);

/**
* 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);

/**
* Get chunks associated with the given uuid.
*
* @return mixed
*/
public function getChunks(string $uuid);

Expand Down
10 changes: 10 additions & 0 deletions src/Uploader/Chunk/Storage/ChunkStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ interface ChunkStorageInterface
{
public function clear(int $maxAge): void;

/**
* @return mixed
*/
public function addChunk(string $uuid, int $index, UploadedFile $chunk, string $original);

/**
* @param mixed $chunks
* @return mixed
*/
public function assembleChunks($chunks, bool $removeChunk, bool $renameChunk);

public function cleanup(string $path): void;

/**
* @return mixed
*/
public function getChunks(string $uuid);
}
5 changes: 3 additions & 2 deletions src/Uploader/File/FileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Oneup\UploaderBundle\Uploader\File;

use Symfony\Component\HttpFoundation\File\File;

/**
* Every function in this interface should be considered unsafe.
* They are only meant to abstract away some basic file functionality.
Expand Down Expand Up @@ -55,5 +53,8 @@ public function getBasename();
*/
public function getExtension(): string;

/**
* @return mixed
*/
public function getFileSystem();
}
3 changes: 3 additions & 0 deletions src/Uploader/File/FilesystemFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function getExtension(): string
return $this->getClientOriginalExtension();
}

/**
* @return null
*/
public function getFileSystem()
{
return null;
Expand Down
2 changes: 2 additions & 0 deletions src/Uploader/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function offsetGet($offset)
* The \ArrayAccess interface does not support multi-dimensional 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
Expand Down
11 changes: 10 additions & 1 deletion src/Uploader/Response/MooUploadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class MooUploadResponse extends AbstractResponse
{
/**
* @var int|string
* @var int|string|null
*/
protected $id;

Expand Down Expand Up @@ -45,13 +45,19 @@ public function assemble(): array
return $data;
}

/**
* @param int|string|null $id
*/
public function setId($id): self
{
$this->id = $id;

return $this;
}

/**
* @return int|string|null
*/
public function getId()
{
return $this->id;
Expand All @@ -69,6 +75,9 @@ public function getName(): ?string
return $this->name;
}

/**
* @param mixed $size
*/
public function setSize($size): self
{
$this->size = (int) $size;
Expand Down
4 changes: 3 additions & 1 deletion src/Uploader/Storage/FlysystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public function upload($file, string $name, string $path = null)
}

if ($file instanceof FileInterface) {
/** @var FilesystemOperator $filesystem */
$filesystem = $file->getFilesystem();
$manager = new MountManager([
'chunks' => $file->getFilesystem(),
'chunks' => $filesystem,
'dest' => $this->filesystem,
]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/AbstractUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ public function testEvents(): void

abstract protected function getRequestParameters(): array;

abstract protected function getRequestFile();
abstract protected function getRequestFile(): mixed;
}
2 changes: 1 addition & 1 deletion tests/Controller/FileBagExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testWithMultipleFiles(): void
$this->assertCount(3, $result);
}

protected function invoke(FileBag $bag)
protected function invoke(FileBag $bag): mixed
{
return $this->method->invoke($this->mock, $bag);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Uploader/Chunk/Storage/FlysystemStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function testGetChunks(): void
$system->dumpFile($dir . '/chunk3', 'test');
$timeTo = time();

/** @var array $files */
$files = $this->storage->getChunks($uuid);
$this->assertCount(3, $files);
$file = $files[0];
Expand Down

0 comments on commit ab844e1

Please sign in to comment.