From 0cd8bd833a83a1ff5c0ebad9a95c375d80f9c3bf Mon Sep 17 00:00:00 2001 From: Abhishek Das Date: Thu, 13 Oct 2022 14:02:37 +0530 Subject: [PATCH 1/3] Add code quality analysis Add code quality analysis with PHPStan and GitHub workflow to be triggered on pull request. --- .github/workflows/codeql-analysis.yml | 20 ++++++++ composer.json | 6 ++- composer.lock | 71 +++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 00000000..f02bcd69 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,20 @@ +name: "CodeQL" + +on: [pull_request] +jobs: + lint: + name: CodeQL + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - run: git checkout HEAD^2 + + - name: Run CodeQL + run: | + docker run --rm -v $PWD:/app composer sh -c \ + "composer install --profile --ignore-platform-reqs && composer check" diff --git a/composer.json b/composer.json index 5635eb96..be2eac47 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,9 @@ "email": "eldad@appwrite.io" } ], + "scripts": { + "check": "./vendor/bin/phpstan analyse --level 0 src tests" + }, "autoload": { "psr-4": {"Utopia\\Storage\\":"src/Storage"} }, @@ -25,7 +28,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.3", - "vimeo/psalm": "4.0.1" + "vimeo/psalm": "4.0.1", + "phpstan/phpstan": "1.9.x-dev" }, "minimum-stability": "dev" } diff --git a/composer.lock b/composer.lock index a3a5d512..190ff2b5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1df212eaa94b7c555a893db0225a179b", + "content-hash": "88babe0da4e927f89e9a71e5cb5e124a", "packages": [ { "name": "utopia-php/framework", @@ -1223,6 +1223,66 @@ }, "time": "2021-12-08T12:19:24+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.9.x-dev", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "9aa375b67ba9dc7c39272be85e0b781c86d1be1c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9aa375b67ba9dc7c39272be85e0b781c86d1be1c", + "reference": "9aa375b67ba9dc7c39272be85e0b781c86d1be1c", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "default-branch": true, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/1.9.x" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2022-10-12T20:57:15+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.x-dev", @@ -3853,12 +3913,17 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "phpstan/phpstan": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { + "ext-fileinfo": "*", + "ext-zlib": "*", + "ext-zstd": "*", "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } From b512d94251718fb4bdfc83fa903a63d630529333 Mon Sep 17 00:00:00 2001 From: Abhishek Das Date: Thu, 13 Oct 2022 23:10:37 +0530 Subject: [PATCH 2/3] Increased analysis level to 5. Fixed code quality issues up to level 5. --- composer.json | 2 +- src/Storage/Device/Local.php | 4 +- src/Storage/Device/S3.php | 23 +++++---- .../Compression/Algorithms/GZIPTest.php | 8 ++-- .../Compression/Algorithms/ZstdTest.php | 8 ++-- tests/Storage/Device/LocalTest.php | 45 +++++++++--------- tests/Storage/S3Base.php | 47 ++++++++++--------- tests/Storage/StorageTest.php | 6 +-- tests/Storage/Validator/FileExtTest.php | 2 +- tests/Storage/Validator/FileNameTest.php | 2 +- tests/Storage/Validator/FileSizeTest.php | 2 +- tests/Storage/Validator/FileTypeTest.php | 2 +- tests/Storage/Validator/UploadTest.php | 2 +- 13 files changed, 82 insertions(+), 71 deletions(-) diff --git a/composer.json b/composer.json index be2eac47..a7a8550f 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "scripts": { - "check": "./vendor/bin/phpstan analyse --level 0 src tests" + "check": "./vendor/bin/phpstan analyse --level 5 src tests" }, "autoload": { "psr-4": {"Utopia\\Storage\\":"src/Storage"} diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index f8db1f95..67ae75e7 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -161,8 +161,8 @@ public function abort(string $path, string $extra = ''): bool * Read file by given path. * * @param string $path - * @param int offset - * @param int length + * @param int $offset + * @param int $length * * @return string */ diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 13080282..bcb52682 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -161,8 +161,8 @@ public function getPath(string $filename, string $prefix = null): string * * @param string $source * @param string $path - * @param int chunk - * @param int chunks + * @param int $chunk + * @param int $chunks * @param array $metadata * * @throws \Exception @@ -172,7 +172,7 @@ public function getPath(string $filename, string $prefix = null): string public function upload(string $source, string $path, int $chunk = 1, int $chunks = 1, array &$metadata = []): int { if($chunk == 1 && $chunks == 1) { - return $this->write($path, \file_get_contents($source), \mime_content_type($source)); + return intval($this->write($path, \file_get_contents($source), \mime_content_type($source))); } $uploadId = $metadata['uploadId'] ?? null; if(empty($uploadId)) { @@ -295,8 +295,8 @@ public function abort(string $path, string $extra = ''): bool * Read file or part of file by given path, offset and length. * * @param string $path - * @param int offset - * @param int length + * @param int $offset + * @param int $length * * @throws \Exception * @@ -391,7 +391,9 @@ public function delete(string $path, bool $recursive = false): bool /** * Get list of objects in the given path. * - * @param string $path + * @param string $prefix + * @param int $maxKeys + * @param string $continuationToken * * @throws \Exception * @@ -577,7 +579,7 @@ private function getInfo(string $path): array * Generate the headers for AWS Signature V4 * @param string $method * @param string $uri - * @param array parameters + * @param array $parameters * * @return string */ @@ -701,7 +703,7 @@ private function call(string $method, string $uri, string $data = '', array $par $response->body .= $data; return \strlen($data); }); - curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl, string $header) use (&$response) { + \curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl, string $header) use (&$response) { $len = strlen($header); $header = explode(':', $header, 2); @@ -742,7 +744,10 @@ private function call(string $method, string $uri, string $data = '', array $par \curl_close($curl); // Parse body into XML - if ((isset($response->headers['content-type']) && $response->headers['content-type'] == 'application/xml') || (str_starts_with($response->body, 'headers['content-type'] ?? '') !== 'image/svg+xml')) { + if (array_key_exists('content-type', $response->headers) + && ($response->headers['content-type'] == 'application/xml' + || (str_starts_with($response->body, 'headers['content-type'] == 'image/svg+xml'))) { + $response->body = \simplexml_load_string($response->body); $response->body = json_decode(json_encode($response->body), true); } diff --git a/tests/Storage/Compression/Algorithms/GZIPTest.php b/tests/Storage/Compression/Algorithms/GZIPTest.php index 117ada57..551b5cf2 100644 --- a/tests/Storage/Compression/Algorithms/GZIPTest.php +++ b/tests/Storage/Compression/Algorithms/GZIPTest.php @@ -21,12 +21,12 @@ public function tearDown(): void { } - public function testName() + public function testName(): void { $this->assertEquals($this->object->getName(), 'gzip'); } - public function testCompressDecompressWithText() + public function testCompressDecompressWithText(): void { $demo = 'This is a demo string'; $demoSize = mb_strlen($demo, '8bit'); @@ -40,7 +40,7 @@ public function testCompressDecompressWithText() $this->assertEquals($this->object->decompress($data), $demo); } - public function testCompressDecompressWithJPGImage() + public function testCompressDecompressWithJPGImage(): void { $demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'); $demoSize = mb_strlen($demo, '8bit'); @@ -59,7 +59,7 @@ public function testCompressDecompressWithJPGImage() $this->assertEquals($dataSize, 599639); } - public function testCompressDecompressWithPNGImage() + public function testCompressDecompressWithPNGImage(): void { $demo = \file_get_contents(__DIR__ . '/../../../resources/disk-b/kitten-1.png'); $demoSize = mb_strlen($demo, '8bit'); diff --git a/tests/Storage/Compression/Algorithms/ZstdTest.php b/tests/Storage/Compression/Algorithms/ZstdTest.php index b1bec685..f04ac604 100644 --- a/tests/Storage/Compression/Algorithms/ZstdTest.php +++ b/tests/Storage/Compression/Algorithms/ZstdTest.php @@ -18,12 +18,12 @@ public function tearDown(): void { } - public function testName() + public function testName(): void { $this->assertEquals($this->object->getName(), 'zstd'); } - public function testCompressDecompressWithText() + public function testCompressDecompressWithText(): void { $demo = 'This is a demo string'; $demoSize = \mb_strlen($demo, '8bit'); @@ -37,7 +37,7 @@ public function testCompressDecompressWithText() $this->assertEquals($demo, $this->object->decompress($data)); } - public function testCompressDecompressWithJPGImage() + public function testCompressDecompressWithJPGImage(): void { $demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'); $demoSize = \mb_strlen($demo, '8bit'); @@ -56,7 +56,7 @@ public function testCompressDecompressWithJPGImage() $this->assertEquals(599639, $dataSize); } - public function testCompressDecompressWithPNGImage() + public function testCompressDecompressWithPNGImage(): void { $demo = \file_get_contents(__DIR__ . '/../../../resources/disk-b/kitten-1.png'); $demoSize = \mb_strlen($demo, '8bit'); diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index 42cae407..d1809601 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -21,27 +21,27 @@ public function tearDown(): void { } - public function testName() + public function testName(): void { $this->assertEquals($this->object->getName(), 'Local Storage'); } - public function testDescription() + public function testDescription(): void { $this->assertEquals($this->object->getDescription(), 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.'); } - public function testRoot() + public function testRoot(): void { $this->assertEquals($this->object->getRoot(),realpath( __DIR__ . '/../../resources/disk-a')); } - public function testPath() + public function testPath(): void { $this->assertEquals($this->object->getPath('image.png'), realpath(__DIR__ . '/../../resources/disk-a').'/image.png'); } - public function testWrite() + public function testWrite(): void { $this->assertEquals($this->object->write($this->object->getPath('text.txt'), 'Hello World'), true); $this->assertEquals(file_exists($this->object->getPath('text.txt')), true); @@ -50,7 +50,7 @@ public function testWrite() $this->object->delete($this->object->getPath('text.txt')); } - public function testRead() + public function testRead(): void { $this->assertEquals($this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World'), true); $this->assertEquals($this->object->read($this->object->getPath('text-for-read.txt')), 'Hello World'); @@ -58,7 +58,7 @@ public function testRead() $this->object->delete($this->object->getPath('text-for-read.txt')); } - public function testFileExists() + public function testFileExists(): void { $this->assertEquals($this->object->write($this->object->getPath('text-for-test-exists.txt'), 'Hello World'), true); $this->assertEquals($this->object->exists($this->object->getPath('text-for-test-exists.txt')), true); @@ -67,7 +67,7 @@ public function testFileExists() $this->object->delete($this->object->getPath('text-for-test-exists.txt')); } - public function testMove() + public function testMove(): void { $this->assertEquals($this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World'), true); $this->assertEquals($this->object->read($this->object->getPath('text-for-move.txt')), 'Hello World'); @@ -81,7 +81,7 @@ public function testMove() $this->object->delete($this->object->getPath('text-for-move-new.txt')); } - public function testDelete() + public function testDelete(): void { $this->assertEquals($this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World'), true); $this->assertEquals($this->object->read($this->object->getPath('text-for-delete.txt')), 'Hello World'); @@ -90,13 +90,13 @@ public function testDelete() $this->assertEquals(is_readable($this->object->getPath('text-for-delete.txt')), false); } - public function testFileSize() + public function testFileSize(): void { $this->assertEquals($this->object->getFileSize(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'), 599639); $this->assertEquals($this->object->getFileSize(__DIR__ . '/../../resources/disk-a/kitten-2.jpg'), 131958); } - public function testFileMimeType() + public function testFileMimeType(): void { $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'), 'image/jpeg'); $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../resources/disk-a/kitten-2.jpg'), 'image/jpeg'); @@ -104,7 +104,7 @@ public function testFileMimeType() $this->assertEquals($this->object->getFileMimeType(__DIR__ . '/../../resources/disk-b/kitten-2.png'), 'image/png'); } - public function testFileHash() + public function testFileHash(): void { $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'), '7551f343143d2e24ab4aaf4624996b6a'); $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../resources/disk-a/kitten-2.jpg'), '81702fdeef2e55b1a22617bce4951cb5'); @@ -112,19 +112,20 @@ public function testFileHash() $this->assertEquals($this->object->getFileHash(__DIR__ . '/../../resources/disk-b/kitten-2.png'), '8a9ed992b77e4b62b10e3a5c8ed72062'); } - public function testDirectorySize() + public function testDirectorySize(): void { $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../resources/disk-a/')); $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../resources/disk-b/')); } - public function testPartUpload() { + public function testPartUpload(): string + { $source = __DIR__ . '/../../resources/disk-a/large_file.mp4'; $dest = $this->object->getPath('uploaded.mp4'); $totalSize = $this->object->getFileSize($source); $chunkSize = 2097152; - $chunks = ceil($totalSize / $chunkSize); + $chunks = intval(ceil($totalSize / $chunkSize)); $chunk = 1; $start = 0; @@ -147,12 +148,13 @@ public function testPartUpload() { return $dest; } - public function testAbort() { + public function testAbort(): void + { $source = __DIR__ . '/../../resources/disk-a/large_file.mp4'; $dest = $this->object->getPath('abcduploaded.mp4'); $totalSize = $this->object->getFileSize($source); $chunkSize = 2097152; - $chunks = ceil($totalSize / $chunkSize); + $chunks = intval(ceil($totalSize / $chunkSize)); $chunk = 1; $start = 0; @@ -176,7 +178,7 @@ public function testAbort() { $dest1 = $this->object->getPath('abcduploaded2.mp4'); $totalSize = $this->object->getFileSize($source); $chunkSize = 2097152; - $chunks = ceil($totalSize / $chunkSize); + $chunks = intval(ceil($totalSize / $chunkSize)); $chunk = 1; $start = 0; @@ -202,7 +204,8 @@ public function testAbort() { /** * @depends testPartUpload */ - public function testPartRead($path) { + public function testPartRead(string $path): void + { $source = __DIR__ . '/../../resources/disk-a/large_file.mp4'; $chunk = file_get_contents($source, false,null, 0, 500); $readChunk = $this->object->read($path, 0, 500); @@ -210,12 +213,12 @@ public function testPartRead($path) { $this->object->delete($path); } - public function testPartitionFreeSpace() + public function testPartitionFreeSpace(): void { $this->assertGreaterThan(0, $this->object->getPartitionFreeSpace()); } - public function testPartitionTotalSpace() + public function testPartitionTotalSpace(): void { $this->assertGreaterThan(0, $this->object->getPartitionTotalSpace()); } diff --git a/tests/Storage/S3Base.php b/tests/Storage/S3Base.php index 30a27b2b..5bdab642 100644 --- a/tests/Storage/S3Base.php +++ b/tests/Storage/S3Base.php @@ -35,7 +35,7 @@ public function setUp(): void $this->uploadTestFiles(); } - private function uploadTestFiles() + private function uploadTestFiles(): void { $this->object->upload(__DIR__ . '/../resources/disk-a/kitten-1.jpg', $this->object->getPath('testing/kitten-1.jpg')); $this->object->upload(__DIR__ . '/../resources/disk-a/kitten-2.jpg', $this->object->getPath('testing/kitten-2.jpg')); @@ -43,7 +43,7 @@ private function uploadTestFiles() $this->object->upload(__DIR__ . '/../resources/disk-b/kitten-2.png', $this->object->getPath('testing/kitten-2.png')); } - private function removeTestFiles() + private function removeTestFiles(): void { $this->object->delete($this->object->getPath('testing/kitten-1.jpg')); $this->object->delete($this->object->getPath('testing/kitten-2.jpg')); @@ -56,34 +56,34 @@ public function tearDown(): void $this->removeTestFiles(); } - public function testName() + public function testName(): void { $this->assertEquals( $this->getAdapterName(), $this->object->getName()); } - public function testDescription() + public function testDescription(): void { $this->assertEquals($this->getAdapterDescription(), $this->object->getDescription()); } - public function testRoot() + public function testRoot(): void { $this->assertEquals($this->root, $this->object->getRoot()); } - public function testPath() + public function testPath(): void { $this->assertEquals($this->root . '/image.png', $this->object->getPath('image.png')); } - public function testWrite() + public function testWrite(): void { $this->assertEquals(true, $this->object->write($this->object->getPath('text.txt'), 'Hello World', 'text/plain')); $this->object->delete($this->object->getPath('text.txt')); } - public function testRead() + public function testRead(): void { $this->assertEquals(true, $this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World', 'text/plain')); $this->assertEquals('Hello World', $this->object->read($this->object->getPath('text-for-read.txt'))); @@ -91,13 +91,13 @@ public function testRead() $this->object->delete($this->object->getPath('text-for-read.txt')); } - public function testFileExists() + public function testFileExists(): void { $this->assertEquals(true, $this->object->exists($this->object->getPath('testing/kitten-1.jpg'))); $this->assertEquals(false, $this->object->exists($this->object->getPath('testing/kitten-5.jpg'))); } - public function testMove() + public function testMove(): void { $this->assertEquals(true, $this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World', 'text/plain')); $this->assertEquals(true, $this->object->exists($this->object->getPath('text-for-move.txt'))); @@ -108,21 +108,22 @@ public function testMove() $this->object->delete($this->object->getPath('text-for-move-new.txt')); } - public function testDelete() + public function testDelete(): void { $this->assertEquals(true, $this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World', 'text/plain')); $this->assertEquals(true, $this->object->exists($this->object->getPath('text-for-delete.txt'))); $this->assertEquals(true, $this->object->delete($this->object->getPath('text-for-delete.txt'))); } - public function testSVGUpload() { + public function testSVGUpload(): void + { $this->assertEquals(true, $this->object->upload(__DIR__ . '/../resources/disk-b/appwrite.svg', $this->object->getPath('testing/appwrite.svg'))); $this->assertEquals(file_get_contents(__DIR__ . '/../resources/disk-b/appwrite.svg'), $this->object->read($this->object->getPath('testing/appwrite.svg'))); $this->assertEquals(true, $this->object->exists($this->object->getPath('testing/appwrite.svg'))); $this->assertEquals(true, $this->object->delete($this->object->getPath('testing/appwrite.svg'))); } - public function testDeletePath() + public function testDeletePath(): void { // Test Single Object $path = $this->object->getPath('text-for-delete-path.txt'); @@ -150,13 +151,13 @@ public function testDeletePath() } - public function testFileSize() + public function testFileSize(): void { $this->assertEquals(599639, $this->object->getFileSize($this->object->getPath('testing/kitten-1.jpg'))); $this->assertEquals(131958, $this->object->getFileSize($this->object->getPath('testing/kitten-2.jpg'))); } - public function testFileMimeType() + public function testFileMimeType(): void { $this->assertEquals('image/jpeg', $this->object->getFileMimeType($this->object->getPath('testing/kitten-1.jpg'))); $this->assertEquals('image/jpeg', $this->object->getFileMimeType($this->object->getPath('testing/kitten-2.jpg'))); @@ -164,7 +165,7 @@ public function testFileMimeType() $this->assertEquals('image/png', $this->object->getFileMimeType($this->object->getPath('testing/kitten-2.png'))); } - public function testFileHash() + public function testFileHash(): void { $this->assertEquals('7551f343143d2e24ab4aaf4624996b6a', $this->object->getFileHash($this->object->getPath('testing/kitten-1.jpg'))); $this->assertEquals('81702fdeef2e55b1a22617bce4951cb5', $this->object->getFileHash($this->object->getPath('testing/kitten-2.jpg'))); @@ -172,29 +173,30 @@ public function testFileHash() $this->assertEquals('8a9ed992b77e4b62b10e3a5c8ed72062', $this->object->getFileHash($this->object->getPath('testing/kitten-2.png'))); } - public function testDirectorySize() + public function testDirectorySize(): void { $this->assertEquals(-1, $this->object->getDirectorySize('resources/disk-a/')); } - public function testPartitionFreeSpace() + public function testPartitionFreeSpace(): void { $this->assertEquals(-1, $this->object->getPartitionFreeSpace()); } - public function testPartitionTotalSpace() + public function testPartitionTotalSpace(): void { $this->assertEquals(-1, $this->object->getPartitionTotalSpace()); } - public function testPartUpload() { + public function testPartUpload(): string + { $source = __DIR__ . '/../resources/disk-a/large_file.mp4'; $dest = $this->object->getPath('uploaded.mp4'); $totalSize = \filesize($source); // AWS S3 requires each part to be at least 5MB except for last part $chunkSize = 5*1024*1024; - $chunks = ceil($totalSize / $chunkSize); + $chunks = intval(ceil($totalSize / $chunkSize)); $chunk = 1; $start = 0; @@ -237,7 +239,8 @@ public function testPartUpload() { /** * @depends testPartUpload */ - public function testPartRead($path) { + public function testPartRead(string $path): void + { $source = __DIR__ . '/../resources/disk-a/large_file.mp4'; $chunk = file_get_contents($source, false,null, 0, 500); $readChunk = $this->object->read($path, 0, 500); diff --git a/tests/Storage/StorageTest.php b/tests/Storage/StorageTest.php index f5b3658a..60564e69 100644 --- a/tests/Storage/StorageTest.php +++ b/tests/Storage/StorageTest.php @@ -20,20 +20,20 @@ public function tearDown(): void { } - public function testGetters() + public function testGetters(): void { $this->assertEquals(get_class(Storage::getDevice('disk-a')), 'Utopia\Storage\Device\Local'); $this->assertEquals(get_class(Storage::getDevice('disk-b')), 'Utopia\Storage\Device\Local'); try { - get_class(Storage::getDevice('disk-c')); + echo(get_class(Storage::getDevice('disk-c'))); $this->fail("Expected exception not thrown"); } catch (Exception $e) { $this->assertEquals('The device "disk-c" is not listed', $e->getMessage()); } } - public function testExists() + public function testExists(): void { $this->assertEquals(Storage::exists('disk-a'), true); $this->assertEquals(Storage::exists('disk-b'), true); diff --git a/tests/Storage/Validator/FileExtTest.php b/tests/Storage/Validator/FileExtTest.php index 0c85955a..25c591be 100644 --- a/tests/Storage/Validator/FileExtTest.php +++ b/tests/Storage/Validator/FileExtTest.php @@ -21,7 +21,7 @@ public function tearDown(): void { } - public function testValues() + public function testValues(): void { $this->assertEquals($this->object->isValid(''), false); $this->assertEquals($this->object->isValid(null), false); diff --git a/tests/Storage/Validator/FileNameTest.php b/tests/Storage/Validator/FileNameTest.php index 290e31d2..5803882a 100644 --- a/tests/Storage/Validator/FileNameTest.php +++ b/tests/Storage/Validator/FileNameTest.php @@ -21,7 +21,7 @@ public function tearDown(): void { } - public function testValues() + public function testValues(): void { $this->assertEquals($this->object->isValid(''), false); $this->assertEquals($this->object->isValid(null), false); diff --git a/tests/Storage/Validator/FileSizeTest.php b/tests/Storage/Validator/FileSizeTest.php index 65213d4f..d4a14733 100644 --- a/tests/Storage/Validator/FileSizeTest.php +++ b/tests/Storage/Validator/FileSizeTest.php @@ -21,7 +21,7 @@ public function tearDown(): void { } - public function testValues() + public function testValues(): void { $this->assertEquals($this->object->isValid(1001), false); $this->assertEquals($this->object->isValid(1000), true); diff --git a/tests/Storage/Validator/FileTypeTest.php b/tests/Storage/Validator/FileTypeTest.php index 0b99ef47..ca048712 100644 --- a/tests/Storage/Validator/FileTypeTest.php +++ b/tests/Storage/Validator/FileTypeTest.php @@ -21,7 +21,7 @@ public function tearDown(): void { } - public function testValues() + public function testValues(): void { $this->assertEquals($this->object->isValid(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'), true); $this->assertEquals($this->object->isValid(__DIR__ . '/../../resources/disk-a/kitten-2.jpg'), true); diff --git a/tests/Storage/Validator/UploadTest.php b/tests/Storage/Validator/UploadTest.php index 0fac3d10..cdba48ca 100644 --- a/tests/Storage/Validator/UploadTest.php +++ b/tests/Storage/Validator/UploadTest.php @@ -21,7 +21,7 @@ public function tearDown(): void { } - public function testValues() + public function testValues(): void { $this->assertEquals($this->object->isValid(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'), false); $this->assertEquals($this->object->isValid(__DIR__ . '/../../resources/disk-a/kitten-2.jpg'), false); From 2ef9c611f9c0321d27d683c2f00a8eb4b295fb6a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 20 Feb 2023 06:36:44 +0000 Subject: [PATCH 3/3] feat: fix codeql issues --- composer.lock | 27 ++++++++++++------------ src/Storage/Device/Local.php | 7 +++--- src/Storage/Device/S3.php | 41 ++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/composer.lock b/composer.lock index d896f5c6..4a2baa8c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0e15ac0ae89a77c7420f825af10e33fc", + "content-hash": "1259284323e29983e5aa6073843ff4f4", "packages": [ { "name": "utopia-php/framework", @@ -1280,12 +1280,12 @@ "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "9aa375b67ba9dc7c39272be85e0b781c86d1be1c" + "reference": "6c0217aa2b146c3e28474e8be3e87188fac55dac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9aa375b67ba9dc7c39272be85e0b781c86d1be1c", - "reference": "9aa375b67ba9dc7c39272be85e0b781c86d1be1c", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/6c0217aa2b146c3e28474e8be3e87188fac55dac", + "reference": "6c0217aa2b146c3e28474e8be3e87188fac55dac", "shasum": "" }, "require": { @@ -1294,7 +1294,6 @@ "conflict": { "phpstan/phpstan-shim": "*" }, - "default-branch": true, "bin": [ "phpstan", "phpstan.phar" @@ -1332,7 +1331,7 @@ "type": "tidelift" } ], - "time": "2022-10-12T20:57:15+00:00" + "time": "2023-02-18T15:01:46+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1340,12 +1339,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "b5f3416da036fb951a557518e8304b3595ff966a" + "reference": "87b660f75adb677ea503dd0b999dc90a999ad720" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/b5f3416da036fb951a557518e8304b3595ff966a", - "reference": "b5f3416da036fb951a557518e8304b3595ff966a", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/87b660f75adb677ea503dd0b999dc90a999ad720", + "reference": "87b660f75adb677ea503dd0b999dc90a999ad720", "shasum": "" }, "require": { @@ -1409,7 +1408,7 @@ "type": "github" } ], - "time": "2023-02-18T16:27:54+00:00" + "time": "2023-02-20T06:08:06+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1658,12 +1657,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c1bf2dd252dec994ee27d804c038a98fd0d1d940" + "reference": "ada16b71314bc852f7b199856bd2e12930951d24" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c1bf2dd252dec994ee27d804c038a98fd0d1d940", - "reference": "c1bf2dd252dec994ee27d804c038a98fd0d1d940", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ada16b71314bc852f7b199856bd2e12930951d24", + "reference": "ada16b71314bc852f7b199856bd2e12930951d24", "shasum": "" }, "require": { @@ -1752,7 +1751,7 @@ "type": "tidelift" } ], - "time": "2023-02-18T16:26:49+00:00" + "time": "2023-02-20T06:03:35+00:00" }, { "name": "psr/container", diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 5e8ee0b0..9e4553d4 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -169,10 +169,9 @@ public function abort(string $path, string $extra = ''): bool /** * Read file by given path. * - * @param string $path - * @param int $offset - * @param int $length - * + * @param string $path + * @param int $offset + * @param int $length * @return string * * @throws Exception diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 35d8d11b..6df5c5cc 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -212,11 +212,11 @@ public function getPath(string $filename, string $prefix = null): string * Upload a file to desired destination in the selected disk. * return number of chunks uploaded or 0 if it fails. * - * @param string $source - * @param string $path - * @param int $chunk - * @param int $chunks - * @param array $metadata + * @param string $source + * @param string $path + * @param int $chunk + * @param int $chunks + * @param array $metadata * * @throws \Exception */ @@ -345,15 +345,13 @@ public function abort(string $path, string $extra = ''): bool /** * Read file or part of file by given path, offset and length. * - * @param string $path - * @param int $offset - * @param int $length - * - * @throws \Exception - * + * @param string $path + * @param int $offset + * @param int $length * @return string * * @throws \Exception + * @throws \Exception */ public function read(string $path, int $offset = 0, int $length = null): string { @@ -443,15 +441,13 @@ public function delete(string $path, bool $recursive = false): bool /** * Get list of objects in the given path. * - * @param string $prefix - * @param int $maxKeys - * @param string $continuationToken - * - * @throws \Exception - * + * @param string $prefix + * @param int $maxKeys + * @param string $continuationToken * @return array * * @throws \Exception + * @throws \Exception */ private function listObjects($prefix = '', $maxKeys = 1000, $continuationToken = '') { @@ -646,10 +642,10 @@ private function getInfo(string $path): array /** * Generate the headers for AWS Signature V4 - * @param string $method - * @param string $uri - * @param array $parameters - * + * + * @param string $method + * @param string $uri + * @param array $parameters * @return string */ private function getSignatureV4(string $method, string $uri, array $parameters = []): string @@ -817,10 +813,9 @@ private function call(string $method, string $uri, string $data = '', array $par \curl_close($curl); // Parse body into XML - if (array_key_exists('content-type', $response->headers) + if (array_key_exists('content-type', $response->headers) && ($response->headers['content-type'] == 'application/xml' || (str_starts_with($response->body, 'headers['content-type'] == 'image/svg+xml'))) { - $response->body = \simplexml_load_string($response->body); $response->body = json_decode(json_encode($response->body), true); }