From 3164d2d6f9a4ded24f9cf6bfc2cfd848e708726f Mon Sep 17 00:00:00 2001 From: Jakob Givoni Date: Wed, 12 Feb 2025 15:50:38 +0100 Subject: [PATCH] Integrate phpcsfixer for development (#27) --- .github/workflows/ci.yml | 33 +++++++++++++++++++++++++++++++++ .gitignore | 10 ++++++---- .php-cs-fixer.php | 17 +++++++++++++++++ aliases | 3 +++ composer.json | 3 ++- src/CacheItemsTrait.php | 4 ++-- tests/CacheAdapter_Test.php | 32 -------------------------------- tests/CacheTestCase.php | 6 +++--- tests/Copy_Test.php | 6 +++--- tests/CreateDirectory_Test.php | 4 ++-- tests/DeleteDirectory_Test.php | 6 +++--- tests/Delete_Test.php | 6 +++--- tests/DirectoryExists_Test.php | 6 +++--- tests/GetChecksum_Test.php | 12 ++++++------ tests/GetFileSize_Test.php | 8 ++++---- tests/GetLastModified_Test.php | 8 ++++---- tests/GetMimetype_Test.php | 10 +++++----- tests/GetVisibility_Test.php | 10 +++++----- tests/ListContents_Test.php | 2 +- tests/Move_Test.php | 6 +++--- tests/Read_Test.php | 10 +++++----- tests/SetVisibility_Test.php | 6 +++--- tests/Write_Test.php | 6 +++--- 23 files changed, 119 insertions(+), 95 deletions(-) create mode 100644 .php-cs-fixer.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36c569e..d43af51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,3 +68,36 @@ jobs: - name: Run unit tests run: docker exec mycontainer vendor/bin/phpunit + + job3: + strategy: + matrix: + php_version: ["8.1"] + name: PhpCsFixer code style checker ${{ matrix.php_version }} + runs-on: ubuntu-latest + steps: + - name: Checkout codebase + uses: actions/checkout@v3 + + - name: Enable caching + uses: satackey/action-docker-layer-caching@v0.0.11 + # Deprecation warnings are expected... + continue-on-error: true + + - name: Build image + run: docker image build . -f Dockerfile -t myimage:latest --build-arg PHP_VERSION=${{ matrix.php_version }} + + - name: Launch container + run: docker run -d -v "$(pwd)/:/app" --name mycontainer myimage:latest tail -f /dev/null + + - name: Check PHP version + run: docker exec mycontainer php -v + + - name: Check composer.json validity + run: docker exec mycontainer composer validate + + - name: Install dependencies + run: docker exec mycontainer composer install + + - name: Run unit tests + run: docker exec mycontainer vendor/friendsofphp/php-cs-fixer/php-cs-fixer check diff --git a/.gitignore b/.gitignore index 11e9c13..cf7e963 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -composer.lock -vendor -.phpunit.cache -.env +/composer.lock +/vendor +/.phpunit.cache +/.php-cs-fixer.cache + +.env \ No newline at end of file diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..efbe5d5 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,17 @@ +ignoreDotFiles(false) + ->ignoreVCSIgnored(true) + ->in(__DIR__); + +return (new Config()) + ->setRules([ + '@PSR12' => true, + 'concat_space' => ['spacing' => 'one'], + 'new_with_parentheses' => false, + ]) + ->setFinder($finder); diff --git a/aliases b/aliases index e1de027..a9c03a7 100644 --- a/aliases +++ b/aliases @@ -4,3 +4,6 @@ alias php='d php "$@"' alias composer='d composer "$@"' alias phpstan='d vendor/bin/phpstan analyze "$@"' alias phpunit='d vendor/bin/phpunit "$@"' +alias phpcsfixer='d vendor/friendsofphp/php-cs-fixer/php-cs-fixer "$@"' + +alias reload='. ./aliases' diff --git a/composer.json b/composer.json index c8ab380..9c508fb 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ "league/flysystem-adapter-test-utilities": "^3.15", "league/flysystem-memory": "^3.15", "symfony/cache": "^6.3|^7.0", - "league/flysystem-aws-s3-v3": "^3.15" + "league/flysystem-aws-s3-v3": "^3.15", + "friendsofphp/php-cs-fixer": "^3.68" }, "suggest": { "symfony/cache": "Ready to use adapters for the most common caching backends" diff --git a/src/CacheItemsTrait.php b/src/CacheItemsTrait.php index 1f99fcf..4cbaf17 100644 --- a/src/CacheItemsTrait.php +++ b/src/CacheItemsTrait.php @@ -21,8 +21,8 @@ */ trait CacheItemsTrait { - static string $CACHE_KEY_PREFIX = 'flysystem_item_'; - static string $CACHE_KEY_HASH_SALT = '563ce5132194441b'; + public static string $CACHE_KEY_PREFIX = 'flysystem_item_'; + public static string $CACHE_KEY_HASH_SALT = '563ce5132194441b'; protected function getCacheItem(string $path): CacheItemInterface { diff --git a/tests/CacheAdapter_Test.php b/tests/CacheAdapter_Test.php index 5a6e9c4..7dcf4d0 100644 --- a/tests/CacheAdapter_Test.php +++ b/tests/CacheAdapter_Test.php @@ -18,36 +18,4 @@ protected static function createFilesystemAdapter(): FilesystemAdapter return new CacheAdapter($fileSystemAdapter, $cache); } - - /** - * @test - * @dataProvider filenameProvider2 - */ - public function writing_and_reading_files_with_special_path(string $path): void - { - parent::writing_and_reading_files_with_special_path($path); - } - - /** - * "Override" function from parent which is not static and thus incompatible with phpunit 10 - * @todo Remove when https://github.com/thephpleague/flysystem-adapter-test-utilities/commit/bf4c950b176bbefcc49c443cdab1ffb62a9fef5c is tagged (version >= 3.16?) - * @return Generator - */ - public static function filenameProvider2(): Generator - { - yield "a path with square brackets in filename 1" => ["some/file[name].txt"]; - yield "a path with square brackets in filename 2" => ["some/file[0].txt"]; - yield "a path with square brackets in filename 3" => ["some/file[10].txt"]; - yield "a path with square brackets in dirname 1" => ["some[name]/file.txt"]; - yield "a path with square brackets in dirname 2" => ["some[0]/file.txt"]; - yield "a path with square brackets in dirname 3" => ["some[10]/file.txt"]; - yield "a path with curly brackets in filename 1" => ["some/file{name}.txt"]; - yield "a path with curly brackets in filename 2" => ["some/file{0}.txt"]; - yield "a path with curly brackets in filename 3" => ["some/file{10}.txt"]; - yield "a path with curly brackets in dirname 1" => ["some{name}/filename.txt"]; - yield "a path with curly brackets in dirname 2" => ["some{0}/filename.txt"]; - yield "a path with curly brackets in dirname 3" => ["some{10}/filename.txt"]; - yield "a path with space in dirname" => ["some dir/filename.txt"]; - yield "a path with space in filename" => ["somedir/file name.txt"]; - } } diff --git a/tests/CacheTestCase.php b/tests/CacheTestCase.php index 2968d77..0006624 100644 --- a/tests/CacheTestCase.php +++ b/tests/CacheTestCase.php @@ -57,7 +57,7 @@ public function setUp(): void } /** - * @param array $items + * @param array $items */ protected function setupCache(array $items): void { @@ -71,7 +71,7 @@ protected function setupCache(array $items): void } /** - * @param array $items + * @param array $items */ protected function setupFiles(array $items): void { @@ -81,7 +81,7 @@ protected function setupFiles(array $items): void } /** - * @param array $items + * @param array $items */ protected function assertCachedItems(array $items): void { diff --git a/tests/Copy_Test.php b/tests/Copy_Test.php index 75dcc78..9db1eb6 100644 --- a/tests/Copy_Test.php +++ b/tests/Copy_Test.php @@ -10,7 +10,7 @@ class Copy_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -27,7 +27,7 @@ public function copy_ok(string $source, FileAttributes|null $expectedSourceCache } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -36,7 +36,7 @@ public static function dataProvider(): iterable yield 'cache item is created' => ['non-cached-file', \null, new FileAttributes('destination')]; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_copy(): void diff --git a/tests/CreateDirectory_Test.php b/tests/CreateDirectory_Test.php index 863f650..bdc0236 100644 --- a/tests/CreateDirectory_Test.php +++ b/tests/CreateDirectory_Test.php @@ -7,7 +7,7 @@ class CreateDirectory_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -21,7 +21,7 @@ public function directory_is_cached_after_creating(string $path): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable diff --git a/tests/DeleteDirectory_Test.php b/tests/DeleteDirectory_Test.php index a3e4104..b8d6042 100644 --- a/tests/DeleteDirectory_Test.php +++ b/tests/DeleteDirectory_Test.php @@ -6,7 +6,7 @@ class DeleteDirectory_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -20,7 +20,7 @@ public function directory_is_not_cached_deleting(string $path): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -41,7 +41,7 @@ public function nested_files_are_purged_from_cache(): void ]); } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_delete(): void diff --git a/tests/Delete_Test.php b/tests/Delete_Test.php index 0fa41f8..0ba1b1e 100644 --- a/tests/Delete_Test.php +++ b/tests/Delete_Test.php @@ -6,7 +6,7 @@ class Delete_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -20,7 +20,7 @@ public function file_is_not_cached_deleting(string $path): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -29,7 +29,7 @@ public static function dataProvider(): iterable yield 'non cached file stays uncached' => ['non-cached-file']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_delete(): void diff --git a/tests/DirectoryExists_Test.php b/tests/DirectoryExists_Test.php index 0ba5887..2e2e590 100644 --- a/tests/DirectoryExists_Test.php +++ b/tests/DirectoryExists_Test.php @@ -6,7 +6,7 @@ class DirectoryExists_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -18,7 +18,7 @@ public function directory_exists_ok(string $path, bool $expectedResult): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -30,7 +30,7 @@ public static function dataProvider(): iterable yield 'file is not a directory' => ['fully-cached-file', false]; } - /** + /** * @test */ public function directory_is_cached_after_checking_filesystem(): void diff --git a/tests/GetChecksum_Test.php b/tests/GetChecksum_Test.php index 7fdaa0b..eebdaad 100644 --- a/tests/GetChecksum_Test.php +++ b/tests/GetChecksum_Test.php @@ -23,7 +23,7 @@ public function setUp(): void ]); } - /** + /** * @test * @dataProvider dataProvider */ @@ -35,7 +35,7 @@ public function get_checksum(string $path, string $expectedChecksum): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -45,7 +45,7 @@ public static function dataProvider(): iterable yield 'file is not cached' => ['non-cached-file', md5('0123456789')]; } - /** + /** * @test * @dataProvider aws_dataProvider */ @@ -62,7 +62,7 @@ public function with_mock_aws_adapter(string $path, string $expectedChecksum): v } /** - * + * * @return iterable> */ public static function aws_dataProvider(): iterable @@ -84,7 +84,7 @@ public function error(string $path): void } /** - * + * * @return iterable> */ public static function errorDataProvider(): iterable @@ -94,7 +94,7 @@ public static function errorDataProvider(): iterable yield 'Path is directory (non-cached)' => ['non-cached-directory']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_get(): void diff --git a/tests/GetFileSize_Test.php b/tests/GetFileSize_Test.php index e608cb9..aa4aaec 100644 --- a/tests/GetFileSize_Test.php +++ b/tests/GetFileSize_Test.php @@ -6,7 +6,7 @@ class GetFileSize_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -18,7 +18,7 @@ public function get_fileSize(string $path, int $expectedFileSize): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -41,7 +41,7 @@ public function error(string $path): void } /** - * + * * @return iterable> */ public static function errorDataProvider(): iterable @@ -51,7 +51,7 @@ public static function errorDataProvider(): iterable yield 'Path is directory (non-cached)' => ['non-cached-directory']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_get(): void diff --git a/tests/GetLastModified_Test.php b/tests/GetLastModified_Test.php index 4506d85..b73e2ee 100644 --- a/tests/GetLastModified_Test.php +++ b/tests/GetLastModified_Test.php @@ -6,7 +6,7 @@ class GetLastModified_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -18,7 +18,7 @@ public function get_lastModified(string $path): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -38,7 +38,7 @@ public function error(string $path): void } /** - * + * * @return iterable> */ public static function errorDataProvider(): iterable @@ -48,7 +48,7 @@ public static function errorDataProvider(): iterable yield 'Path is directory (non-cached)' => ['non-cached-directory']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_get(): void diff --git a/tests/GetMimetype_Test.php b/tests/GetMimetype_Test.php index 7ac1f1e..49cdded 100644 --- a/tests/GetMimetype_Test.php +++ b/tests/GetMimetype_Test.php @@ -10,7 +10,7 @@ class GetMimetype_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -22,7 +22,7 @@ public function get_mimetype(string $path, StorageAttributes $expectedResult): v } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -32,7 +32,7 @@ public static function dataProvider(): iterable yield 'file is not cached but exists in the filesystem' => ['non-cached-file.txt', new FileAttributes('non-cached-file.txt', mimeType: 'text/plain')]; } - /** + /** * @test */ public function file_is_cached_after_checking_filesystem(): void @@ -57,7 +57,7 @@ public function error(string $path): void } /** - * + * * @return iterable> */ public static function errorDataProvider(): iterable @@ -67,7 +67,7 @@ public static function errorDataProvider(): iterable yield 'Path is directory (non-cached)' => ['non-cached-directory']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_get(): void diff --git a/tests/GetVisibility_Test.php b/tests/GetVisibility_Test.php index 68e56b1..d272ed3 100644 --- a/tests/GetVisibility_Test.php +++ b/tests/GetVisibility_Test.php @@ -9,7 +9,7 @@ class GetVisibility_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -21,7 +21,7 @@ public function get_visibility(string $path, StorageAttributes $expectedResult): } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -31,7 +31,7 @@ public static function dataProvider(): iterable yield 'file is not cached but exists in the filesystem' => ['non-cached-file', new FileAttributes('non-cached-file', visibility: Visibility::PUBLIC)]; } - /** + /** * @test */ public function file_is_cached_after_checking_filesystem(): void @@ -56,7 +56,7 @@ public function error(string $path): void } /** - * + * * @return iterable> */ public static function errorDataProvider(): iterable @@ -66,7 +66,7 @@ public static function errorDataProvider(): iterable yield 'Path is directory (non-cached)' => ['non-cached-directory']; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_get(): void diff --git a/tests/ListContents_Test.php b/tests/ListContents_Test.php index 8b1a0dc..f068f66 100644 --- a/tests/ListContents_Test.php +++ b/tests/ListContents_Test.php @@ -7,7 +7,7 @@ class ListContents_Test extends CacheTestCase { - /** + /** * @test */ public function content_list_is_cached(): void diff --git a/tests/Move_Test.php b/tests/Move_Test.php index 220e713..33d76ca 100644 --- a/tests/Move_Test.php +++ b/tests/Move_Test.php @@ -9,7 +9,7 @@ class Move_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -26,7 +26,7 @@ public function move_ok(string $source, FileAttributes $expectedDestinationCache } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -35,7 +35,7 @@ public static function dataProvider(): iterable yield 'cache item is created' => ['non-cached-file', new FileAttributes('destination')]; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_move(): void diff --git a/tests/Read_Test.php b/tests/Read_Test.php index 9969f9d..7b2b55f 100644 --- a/tests/Read_Test.php +++ b/tests/Read_Test.php @@ -8,7 +8,7 @@ class Read_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -21,7 +21,7 @@ public function file_is_cached_after_reading(string $path, int|null $expectedSiz ]); } - /** + /** * @test * @dataProvider dataProvider */ @@ -35,7 +35,7 @@ public function file_is_cached_after_reading_stream(string $path, int|null $expe } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -45,7 +45,7 @@ public static function dataProvider(): iterable yield 'cached file attributes are unchanged after reading' => ['fully-cached-file', 10, Visibility::PUBLIC]; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_read(): void @@ -61,7 +61,7 @@ public function cache_is_purged_after_unsuccessful_read(): void } } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_readStream(): void diff --git a/tests/SetVisibility_Test.php b/tests/SetVisibility_Test.php index 0c9e120..54a4778 100644 --- a/tests/SetVisibility_Test.php +++ b/tests/SetVisibility_Test.php @@ -9,7 +9,7 @@ class SetVisibility_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -23,7 +23,7 @@ public function set_visibility_ok(string $path, string $visibility, StorageAttri } /** - * + * * @return iterable> */ public static function dataProvider(): iterable @@ -36,7 +36,7 @@ public static function dataProvider(): iterable // yield 'cached directory, set private' => ['cached-directory', Visibility::PRIVATE, new DirectoryAttributes('fully-cached-file', visibility: Visibility::PRIVATE)]; } - /** + /** * @test */ public function cache_is_purged_after_unsuccessful_set(): void diff --git a/tests/Write_Test.php b/tests/Write_Test.php index 47def45..506d3db 100644 --- a/tests/Write_Test.php +++ b/tests/Write_Test.php @@ -8,7 +8,7 @@ class Write_Test extends CacheTestCase { - /** + /** * @test * @dataProvider dataProvider */ @@ -21,7 +21,7 @@ public function file_is_cached_after_writing(string $path): void ]); } - /** + /** * @test * @dataProvider dataProvider */ @@ -37,7 +37,7 @@ public function file_is_cached_after_writing_stream(string $path): void } /** - * + * * @return iterable> */ public static function dataProvider(): iterable