From f79968d3f1fe67055d8d9b5aef7714fdf76beafe Mon Sep 17 00:00:00 2001 From: peter279k Date: Wed, 8 Dec 2021 15:26:19 +0800 Subject: [PATCH] Upgrade PHPStan & PHP-CS-Fixer and improve scripts --- .cs.php | 6 +++--- .github/workflows/build.yml | 4 ++-- composer.json | 31 ++++++++++++++++--------------- phpstan.neon | 5 +++++ src/Detector/JpegDetector.php | 2 +- src/ImageTypeDetector.php | 4 ---- src/Provider/HdrProvider.php | 2 +- src/Provider/RasterProvider.php | 4 ++-- src/Provider/RawProvider.php | 2 +- tests/ImageTypeDetectorTest.php | 6 +----- tests/ImageTypeTest.php | 6 ------ 11 files changed, 32 insertions(+), 40 deletions(-) diff --git a/.cs.php b/.cs.php index 44dbffa..fe2a42c 100644 --- a/.cs.php +++ b/.cs.php @@ -1,6 +1,6 @@ setUsingCache(false) ->setRiskyAllowed(true) //->setCacheFile(__DIR__ . '/.php_cs.cache') @@ -8,7 +8,7 @@ '@PSR1' => true, '@PSR2' => true, '@Symfony' => true, - 'psr4' => true, + 'psr_autoloading' => true, 'yoda_style' => false, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'short'], @@ -17,7 +17,7 @@ 'compact_nullable_typehint' => true, 'increment_style' => ['style' => 'post'], 'declare_equal_normalize' => ['space' => 'single'], - 'no_short_echo_tag' => true, + 'echo_tag_syntax' => ['format' => 'long'], 'protected_to_private' => false, 'phpdoc_align' => false, 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 61e70df..e988cc0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: uses: actions/checkout@v1 - name: Setup PHP - uses: shivammathur/setup-php@v1 + uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} extensions: mbstring, pdo, pdo_mysql, intl, zip @@ -38,4 +38,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-suggest - name: Run test suite - run: composer check-all + run: composer check diff --git a/composer.json b/composer.json index 653d2b2..8e0ff64 100644 --- a/composer.json +++ b/composer.json @@ -15,25 +15,26 @@ "require-dev": { "overtrue/phplint": "^1.1", "phpunit/phpunit": "^8", - "phpstan/phpstan": "^0.12", - "squizlabs/php_codesniffer": "^3.4" + "phpstan/phpstan": "^1", + "squizlabs/php_codesniffer": "^3.4", + "friendsofphp/php-cs-fixer": "^3" }, "scripts": { - "test": "phpunit --configuration phpunit.xml", - "test-coverage": "phpunit --configuration phpunit.xml --coverage-clover build/logs/clover.xml --coverage-html build/coverage", - "check-style": "phpcs --standard=phpcs.xml", - "fix-style": "phpcbf --standard=phpcs.xml", - "phpstan": "phpstan analyse src tests --level=max -c phpstan.neon --no-progress", - "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", - "install-cs": "php -r \"@mkdir('build'); copy('https://cs.symfony.com/download/php-cs-fixer-v2.phar', 'build/php-cs-fixer-v2.phar');\"", - "fix-cs": "php build/php-cs-fixer-v2.phar fix --config=.cs.php", - "check-cs": "php build/php-cs-fixer-v2.phar fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php", - "check-all": [ + "check": [ "@lint", - "@check-style", + "@cs:check", + "@sniffer:check", "@phpstan", - "@test-coverage" - ] + "@test:coverage" + ], + "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php", + "cs:fix": "php-cs-fixer fix --config=.cs.php", + "lint": "phplint ./ --exclude=vendor --no-interaction --no-cache", + "phpstan": "phpstan analyse -c phpstan.neon --no-progress --ansi", + "sniffer:check": "phpcs --standard=phpcs.xml", + "sniffer:fix": "phpcbf --standard=phpcs.xml", + "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always", + "test:coverage": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index e69de29..a4c8812 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: max + paths: + - src + - tests diff --git a/src/Detector/JpegDetector.php b/src/Detector/JpegDetector.php index 2fdcd32..d7223b3 100644 --- a/src/Detector/JpegDetector.php +++ b/src/Detector/JpegDetector.php @@ -21,7 +21,7 @@ final class JpegDetector implements DetectorInterface */ public function detect(SplFileObject $file): ?ImageType { - return $file->fread(2) === chr(0xFF) . chr(0xd8) ? new ImageType( + return $file->fread(2) === chr(0xFF) . chr(0xD8) ? new ImageType( ImageFormat::JPEG, MimeType::IMAGE_JPEG ) : null; diff --git a/src/ImageTypeDetector.php b/src/ImageTypeDetector.php index 6145008..4027377 100644 --- a/src/ImageTypeDetector.php +++ b/src/ImageTypeDetector.php @@ -30,8 +30,6 @@ public function addDetector(DetectorInterface $detector): void * Add image detector provider. * * @param ProviderInterface $provider The provider - * - * @return void */ public function addProvider(ProviderInterface $provider): void { @@ -64,8 +62,6 @@ public function getImageTypeFromFile(SplFileObject $file): ImageType * Reads and returns the type of the image. * * @param SplFileObject $file The image file - * - * @return ImageType|null */ private function detectFile(SplFileObject $file): ?ImageType { diff --git a/src/Provider/HdrProvider.php b/src/Provider/HdrProvider.php index d3997e6..562ea44 100644 --- a/src/Provider/HdrProvider.php +++ b/src/Provider/HdrProvider.php @@ -5,10 +5,10 @@ use Selective\ImageType\Detector\CineonDetector; use Selective\ImageType\Detector\DpxDetector; use Selective\ImageType\Detector\ExrDetector; +use Selective\ImageType\Detector\HdrDetector; use Selective\ImageType\Detector\JpegHdrDetector; use Selective\ImageType\Detector\PbmDetector; use Selective\ImageType\Detector\PfmDetector; -use Selective\ImageType\Detector\HdrDetector; /** * Provider. diff --git a/src/Provider/RasterProvider.php b/src/Provider/RasterProvider.php index 4fe49f4..d77fc6e 100644 --- a/src/Provider/RasterProvider.php +++ b/src/Provider/RasterProvider.php @@ -15,7 +15,9 @@ use Selective\ImageType\Detector\JpmDetector; use Selective\ImageType\Detector\MngDetector; use Selective\ImageType\Detector\PdnDetector; +use Selective\ImageType\Detector\PgmDetector; use Selective\ImageType\Detector\PngDetector; +use Selective\ImageType\Detector\PpmDetector; use Selective\ImageType\Detector\PsbDetector; use Selective\ImageType\Detector\PsdDetector; use Selective\ImageType\Detector\SvgDetector; @@ -23,8 +25,6 @@ use Selective\ImageType\Detector\TiffDetector; use Selective\ImageType\Detector\WebpDetector; use Selective\ImageType\Detector\XcfDetector; -use Selective\ImageType\Detector\PgmDetector; -use Selective\ImageType\Detector\PpmDetector; /** * Raster Provider. diff --git a/src/Provider/RawProvider.php b/src/Provider/RawProvider.php index 3838c7e..a5223ee 100644 --- a/src/Provider/RawProvider.php +++ b/src/Provider/RawProvider.php @@ -4,11 +4,11 @@ use Selective\ImageType\Detector\Cr2Detector; use Selective\ImageType\Detector\Cr3Detector; +use Selective\ImageType\Detector\DngDetector; use Selective\ImageType\Detector\Fr3Detector; use Selective\ImageType\Detector\OrfDetector; use Selective\ImageType\Detector\PefDetector; use Selective\ImageType\Detector\Rw2Detector; -use Selective\ImageType\Detector\DngDetector; /** * Provider. diff --git a/tests/ImageTypeDetectorTest.php b/tests/ImageTypeDetectorTest.php index d4d8c2d..850c79b 100644 --- a/tests/ImageTypeDetectorTest.php +++ b/tests/ImageTypeDetectorTest.php @@ -9,8 +9,8 @@ use Selective\ImageType\ImageTypeDetectorException; use Selective\ImageType\MimeType; use Selective\ImageType\Provider\CompoundProvider; -use Selective\ImageType\Provider\RasterProvider; use Selective\ImageType\Provider\HdrProvider; +use Selective\ImageType\Provider\RasterProvider; use Selective\ImageType\Provider\RawProvider; use Selective\ImageType\Provider\VectorProvider; use SplFileObject; @@ -47,8 +47,6 @@ private function createDetector(): ImageTypeDetector * @param string $file The file * @param string $format The expected format * @param string $mime The expected mime type - * - * @return void */ public function testGetImageTypeFromFile(string $file, string $format, string $mime): void { @@ -136,8 +134,6 @@ public function providerGetImageTypeFromFile(): array /** * Test. - * - * @return void */ public function testGetImageTypeWithUnknownFormat(): void { diff --git a/tests/ImageTypeTest.php b/tests/ImageTypeTest.php index a4a8547..fdff22d 100644 --- a/tests/ImageTypeTest.php +++ b/tests/ImageTypeTest.php @@ -15,8 +15,6 @@ class ImageTypeTest extends TestCase { /** * Test. - * - * @return void */ public function testCreateInstance(): void { @@ -28,8 +26,6 @@ public function testCreateInstance(): void /** * Test. - * - * @return void */ public function testCreateInstanceWithError(): void { @@ -39,8 +35,6 @@ public function testCreateInstanceWithError(): void /** * Test. - * - * @return void */ public function testCreateInstanceWithError2(): void {