From 6227d852c023e2d22bdb13dc1be0341b4f5b1f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin?= Date: Mon, 30 Oct 2023 15:37:36 +0100 Subject: [PATCH] feat: add typehint on adapter (#694) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add typehint on adapter * Type interface for adapters * Type AsyncAwsS3 * Type AwsS3 * Type AzureBlobStorage * Type DoctrineDbal * fix constructor AsyncAwsS3 * Type Flysystem * Type PhpseclibSftp * Type Zip * Type GridFS and fix declaration checksum * fix muchafm review * refacto: add types on adapter * fix: review * fix: review * Refacto: Add type on Stream classes * fix pedro review * refactor: add typeint to FilesystemMap, StreamWrapper and StreamMode * Refacto: Add type on Util classes * fix muchafm review * fix: pedro review * fix pedro's review * refacto: spec types * fix: review * refactor: fix Ftp and StreamWrapper errors * refactor: fix lvl5 errors for AwsS3 and AzureBlobStorage adapters * ci: upgrade phpstan to 1.10.39 * test: fix filesystem typehint in phpunit tests * fix: fix phpspec tests * refactor: apply cs fixing * test: fix phpspec tests * Update src/Gaufrette/Adapter/AwsS3.php Co-authored-by: Grégoire Drapeau * Update src/Gaufrette/Adapter/PhpseclibSftp.php Co-authored-by: Grégoire Drapeau * Update src/Gaufrette/Adapter/AzureBlobStorage.php Co-authored-by: Kévin * Update src/Gaufrette/Adapter.php * Update src/Gaufrette/Adapter/AzureBlobStorage.php Co-authored-by: Kévin * Update src/Gaufrette/Adapter/Ftp.php Co-authored-by: Kévin * Update src/Gaufrette/Adapter/AzureBlobStorage.php Co-authored-by: Kévin * Update src/Gaufrette/Adapter/DoctrineDbal.php * refactor: set type for mimetype aware adapters * docs: add explanation for bitwire value in StreamWrapper * fix: introduction of readonly promoted properties * Update src/Gaufrette/Adapter/AwsS3.php Co-authored-by: Grégoire Drapeau * Update src/Gaufrette/Stream/InMemoryBuffer.php Co-authored-by: Kévin * Update src/Gaufrette/Stream/Local.php Co-authored-by: Kévin * Update src/Gaufrette/Stream/Local.php Co-authored-by: Kévin * Update src/Gaufrette/Adapter/AsyncAwsS3.php * Update src/Gaufrette/Stream/InMemoryBuffer.php Co-authored-by: Kévin --------- Co-authored-by: Sophie Roussel Co-authored-by: PedroTroller Co-authored-by: Grégoire Drapeau --- .github/workflows/static.yml | 2 +- spec/Gaufrette/Adapter/AsyncAwsS3Spec.php | 3 - spec/Gaufrette/Adapter/AwsS3Spec.php | 3 - .../AzureBlobStorage/BlobProxyFactory.php | 5 +- .../Adapter/AzureBlobStorageSpec.php | 147 ++++-------------- spec/Gaufrette/Adapter/DoctrineDbalSpec.php | 32 +--- spec/Gaufrette/Adapter/FlysystemSpec.php | 4 +- spec/Gaufrette/Adapter/GridFSSpec.php | 20 +-- spec/Gaufrette/Adapter/PhpseclibSftpSpec.php | 34 +--- spec/Gaufrette/Adapter/ZipSpec.php | 19 +++ spec/Gaufrette/Adapter/functions.php | 14 +- spec/Gaufrette/FileSpec.php | 46 ------ spec/Gaufrette/FilesystemMapSpec.php | 12 -- spec/Gaufrette/FilesystemSpec.php | 93 +---------- spec/Gaufrette/StreamWrapperSpec.php | 93 ++--------- src/Gaufrette/Adapter.php | 44 ++---- src/Gaufrette/Adapter/AsyncAwsS3.php | 86 +++++----- src/Gaufrette/Adapter/AwsS3.php | 102 +++++------- src/Gaufrette/Adapter/AzureBlobStorage.php | 145 +++++------------ .../AzureBlobStorage/BlobProxyFactory.php | 15 +- src/Gaufrette/Adapter/ChecksumCalculator.php | 6 +- src/Gaufrette/Adapter/DoctrineDbal.php | 42 ++--- src/Gaufrette/Adapter/FileFactory.php | 7 +- src/Gaufrette/Adapter/Flysystem.php | 47 +++--- src/Gaufrette/Adapter/Ftp.php | 110 +++++-------- src/Gaufrette/Adapter/GoogleCloudStorage.php | 86 ++++------ src/Gaufrette/Adapter/GridFS.php | 37 ++--- src/Gaufrette/Adapter/InMemory.php | 53 ++----- src/Gaufrette/Adapter/ListKeysAware.php | 6 +- src/Gaufrette/Adapter/Local.php | 79 ++++------ src/Gaufrette/Adapter/MetadataSupporter.php | 13 +- src/Gaufrette/Adapter/MimeTypeProvider.php | 8 +- src/Gaufrette/Adapter/PhpseclibSftp.php | 47 +++--- src/Gaufrette/Adapter/SafeLocal.php | 10 +- src/Gaufrette/Adapter/SizeCalculator.php | 6 +- src/Gaufrette/Adapter/StreamFactory.php | 8 +- src/Gaufrette/Adapter/Zip.php | 41 ++--- src/Gaufrette/FilesystemInterface.php | 2 +- src/Gaufrette/FilesystemMap.php | 32 ++-- src/Gaufrette/FilesystemMapInterface.php | 14 +- src/Gaufrette/Stream.php | 45 ++---- src/Gaufrette/Stream/InMemoryBuffer.php | 58 +++---- src/Gaufrette/Stream/Local.php | 68 +++----- src/Gaufrette/StreamMode.php | 52 ++----- src/Gaufrette/StreamWrapper.php | 137 +++++++--------- src/Gaufrette/Util/Checksum.php | 16 +- src/Gaufrette/Util/Path.php | 22 +-- src/Gaufrette/Util/Size.php | 19 +-- .../Functional/Adapter/AsyncAwsS3Test.php | 6 +- .../Functional/Adapter/AwsS3Test.php | 9 +- .../Adapter/AzureBlobStorageTest.php | 7 +- .../AzureMultiContainerBlobStorageTest.php | 4 +- .../Functional/Adapter/DoctrineDbalTest.php | 4 +- .../Functional/Adapter/FunctionalTestCase.php | 5 +- .../Functional/Adapter/LocalTest.php | 2 +- .../Functional/Adapter/PhpseclibSftpTest.php | 6 +- .../FileStream/FunctionalTestCase.php | 3 +- .../Functional/FileStream/LocalTest.php | 2 +- 58 files changed, 619 insertions(+), 1419 deletions(-) create mode 100644 spec/Gaufrette/Adapter/ZipSpec.php diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 3f7c8c86d..19c32b905 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -20,7 +20,7 @@ jobs: - name: Download dependencies run: | make require-all - composer require --dev --no-update phpstan/phpstan:1.8.8 + composer require --dev --no-update phpstan/phpstan:1.10.39 composer update --no-interaction --prefer-dist - name: PHPStan diff --git a/spec/Gaufrette/Adapter/AsyncAwsS3Spec.php b/spec/Gaufrette/Adapter/AsyncAwsS3Spec.php index 6c0ae7408..1556cf9d6 100644 --- a/spec/Gaufrette/Adapter/AsyncAwsS3Spec.php +++ b/spec/Gaufrette/Adapter/AsyncAwsS3Spec.php @@ -8,9 +8,6 @@ class AsyncAwsS3Spec extends ObjectBehavior { - /** - * @param \AsyncAws\SimpleS3\SimpleS3Client $service - */ function let(SimpleS3Client $service) { $this->beConstructedWith($service, 'bucketName'); diff --git a/spec/Gaufrette/Adapter/AwsS3Spec.php b/spec/Gaufrette/Adapter/AwsS3Spec.php index 7dd38a8f9..6fa971cc5 100644 --- a/spec/Gaufrette/Adapter/AwsS3Spec.php +++ b/spec/Gaufrette/Adapter/AwsS3Spec.php @@ -8,9 +8,6 @@ class AwsS3Spec extends ObjectBehavior { - /** - * @param \Aws\S3\S3Client $service - */ function let(S3Client $service) { $this->beConstructedWith($service, 'bucketName'); diff --git a/spec/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php b/spec/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php index 88b9f50c9..85ab1c7ae 100644 --- a/spec/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php +++ b/spec/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php @@ -6,10 +6,7 @@ class BlobProxyFactory extends ObjectBehavior { - /** - * @param string $connectionString - */ - function let($connectionString) + function let(string $connectionString) { $this->beConstructedWith($connectionString); } diff --git a/spec/Gaufrette/Adapter/AzureBlobStorageSpec.php b/spec/Gaufrette/Adapter/AzureBlobStorageSpec.php index 977cdcf56..b94308b9b 100644 --- a/spec/Gaufrette/Adapter/AzureBlobStorageSpec.php +++ b/spec/Gaufrette/Adapter/AzureBlobStorageSpec.php @@ -2,17 +2,17 @@ namespace spec\Gaufrette\Adapter; +use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface; use PhpSpec\ObjectBehavior; - +use WindowsAzure\Blob\Internal\IBlob; use WindowsAzure\Blob\Models\Blob; +use WindowsAzure\Blob\Models\GetBlobResult; +use WindowsAzure\Blob\Models\ListBlobsResult; use WindowsAzure\Common\ServiceException; class AzureBlobStorageSpec extends ObjectBehavior { - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - */ - function let($blobProxyFactory) + function let(BlobProxyFactoryInterface $blobProxyFactory) { $this->beConstructedWith($blobProxyFactory, 'containerName'); } @@ -24,12 +24,7 @@ function it_should_be_initializable() $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - * @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult - */ - function it_should_read_file($blobProxyFactory, $blobProxy, $getBlobResult) + function it_should_read_file(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy, GetBlobResult $getBlobResult) { $getBlobResult ->getContentStream() @@ -50,11 +45,7 @@ function it_should_read_file($blobProxyFactory, $blobProxy, $getBlobResult) $this->read('filename')->shouldReturn('some content'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_return_false_when_cannot_read($blobProxyFactory, $blobProxy) + function it_should_return_false_when_cannot_read(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->getBlob('containerName', 'filename') @@ -69,11 +60,7 @@ function it_should_return_false_when_cannot_read($blobProxyFactory, $blobProxy) $this->read('filename')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_read($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_read(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->getBlob('containerName', 'filename') @@ -88,11 +75,7 @@ function it_should_not_mask_exception_when_read($blobProxyFactory, $blobProxy) $this->shouldThrow(new \RuntimeException('read'))->duringRead('filename'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_rename_file($blobProxyFactory, $blobProxy) + function it_should_rename_file(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->copyBlob('containerName', 'filename2', 'containerName', 'filename1') @@ -110,11 +93,7 @@ function it_should_rename_file($blobProxyFactory, $blobProxy) $this->rename('filename1', 'filename2')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_return_false_when_cannot_rename($blobProxyFactory, $blobProxy) + function it_should_return_false_when_cannot_rename(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->copyBlob('containerName', 'filename2', 'containerName', 'filename1') @@ -129,11 +108,7 @@ function it_should_return_false_when_cannot_rename($blobProxyFactory, $blobProxy $this->rename('filename1', 'filename2')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_rename($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_rename(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->copyBlob('containerName', 'filename2', 'containerName', 'filename1') @@ -148,11 +123,7 @@ function it_should_not_mask_exception_when_rename($blobProxyFactory, $blobProxy) $this->shouldThrow(new \RuntimeException('rename'))->duringRename('filename1', 'filename2'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_write_file($blobProxyFactory, $blobProxy) + function it_should_write_file(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->createBlockBlob( @@ -171,11 +142,7 @@ function it_should_write_file($blobProxyFactory, $blobProxy) $this->write('filename', 'some content')->shouldReturn(12); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_return_false_when_cannot_write($blobProxyFactory, $blobProxy) + function it_should_return_false_when_cannot_write(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->createBlockBlob( @@ -194,11 +161,7 @@ function it_should_return_false_when_cannot_write($blobProxyFactory, $blobProxy) $this->write('filename', 'some content')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_write($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_write(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxy ->createBlockBlob( @@ -217,12 +180,7 @@ function it_should_not_mask_exception_when_write($blobProxyFactory, $blobProxy) $this->shouldThrow(new \RuntimeException('write'))->duringWrite('filename', 'some content'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - * @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult - */ - function it_should_check_if_file_exists($blobProxyFactory, $blobProxy, $getBlobResult) + function it_should_check_if_file_exists(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy, GetBlobResult $getBlobResult) { $blobProxyFactory ->create() @@ -244,11 +202,7 @@ function it_should_check_if_file_exists($blobProxyFactory, $blobProxy, $getBlobR $this->exists('filename2')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_check_if_file_exists($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_check_if_file_exists(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -263,13 +217,7 @@ function it_should_not_mask_exception_when_check_if_file_exists($blobProxyFactor $this->shouldThrow(new \RuntimeException('exists'))->duringExists('filename'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - * @param \WindowsAzure\Blob\Models\GetBlobPropertiesResult $getBlobPropertiesResult - * @param \WindowsAzure\Blob\Models\BlobProperties $blobProperties - */ - function it_should_get_file_mtime($blobProxyFactory, $blobProxy, $getBlobPropertiesResult, $blobProperties) + function it_should_get_file_mtime(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy, GetBlobPropertiesResult $getBlobPropertiesResult, BlobProperties $blobProperties) { $blobProxyFactory ->create() @@ -294,11 +242,7 @@ function it_should_get_file_mtime($blobProxyFactory, $blobProxy, $getBlobPropert $this->mtime('filename')->shouldReturn(strtotime('1987-12-28 20:00:00')); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_return_false_when_cannot_mtime($blobProxyFactory, $blobProxy) + function it_should_return_false_when_cannot_mtime(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -313,11 +257,7 @@ function it_should_return_false_when_cannot_mtime($blobProxyFactory, $blobProxy) $this->mtime('filename')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_get_mtime($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_get_mtime(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -332,11 +272,7 @@ function it_should_not_mask_exception_when_get_mtime($blobProxyFactory, $blobPro $this->shouldThrow(new \RuntimeException('mtime'))->duringMtime('filename'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_delete_file($blobProxyFactory, $blobProxy) + function it_should_delete_file(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -350,11 +286,7 @@ function it_should_delete_file($blobProxyFactory, $blobProxy) $this->delete('filename')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_return_false_when_cannot_delete_file($blobProxyFactory, $blobProxy) + function it_should_return_false_when_cannot_delete_file(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -369,11 +301,7 @@ function it_should_return_false_when_cannot_delete_file($blobProxyFactory, $blob $this->delete('filename')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_delete($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_delete(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -388,12 +316,7 @@ function it_should_not_mask_exception_when_delete($blobProxyFactory, $blobProxy) $this->shouldThrow(new \RuntimeException('delete'))->duringDelete('filename'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - * @param \WindowsAzure\Blob\Models\ListBlobsResult $listBlobResult - */ - function it_should_get_keys($blobProxyFactory, $blobProxy, $listBlobResult) + function it_should_get_keys(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy, ListBlobsResult $listBlobResult) { $fileNames = ['aaa', 'aaa/filename', 'filename1', 'filename2']; $blobs = []; @@ -421,11 +344,7 @@ function it_should_get_keys($blobProxyFactory, $blobProxy, $listBlobResult) $this->keys()->shouldReturn(['aaa', 'aaa/filename', 'filename1', 'filename2']); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_not_mask_exception_when_get_keys($blobProxyFactory, $blobProxy) + function it_should_not_mask_exception_when_get_keys(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -440,11 +359,7 @@ function it_should_not_mask_exception_when_get_keys($blobProxyFactory, $blobProx $this->shouldThrow(new \RuntimeException('keys'))->duringKeys(); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_handle_dirs($blobProxyFactory, $blobProxy) + function it_should_handle_dirs(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -466,11 +381,7 @@ function it_should_handle_dirs($blobProxyFactory, $blobProxy) $this->isDirectory('dirname')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_create_container($blobProxyFactory, $blobProxy) + function it_should_create_container(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() @@ -484,11 +395,7 @@ function it_should_create_container($blobProxyFactory, $blobProxy) $this->createContainer('containerName'); } - /** - * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy - */ - function it_should_fail_when_cannot_create_container($blobProxyFactory, $blobProxy) + function it_should_fail_when_cannot_create_container(BlobProxyFactoryInterface $blobProxyFactory, IBlob $blobProxy) { $blobProxyFactory ->create() diff --git a/spec/Gaufrette/Adapter/DoctrineDbalSpec.php b/spec/Gaufrette/Adapter/DoctrineDbalSpec.php index 4e723e148..259e09aa8 100644 --- a/spec/Gaufrette/Adapter/DoctrineDbalSpec.php +++ b/spec/Gaufrette/Adapter/DoctrineDbalSpec.php @@ -12,9 +12,6 @@ class DoctrineDbalSpec extends ObjectBehavior { - /** - * @param \Doctrine\DBAL\Connection $connection - */ function let(Connection $connection) { $this->beConstructedWith($connection, 'someTableName'); @@ -35,9 +32,6 @@ function it_does_not_handle_directories() $this->isDirectory('filename')->shouldReturn(false); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_checks_if_file_exists(Connection $connection) { $connection @@ -62,9 +56,6 @@ function it_checks_if_file_exists(Connection $connection) $this->exists('filename')->shouldReturn(false); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_writes_to_new_file(Connection $connection) { $connection @@ -96,9 +87,6 @@ function it_writes_to_new_file(Connection $connection) $this->write('filename', 'some content')->shouldReturn(12); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_write_file(Connection $connection) { $method = 'fetchOne'; // dbal 3.x @@ -131,9 +119,6 @@ function it_write_file(Connection $connection) $this->write('filename', 'some content')->shouldReturn(12); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_reads_file(Connection $connection) { $method = 'fetchOne'; // dbal 3.x @@ -153,9 +138,6 @@ function it_reads_file(Connection $connection) $this->read('filename')->shouldReturn('some content'); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_calculates_checksum(Connection $connection) { $method = 'fetchOne'; // dbal 3.x @@ -172,12 +154,9 @@ function it_calculates_checksum(Connection $connection) ->$method('SELECT "checksum" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename']) ->willReturn(1234); - $this->checksum('filename')->shouldReturn(1234); + $this->checksum('filename')->shouldReturn('1234'); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_gets_mtime(Connection $connection) { $method = 'fetchOne'; // dbal 3.x @@ -197,9 +176,6 @@ function it_gets_mtime(Connection $connection) $this->mtime('filename')->shouldReturn(1234); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_renames_file(Connection $connection) { $connection @@ -223,9 +199,6 @@ function it_renames_file(Connection $connection) $this->rename('filename', 'newFile')->shouldReturn(true); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_get_keys(Connection $connection, $result) { if (class_exists(Result::class)) { @@ -250,9 +223,6 @@ function it_get_keys(Connection $connection, $result) $this->keys()->shouldReturn(['filename', 'filename1', 'filename2']); } - /** - * @param \Doctrine\DBAL\Connection $connection - */ function it_deletes_file(Connection $connection) { $connection diff --git a/spec/Gaufrette/Adapter/FlysystemSpec.php b/spec/Gaufrette/Adapter/FlysystemSpec.php index 48f9934da..bf0c0031f 100644 --- a/spec/Gaufrette/Adapter/FlysystemSpec.php +++ b/spec/Gaufrette/Adapter/FlysystemSpec.php @@ -34,7 +34,7 @@ function it_writes_file(AdapterInterface $adapter, Config $config) { $adapter->write('filename', 'Hello.', $config)->willReturn([]); - $this->write('filename', 'Hello.')->shouldReturn([]); + $this->write('filename', 'Hello.')->shouldReturn(0); } function it_checks_if_file_exists(AdapterInterface $adapter) @@ -99,7 +99,7 @@ function it_renames_file(AdapterInterface $adapter) $this->rename('oldfilename', 'newfilename')->shouldReturn(true); } - function it_does_not_support_is_directory(AdapterInterface $adapter) + function it_does_not_support_is_directory() { $this->shouldThrow('Gaufrette\Exception\UnsupportedAdapterMethodException')->duringisDirectory('folder'); } diff --git a/spec/Gaufrette/Adapter/GridFSSpec.php b/spec/Gaufrette/Adapter/GridFSSpec.php index 87ec43f17..271a296de 100644 --- a/spec/Gaufrette/Adapter/GridFSSpec.php +++ b/spec/Gaufrette/Adapter/GridFSSpec.php @@ -46,7 +46,7 @@ function it_supports_native_list_keys() $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware'); } - function it_reads_file($bucket) + function it_reads_file(Bucket $bucket) { $this->resources[] = $readable = fopen('php://memory', 'rw'); fwrite($readable, 'some content'); @@ -61,14 +61,14 @@ function it_reads_file($bucket) $this->read('filename')->shouldReturn('some content'); } - function it_does_not_fail_when_cannot_read($bucket) + function it_does_not_fail_when_cannot_read(Bucket $bucket) { $bucket->openDownloadStreamByName('filename')->willThrow(FileNotFoundException::class); $this->read('filename')->shouldReturn(false); } - function it_checks_if_file_exists($bucket, BSONDocument $file) + function it_checks_if_file_exists(Bucket $bucket, BSONDocument $file) { $bucket ->findOne(['filename' => 'filename']) @@ -83,7 +83,7 @@ function it_checks_if_file_exists($bucket, BSONDocument $file) $this->exists('filename2')->shouldReturn(false); } - function it_deletes_file($bucket) + function it_deletes_file(Bucket $bucket) { $bucket ->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]]) @@ -94,14 +94,14 @@ function it_deletes_file($bucket) $this->delete('filename')->shouldReturn(true); } - function it_does_not_delete_file($bucket) + function it_does_not_delete_file(Bucket $bucket) { $bucket->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])->willReturn(null); $this->delete('filename')->shouldReturn(false); } - function it_writes_file($bucket) + function it_writes_file(Bucket $bucket) { $this->resources[] = $writable = fopen('php://memory', 'rw'); @@ -117,7 +117,7 @@ function it_writes_file($bucket) ; } - function it_renames_file($bucket) + function it_renames_file(Bucket $bucket) { $this->resources[] = $writable = fopen('php://memory', 'rw'); $this->resources[] = $readable = fopen('php://memory', 'rw'); @@ -137,7 +137,7 @@ function it_renames_file($bucket) $this->rename('filename', 'otherFilename')->shouldReturn(true); } - function it_fetches_keys($bucket) + function it_fetches_keys(Bucket $bucket) { $bucket ->find([], ['projection' => ['filename' => 1]]) @@ -147,7 +147,7 @@ function it_fetches_keys($bucket) $this->keys()->shouldReturn(['filename', 'otherFilename']); } - function it_fetches_mtime($bucket) + function it_fetches_mtime(Bucket $bucket) { $bucket ->findOne(['filename' => 'filename'], ['projection' => ['uploadDate' => 1]]) @@ -157,7 +157,7 @@ function it_fetches_mtime($bucket) $this->mtime('filename')->shouldReturn(12345); } - function it_calculates_checksum($bucket) + function it_calculates_checksum(Bucket $bucket) { $bucket ->findOne(['filename' => 'filename'], ['projection' => ['md5' => 1]]) diff --git a/spec/Gaufrette/Adapter/PhpseclibSftpSpec.php b/spec/Gaufrette/Adapter/PhpseclibSftpSpec.php index 7f6703cdd..622aa1cd8 100644 --- a/spec/Gaufrette/Adapter/PhpseclibSftpSpec.php +++ b/spec/Gaufrette/Adapter/PhpseclibSftpSpec.php @@ -11,21 +11,11 @@ } use Gaufrette\Filesystem; -use phpseclib\Net\SFTP as Base; +use phpseclib\Net\SFTP; use PhpSpec\ObjectBehavior; -class SFTP extends Base -{ - public function __construct() - { - } -} - class PhpseclibSftpSpec extends ObjectBehavior { - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function let(SFTP $sftp) { $this->beConstructedWith($sftp, '/home/l3l0', false, 'l3lo', 'password'); @@ -46,9 +36,6 @@ function it_supports_native_list_keys() $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware'); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_fetches_keys(SFTP $sftp) { $sftp @@ -73,9 +60,6 @@ function it_fetches_keys(SFTP $sftp) $this->keys()->shouldReturn(['filename', 'filename1', 'aaa', 'aaa/filename']); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_reads_file(SFTP $sftp) { $sftp->get('/home/l3l0/filename')->willReturn('some content'); @@ -83,9 +67,6 @@ function it_reads_file(SFTP $sftp) $this->read('filename')->shouldReturn('some content'); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_creates_and_writes_file(SFTP $sftp) { $sftp->pwd()->willReturn('/home/l3l0'); @@ -96,9 +77,6 @@ function it_creates_and_writes_file(SFTP $sftp) $this->write('filename', 'some content')->shouldReturn(12); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_renames_file(SFTP $sftp) { $sftp->pwd()->willReturn('/home/l3l0'); @@ -111,9 +89,6 @@ function it_renames_file(SFTP $sftp) $this->rename('filename', 'filename1')->shouldReturn(true); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_should_check_if_file_exists(SFTP $sftp) { $sftp->pwd()->willReturn('/home/l3l0'); @@ -127,9 +102,6 @@ function it_should_check_if_file_exists(SFTP $sftp) $this->exists('filename1')->shouldReturn(false); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - */ function it_should_check_is_directory(SFTP $sftp) { $sftp->pwd()->willReturn('/home/l3l0'); @@ -141,10 +113,6 @@ function it_should_check_is_directory(SFTP $sftp) $this->isDirectory('filename')->shouldReturn(false); } - /** - * @param \spec\Gaufrette\Adapter\SFTP $sftp - * @param \Gaufrette\Filesystem $filesystem - */ function it_should_create_file(SFTP $sftp, Filesystem $filesystem) { $sftp->stat('/home/l3l0/filename')->willReturn([ diff --git a/spec/Gaufrette/Adapter/ZipSpec.php b/spec/Gaufrette/Adapter/ZipSpec.php new file mode 100644 index 000000000..ca7c1df28 --- /dev/null +++ b/spec/Gaufrette/Adapter/ZipSpec.php @@ -0,0 +1,19 @@ +beConstructedWith('somefile'); + } + + function it_is_adapter() + { + $this->shouldHaveType(Adapter::class); + } +} diff --git a/spec/Gaufrette/Adapter/functions.php b/spec/Gaufrette/Adapter/functions.php index 177bf120f..bfc066742 100644 --- a/spec/Gaufrette/Adapter/functions.php +++ b/spec/Gaufrette/Adapter/functions.php @@ -9,7 +9,7 @@ function time() return \strtotime('2012-10-10 23:10:10'); } -function file_exists($path) +function file_exists(string $path) { //fake it for ssh+ssl: protocol for SFTP testing, otherwise delegate to global if (strpos($path, 'ssh+ssl:') === 0) { @@ -19,7 +19,7 @@ function file_exists($path) return \file_exists($path); } -function extension_loaded($name) +function extension_loaded() { global $extensionLoaded; @@ -30,17 +30,17 @@ function extension_loaded($name) return $extensionLoaded; } -function opendir($url) +function opendir(string $url) { return true; } -function apc_fetch($path) +function apc_fetch(string $path) { return sprintf('%s content', $path); } -function apc_store($path, $content, $ttl) +function apc_store(string $path, mixed $content, int $ttl) { if ('prefix-apc-test/invalid' === $path) { return false; @@ -49,7 +49,7 @@ function apc_store($path, $content, $ttl) return sprintf('%s content', $path); } -function apc_delete($path) +function apc_delete(string $path) { if ('prefix-apc-test/invalid' === $path) { return false; @@ -58,7 +58,7 @@ function apc_delete($path) return true; } -function apc_exists($path) +function apc_exists(mixed $path) { if ('prefix-apc-test/invalid' === $path) { return false; diff --git a/spec/Gaufrette/FileSpec.php b/spec/Gaufrette/FileSpec.php index 80576bbda..4ebddd974 100644 --- a/spec/Gaufrette/FileSpec.php +++ b/spec/Gaufrette/FileSpec.php @@ -12,9 +12,6 @@ interface MetadataAdapter extends \Gaufrette\Adapter, class FileSpec extends ObjectBehavior { - /** - * @param \Gaufrette\Filesystem $filesystem - */ function let(Filesystem $filesystem) { $this->beConstructedWith('filename', $filesystem); @@ -30,9 +27,6 @@ function it_gives_access_to_key() $this->getKey()->shouldReturn('filename'); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_gets_content(Filesystem $filesystem) { $filesystem->read('filename')->shouldBeCalled()->willReturn('Some content'); @@ -40,9 +34,6 @@ function it_gets_content(Filesystem $filesystem) $this->getContent()->shouldReturn('Some content'); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_gets_mtime(Filesystem $filesystem) { $filesystem->mtime('filename')->shouldBeCalled()->willReturn(1358797854); @@ -50,10 +41,6 @@ function it_gets_mtime(Filesystem $filesystem) $this->getMtime()->shouldReturn(1358797854); } - /** - * @param \Gaufrette\Filesystem $filesystem - * @param \spec\Gaufrette\MetadataAdapter $adapter - */ function it_pass_metadata_when_write_content(Filesystem $filesystem, MetadataAdapter $adapter) { $metadata = ['id' => '123']; @@ -64,10 +51,6 @@ function it_pass_metadata_when_write_content(Filesystem $filesystem, MetadataAda $this->setContent('some content', $metadata); } - /** - * @param \Gaufrette\Filesystem $filesystem - * @param \spec\Gaufrette\MetadataAdapter $adapter - */ function it_pass_metadata_when_read_content(Filesystem $filesystem, MetadataAdapter $adapter) { $metadata = ['id' => '123']; @@ -78,10 +61,6 @@ function it_pass_metadata_when_read_content(Filesystem $filesystem, MetadataAdap $this->getContent($metadata); } - /** - * @param \Gaufrette\Filesystem $filesystem - * @param \spec\Gaufrette\MetadataAdapter $adapter - */ function it_pass_metadata_when_delete_content(Filesystem $filesystem, MetadataAdapter $adapter) { $metadata = ['id' => '123']; @@ -92,10 +71,6 @@ function it_pass_metadata_when_delete_content(Filesystem $filesystem, MetadataAd $this->delete($metadata); } - /** - * @param \Gaufrette\Filesystem $filesystem - * @param \spec\Gaufrette\MetadataAdapter $adapter - */ function it_sets_content_of_file(Filesystem $filesystem, MetadataAdapter $adapter) { $adapter->setMetadata('filename', [])->shouldNotBeCalled(); @@ -117,9 +92,6 @@ function it_sets_name() $this->getName()->shouldReturn('name'); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_sets_size_for_new_file(Filesystem $filesystem) { $filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21); @@ -128,9 +100,6 @@ function it_sets_size_for_new_file(Filesystem $filesystem) $this->getSize()->shouldReturn(21); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_calculates_size_from_filesystem(Filesystem $filesystem) { $filesystem->size('filename')->shouldBeCalled()->willReturn(12); @@ -138,9 +107,6 @@ function it_calculates_size_from_filesystem(Filesystem $filesystem) $this->getSize()->shouldReturn(12); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_allows_to_set_size(Filesystem $filesystem) { $filesystem->read('filename')->shouldNotBeCalled(); @@ -149,9 +115,6 @@ function it_allows_to_set_size(Filesystem $filesystem) $this->getSize()->shouldReturn(21); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_gets_zero_size_when_file_not_found(Filesystem $filesystem) { $filesystem->size('filename')->willThrow(new \Gaufrette\Exception\FileNotFound('filename')); @@ -159,9 +122,6 @@ function it_gets_zero_size_when_file_not_found(Filesystem $filesystem) $this->getSize()->shouldReturn(0); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_check_if_file_with_key_exists_in_filesystem(Filesystem $filesystem) { $filesystem->has('filename')->willReturn(true); @@ -171,18 +131,12 @@ function it_check_if_file_with_key_exists_in_filesystem(Filesystem $filesystem) $this->exists()->shouldReturn(false); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_deletes_file_from_filesystem(Filesystem $filesystem) { $filesystem->delete('filename')->shouldBeCalled()->willReturn(true); $this->delete()->shouldReturn(true); } - /** - * @param \Gaufrette\Filesystem $filesystem - */ function it_renames_file_from_filesystem(Filesystem $filesystem) { $filesystem->rename('filename', 'newname')->shouldBeCalled(); diff --git a/spec/Gaufrette/FilesystemMapSpec.php b/spec/Gaufrette/FilesystemMapSpec.php index 358b94241..33263d1c3 100644 --- a/spec/Gaufrette/FilesystemMapSpec.php +++ b/spec/Gaufrette/FilesystemMapSpec.php @@ -12,9 +12,6 @@ function it_is_initializable() $this->shouldHaveType('Gaufrette\FilesystemMap'); } - /** - * @param Gaufrette\Filesystem $filesystem - */ function it_checks_if_has_mapped_filesystem(Filesystem $filesystem) { $this->set('some', $filesystem); @@ -22,9 +19,6 @@ function it_checks_if_has_mapped_filesystem(Filesystem $filesystem) $this->has('other')->shouldReturn(false); } - /** - * @param Gaufrette\Filesystem $filesystem - */ function it_sets_mapped_filesystem(Filesystem $filesystem) { $this->set('some', $filesystem); @@ -39,9 +33,6 @@ function it_fails_when_get_filesystem_which_was_not_mapped() ; } - /** - * @param Gaufrette\Filesystem $filesystem - */ function it_removes_mapped_filesystem(Filesystem $filesystem) { $this->set('some', $filesystem); @@ -58,9 +49,6 @@ function it_fails_when_try_to_remove_filesystem_which_was_not_mapped() ; } - /** - * @param Gaufrette\Filesystem $filesystem - */ function it_removes_all_filesystems(Filesystem $filesystem) { $this->set('some', $filesystem); diff --git a/spec/Gaufrette/FilesystemSpec.php b/spec/Gaufrette/FilesystemSpec.php index ad8d66405..f40b13b01 100644 --- a/spec/Gaufrette/FilesystemSpec.php +++ b/spec/Gaufrette/FilesystemSpec.php @@ -18,9 +18,6 @@ interface ExtendedAdapter extends \Gaufrette\Adapter, class FilesystemSpec extends ObjectBehavior { - /** - * @param \Gaufrette\Adapter $adapter - */ function let(Adapter $adapter) { $this->beConstructedWith($adapter); @@ -32,17 +29,11 @@ function it_is_initializable() $this->shouldBeAnInstanceOf('Gaufrette\FilesystemInterface'); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_gives_access_to_adapter(Adapter $adapter) { $this->getAdapter()->shouldBe($adapter); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_check_if_file_exists_using_adapter(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -52,9 +43,6 @@ function it_check_if_file_exists_using_adapter(Adapter $adapter) $this->has('otherFilename')->shouldReturn(false); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_renames_file(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(true); @@ -64,9 +52,6 @@ function it_renames_file(Adapter $adapter) $this->rename('filename', 'otherFilename')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_renamed_source_file_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -77,9 +62,6 @@ function it_fails_when_renamed_source_file_does_not_exist(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_renamed_target_file_exists(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -91,9 +73,6 @@ function it_fails_when_renamed_target_file_exists(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_rename_is_not_successful(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -106,9 +85,6 @@ function it_fails_when_rename_is_not_successful(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_creates_file_object_for_key(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -116,9 +92,6 @@ function it_creates_file_object_for_key(Adapter $adapter) $this->get('filename')->shouldBeAnInstanceOf('Gaufrette\File'); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_get_file_object_when_file_with_key_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -129,9 +102,6 @@ function it_does_not_get_file_object_when_file_with_key_does_not_exist(Adapter $ ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_gets_file_object_when_file_does_not_exist_but_can_be_created(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -139,10 +109,6 @@ function it_gets_file_object_when_file_does_not_exist_but_can_be_created(Adapter $this->get('filename', true)->shouldBeAnInstanceOf('Gaufrette\File'); } - /** - * @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter - * @param \Gaufrette\File $file - */ function it_delegates_file_creation_to_adapter_when_adapter_is_file_factory(ExtendedAdapter $extendedAdapter, File $file) { $this->beConstructedWith($extendedAdapter); @@ -152,9 +118,6 @@ function it_delegates_file_creation_to_adapter_when_adapter_is_file_factory(Exte $this->get('filename')->shouldBe($file); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_writes_content_to_new_file(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(false); @@ -163,9 +126,6 @@ function it_writes_content_to_new_file(Adapter $adapter) $this->write('filename', 'some content to write')->shouldReturn(21); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_updates_content_of_file(Adapter $adapter) { $adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21); @@ -173,9 +133,6 @@ function it_updates_content_of_file(Adapter $adapter) $this->write('filename', 'some content to write', true)->shouldReturn(21); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_update_content_of_file_when_file_cannot_be_overwriten(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -187,9 +144,6 @@ function it_does_not_update_content_of_file_when_file_cannot_be_overwriten(Adapt ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_write_is_not_successful(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -201,9 +155,6 @@ function it_fails_when_write_is_not_successful(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_read_file(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(true); @@ -212,9 +163,6 @@ function it_read_file(Adapter $adapter) $this->read('filename')->shouldReturn('Some content'); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_read_file_which_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -224,9 +172,6 @@ function it_does_not_read_file_which_does_not_exist(Adapter $adapter) ->duringRead('filename'); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_read_is_not_successful(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -238,9 +183,6 @@ function it_fails_when_read_is_not_successful(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_deletes_file(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(true); @@ -249,9 +191,6 @@ function it_deletes_file(Adapter $adapter) $this->delete('filename')->shouldReturn(true); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_delete_file_which_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -262,9 +201,6 @@ function it_does_not_delete_file_which_does_not_exist(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_fails_when_delete_is_not_successful(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -276,9 +212,6 @@ function it_fails_when_delete_is_not_successful(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_should_get_all_keys(Adapter $adapter) { $keys = ['filename', 'filename1', 'filename2']; @@ -287,9 +220,6 @@ function it_should_get_all_keys(Adapter $adapter) $this->keys()->shouldReturn($keys); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_match_listed_keys_using_specified_pattern(Adapter $adapter) { $keys = ['filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey']; @@ -317,9 +247,6 @@ function it_match_listed_keys_using_specified_pattern(Adapter $adapter) ); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_listing_directories_using_adapter_is_directory_method(Adapter $adapter) { $keys = ['filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey']; @@ -353,9 +280,6 @@ function it_listing_directories_using_adapter_is_directory_method(Adapter $adapt ); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_gets_mtime_of_file_using_adapter(Adapter $adapter) { $adapter->exists('filename')->willReturn(true); @@ -364,9 +288,6 @@ function it_gets_mtime_of_file_using_adapter(Adapter $adapter) $this->mtime('filename')->shouldReturn(1234567); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_get_mtime_of_file_which_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->willReturn(false); @@ -377,9 +298,6 @@ function it_does_not_get_mtime_of_file_which_does_not_exist(Adapter $adapter) ; } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_calculates_file_checksum(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(true); @@ -388,9 +306,6 @@ function it_calculates_file_checksum(Adapter $adapter) $this->checksum('filename')->shouldReturn(md5('some content')); } - /** - * @param \Gaufrette\Adapter $adapter - */ function it_does_not_calculate_checksum_of_file_which_does_not_exist(Adapter $adapter) { $adapter->exists('filename')->shouldBeCalled()->willReturn(false); @@ -400,9 +315,6 @@ function it_does_not_calculate_checksum_of_file_which_does_not_exist(Adapter $ad ->duringChecksum('filename'); } - /** - * @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter - */ function it_delegates_checksum_calculation_to_adapter_when_adapter_is_checksum_calculator(ExtendedAdapter $extendedAdapter) { $this->beConstructedWith($extendedAdapter); @@ -410,12 +322,9 @@ function it_delegates_checksum_calculation_to_adapter_when_adapter_is_checksum_c $extendedAdapter->read('filename')->shouldNotBeCalled(); $extendedAdapter->checksum('filename')->shouldBeCalled()->willReturn(12); - $this->checksum('filename')->shouldReturn(12); + $this->checksum('filename')->shouldReturn('12'); } - /** - * @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter - */ function it_delegates_mime_type_resolution_to_adapter_when_adapter_is_mime_type_provider(ExtendedAdapter $extendedAdapter) { $this->beConstructedWith($extendedAdapter); diff --git a/spec/Gaufrette/StreamWrapperSpec.php b/spec/Gaufrette/StreamWrapperSpec.php index 00eafce40..f8b6dd94a 100644 --- a/spec/Gaufrette/StreamWrapperSpec.php +++ b/spec/Gaufrette/StreamWrapperSpec.php @@ -5,16 +5,12 @@ use Gaufrette\FilesystemMap; use Gaufrette\Filesystem; use Gaufrette\Stream; +use Gaufrette\StreamWrapper; use PhpSpec\ObjectBehavior; use Prophecy\Argument; class StreamWrapperSpec extends ObjectBehavior { - /** - * @param \Gaufrette\FilesystemMap $map - * @param \Gaufrette\Filesystem $filesystem - * @param \Gaufrette\Stream $stream - */ function let(FilesystemMap $map, Filesystem $filesystem, Stream $stream) { $filesystem->createStream('filename')->willReturn($stream); @@ -24,31 +20,31 @@ function let(FilesystemMap $map, Filesystem $filesystem, Stream $stream) function it_is_initializable() { - $this->shouldHaveType('Gaufrette\StreamWrapper'); + $this->shouldHaveType(StreamWrapper::class); } - /** - * @param \Gaufrette\Stream $stream - */ function it_opens_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); - $this->stream_open('gaufrette://some/filename', 'r+')->shouldReturn(true); + $this + ->stream_open('gaufrette://some/filename', 'r+', STREAM_REPORT_ERRORS) + ->shouldReturn(true) + ; } function it_does_not_open_stream_when_key_is_not_defined() { $this ->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette://some) is invalid.')) - ->duringStream_open('gaufrette://some', 'r+'); + ->duringStream_open('gaufrette://some', 'r+', STREAM_REPORT_ERRORS); } function it_does_not_open_stream_when_host_is_not_defined() { $this ->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette:///somefile) is invalid.')) - ->duringStream_open('gaufrette:///somefile', 'r+') + ->duringStream_open('gaufrette:///somefile', 'r+', STREAM_REPORT_ERRORS) ; } @@ -57,15 +53,12 @@ function it_does_not_read_from_stream_when_is_not_opened() $this->stream_read(10)->shouldReturn(false); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_read_from_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->read(4)->willReturn('some'); - $this->stream_open('gaufrette://some/filename', 'r+'); + $this->stream_open('gaufrette://some/filename', 'r+', STREAM_REPORT_ERRORS); $this->stream_read(4)->shouldReturn('some'); } @@ -74,114 +67,81 @@ function it_does_not_write_to_stream_when_is_not_opened() $this->stream_write('some content')->shouldReturn(0); } - /** - * @param \Gaufrette\Stream $stream - */ function it_writes_to_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->write('some content')->shouldBeCalled()->willReturn(12); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_write('some content')->shouldReturn(12); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_close_stream_when_is_not_opened($stream) { $stream->close()->shouldNotBeCalled(); $this->stream_close(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_closes_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->close()->shouldBeCalled(); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_close(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_flush_stream_when_is_not_opened(Stream $stream) { $stream->flush()->shouldNotBeCalled(); $this->stream_flush(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_flushes_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->flush()->shouldBeCalled(); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_flush(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_seek_in_stream_when_is_not_opened(Stream $stream) { $stream->seek(12, SEEK_SET)->shouldNotBeCalled(); $this->stream_seek(12, SEEK_SET); } - /** - * @param \Gaufrette\Stream $stream - */ function it_seeks_in_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->seek(12, SEEK_SET)->shouldBeCalled()->willReturn(true); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_seek(12, SEEK_SET)->shouldReturn(true); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_tell_about_position_in_stream_when_is_not_opened(Stream $stream) { $stream->tell()->shouldNotBeCalled(); $this->stream_tell(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_tell_about_position_in_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->tell()->shouldBeCalled()->willReturn(12); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_tell()->shouldReturn(12); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_mark_as_eof_if_stream_is_not_opened(Stream $stream) { $stream->eof()->shouldNotBeCalled(); $this->stream_eof(); } - /** - * @param \Gaufrette\Stream $stream - */ function it_checks_if_eof(Stream $stream) { $stream->open(Argument::any())->willReturn(true); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $stream->eof()->willReturn(false); $this->stream_eof()->shouldReturn(false); @@ -195,9 +155,6 @@ function it_does_not_get_stat_when_is_not_open() $this->stream_stat()->shouldReturn(false); } - /** - * @param \Gaufrette\Stream $stream - */ function it_stats_file(Stream $stream) { $stat = [ @@ -218,13 +175,10 @@ function it_stats_file(Stream $stream) $stream->open(Argument::any())->willReturn(true); $stream->stat()->willReturn($stat); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_stat()->shouldReturn($stat); } - /** - * @param \Gaufrette\Stream $stream - */ function it_should_stat_from_url(Stream $stream) { $stat = [ @@ -248,10 +202,6 @@ function it_should_stat_from_url(Stream $stream) $this->url_stat('gaufrette://some/filename', STREAM_URL_STAT_LINK)->shouldReturn($stat); } - /** - * @param \Gaufrette\Filesystem $stream - * @param \Gaufrette\Stream $stream - */ function it_stats_even_if_it_cannot_be_open(Filesystem $filesystem, Stream $stream) { $filesystem->createStream('dir/')->willReturn($stream); @@ -260,18 +210,12 @@ function it_stats_even_if_it_cannot_be_open(Filesystem $filesystem, Stream $stre $this->url_stat('gaufrette://some/dir/', STREAM_URL_STAT_LINK)->shouldReturn(['mode' => 16893]); } - /** - * @param \Gaufrette\Stream $stream - */ function it_does_not_unlink_when_cannot_open(Stream $stream) { $stream->open(Argument::any())->willThrow(new \RuntimeException); $this->unlink('gaufrette://some/filename')->shouldReturn(false); } - /** - * @param \Gaufrette\Stream $stream - */ function it_unlinks_file(Stream $stream) { $stream->open(Argument::any())->willReturn(true); @@ -285,15 +229,12 @@ function it_does_not_cast_stream_if_is_not_opened() $this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn(false); } - /** - * @param \Gaufrette\Stream $stream - */ function it_casts_stream(Stream $stream) { $stream->open(Argument::any())->willReturn(true); $stream->cast(STREAM_CAST_FOR_SELECT)->willReturn('resource'); - $this->stream_open('gaufrette://some/filename', 'w+'); + $this->stream_open('gaufrette://some/filename', 'w+', STREAM_REPORT_ERRORS); $this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn('resource'); } } diff --git a/src/Gaufrette/Adapter.php b/src/Gaufrette/Adapter.php index c6c4a95ba..916980ef4 100644 --- a/src/Gaufrette/Adapter.php +++ b/src/Gaufrette/Adapter.php @@ -13,72 +13,48 @@ interface Adapter /** * Reads the content of the file. * - * @param string $key - * - * @return string|bool if cannot read content + * @return string|false Returns FALSE in content is not readable */ - public function read($key); + public function read(string $key): string|bool; /** * Writes the given content into the file. * - * @param string $key - * @param string $content - * * @return int|bool The number of bytes that were written into the file */ - public function write($key, $content); + public function write(string $key, mixed $content): int|bool; /** * Indicates whether the file exists. - * - * @param string $key - * - * @return bool */ - public function exists($key); + public function exists(string $key): bool; /** * Returns an array of all keys (files and directories). * - * @return array + * @return array */ - public function keys(); + public function keys(): array; /** * Returns the last modified time. * - * @param string $key - * * @return int|bool An UNIX like timestamp or false */ - public function mtime($key); + public function mtime(string $key): int|bool; /** * Deletes the file. - * - * @param string $key - * - * @return bool */ - public function delete($key); + public function delete(string $key): bool; /** * Renames a file. - * - * @param string $sourceKey - * @param string $targetKey - * - * @return bool */ - public function rename($sourceKey, $targetKey); + public function rename(string $sourceKey, string $targetKey): bool; /** * Check if key is directory. - * - * @param string $key - * - * @return bool */ - public function isDirectory($key); + public function isDirectory(string $key): bool; } diff --git a/src/Gaufrette/Adapter/AsyncAwsS3.php b/src/Gaufrette/Adapter/AsyncAwsS3.php index 0ee94bb6d..f283c8491 100644 --- a/src/Gaufrette/Adapter/AsyncAwsS3.php +++ b/src/Gaufrette/Adapter/AsyncAwsS3.php @@ -10,37 +10,31 @@ /** * Amazon S3 adapter using the AsyncAws. * - * @author Michael Dowling + * @author Michael Dowling * @author Tobias Nyholm */ class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider { - /** @var SimpleS3Client */ - protected $service; - /** @var string */ - protected $bucket; - /** @var array */ - protected $options; - /** @var bool */ - protected $bucketExists; - /** @var array */ - protected $metadata = []; - /** @var bool */ - protected $detectContentType; - - /** - * @param SimpleS3Client $service - * @param string $bucket - * @param array $options - * @param bool $detectContentType - */ - public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false) - { + protected SimpleS3Client $service; + protected string $bucket; + protected array $options; + protected bool $bucketExists; + protected array $metadata = []; + protected bool $detectContentType; + + public function __construct( + SimpleS3Client $service, + string $bucket, + array $options = [], + bool $detectContentType = false + ) { if (!class_exists(SimpleS3Client::class)) { throw new \LogicException('You need to install package "async-aws/simple-s3" to use this adapter'); } + $this->service = $service; $this->bucket = $bucket; + $this->options = array_replace( [ 'create' => false, @@ -56,7 +50,7 @@ public function __construct(SimpleS3Client $service, $bucket, array $options = [ /** * {@inheritdoc} */ - public function setMetadata($key, $content) + public function setMetadata(string $key, array $content): void { // BC with AmazonS3 adapter if (isset($content['contentType'])) { @@ -70,7 +64,7 @@ public function setMetadata($key, $content) /** * {@inheritdoc} */ - public function getMetadata($key) + public function getMetadata(string $key): array { return $this->metadata[$key] ?? []; } @@ -78,7 +72,7 @@ public function getMetadata($key) /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { $this->ensureBucketExists(); $options = $this->getOptions($key); @@ -102,7 +96,7 @@ public function read($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->ensureBucketExists(); $options = $this->getOptions( @@ -121,9 +115,8 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} - * @param string|resource $content */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->ensureBucketExists(); $options = $this->getOptions($key); @@ -153,7 +146,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return $this->service->has($this->bucket, $this->computePath($key)); } @@ -161,7 +154,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { try { $result = $this->service->headObject($this->getOptions($key)); @@ -175,24 +168,24 @@ public function mtime($key) /** * {@inheritdoc} */ - public function size($key) + public function size(string $key): int { $result = $this->service->headObject($this->getOptions($key)); return (int) $result->getContentLength(); } - public function mimeType($key) + public function mimeType(string $key): string|bool { $result = $this->service->headObject($this->getOptions($key)); - return $result->getContentType(); + return $result->getContentType() ?: false; } /** * {@inheritdoc} */ - public function keys() + public function keys(): array { return $this->listKeys(); } @@ -200,7 +193,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $this->ensureBucketExists(); @@ -223,7 +216,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { try { $this->service->deleteObject($this->getOptions($key)); @@ -237,7 +230,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $result = $this->service->listObjectsV2([ 'Bucket' => $this->bucket, @@ -261,7 +254,7 @@ public function isDirectory($key) * @throws \RuntimeException if the bucket does not exists or could not be * created */ - protected function ensureBucketExists() + protected function ensureBucketExists(): bool { if ($this->bucketExists) { return true; @@ -288,7 +281,7 @@ protected function ensureBucketExists() return true; } - protected function getOptions($key, array $options = []) + protected function getOptions(string $key, array $options = []): array { $options['ACL'] = $this->options['acl']; $options['Bucket'] = $this->bucket; @@ -303,7 +296,7 @@ protected function getOptions($key, array $options = []) return $options; } - protected function computePath($key) + protected function computePath(string $key): string { if (empty($this->options['directory'])) { return $key; @@ -314,22 +307,13 @@ protected function computePath($key) /** * Computes the key from the specified path. - * - * @param string $path - * - * return string */ - protected function computeKey($path) + protected function computeKey(string $path): string { return ltrim(substr($path, strlen($this->options['directory'])), '/'); } - /** - * @param string|resource $content - * - * @return string - */ - private function guessContentType($content) + private function guessContentType(mixed $content): bool|string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); diff --git a/src/Gaufrette/Adapter/AwsS3.php b/src/Gaufrette/Adapter/AwsS3.php index 2d335a1c6..185446fd7 100644 --- a/src/Gaufrette/Adapter/AwsS3.php +++ b/src/Gaufrette/Adapter/AwsS3.php @@ -9,31 +9,23 @@ /** * Amazon S3 adapter using the AWS SDK for PHP v2.x. * - * @author Michael Dowling + * @author Michael Dowling */ class AwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider { - /** @var S3Client */ - protected $service; - /** @var string */ - protected $bucket; - /** @var array */ - protected $options; - /** @var bool */ - protected $bucketExists; - /** @var array */ - protected $metadata = []; - /** @var bool */ - protected $detectContentType; - - /** - * @param S3Client $service - * @param string $bucket - * @param array $options - * @param bool $detectContentType - */ - public function __construct(S3Client $service, $bucket, array $options = [], $detectContentType = false) - { + protected S3Client $service; + protected string $bucket; + protected array $options; + protected bool $bucketExists; + protected array $metadata = []; + protected bool $detectContentType; + + public function __construct( + S3Client $service, + string $bucket, + array $options = [], + bool $detectContentType = false + ) { if (!class_exists(S3Client::class)) { throw new \LogicException('You need to install package "aws/aws-sdk-php" to use this adapter'); } @@ -47,14 +39,13 @@ public function __construct(S3Client $service, $bucket, array $options = [], $de ], $options ); - $this->detectContentType = $detectContentType; } /** * {@inheritdoc} */ - public function setMetadata($key, $metadata) + public function setMetadata(string $key, array $metadata): void { // BC with AmazonS3 adapter if (isset($metadata['contentType'])) { @@ -68,7 +59,7 @@ public function setMetadata($key, $metadata) /** * {@inheritdoc} */ - public function getMetadata($key) + public function getMetadata(string $key): array { return $this->metadata[$key] ?? []; } @@ -76,7 +67,7 @@ public function getMetadata($key) /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { $this->ensureBucketExists(); $options = $this->getOptions($key); @@ -100,7 +91,7 @@ public function read($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->ensureBucketExists(); $options = $this->getOptions( @@ -120,7 +111,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->ensureBucketExists(); $options = $this->getOptions($key, ['Body' => $content]); @@ -149,7 +140,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return $this->service->doesObjectExist($this->bucket, $this->computePath($key)); } @@ -157,7 +148,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { try { $result = $this->service->headObject($this->getOptions($key)); @@ -171,7 +162,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function size($key) + public function size(string $key): int { try { $result = $this->service->headObject($this->getOptions($key)); @@ -182,10 +173,21 @@ public function size($key) } } + public function mimeType(string $key): bool|string + { + try { + $result = $this->service->headObject($this->getOptions($key)); + + return ($result['ContentType']); + } catch (\Exception $e) { + return false; + } + } + /** * {@inheritdoc} */ - public function keys() + public function keys(): array { return $this->listKeys(); } @@ -193,7 +195,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $this->ensureBucketExists(); @@ -216,7 +218,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { try { $this->service->deleteObject($this->getOptions($key)); @@ -230,7 +232,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $result = $this->service->listObjects([ 'Bucket' => $this->bucket, @@ -255,7 +257,7 @@ public function isDirectory($key) * @throws \RuntimeException if the bucket does not exists or could not be * created */ - protected function ensureBucketExists() + protected function ensureBucketExists(): bool { if ($this->bucketExists) { return true; @@ -281,7 +283,7 @@ protected function ensureBucketExists() return true; } - protected function getOptions($key, array $options = []) + protected function getOptions(string $key, array $options = []): array { $options['ACL'] = $this->options['acl']; $options['Bucket'] = $this->bucket; @@ -296,7 +298,7 @@ protected function getOptions($key, array $options = []) return $options; } - protected function computePath($key) + protected function computePath(string $key): string { if (empty($this->options['directory'])) { return $key; @@ -307,22 +309,13 @@ protected function computePath($key) /** * Computes the key from the specified path. - * - * @param string $path - * - * return string */ - protected function computeKey($path) + protected function computeKey(string $path): string { return ltrim(substr($path, strlen($this->options['directory'])), '/'); } - /** - * @param string $content - * - * @return string - */ - private function guessContentType($content) + private function guessContentType(mixed $content): bool|string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); @@ -332,15 +325,4 @@ private function guessContentType($content) return $fileInfo->buffer($content); } - - public function mimeType($key) - { - try { - $result = $this->service->headObject($this->getOptions($key)); - - return ($result['ContentType']); - } catch (\Exception $e) { - return false; - } - } } diff --git a/src/Gaufrette/Adapter/AzureBlobStorage.php b/src/Gaufrette/Adapter/AzureBlobStorage.php index c153f9c01..ce3daf01d 100644 --- a/src/Gaufrette/Adapter/AzureBlobStorage.php +++ b/src/Gaufrette/Adapter/AzureBlobStorage.php @@ -5,6 +5,7 @@ use Gaufrette\Adapter; use Gaufrette\Util; use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface; +use MicrosoftAzure\Storage\Blob\Internal\IBlob; use MicrosoftAzure\Storage\Blob\Models\Blob; use MicrosoftAzure\Storage\Blob\Models\BlobServiceOptions; use MicrosoftAzure\Storage\Blob\Models\Container; @@ -28,49 +29,18 @@ class AzureBlobStorage implements Adapter, MetadataSupporter, SizeCalculator, Ch const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; - /** - * @var AzureBlobStorage\BlobProxyFactoryInterface - */ - protected $blobProxyFactory; + protected ?IBlob $blobProxy = null; - /** - * @var string - */ - protected $containerName; + protected bool $multiContainerMode = false; - /** - * @var bool - */ - protected $detectContentType; + protected CreateContainerOptions $createContainerOptions; - /** - * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob - */ - protected $blobProxy; - - /** - * @var bool - */ - protected $multiContainerMode = false; - - /** - * @var CreateContainerOptions - */ - protected $createContainerOptions; - - /** - * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory - * @param string|null $containerName - * @param bool $create - * @param bool $detectContentType - * - * @throws \RuntimeException - */ - public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) - { - $this->blobProxyFactory = $blobProxyFactory; - $this->containerName = $containerName; - $this->detectContentType = $detectContentType; + public function __construct( + private BlobProxyFactoryInterface $blobProxyFactory, + private ?string $containerName = null, + bool $create = false, + private bool $detectContentType = true + ) { if (null === $containerName) { $this->multiContainerMode = true; } elseif ($create) { @@ -78,18 +48,12 @@ public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $contai } } - /** - * @return CreateContainerOptions - */ - public function getCreateContainerOptions() + public function getCreateContainerOptions(): CreateContainerOptions { return $this->createContainerOptions; } - /** - * @param CreateContainerOptions $options - */ - public function setCreateContainerOptions(CreateContainerOptions $options) + public function setCreateContainerOptions(CreateContainerOptions $options): void { $this->createContainerOptions = $options; } @@ -97,12 +61,9 @@ public function setCreateContainerOptions(CreateContainerOptions $options) /** * Creates a new container. * - * @param string $containerName - * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options - * * @throws \RuntimeException if cannot create the container */ - public function createContainer($containerName, CreateContainerOptions $options = null) + public function createContainer(string $containerName, CreateContainerOptions $options = null): void { $this->init(); @@ -129,12 +90,9 @@ public function createContainer($containerName, CreateContainerOptions $options /** * Deletes a container. * - * @param string $containerName - * @param BlobServiceOptions $options - * * @throws \RuntimeException if cannot delete the container */ - public function deleteContainer($containerName, BlobServiceOptions $options = null) + public function deleteContainer(string $containerName, BlobServiceOptions $options = null): void { $this->init(); @@ -156,10 +114,8 @@ public function deleteContainer($containerName, BlobServiceOptions $options = nu /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function read($key) + public function read(string $key): string|bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -177,10 +133,8 @@ public function read($key) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -220,10 +174,8 @@ public function write($key, $content) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function exists($key) + public function exists(string $key): bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -260,9 +212,8 @@ public function exists($key) /** * {@inheritdoc} - * @throws \RuntimeException */ - public function keys() + public function keys(): array { $this->init(); @@ -296,10 +247,8 @@ function (Container $container) { /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function mtime($key) + public function mtime(string $key): int|bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -318,7 +267,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function size($key) + public function size(string $key): int { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -337,7 +286,7 @@ public function size($key) /** * {@inheritdoc} */ - public function mimeType($key) + public function mimeType(string $key): string|bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -356,7 +305,7 @@ public function mimeType($key) /** * {@inheritdoc} */ - public function checksum($key) + public function checksum(string $key): string|bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -375,10 +324,8 @@ public function checksum($key) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function delete($key) + public function delete(string $key): bool { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -396,10 +343,8 @@ public function delete($key) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->init(); @@ -424,7 +369,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { // Windows Azure Blob Storage does not support directories return false; @@ -432,10 +377,8 @@ public function isDirectory($key) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function setMetadata($key, $content) + public function setMetadata(string $key, array $content): void { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -457,10 +400,8 @@ public function setMetadata($key, $content) /** * {@inheritdoc} - * @throws \RuntimeException - * @throws \InvalidArgumentException */ - public function getMetadata($key) + public function getMetadata(string $key): array { $this->init(); list($containerName, $key) = $this->tokenizeKey($key); @@ -485,7 +426,7 @@ public function getMetadata($key) /** * Lazy initialization, automatically called when some method is called after construction. */ - protected function init() + protected function init(): void { if ($this->blobProxy === null) { $this->blobProxy = $this->blobProxyFactory->create(); @@ -495,13 +436,9 @@ protected function init() /** * Throws a runtime exception if a give ServiceException derived from a "container not found" error. * - * @param ServiceException $exception - * @param string $action - * @param string $containerName - * * @throws \RuntimeException */ - protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) + protected function failIfContainerNotFound(ServiceException $exception, string $action, string $containerName): void { $errorCode = $this->getErrorCodeFromServiceException($exception); @@ -516,12 +453,8 @@ protected function failIfContainerNotFound(ServiceException $exception, $action, /** * Extracts the error code from a service exception. - * - * @param ServiceException $exception - * - * @return string */ - protected function getErrorCodeFromServiceException(ServiceException $exception) + protected function getErrorCodeFromServiceException(ServiceException $exception): string { $xml = @simplexml_load_string($exception->getResponse()->getBody()); @@ -534,10 +467,8 @@ protected function getErrorCodeFromServiceException(ServiceException $exception) /** * @param string|resource $content - * - * @return string */ - private function guessContentType($content) + private function guessContentType($content): string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); @@ -549,12 +480,9 @@ private function guessContentType($content) } /** - * @param string $key - * - * @return array - * @throws \InvalidArgumentException + * @return array{string, string} */ - private function tokenizeKey($key) + private function tokenizeKey(string $key): array { $containerName = $this->containerName; if (false === $this->multiContainerMode) { @@ -574,12 +502,9 @@ private function tokenizeKey($key) } /** - * @param string $containerName - * @param null $prefix - * - * @return array + * @return array */ - private function fetchBlobs($containerName, $prefix = null) + private function fetchBlobs(string $containerName, ?string $prefix = null): array { $blobList = $this->blobProxy->listBlobs($containerName); diff --git a/src/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php b/src/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php index 441efae4e..e22e3bf2e 100644 --- a/src/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php +++ b/src/Gaufrette/Adapter/AzureBlobStorage/BlobProxyFactory.php @@ -12,15 +12,9 @@ */ class BlobProxyFactory implements BlobProxyFactoryInterface { - /** - * @var string - */ - protected $connectionString; + protected string $connectionString; - /** - * @param string $connectionString - */ - public function __construct($connectionString) + public function __construct(string $connectionString) { if (!class_exists(ServicesBuilder::class) && !class_exists(BlobRestProxy::class)) { throw new \LogicException('You need to install package "microsoft/azure-storage-blob" to use this adapter'); @@ -28,10 +22,7 @@ public function __construct($connectionString) $this->connectionString = $connectionString; } - /** - * {@inheritdoc} - */ - public function create() + public function create(): BlobRestProxy { if (class_exists(ServicesBuilder::class)) { // for microsoft/azure-storage < 1.0 diff --git a/src/Gaufrette/Adapter/ChecksumCalculator.php b/src/Gaufrette/Adapter/ChecksumCalculator.php index 46ca2b7db..177ac18d5 100644 --- a/src/Gaufrette/Adapter/ChecksumCalculator.php +++ b/src/Gaufrette/Adapter/ChecksumCalculator.php @@ -11,10 +11,6 @@ interface ChecksumCalculator { /** * Returns the checksum of the specified key. - * - * @param string $key - * - * @return string */ - public function checksum($key); + public function checksum(string $key): string|bool; } diff --git a/src/Gaufrette/Adapter/DoctrineDbal.php b/src/Gaufrette/Adapter/DoctrineDbal.php index 77066d2fb..467639d06 100644 --- a/src/Gaufrette/Adapter/DoctrineDbal.php +++ b/src/Gaufrette/Adapter/DoctrineDbal.php @@ -16,9 +16,7 @@ */ class DoctrineDbal implements Adapter, ChecksumCalculator, ListKeysAware { - protected $connection; - protected $table; - protected $columns = [ + protected array $columns = [ 'key' => 'key', 'content' => 'content', 'mtime' => 'mtime', @@ -30,21 +28,22 @@ class DoctrineDbal implements Adapter, ChecksumCalculator, ListKeysAware * @param string $table The files table * @param array $columns The column names */ - public function __construct(Connection $connection, $table, array $columns = []) - { + public function __construct( + private Connection $connection, + private string $table, + array $columns = [] + ) { if (!class_exists(Connection::class)) { throw new \LogicException('You need to install package "doctrine/dbal" to use this adapter'); } - $this->connection = $connection; - $this->table = $table; $this->columns = array_replace($this->columns, $columns); } /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = []; $stmt = $this->connection->executeQuery(sprintf( @@ -65,7 +64,7 @@ public function keys() /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { return (boolean) $this->connection->update( $this->table, @@ -77,7 +76,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { return $this->getColumnValue($key, 'mtime'); } @@ -85,7 +84,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function checksum($key) + public function checksum(string $key): string|bool { return $this->getColumnValue($key, 'checksum'); } @@ -93,7 +92,7 @@ public function checksum($key) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { $method = 'fetchOne'; // dbal 3.x if (!method_exists(Connection::class, $method)) { @@ -114,7 +113,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { return $this->getColumnValue($key, 'content'); } @@ -122,7 +121,7 @@ public function read($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { return (boolean) $this->connection->delete( $this->table, @@ -133,7 +132,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $values = [ $this->getQuotedColumn('content') => $content, @@ -158,12 +157,15 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { return false; } - private function getColumnValue($key, $column) + /** + * @return mixed + */ + private function getColumnValue(string $key, string $column) { $method = 'fetchOne'; // dbal 3.x if (!method_exists(Connection::class, $method)) { @@ -186,7 +188,7 @@ private function getColumnValue($key, $column) /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $prefix = trim($prefix); @@ -216,12 +218,12 @@ function ($value) { ]; } - private function getQuotedTable() + private function getQuotedTable(): string { return $this->connection->quoteIdentifier($this->table); } - private function getQuotedColumn($column) + private function getQuotedColumn(string $column) { return $this->connection->quoteIdentifier($this->columns[$column]); } diff --git a/src/Gaufrette/Adapter/FileFactory.php b/src/Gaufrette/Adapter/FileFactory.php index f71be89f0..19faba180 100644 --- a/src/Gaufrette/Adapter/FileFactory.php +++ b/src/Gaufrette/Adapter/FileFactory.php @@ -14,11 +14,6 @@ interface FileFactory { /** * Creates a new File instance and returns it. - * - * @param string $key - * @param Filesystem $filesystem - * - * @return File */ - public function createFile($key, Filesystem $filesystem); + public function createFile(string $key, Filesystem $filesystem): File; } diff --git a/src/Gaufrette/Adapter/Flysystem.php b/src/Gaufrette/Adapter/Flysystem.php index b4038e5ce..c3a231144 100644 --- a/src/Gaufrette/Adapter/Flysystem.php +++ b/src/Gaufrette/Adapter/Flysystem.php @@ -6,37 +6,28 @@ use Gaufrette\Exception\UnsupportedAdapterMethodException; use League\Flysystem\AdapterInterface; use League\Flysystem\Util; +use League\Flysystem\Config; class Flysystem implements Adapter, ListKeysAware { - /** - * @var AdapterInterface - */ - private $adapter; - - /** - * @var Config - */ - private $config; + private Config $config; /** - * @param AdapterInterface $adapter - * @param \League\Flysystem\Config|array|null $config + * @param Config|array|null $config */ - public function __construct(AdapterInterface $adapter, $config = null) + public function __construct(private AdapterInterface $adapter, $config = null) { if (!interface_exists(AdapterInterface::class)) { throw new \LogicException('You need to install package "league/flysystem" to use this adapter'); } - $this->adapter = $adapter; $this->config = Util::ensureConfig($config); } /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { return $this->adapter->read($key)['contents']; } @@ -44,15 +35,25 @@ public function read($key) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { - return $this->adapter->write($key, $content, $this->config); + $metadata = $this->adapter->write($key, $content, $this->config); + + if (false === $metadata) { + return false; + } + + if (is_int($metadata['size'] ?? 0)) { + return $metadata['size'] ?? 0; + } + + return 0; } /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return (bool) $this->adapter->has($key); } @@ -60,7 +61,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { return array_map(function ($content) { return $content['path']; @@ -70,7 +71,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $dirs = []; $keys = []; @@ -94,7 +95,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { return $this->adapter->getTimestamp($key); } @@ -102,7 +103,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { return $this->adapter->delete($key); } @@ -110,7 +111,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { return $this->adapter->rename($sourceKey, $targetKey); } @@ -118,7 +119,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { throw new UnsupportedAdapterMethodException('isDirectory is not supported by this adapter.'); } diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 8ea3bb141..fdb29bebf 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -15,25 +15,25 @@ class Ftp implements Adapter, FileFactory, ListKeysAware, SizeCalculator { /** @var null|resource|\FTP\Connection */ protected $connection = null; - protected $directory; - protected $host; - protected $port; - protected $username; - protected $password; - protected $passive; - protected $create; - protected $mode; - protected $ssl; - protected $timeout; - protected $fileData = []; - protected $utf8; + protected string $directory; + protected string $host; + protected mixed $port; + protected mixed $username; + protected mixed $password; + protected mixed $passive; + protected mixed $create; + protected mixed $mode; + protected mixed $ssl; + protected mixed $timeout; + protected array $fileData; + protected mixed $utf8; /** * @param string $directory The directory to use in the ftp server * @param string $host The host of the ftp server - * @param array $options The options like port, username, password, passive, create, mode + * @param array $options The options like port, username, password, passive, create, mode */ - public function __construct($directory, $host, $options = []) + public function __construct(string $directory, string $host, array $options = []) { if (!extension_loaded('ftp')) { throw new \RuntimeException('Unable to use Gaufrette\Adapter\Ftp as the FTP extension is not available.'); @@ -52,10 +52,7 @@ public function __construct($directory, $host, $options = []) $this->utf8 = $options['utf8'] ?? false; } - /** - * {@inheritdoc} - */ - public function read($key) + public function read(string $key): string|bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -72,10 +69,7 @@ public function read($key) return $contents; } - /** - * {@inheritdoc} - */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -99,10 +93,7 @@ public function write($key, $content) return $size; } - /** - * {@inheritdoc} - */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -114,10 +105,7 @@ public function rename($sourceKey, $targetKey) return ftp_rename($this->getConnection(), $sourcePath, $targetPath); } - /** - * {@inheritdoc} - */ - public function exists($key) + public function exists(string $key): bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -141,7 +129,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $this->ensureDirectoryExists($this->directory, $this->create); @@ -150,10 +138,7 @@ public function keys() return $keys['keys']; } - /** - * {@inheritdoc} - */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $this->ensureDirectoryExists($this->directory, $this->create); @@ -179,10 +164,7 @@ public function listKeys($prefix = '') return $filteredKeys; } - /** - * {@inheritdoc} - */ - public function mtime($key) + public function mtime(string $key): int|bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -196,10 +178,7 @@ public function mtime($key) return $mtime; } - /** - * {@inheritdoc} - */ - public function delete($key) + public function delete(string $key): bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -210,10 +189,7 @@ public function delete($key) return ftp_delete($this->getConnection(), $this->computePath($key)); } - /** - * {@inheritdoc} - */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $this->ensureDirectoryExists($this->directory, $this->create); @@ -228,7 +204,7 @@ public function isDirectory($key) * * @return array An array of keys and dirs */ - public function listDirectory($directory = '') + public function listDirectory(string $directory = ''): array { $this->ensureDirectoryExists($this->directory, $this->create); @@ -266,10 +242,7 @@ public function listDirectory($directory = '') ]; } - /** - * {@inheritdoc} - */ - public function createFile($key, Filesystem $filesystem) + public function createFile(mixed $key, Filesystem $filesystem): File { $this->ensureDirectoryExists($this->directory, $this->create); @@ -298,7 +271,7 @@ public function createFile($key, Filesystem $filesystem) * * @throws \RuntimeException */ - public function size($key) + public function size(string $key): int { $this->ensureDirectoryExists($this->directory, $this->create); @@ -320,7 +293,7 @@ public function size($key) * @throws RuntimeException if the directory does not exist and could not * be created */ - protected function ensureDirectoryExists($directory, $create = false) + protected function ensureDirectoryExists(string $directory, bool $create = false): void { if (!$this->isDir($directory)) { if (!$create) { @@ -338,7 +311,7 @@ protected function ensureDirectoryExists($directory, $create = false) * * @throws RuntimeException if the directory could not be created */ - protected function createDirectory($directory) + protected function createDirectory(string $directory): void { // create parent directory if needed $parent = \Gaufrette\Util\Path::dirname($directory); @@ -358,7 +331,7 @@ protected function createDirectory($directory) * * @return bool */ - private function isDir($directory) + private function isDir(string $directory): bool { if ('/' === $directory) { return true; @@ -374,7 +347,7 @@ private function isDir($directory) return true; } - private function fetchKeys($directory = '', $onlyKeys = true) + private function fetchKeys(string $directory = '', bool $onlyKeys = true): array { $directory = preg_replace('/^[\/]*([^\/].*)$/', '/$1', $directory); @@ -437,11 +410,11 @@ private function fetchKeys($directory = '', $onlyKeys = true) /** * Parses the given raw list. * - * @param array $rawlist + * @param array $rawlist * - * @return array + * @return array */ - private function parseRawlist(array $rawlist) + private function parseRawlist(array $rawlist): array { $parsed = []; foreach ($rawlist as $line) { @@ -475,20 +448,16 @@ private function parseRawlist(array $rawlist) /** * Computes the path for the given key. - * - * @param string $key */ - private function computePath($key) + private function computePath(string $key): string { return rtrim($this->directory, '/') . '/' . $key; } /** * Indicates whether the adapter has an open ftp connection. - * - * @return bool */ - private function isConnected() + private function isConnected(): bool { if (class_exists('\FTP\Connection')) { return $this->connection instanceof \FTP\Connection; @@ -517,7 +486,7 @@ private function getConnection() * * @throws RuntimeException if could not connect */ - private function connect() + private function connect(): void { if ($this->ssl && !function_exists('ftp_ssl_connect')) { throw new \RuntimeException('This Server Has No SSL-FTP Available.'); @@ -582,14 +551,17 @@ private function connect() /** * Closes the adapter's ftp connection. */ - public function close() + public function close(): void { if ($this->isConnected()) { ftp_close($this->connection); } } - private function isLinuxListing($info) + /** + * @param array|false $info + */ + private function isLinuxListing(bool|array $info): bool { return count($info) >= 9; } diff --git a/src/Gaufrette/Adapter/GoogleCloudStorage.php b/src/Gaufrette/Adapter/GoogleCloudStorage.php index 295dcad14..04f3102bb 100644 --- a/src/Gaufrette/Adapter/GoogleCloudStorage.php +++ b/src/Gaufrette/Adapter/GoogleCloudStorage.php @@ -10,6 +10,8 @@ use Google\Service\Storage\BucketIamConfiguration; use Google\Service\Storage\BucketIamConfigurationUniformBucketLevelAccess; use GuzzleHttp; +use RuntimeException; +use ReflectionException; /** * Google Cloud Storage adapter using the Google APIs Client Library for PHP. @@ -44,9 +46,9 @@ class GoogleCloudStorage implements Adapter, MetadataSupporter, ListKeysAware */ public function __construct( Storage $service, - $bucket, + string $bucket, array $options = [], - $detectContentType = false + bool $detectContentType = false ) { if (!class_exists(Storage::class)) { throw new \LogicException('You need to install package "google/apiclient" to use this adapter'); @@ -63,44 +65,36 @@ public function __construct( } /** - * @return array The actual options + * @return array The actual options */ - public function getOptions() + public function getOptions(): array { return $this->options; } /** - * @param array $options The new options + * @param array $options The new options */ - public function setOptions($options) + public function setOptions(array $options): void { $this->options = array_replace($this->options, $options); } - /** - * @return string The current bucket name - */ - public function getBucket() + public function getBucket(): string { return $this->bucket; } /** * Sets a new bucket name. - * - * @param string $bucket The new bucket name */ - public function setBucket($bucket) + public function setBucket(string $bucket): void { $this->bucketExists = null; $this->bucket = $bucket; } - /** - * {@inheritdoc} - */ - public function read($key) + public function read(string $key): string|bool { $this->ensureBucketExists(); $path = $this->computePath($key); @@ -133,10 +127,7 @@ public function read($key) return false; } - /** - * {@inheritdoc} - */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->ensureBucketExists(); $path = $this->computePath($key); @@ -201,10 +192,7 @@ public function write($key, $content) } } - /** - * {@inheritdoc} - */ - public function exists($key) + public function exists(string $key): bool { $this->ensureBucketExists(); $path = $this->computePath($key); @@ -221,15 +209,12 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { return $this->listKeys(); } - /** - * {@inheritdoc} - */ - public function mtime($key) + public function mtime(string $key): int|bool { $this->ensureBucketExists(); $path = $this->computePath($key); @@ -239,10 +224,7 @@ public function mtime($key) return $object ? strtotime($object->getUpdated()) : false; } - /** - * {@inheritdoc} - */ - public function delete($key) + public function delete(string $key): bool { $this->ensureBucketExists(); $path = $this->computePath($key); @@ -256,10 +238,7 @@ public function delete($key) return true; } - /** - * {@inheritdoc} - */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->ensureBucketExists(); $sourcePath = $this->computePath($sourceKey); @@ -280,10 +259,7 @@ public function rename($sourceKey, $targetKey) return true; } - /** - * {@inheritdoc} - */ - public function isDirectory($key) + public function isDirectory(string $key): bool { if ($this->exists($key . '/')) { return true; @@ -293,9 +269,11 @@ public function isDirectory($key) } /** - * {@inheritdoc} + * @return array + * @throws RuntimeException + * @throws ReflectionException */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $this->ensureBucketExists(); @@ -326,9 +304,9 @@ public function listKeys($prefix = '') } /** - * {@inheritdoc} + * @param array $content */ - public function setMetadata($key, $content) + public function setMetadata(string $key, array $content): void { $path = $this->computePath($key); @@ -336,9 +314,9 @@ public function setMetadata($key, $content) } /** - * {@inheritdoc} + * @return array */ - public function getMetadata($key) + public function getMetadata(string $key): array { $path = $this->computePath($key); @@ -350,7 +328,7 @@ public function getMetadata($key) * * @throws \RuntimeException if the bucket does not exists */ - protected function ensureBucketExists() + protected function ensureBucketExists(): void { if ($this->bucketExists) { return; @@ -405,7 +383,7 @@ protected function ensureBucketExists() } } - protected function computePath($key) + protected function computePath(string $key): string { if (empty($this->options['directory'])) { return $key; @@ -416,11 +394,9 @@ protected function computePath($key) /** * @param string $path - * @param array $options - * - * @return bool|StorageObject + * @param array $options */ - private function getObjectData($path, $options = []) + private function getObjectData(string $path, array $options = []): bool|StorageObject { try { return $this->service->objects->get($this->bucket, $path, $options); @@ -434,7 +410,7 @@ private function getObjectData($path, $options = []) * * @return string */ - private function guessContentType($content) + private function guessContentType(string $content): string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); diff --git a/src/Gaufrette/Adapter/GridFS.php b/src/Gaufrette/Adapter/GridFS.php index de9bee6d0..e71d5cae2 100644 --- a/src/Gaufrette/Adapter/GridFS.php +++ b/src/Gaufrette/Adapter/GridFS.php @@ -16,27 +16,22 @@ */ class GridFS implements Adapter, ChecksumCalculator, MetadataSupporter, ListKeysAware, SizeCalculator { - /** @var array */ - private $metadata = []; - - /** @var Bucket */ - private $bucket; + private array $metadata = []; /** * @param Bucket $bucket */ - public function __construct(Bucket $bucket) + public function __construct(private Bucket $bucket) { if (!class_exists(Bucket::class)) { throw new \LogicException('You need to install package "mongodb/mongodb" to use this adapter'); } - $this->bucket = $bucket; } /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { try { $stream = $this->bucket->openDownloadStreamByName($key); @@ -54,7 +49,7 @@ public function read($key) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $stream = $this->bucket->openUploadStream($key, ['metadata' => $this->getMetadata($key)]); @@ -63,14 +58,12 @@ public function write($key, $content) } finally { fclose($stream); } - - return false; } /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { return false; } @@ -78,7 +71,7 @@ public function isDirectory($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $metadata = $this->getMetadata($sourceKey); $writable = $this->bucket->openUploadStream($targetKey, ['metadata' => $metadata]); @@ -99,7 +92,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return (boolean) $this->bucket->findOne(['filename' => $key]); } @@ -107,7 +100,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = []; $cursor = $this->bucket->find([], ['projection' => ['filename' => 1]]); @@ -122,7 +115,7 @@ public function keys() /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['uploadDate' => 1]]); @@ -132,7 +125,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function checksum($key) + public function checksum(string $key): string|bool { $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['md5' => 1]]); @@ -142,7 +135,7 @@ public function checksum($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { if (null === $file = $this->bucket->findOne(['filename' => $key], ['projection' => ['_id' => 1]])) { return false; @@ -156,7 +149,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function setMetadata($key, $metadata) + public function setMetadata(string $key, array $metadata): void { $this->metadata[$key] = $metadata; } @@ -164,7 +157,7 @@ public function setMetadata($key, $metadata) /** * {@inheritdoc} */ - public function getMetadata($key) + public function getMetadata(string $key): array { if (isset($this->metadata[$key])) { return $this->metadata[$key]; @@ -183,7 +176,7 @@ public function getMetadata($key) /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { $prefix = trim($prefix); @@ -208,7 +201,7 @@ public function listKeys($prefix = '') return $result; } - public function size($key) + public function size(string $key): int { if (!$this->exists($key)) { return false; diff --git a/src/Gaufrette/Adapter/InMemory.php b/src/Gaufrette/Adapter/InMemory.php index b58cdcb42..f87d68924 100644 --- a/src/Gaufrette/Adapter/InMemory.php +++ b/src/Gaufrette/Adapter/InMemory.php @@ -14,10 +14,10 @@ */ class InMemory implements Adapter, MimeTypeProvider { - protected $files = []; + protected array $files = []; /** - * @param array $files An array of files + * @param array $files An array of files */ public function __construct(array $files = []) { @@ -27,9 +27,9 @@ public function __construct(array $files = []) /** * Defines the files. * - * @param array $files An array of files + * @param array $files An array of files */ - public function setFiles(array $files) + public function setFiles(array $files): void { $this->files = []; foreach ($files as $key => $file) { @@ -53,7 +53,7 @@ public function setFiles(array $files) * @param string $content The content * @param int $mtime The last modified time (automatically set to now if NULL) */ - public function setFile($key, $content = null, $mtime = null) + public function setFile(string $key, string $content = null, int $mtime = null): void { if (null === $mtime) { $mtime = time(); @@ -65,18 +65,12 @@ public function setFile($key, $content = null, $mtime = null) ]; } - /** - * {@inheritdoc} - */ - public function read($key) + public function read(string $key): string|bool { return $this->files[$key]['content']; } - /** - * {@inheritdoc} - */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, mixed $targetKey): bool { $content = $this->read($sourceKey); $this->delete($sourceKey); @@ -85,9 +79,9 @@ public function rename($sourceKey, $targetKey) } /** - * {@inheritdoc} + * @param ?array $metadata */ - public function write($key, $content, array $metadata = null) + public function write(string $key, mixed $content, array $metadata = null): int|bool { $this->files[$key]['content'] = $content; $this->files[$key]['mtime'] = time(); @@ -95,34 +89,25 @@ public function write($key, $content, array $metadata = null) return Util\Size::fromContent($content); } - /** - * {@inheritdoc} - */ - public function exists($key) + public function exists(string $key): bool { return array_key_exists($key, $this->files); } /** - * {@inheritdoc} + * @return array */ - public function keys() + public function keys(): array { return array_keys($this->files); } - /** - * {@inheritdoc} - */ - public function mtime($key) + public function mtime(string $key): int|bool { return $this->files[$key]['mtime'] ?? false; } - /** - * {@inheritdoc} - */ - public function delete($key) + public function delete(string $key): bool { unset($this->files[$key]); clearstatcache(); @@ -130,18 +115,12 @@ public function delete($key) return true; } - /** - * {@inheritdoc} - */ - public function isDirectory($path) + public function isDirectory(string $path): bool { return false; } - /** - * {@inheritdoc} - */ - public function mimeType($key) + public function mimeType(string $key): bool|string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); diff --git a/src/Gaufrette/Adapter/ListKeysAware.php b/src/Gaufrette/Adapter/ListKeysAware.php index bea2a3d8d..7b6d559d0 100644 --- a/src/Gaufrette/Adapter/ListKeysAware.php +++ b/src/Gaufrette/Adapter/ListKeysAware.php @@ -12,10 +12,6 @@ interface ListKeysAware /** * Lists keys beginning with pattern given * (no wildcard / regex matching). - * - * @param string $prefix - * - * @return array */ - public function listKeys($prefix = ''); + public function listKeys(string $prefix = ''): array; } diff --git a/src/Gaufrette/Adapter/Local.php b/src/Gaufrette/Adapter/Local.php index 81b253f8a..e56da5f0a 100644 --- a/src/Gaufrette/Adapter/Local.php +++ b/src/Gaufrette/Adapter/Local.php @@ -14,10 +14,9 @@ */ class Local implements Adapter, StreamFactory, ChecksumCalculator, SizeCalculator, MimeTypeProvider { - protected $directory; - private $create; - private $mode; - + protected string $directory; + private bool $create; + private int $mode; /** * @param string $directory Directory where the filesystem is located * @param bool $create Whether to create the directory if it does not @@ -27,8 +26,11 @@ class Local implements Adapter, StreamFactory, ChecksumCalculator, SizeCalculato * @throws \RuntimeException if the specified directory does not exist and * could not be created */ - public function __construct($directory, $create = false, $mode = 0777) - { + public function __construct( + string $directory, + bool $create = false, + int $mode = 0777 + ) { $this->directory = Util\Path::normalize($directory); if (is_link($this->directory)) { @@ -40,13 +42,11 @@ public function __construct($directory, $create = false, $mode = 0777) } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function read($key) + public function read(string $key): string|bool { if ($this->isDirectory($key)) { return false; @@ -56,13 +56,11 @@ public function read($key) } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $path = $this->computePath($key); $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($path), true); @@ -71,13 +69,11 @@ public function write($key, $content) } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $targetPath = $this->computePath($targetKey); $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($targetPath), true); @@ -85,22 +81,18 @@ public function rename($sourceKey, $targetKey) return rename($this->computePath($sourceKey), $targetPath); } - /** - * {@inheritdoc} - */ - public function exists($key) + public function exists(string $key): bool { return is_file($this->computePath($key)); } /** - * {@inheritdoc} - * + * @return array * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function keys() + public function keys(): array { $this->ensureDirectoryExists($this->directory, $this->create); @@ -126,24 +118,20 @@ public function keys() } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function mtime($key) + public function mtime(string $key): int|bool { return filemtime($this->computePath($key)); } /** - * {@inheritdoc} - * * Can also delete a directory recursively when the given $key matches a * directory. */ - public function delete($key) + public function delete(string $key): bool { if ($this->isDirectory($key)) { return $this->deleteDirectory($this->computePath($key)); @@ -155,63 +143,52 @@ public function delete($key) } /** - * @param string $key - * - * @return bool - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function isDirectory($key) + public function isDirectory(string $key): bool { return is_dir($this->computePath($key)); } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function createStream($key) + public function createStream(string $key): Stream\Local { return new Stream\Local($this->computePath($key), $this->mode); } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function checksum($key) + public function checksum(string $key): string|bool { return Util\Checksum::fromFile($this->computePath($key)); } /** - * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function size($key) + public function size(string $key): int { return Util\Size::fromFile($this->computePath($key)); } /** * {@inheritdoc} - * * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function mimeType($key) + public function mimeType(string $key): bool|string { $fileInfo = new \finfo(FILEINFO_MIME_TYPE); @@ -228,7 +205,7 @@ public function mimeType($key) * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - public function computeKey($path) + public function computeKey(string $path): string { $path = $this->normalizePath($path); @@ -246,7 +223,7 @@ public function computeKey($path) * @throws \OutOfBoundsException If the computed path is out of the directory * @throws \RuntimeException If directory does not exists and cannot be created */ - protected function computePath($key) + protected function computePath(string $key): string { $this->ensureDirectoryExists($this->directory, $this->create); @@ -262,7 +239,7 @@ protected function computePath($key) * @throws \OutOfBoundsException If the computed path is out of the * directory */ - protected function normalizePath($path) + protected function normalizePath(string $path): string { $path = Util\Path::normalize($path); @@ -284,7 +261,7 @@ protected function normalizePath($path) * @throws \RuntimeException if the directory does not exists and could not * be created */ - protected function ensureDirectoryExists($directory, $create = false) + protected function ensureDirectoryExists(string $directory, bool $create = false): void { if (!is_dir($directory)) { if (!$create) { @@ -303,7 +280,7 @@ protected function ensureDirectoryExists($directory, $create = false) * @throws \InvalidArgumentException if the directory already exists * @throws \RuntimeException if the directory could not be created */ - protected function createDirectory($directory) + protected function createDirectory(string $directory): void { if (!@mkdir($directory, $this->mode, true) && !is_dir($directory)) { throw new \RuntimeException(sprintf('The directory \'%s\' could not be created.', $directory)); @@ -316,9 +293,9 @@ protected function createDirectory($directory) * @throws \InvalidArgumentException When attempting to delete the root * directory of this adapter. * - * @return bool Wheter the operation succeeded or not + * @return bool Whether the operation succeeded or not */ - private function deleteDirectory($directory) + private function deleteDirectory(string $directory): bool { if ($this->directory === $directory) { throw new \InvalidArgumentException( diff --git a/src/Gaufrette/Adapter/MetadataSupporter.php b/src/Gaufrette/Adapter/MetadataSupporter.php index 16a4daeb4..c40f5f64f 100644 --- a/src/Gaufrette/Adapter/MetadataSupporter.php +++ b/src/Gaufrette/Adapter/MetadataSupporter.php @@ -9,16 +9,7 @@ */ interface MetadataSupporter { - /** - * @param string $key - * @param array $content - */ - public function setMetadata($key, $content); + public function setMetadata(string $key, array $content): void; - /** - * @param string $key - * - * @return array - */ - public function getMetadata($key); + public function getMetadata(string $key): array; } diff --git a/src/Gaufrette/Adapter/MimeTypeProvider.php b/src/Gaufrette/Adapter/MimeTypeProvider.php index 16b1fc88a..863e9a75e 100644 --- a/src/Gaufrette/Adapter/MimeTypeProvider.php +++ b/src/Gaufrette/Adapter/MimeTypeProvider.php @@ -10,11 +10,7 @@ interface MimeTypeProvider { /** - * Returns the mime type of the specified key. - * - * @param string $key - * - * @return string + * @return false|string the mime type of the specified key. */ - public function mimeType($key); + public function mimeType(string $key): string|bool; } diff --git a/src/Gaufrette/Adapter/PhpseclibSftp.php b/src/Gaufrette/Adapter/PhpseclibSftp.php index 2e71d625c..14e87cfe2 100644 --- a/src/Gaufrette/Adapter/PhpseclibSftp.php +++ b/src/Gaufrette/Adapter/PhpseclibSftp.php @@ -9,31 +9,27 @@ class PhpseclibSftp implements Adapter, FileFactory, ListKeysAware { - protected $sftp; - protected $directory; - protected $create; - protected $initialized = false; + protected bool $initialized = false; /** - * @param SecLibSFTP $sftp An Sftp instance * @param string $directory The distant directory * @param bool $create Whether to create the remote directory if it * does not exist */ - public function __construct(SecLibSFTP $sftp, $directory = null, $create = false) - { + public function __construct( + private SecLibSFTP $sftp, + private ?string $directory = null, + private bool $create = false + ) { if (!class_exists(SecLibSFTP::class)) { throw new \LogicException('You need to install package "phpseclib/phpseclib" to use this adapter'); } - $this->sftp = $sftp; - $this->directory = $directory; - $this->create = $create; } /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { return $this->sftp->get($this->computePath($key)); } @@ -41,7 +37,7 @@ public function read($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { $this->initialize(); @@ -56,7 +52,7 @@ public function rename($sourceKey, $targetKey) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { $this->initialize(); @@ -72,7 +68,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { $this->initialize(); @@ -82,7 +78,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { $this->initialize(); @@ -99,7 +95,7 @@ public function isDirectory($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = $this->fetchKeys(); @@ -109,7 +105,7 @@ public function keys() /** * {@inheritdoc} */ - public function listKeys($prefix = '') + public function listKeys(string $prefix = ''): array { preg_match('/(.*?)[^\/]*$/', $prefix, $match); $directory = rtrim($match[1], '/'); @@ -136,7 +132,7 @@ public function listKeys($prefix = '') /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { $this->initialize(); @@ -148,7 +144,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { return $this->sftp->delete($this->computePath($key), false); } @@ -156,7 +152,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function createFile($key, Filesystem $filesystem) + public function createFile(string $key, Filesystem $filesystem): File { $file = new File($key, $filesystem); @@ -173,7 +169,7 @@ public function createFile($key, Filesystem $filesystem) * * It will ensure the root directory exists */ - protected function initialize() + protected function initialize(): void { if ($this->initialized) { return; @@ -184,7 +180,7 @@ protected function initialize() $this->initialized = true; } - protected function ensureDirectoryExists($directory, $create) + protected function ensureDirectoryExists(string $directory, bool $create): void { $pwd = $this->sftp->pwd(); if ($this->sftp->chdir($directory)) { @@ -198,12 +194,15 @@ protected function ensureDirectoryExists($directory, $create) } } - protected function computePath($key) + protected function computePath(string $key): string { return $this->directory . '/' . ltrim($key, '/'); } - protected function fetchKeys($directory = '', $onlyKeys = true) + /** + * @return array> + */ + protected function fetchKeys(string $directory = '', bool $onlyKeys = true): array { $keys = ['keys' => [], 'dirs' => []]; $computedPath = $this->computePath($directory); diff --git a/src/Gaufrette/Adapter/SafeLocal.php b/src/Gaufrette/Adapter/SafeLocal.php index 9c3dc6570..d709e36b6 100644 --- a/src/Gaufrette/Adapter/SafeLocal.php +++ b/src/Gaufrette/Adapter/SafeLocal.php @@ -10,18 +10,12 @@ */ class SafeLocal extends Local { - /** - * {@inheritdoc} - */ - public function computeKey($path) + public function computeKey(string $path): string { return base64_decode(parent::computeKey($path)); } - /** - * {@inheritdoc} - */ - protected function computePath($key) + protected function computePath(string $key): string { return parent::computePath(base64_encode($key)); } diff --git a/src/Gaufrette/Adapter/SizeCalculator.php b/src/Gaufrette/Adapter/SizeCalculator.php index 512537a5b..fa1e08100 100644 --- a/src/Gaufrette/Adapter/SizeCalculator.php +++ b/src/Gaufrette/Adapter/SizeCalculator.php @@ -11,10 +11,6 @@ interface SizeCalculator { /** * Returns the size of the specified key. - * - * @param string $key - * - * @return int */ - public function size($key); + public function size(string $key): int; } diff --git a/src/Gaufrette/Adapter/StreamFactory.php b/src/Gaufrette/Adapter/StreamFactory.php index bbf8f21bd..85ce4e6f8 100644 --- a/src/Gaufrette/Adapter/StreamFactory.php +++ b/src/Gaufrette/Adapter/StreamFactory.php @@ -2,6 +2,8 @@ namespace Gaufrette\Adapter; +use Gaufrette\Stream; + /** * Interface for the stream creation class. * @@ -11,10 +13,6 @@ interface StreamFactory { /** * Creates a new stream instance of the specified file. - * - * @param string $key - * - * @return \Gaufrette\Stream */ - public function createStream($key); + public function createStream(string $key): Stream; } diff --git a/src/Gaufrette/Adapter/Zip.php b/src/Gaufrette/Adapter/Zip.php index 4f261f261..4a45a1c43 100644 --- a/src/Gaufrette/Adapter/Zip.php +++ b/src/Gaufrette/Adapter/Zip.php @@ -14,30 +14,21 @@ */ class Zip implements Adapter { - /** - * @var string The zip archive full path - */ - protected $zipFile; + protected ZipArchive $zipArchive; - /** - * @var ZipArchive - */ - protected $zipArchive; - - public function __construct($zipFile) + public function __construct(private string $zipFile) { if (!extension_loaded('zip')) { throw new \RuntimeException(sprintf('Unable to use %s as the ZIP extension is not available.', __CLASS__)); } - $this->zipFile = $zipFile; $this->reinitZipArchive(); } /** * {@inheritdoc} */ - public function read($key) + public function read(string $key): string|bool { if (false === ($content = $this->zipArchive->getFromName($key, 0))) { return false; @@ -49,7 +40,7 @@ public function read($key) /** * {@inheritdoc} */ - public function write($key, $content) + public function write(string $key, mixed $content): int|bool { if (!$this->zipArchive->addFromString($key, $content)) { return false; @@ -65,7 +56,7 @@ public function write($key, $content) /** * {@inheritdoc} */ - public function exists($key) + public function exists(string $key): bool { return (boolean) $this->getStat($key); } @@ -73,7 +64,7 @@ public function exists($key) /** * {@inheritdoc} */ - public function keys() + public function keys(): array { $keys = []; @@ -89,7 +80,7 @@ public function keys() * * {@inheritdoc} */ - public function isDirectory($key) + public function isDirectory(string $key): bool { return false; } @@ -97,7 +88,7 @@ public function isDirectory($key) /** * {@inheritdoc} */ - public function mtime($key) + public function mtime(string $key): int|bool { $stat = $this->getStat($key); @@ -107,7 +98,7 @@ public function mtime($key) /** * {@inheritdoc} */ - public function delete($key) + public function delete(string $key): bool { if (!$this->zipArchive->deleteName($key)) { return false; @@ -119,7 +110,7 @@ public function delete($key) /** * {@inheritdoc} */ - public function rename($sourceKey, $targetKey) + public function rename(string $sourceKey, string $targetKey): bool { if (!$this->zipArchive->renameName($sourceKey, $targetKey)) { return false; @@ -131,12 +122,8 @@ public function rename($sourceKey, $targetKey) /** * Returns the stat of a file in the zip archive * (name, index, crc, mtime, compression size, compression method, filesize). - * - * @param $key - * - * @return array|bool */ - public function getStat($key) + public function getStat(string $key): array { $stat = $this->zipArchive->statName($key); if (false === $stat) { @@ -148,7 +135,7 @@ public function getStat($key) public function __destruct() { - if ($this->zipArchive) { + if (isset($this->zipArchive)) { try { $this->zipArchive->close(); } catch (\Exception $e) { @@ -157,7 +144,7 @@ public function __destruct() } } - protected function reinitZipArchive() + protected function reinitZipArchive(): self { $this->zipArchive = new ZipArchive(); @@ -216,7 +203,7 @@ protected function reinitZipArchive() * * @throws \RuntimeException If file could not be saved */ - protected function save() + protected function save(): bool { // Close to save modification if (!$this->zipArchive->close()) { diff --git a/src/Gaufrette/FilesystemInterface.php b/src/Gaufrette/FilesystemInterface.php index 9a686ab8f..57d16dd76 100644 --- a/src/Gaufrette/FilesystemInterface.php +++ b/src/Gaufrette/FilesystemInterface.php @@ -167,7 +167,7 @@ public function createFile($key); * * @param string $key * - * @return string + * @return string|false * * @throws \InvalidArgumentException If $key is invalid */ diff --git a/src/Gaufrette/FilesystemMap.php b/src/Gaufrette/FilesystemMap.php index 2a700dcaa..0eddd6a32 100644 --- a/src/Gaufrette/FilesystemMap.php +++ b/src/Gaufrette/FilesystemMap.php @@ -9,15 +9,18 @@ */ class FilesystemMap implements FilesystemMapInterface { - private $filesystems = []; + /** + * @var array + */ + private array $filesystems = []; /** * Returns an array of all the registered filesystems where the key is the * name and the value the filesystem. * - * @return array + * @return array */ - public function all() + public function all(): array { return $this->filesystems; } @@ -25,13 +28,10 @@ public function all() /** * Register the given filesystem for the specified name. * - * @param string $name - * @param FilesystemInterface $filesystem - * * @throws \InvalidArgumentException when the specified name contains - * forbidden characters + * forbidden characters */ - public function set($name, FilesystemInterface $filesystem) + public function set(string $name, FilesystemInterface $filesystem): void { if (!preg_match('/^[-_a-zA-Z0-9]+$/', $name)) { throw new \InvalidArgumentException(sprintf( @@ -43,18 +43,12 @@ public function set($name, FilesystemInterface $filesystem) $this->filesystems[$name] = $filesystem; } - /** - * {@inheritdoc} - */ - public function has($name) + public function has(string $name): bool { return isset($this->filesystems[$name]); } - /** - * {@inheritdoc} - */ - public function get($name) + public function get(string $name): FilesystemInterface { if (!$this->has($name)) { throw new \InvalidArgumentException(sprintf( @@ -68,10 +62,8 @@ public function get($name) /** * Removes the filesystem registered for the specified name. - * - * @param string $name */ - public function remove($name) + public function remove(string $name): void { if (!$this->has($name)) { throw new \InvalidArgumentException(sprintf( @@ -86,7 +78,7 @@ public function remove($name) /** * Clears all the registered filesystems. */ - public function clear() + public function clear(): void { $this->filesystems = []; } diff --git a/src/Gaufrette/FilesystemMapInterface.php b/src/Gaufrette/FilesystemMapInterface.php index b2ced3bea..970ca5a9c 100644 --- a/src/Gaufrette/FilesystemMapInterface.php +++ b/src/Gaufrette/FilesystemMapInterface.php @@ -10,22 +10,14 @@ interface FilesystemMapInterface /** * Indicates whether there is a filesystem registered for the specified * name. - * - * @param string $name - * - * @return bool */ - public function has($name); + public function has(string $name): bool; /** * Returns the filesystem registered for the specified name. * - * @param string $name - * - * @return FilesystemInterface - * * @throw \InvalidArgumentException when there is no filesystem registered - * for the specified name + * for the specified name */ - public function get($name); + public function get(string $name): FilesystemInterface; } diff --git a/src/Gaufrette/Stream.php b/src/Gaufrette/Stream.php index 7f640b80d..9d99e140d 100644 --- a/src/Gaufrette/Stream.php +++ b/src/Gaufrette/Stream.php @@ -16,7 +16,7 @@ interface Stream * * @return bool TRUE on success or FALSE on failure */ - public function open(StreamMode $mode); + public function open(StreamMode $mode): bool; /** * Reads the specified number of bytes from the current position. @@ -25,10 +25,8 @@ public function open(StreamMode $mode); * string. * * @param int $count The number of bytes - * - * @return string */ - public function read($count); + public function read(int $count): string|bool; /** * Writes the specified data. @@ -36,11 +34,9 @@ public function read($count); * Don't forget to update the current position of the stream by number of * bytes that were successfully written. * - * @param string $data - * * @return int The number of bytes that were successfully written */ - public function write($data); + public function write(string $data): int; /** * Closes the stream. @@ -48,7 +44,7 @@ public function write($data); * It must free all the resources. If there is any data to flush, you * should do so */ - public function close(); + public function close(): void; /** * Flushes the output. @@ -58,52 +54,45 @@ public function close(); * * @return bool TRUE on success or FALSE on failure */ - public function flush(); + public function flush(): bool; /** * Seeks to the specified offset. - * - * @param int $offset - * @param int $whence - * - * @return bool */ - public function seek($offset, $whence = SEEK_SET); + /** + * @param SEEK_SET|SEEK_CUR|SEEK_END $whence + */ + public function seek(int $offset, int $whence = SEEK_SET): bool; /** * Returns the current position. - * - * @return int */ - public function tell(); + public function tell(): int; /** * Indicates whether the current position is the end-of-file. - * - * @return bool */ - public function eof(); + public function eof(): bool; /** * Gathers statistics of the stream. * - * @return array + * @return array|false */ - public function stat(); + public function stat(): array|bool; /** * Retrieve the underlying resource. * - * @param int $castAs - * - * @return mixed using resource or false + * @param STREAM_CAST_FOR_SELECT|STREAM_CAST_AS_STREAM $castAs + * @return resource|false using resource or false */ - public function cast($castAs); + public function cast(int $castAs); /** * Delete a file. * * @return bool TRUE on success FALSE otherwise */ - public function unlink(); + public function unlink(): bool; } diff --git a/src/Gaufrette/Stream/InMemoryBuffer.php b/src/Gaufrette/Stream/InMemoryBuffer.php index 30ac9be62..7d74c70e6 100644 --- a/src/Gaufrette/Stream/InMemoryBuffer.php +++ b/src/Gaufrette/Stream/InMemoryBuffer.php @@ -9,28 +9,25 @@ class InMemoryBuffer implements Stream { - private $filesystem; - private $key; - private $mode; - private $content; - private $numBytes; - private $position; - private $synchronized; + private Filesystem $filesystem; + private string $key; + private ?StreamMode $mode = null; + private string $content; + private int $numBytes; + private int $position; + private bool $synchronized = false; /** * @param Filesystem $filesystem The filesystem managing the file to stream * @param string $key The file key */ - public function __construct(Filesystem $filesystem, $key) + public function __construct(Filesystem $filesystem, string $key) { $this->filesystem = $filesystem; $this->key = $key; } - /** - * {@inheritdoc} - */ - public function open(StreamMode $mode) + public function open(StreamMode $mode): bool { $this->mode = $mode; @@ -57,7 +54,7 @@ public function open(StreamMode $mode) return true; } - public function read($count) + public function read(int $count): string|bool { if (false === $this->mode->allowsRead()) { throw new \LogicException('The stream does not allow read.'); @@ -69,7 +66,7 @@ public function read($count) return $chunk; } - public function write($data) + public function write(string $data): int { if (false === $this->mode->allowsWrite()) { throw new \LogicException('The stream does not allow write.'); @@ -99,14 +96,14 @@ public function write($data) return $numWrittenBytes; } - public function close() + public function close(): void { if (!$this->synchronized) { $this->flush(); } } - public function seek($offset, $whence = SEEK_SET) + public function seek(int $offset, int $whence = SEEK_SET): bool { switch ($whence) { case SEEK_SET: @@ -128,12 +125,12 @@ public function seek($offset, $whence = SEEK_SET) return true; } - public function tell() + public function tell(): int { return $this->position; } - public function flush() + public function flush(): bool { if ($this->synchronized) { return true; @@ -148,15 +145,15 @@ public function flush() return true; } - public function eof() + public function eof(): bool { return $this->position >= $this->numBytes; } /** - * {@inheritdoc} + * @return array|false */ - public function stat() + public function stat(): array|bool { if ($this->filesystem->has($this->key)) { $isDirectory = $this->filesystem->isDirectory($this->key); @@ -184,18 +181,12 @@ public function stat() return false; } - /** - * {@inheritdoc} - */ - public function cast($castAst) + public function cast(int $castAs): bool { return false; } - /** - * {@inheritdoc} - */ - public function unlink() + public function unlink(): bool { if ($this->mode && $this->mode->impliesExistingContentDeletion()) { return $this->filesystem->delete($this->key); @@ -204,10 +195,7 @@ public function unlink() return false; } - /** - * @return bool - */ - protected function hasNewContentAtFurtherPosition() + protected function hasNewContentAtFurtherPosition(): bool { return $this->position > 0 && !$this->content; } @@ -215,10 +203,8 @@ protected function hasNewContentAtFurtherPosition() /** * @param string $content Empty string by default * @param bool $overwrite Overwrite by default - * - * @return string */ - protected function writeContent($content = '', $overwrite = true) + protected function writeContent(string $content = '', bool $overwrite = true): string { $this->filesystem->write($this->key, $content, $overwrite); diff --git a/src/Gaufrette/Stream/Local.php b/src/Gaufrette/Stream/Local.php index 306119c5e..689bfef9d 100644 --- a/src/Gaufrette/Stream/Local.php +++ b/src/Gaufrette/Stream/Local.php @@ -12,25 +12,22 @@ */ class Local implements Stream { - private $path; + private string $path; private $mode; private $fileHandle; - private $mkdirMode; + private int $mkdirMode; /** * @param string $path * @param int $mkdirMode */ - public function __construct($path, $mkdirMode = 0755) + public function __construct(string $path, int $mkdirMode = 0755) { $this->path = $path; $this->mkdirMode = $mkdirMode; } - /** - * {@inheritdoc} - */ - public function open(StreamMode $mode) + public function open(StreamMode $mode): bool { $baseDirPath = \Gaufrette\Util\Path::dirname($this->path); if ($mode->allowsWrite() && !is_dir($baseDirPath)) { @@ -53,10 +50,7 @@ public function open(StreamMode $mode) return true; } - /** - * {@inheritdoc} - */ - public function read($count) + public function read(int $count): string|bool { if (!$this->fileHandle) { return false; @@ -69,29 +63,23 @@ public function read($count) return fread($this->fileHandle, $count); } - /** - * {@inheritdoc} - */ - public function write($data) + public function write(string $data): int { if (!$this->fileHandle) { - return false; + return 0; } if (false === $this->mode->allowsWrite()) { throw new \LogicException('The stream does not allow write.'); } - return fwrite($this->fileHandle, $data); + return fwrite($this->fileHandle, $data)?: 0; } - /** - * {@inheritdoc} - */ - public function close() + public function close(): void { if (!$this->fileHandle) { - return false; + return; } $closed = fclose($this->fileHandle); @@ -100,14 +88,9 @@ public function close() $this->mode = null; $this->fileHandle = null; } - - return $closed; } - /** - * {@inheritdoc} - */ - public function flush() + public function flush(): bool { if ($this->fileHandle) { return fflush($this->fileHandle); @@ -116,10 +99,7 @@ public function flush() return false; } - /** - * {@inheritdoc} - */ - public function seek($offset, $whence = SEEK_SET) + public function seek(int $offset, int $whence = SEEK_SET): bool { if ($this->fileHandle) { return 0 === fseek($this->fileHandle, $offset, $whence); @@ -128,10 +108,7 @@ public function seek($offset, $whence = SEEK_SET) return false; } - /** - * {@inheritdoc} - */ - public function tell() + public function tell(): int { if ($this->fileHandle) { return ftell($this->fileHandle); @@ -140,10 +117,7 @@ public function tell() return false; } - /** - * {@inheritdoc} - */ - public function eof() + public function eof(): bool { if ($this->fileHandle) { return feof($this->fileHandle); @@ -153,9 +127,9 @@ public function eof() } /** - * {@inheritdoc} + * @return array|false */ - public function stat() + public function stat(): array|bool { if ($this->fileHandle) { return fstat($this->fileHandle); @@ -166,10 +140,7 @@ public function stat() return false; } - /** - * {@inheritdoc} - */ - public function cast($castAs) + public function cast(int $castAs) { if ($this->fileHandle) { return $this->fileHandle; @@ -178,10 +149,7 @@ public function cast($castAs) return false; } - /** - * {@inheritdoc} - */ - public function unlink() + public function unlink(): bool { if ($this->mode && $this->mode->impliesExistingContentDeletion()) { return @unlink($this->path); diff --git a/src/Gaufrette/StreamMode.php b/src/Gaufrette/StreamMode.php index 7dbf83a1c..82ced3b95 100644 --- a/src/Gaufrette/StreamMode.php +++ b/src/Gaufrette/StreamMode.php @@ -9,15 +9,17 @@ */ class StreamMode { - private $mode; - private $base; - private $plus; - private $flag; + private string $mode; + private string $base; + private bool $plus; + private string $flag; /** * @param string $mode A stream mode as for the use of fopen() + * + * @see https://www.php.net/manual/en/function.fopen.php */ - public function __construct($mode) + public function __construct(string $mode) { $this->mode = $mode; @@ -31,20 +33,16 @@ public function __construct($mode) /** * Returns the underlying mode. - * - * @return string */ - public function getMode() + public function getMode(): string { return $this->mode; } /** * Indicates whether the mode allows to read. - * - * @return bool */ - public function allowsRead() + public function allowsRead(): bool { if ($this->plus) { return true; @@ -55,10 +53,8 @@ public function allowsRead() /** * Indicates whether the mode allows to write. - * - * @return bool */ - public function allowsWrite() + public function allowsWrite(): bool { if ($this->plus) { return true; @@ -69,20 +65,16 @@ public function allowsWrite() /** * Indicates whether the mode allows to open an existing file. - * - * @return bool */ - public function allowsExistingFileOpening() + public function allowsExistingFileOpening(): bool { return 'x' !== $this->base; } /** * Indicates whether the mode allows to create a new file. - * - * @return bool */ - public function allowsNewFileOpening() + public function allowsNewFileOpening(): bool { return 'r' !== $this->base; } @@ -90,10 +82,8 @@ public function allowsNewFileOpening() /** * Indicates whether the mode implies to delete the existing content of the * file when it already exists. - * - * @return bool */ - public function impliesExistingContentDeletion() + public function impliesExistingContentDeletion(): bool { return 'w' === $this->base; } @@ -101,10 +91,8 @@ public function impliesExistingContentDeletion() /** * Indicates whether the mode implies positioning the cursor at the * beginning of the file. - * - * @return bool */ - public function impliesPositioningCursorAtTheBeginning() + public function impliesPositioningCursorAtTheBeginning(): bool { return 'a' !== $this->base; } @@ -112,30 +100,24 @@ public function impliesPositioningCursorAtTheBeginning() /** * Indicates whether the mode implies positioning the cursor at the end of * the file. - * - * @return bool */ - public function impliesPositioningCursorAtTheEnd() + public function impliesPositioningCursorAtTheEnd(): bool { return 'a' === $this->base; } /** * Indicates whether the stream is in binary mode. - * - * @return bool */ - public function isBinary() + public function isBinary(): bool { return 'b' === $this->flag; } /** * Indicates whether the stream is in text mode. - * - * @return bool */ - public function isText() + public function isText(): bool { return false === $this->isBinary(); } diff --git a/src/Gaufrette/StreamWrapper.php b/src/Gaufrette/StreamWrapper.php index bde74a61d..2e6759bb4 100644 --- a/src/Gaufrette/StreamWrapper.php +++ b/src/Gaufrette/StreamWrapper.php @@ -10,30 +10,30 @@ */ class StreamWrapper { - private static $filesystemMap; + private static FilesystemMap $filesystemMap; - private $stream; + private Stream $stream; + /** + * @var ?resource + * @see https://www.php.net/manual/en/class.streamwrapper.php#streamwrapper.props.context + */ public $context; /** * Defines the filesystem map. - * - * @param FilesystemMap $map */ - public static function setFilesystemMap(FilesystemMap $map) + public static function setFilesystemMap(FilesystemMap $map): void { self::$filesystemMap = $map; } /** * Returns the filesystem map. - * - * @return FilesystemMap $map */ - public static function getFilesystemMap() + public static function getFilesystemMap(): FilesystemMap { - if (null === self::$filesystemMap) { + if (false === isset(self::$filesystemMap)) { self::$filesystemMap = self::createFilesystemMap(); } @@ -42,10 +42,8 @@ public static function getFilesystemMap() /** * Registers the stream wrapper to handle the specified scheme. - * - * @param string $scheme Default is gaufrette */ - public static function register($scheme = 'gaufrette') + public static function register(string $scheme = 'gaufrette'): void { self::streamWrapperUnregister($scheme); @@ -58,36 +56,35 @@ public static function register($scheme = 'gaufrette') } } - /** - * @return FilesystemMap - */ - protected static function createFilesystemMap() + protected static function createFilesystemMap(): FilesystemMap { return new FilesystemMap(); } /** - * @param string $scheme - protocol scheme + * @param string $scheme Protocol scheme */ - protected static function streamWrapperUnregister($scheme) + protected static function streamWrapperUnregister(string $scheme): bool { if (in_array($scheme, stream_get_wrappers())) { return stream_wrapper_unregister($scheme); } + + return false; } /** - * @param string $scheme - protocol scheme - * @param string $className - * - * @return bool + * @param string $scheme Protocol scheme */ - protected static function streamWrapperRegister($scheme, $className) + protected static function streamWrapperRegister(string $scheme, string $className): bool { return stream_wrapper_register($scheme, $className); } - public function stream_open($path, $mode) + /** + * @param STREAM_USE_PATH|STREAM_REPORT_ERRORS|9 $options "9" is the result of a bitwize operation between STREAM_USE_PATH and STREAM_REPORT_ERRORS (STREAM_USE_PATH|STREAM_REPORT_ERRORS). + */ + public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool { $this->stream = $this->createStream($path); @@ -95,46 +92,36 @@ public function stream_open($path, $mode) } /** - * @param int $bytes - * - * @return mixed + * @return string|false */ - public function stream_read($bytes) + public function stream_read(int $count): string|bool { - if ($this->stream) { - return $this->stream->read($bytes); + if (isset($this->stream)) { + return $this->stream->read($count); } return false; } - /** - * @param string $data - * - * @return int - */ - public function stream_write($data) + public function stream_write(string $data): int { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->write($data); } return 0; } - public function stream_close() + public function stream_close(): void { - if ($this->stream) { + if (isset($this->stream)) { $this->stream->close(); } } - /** - * @return bool - */ - public function stream_flush() + public function stream_flush(): bool { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->flush(); } @@ -142,14 +129,11 @@ public function stream_flush() } /** - * @param int $offset - * @param int $whence - one of values [SEEK_SET, SEEK_CUR, SEEK_END] - * - * @return bool + * @param SEEK_SET|SEEK_CUR|SEEK_END $whence */ - public function stream_seek($offset, $whence = SEEK_SET) + public function stream_seek(int $offset, int $whence = SEEK_SET): bool { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->seek($offset, $whence); } @@ -157,23 +141,20 @@ public function stream_seek($offset, $whence = SEEK_SET) } /** - * @return mixed + * Retrieve the current position of a stream */ - public function stream_tell() + public function stream_tell(): int { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->tell(); } - return false; + return 0; } - /** - * @return bool - */ - public function stream_eof() + public function stream_eof(): bool { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->eof(); } @@ -181,11 +162,11 @@ public function stream_eof() } /** - * @return mixed + * @return array|false */ - public function stream_stat() + public function stream_stat(): array|bool { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->stat(); } @@ -193,14 +174,11 @@ public function stream_stat() } /** - * @param string $path - * @param int $flags - * - * @return mixed + * @return array|false * - * @todo handle $flags parameter + * @TODO handle $flags parameter */ - public function url_stat($path, $flags) + public function url_stat(string $path, int $flags): array|bool { $stream = $this->createStream($path); @@ -212,12 +190,7 @@ public function url_stat($path, $flags) return $stream->stat(); } - /** - * @param string $path - * - * @return mixed - */ - public function unlink($path) + public function unlink(string $path): bool { $stream = $this->createStream($path); @@ -231,18 +204,19 @@ public function unlink($path) } /** - * @return mixed + * @return resource|false + * @param STREAM_CAST_FOR_SELECT|STREAM_CAST_AS_STREAM $castAs */ - public function stream_cast($castAs) + public function stream_cast(int $castAs) { - if ($this->stream) { + if (isset($this->stream)) { return $this->stream->cast($castAs); } return false; } - protected function createStream($path) + protected function createStream(string $path): Stream { $parts = array_merge( [ @@ -273,10 +247,13 @@ protected function createStream($path) )); } - return self::getFilesystemMap()->get($domain)->createStream($key); + return self::getFilesystemMap() + ->get($domain) + ->createStream($key) + ; } - protected function createStreamMode($mode) + protected function createStreamMode(string $mode): StreamMode { return new StreamMode($mode); } diff --git a/src/Gaufrette/Util/Checksum.php b/src/Gaufrette/Util/Checksum.php index 63afffaed..3e50326af 100644 --- a/src/Gaufrette/Util/Checksum.php +++ b/src/Gaufrette/Util/Checksum.php @@ -5,31 +5,23 @@ /** * Checksum utils. * - * @author Antoine Hérault + * @author Antoine Hérault */ class Checksum { /** * Returns the checksum of the given content. - * - * @param string $content - * - * @return string */ - public static function fromContent($content) + public static function fromContent(string $content): string { return md5($content); } /** * Returns the checksum of the specified file. - * - * @param string $filename - * - * @return string */ - public static function fromFile($filename) + public static function fromFile(string $filename): string { - return md5_file($filename); + return md5_file($filename)?: ''; } } diff --git a/src/Gaufrette/Util/Path.php b/src/Gaufrette/Util/Path.php index 4dec5c50a..c2d8d2af5 100644 --- a/src/Gaufrette/Util/Path.php +++ b/src/Gaufrette/Util/Path.php @@ -5,18 +5,14 @@ /** * Path utils. * - * @author Antoine Hérault + * @author Antoine Hérault */ class Path { /** * Normalizes the given path. - * - * @param string $path - * - * @return string */ - public static function normalize($path) + public static function normalize(string $path): string { $path = str_replace('\\', '/', $path); $prefix = static::getAbsolutePrefix($path); @@ -48,10 +44,8 @@ public static function normalize($path) * Indicates whether the given path is absolute or not. * * @param string $path A normalized path - * - * @return bool */ - public static function isAbsolute($path) + public static function isAbsolute(string $path): bool { return '' !== static::getAbsolutePrefix($path); } @@ -60,10 +54,8 @@ public static function isAbsolute($path) * Returns the absolute prefix of the given path. * * @param string $path A normalized path - * - * @return string */ - public static function getAbsolutePrefix($path) + public static function getAbsolutePrefix(string $path): string { preg_match('|^(?P([a-zA-Z]+:)?//?)|', $path, $matches); @@ -77,13 +69,9 @@ public static function getAbsolutePrefix($path) /** * Wrap native dirname function in order to handle only UNIX-style paths * - * @param string $path - * - * @return string - * * @see http://php.net/manual/en/function.dirname.php */ - public static function dirname($path) + public static function dirname(string $path): string { return str_replace('\\', '/', \dirname($path)); } diff --git a/src/Gaufrette/Util/Size.php b/src/Gaufrette/Util/Size.php index 7dd53aba3..f2cb5b917 100644 --- a/src/Gaufrette/Util/Size.php +++ b/src/Gaufrette/Util/Size.php @@ -12,13 +12,9 @@ class Size /** * Returns the size in bytes from the given content. * - * @param string $content - * - * @return int - * * @todo handle the case the mbstring is not loaded */ - public static function fromContent($content) + public static function fromContent(string $content): int { // Make sure to get the real length in byte and not // accidentally mistake some bytes as a UTF BOM. @@ -27,26 +23,21 @@ public static function fromContent($content) /** * Returns the size in bytes from the given file. - * - * @param string $filename - * - * @return int */ - public static function fromFile($filename) + public static function fromFile(string $filename): int { - return filesize($filename); + return filesize($filename)?: 0; } /** * Returns the size in bytes from the given resource. * * @param resource $handle - * - * @return string */ - public static function fromResource($handle) + public static function fromResource($handle): int { $cStat = fstat($handle); + // if the resource is a remote file, $cStat will be false return $cStat ? $cStat['size'] : 0; } diff --git a/tests/Gaufrette/Functional/Adapter/AsyncAwsS3Test.php b/tests/Gaufrette/Functional/Adapter/AsyncAwsS3Test.php index 559010188..ca1db955e 100644 --- a/tests/Gaufrette/Functional/Adapter/AsyncAwsS3Test.php +++ b/tests/Gaufrette/Functional/Adapter/AsyncAwsS3Test.php @@ -9,11 +9,9 @@ class AsyncAwsS3Test extends FunctionalTestCase { - /** @var string */ - private $bucket; + private string $bucket; - /** @var SimpleS3Client */ - private $client; + private SimpleS3Client $client; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/Adapter/AwsS3Test.php b/tests/Gaufrette/Functional/Adapter/AwsS3Test.php index 037ba72a3..0b79171c2 100644 --- a/tests/Gaufrette/Functional/Adapter/AwsS3Test.php +++ b/tests/Gaufrette/Functional/Adapter/AwsS3Test.php @@ -8,14 +8,11 @@ class AwsS3Test extends FunctionalTestCase { - /** @var int */ - private static $SDK_VERSION; + private static int $SDK_VERSION; - /** @var string */ - private $bucket; + private string $bucket; - /** @var S3Client */ - private $client; + private S3Client $client; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/Adapter/AzureBlobStorageTest.php b/tests/Gaufrette/Functional/Adapter/AzureBlobStorageTest.php index 88fda4301..d21c01edf 100644 --- a/tests/Gaufrette/Functional/Adapter/AzureBlobStorageTest.php +++ b/tests/Gaufrette/Functional/Adapter/AzureBlobStorageTest.php @@ -12,11 +12,10 @@ */ class AzureBlobStorageTest extends FunctionalTestCase { - /** @var string Name of the Azure container used */ - private $container; + /** Name of the Azure container used */ + private string $container; - /** @var AzureBlobStorage */ - private $adapter; + private AzureBlobStorage $adapter; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/Adapter/AzureMultiContainerBlobStorageTest.php b/tests/Gaufrette/Functional/Adapter/AzureMultiContainerBlobStorageTest.php index f01989aae..d8a678336 100644 --- a/tests/Gaufrette/Functional/Adapter/AzureMultiContainerBlobStorageTest.php +++ b/tests/Gaufrette/Functional/Adapter/AzureMultiContainerBlobStorageTest.php @@ -13,9 +13,9 @@ */ class AzureMultiContainerBlobStorageTest extends FunctionalTestCase { - private $adapter; + private AzureBlobStorage $adapter; - private $containers = []; + private array $containers = []; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/Adapter/DoctrineDbalTest.php b/tests/Gaufrette/Functional/Adapter/DoctrineDbalTest.php index aa9d8a39f..581acb791 100644 --- a/tests/Gaufrette/Functional/Adapter/DoctrineDbalTest.php +++ b/tests/Gaufrette/Functional/Adapter/DoctrineDbalTest.php @@ -2,14 +2,14 @@ namespace Gaufrette\Functional\Adapter; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; use Gaufrette\Adapter\DoctrineDbal; use Gaufrette\Filesystem; class DoctrineDbalTest extends FunctionalTestCase { - /** @var \Doctrine\DBAL\Connection */ - private $connection; + private Connection $connection; public static function setUpBeforeClass(): void { diff --git a/tests/Gaufrette/Functional/Adapter/FunctionalTestCase.php b/tests/Gaufrette/Functional/Adapter/FunctionalTestCase.php index a78aa0edf..204f77806 100644 --- a/tests/Gaufrette/Functional/Adapter/FunctionalTestCase.php +++ b/tests/Gaufrette/Functional/Adapter/FunctionalTestCase.php @@ -8,10 +8,7 @@ abstract class FunctionalTestCase extends TestCase { - /** - * @var Filesystem - */ - protected $filesystem; + protected ?Filesystem $filesystem = null; public function getAdapterName() { diff --git a/tests/Gaufrette/Functional/Adapter/LocalTest.php b/tests/Gaufrette/Functional/Adapter/LocalTest.php index 4b319a7be..e4400810a 100644 --- a/tests/Gaufrette/Functional/Adapter/LocalTest.php +++ b/tests/Gaufrette/Functional/Adapter/LocalTest.php @@ -7,7 +7,7 @@ class LocalTest extends FunctionalTestCase { - private $directory; + private string $directory; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/Adapter/PhpseclibSftpTest.php b/tests/Gaufrette/Functional/Adapter/PhpseclibSftpTest.php index 9a50ab685..d840a91fb 100644 --- a/tests/Gaufrette/Functional/Adapter/PhpseclibSftpTest.php +++ b/tests/Gaufrette/Functional/Adapter/PhpseclibSftpTest.php @@ -8,11 +8,9 @@ class PhpseclibSftpTest extends FunctionalTestCase { - /** @var SFTP */ - private $sftp; + private SFTP $sftp; - /** @var string */ - private $baseDir; + private string $baseDir; protected function setUp(): void { diff --git a/tests/Gaufrette/Functional/FileStream/FunctionalTestCase.php b/tests/Gaufrette/Functional/FileStream/FunctionalTestCase.php index 4a041436d..112b71c1f 100644 --- a/tests/Gaufrette/Functional/FileStream/FunctionalTestCase.php +++ b/tests/Gaufrette/Functional/FileStream/FunctionalTestCase.php @@ -2,12 +2,13 @@ namespace Gaufrette\Functional\FileStream; +use Gaufrette\Filesystem; use Gaufrette\StreamWrapper; use PHPUnit\Framework\TestCase; abstract class FunctionalTestCase extends TestCase { - protected $filesystem; + protected ?Filesystem $filesystem = null; /** * @test diff --git a/tests/Gaufrette/Functional/FileStream/LocalTest.php b/tests/Gaufrette/Functional/FileStream/LocalTest.php index 49e5a8e60..8a23f1cb7 100644 --- a/tests/Gaufrette/Functional/FileStream/LocalTest.php +++ b/tests/Gaufrette/Functional/FileStream/LocalTest.php @@ -7,7 +7,7 @@ class LocalTest extends FunctionalTestCase { - protected $directory; + protected string $directory; protected function setUp(): void {