From f1bdf1320b0556332135cca267aee27e9ca62ed2 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 5 Dec 2024 10:01:14 +0100 Subject: [PATCH 1/4] refactor(src) PHP 8.4 compatibility --- .github/workflows/ci.yaml | 20 +++++++++++++++++++- src/AbstractSnapshot.php | 26 ++++++++++++++------------ src/CodeSnapshot.php | 1 + src/DirectorySnapshot.php | 13 ++++++------- src/HtmlSnapshot.php | 2 ++ src/JsonSnapshot.php | 1 + src/SnapshotAssertions.php | 10 +++++----- src/StringSnapshot.php | 2 ++ 8 files changed, 50 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f267ae1..e290455 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,7 +38,25 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.1/install@master + - uses: MilesChou/composer-action/8.2/install@master - uses: docker://php:8.2-alpine with: args: vendor/bin/codecept run + php83: + name: Test on PHP 8.3 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - uses: MilesChou/composer-action/8.3/install@master + - uses: docker://php:8.3-alpine + with: + args: vendor/bin/codecept run + php84: + name: Test on PHP 8.4 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - uses: MilesChou/composer-action/8.4/install@master + - uses: docker://php:8.4-alpine + with: + args: vendor/bin/codecept run diff --git a/src/AbstractSnapshot.php b/src/AbstractSnapshot.php index 253cee6..5c09106 100644 --- a/src/AbstractSnapshot.php +++ b/src/AbstractSnapshot.php @@ -53,7 +53,7 @@ class AbstractSnapshot extends Snapshot * @param array|string|false|null $current The current value. * @param bool|null $refresh Whether to refresh the snapshot or not. */ - public function __construct(protected mixed $current = null, bool $refresh = null) + public function __construct(protected mixed $current = null, ?bool $refresh = null) { $this->refresh = $refresh ?? (Configuration::getRefresh() && Debug::isEnabled()); } @@ -76,6 +76,7 @@ public function snapshotFileName(): string * @return string The snapshot file name, including the file extension. * @throws ReflectionException If the class that called the class cannot be reflected. */ + #[\Override] protected function getFileName(bool $increment = false): string { if ($this->fileName !== null) { @@ -89,13 +90,11 @@ protected function getFileName(bool $increment = false): string | DEBUG_BACKTRACE_PROVIDE_OBJECT, 5 ), - static function (array $backtraceEntry) use ($traitMethods): bool { - return isset($backtraceEntry['class']) && !in_array( - $backtraceEntry['class'], - [Snapshot::class, static::class, self::class, SnapshotAssertions::class], - true - ) && !in_array($backtraceEntry['function'], $traitMethods, true); - } + static fn(array $backtraceEntry): bool => isset($backtraceEntry['class']) && !in_array( + $backtraceEntry['class'], + [Snapshot::class, static::class, self::class, SnapshotAssertions::class], + true + ) && !in_array($backtraceEntry['function'], $traitMethods, true) )); $class = $backtrace[0]['class']; $classFrags = explode('\\', $class); @@ -146,9 +145,7 @@ protected static function getTraitMethods(): array } $reflection = new ReflectionClass(SnapshotAssertions::class); - static::$traitMethods = array_map(static function (ReflectionMethod $method): string { - return $method->name; - }, $reflection->getMethods()); + static::$traitMethods = array_map(static fn(ReflectionMethod $method): string => $method->name, $reflection->getMethods()); return static::$traitMethods; } @@ -214,6 +211,7 @@ public function snapshotPutContents(string|false $contents): void * * @throws Exception If there's an issue reading or saving the snapshot. */ + #[\Override] protected function save(bool $increment = true): void { $fileName = $this->getFileName($increment); @@ -245,6 +243,7 @@ public function prepareSnapshotForDump(): string|false * @throws Exception * @throws Exception */ + #[\Override] public function assert(): void { // Fetch data. @@ -299,6 +298,7 @@ public function assert(): void * * @return string|false The fetched data, the current data by default. */ + #[\Override] protected function fetchData(): string|false { if (!(is_string($this->current) || $this->current === false)) { @@ -313,6 +313,7 @@ protected function fetchData(): string|false * * @throws ReflectionException */ + #[\Override] protected function load(): void { $filename = $this->getFileName(); @@ -335,7 +336,7 @@ protected function load(): void */ protected function printDebug(string $message): void { - Debug::debug($this::class . ': ' . $message); + Debug::debug(static::class . ': ' . $message); } /** @@ -373,6 +374,7 @@ protected function getDataName(TestCase $testCase): string * * @param mixed $data The data to check. */ + #[\Override] protected function assertData(mixed $data): void { if ($this->dataVisitor !== null) { diff --git a/src/CodeSnapshot.php b/src/CodeSnapshot.php index 5425bf7..99e7231 100644 --- a/src/CodeSnapshot.php +++ b/src/CodeSnapshot.php @@ -33,6 +33,7 @@ public function __construct($current = null, string $extension = 'php') /** * {@inheritDoc} */ + #[\Override] public function fileExtension(): string { return 'snapshot.'.$this->extension; diff --git a/src/DirectorySnapshot.php b/src/DirectorySnapshot.php index ecd70c3..826f8d2 100644 --- a/src/DirectorySnapshot.php +++ b/src/DirectorySnapshot.php @@ -42,6 +42,7 @@ public function __construct($current = null) /** * {@inheritDoc} */ + #[\Override] protected function fetchData(): string|false { return $this->prepareSnapshotForDump(); @@ -69,6 +70,7 @@ protected function buildIterator(string $dir): RecursiveIteratorIterator /** * {@inheritDoc} */ + #[\Override] public function prepareSnapshotForDump(): string { if ($this->preparedSnapshot !== null) { @@ -103,7 +105,7 @@ protected function getFileSectionHeadersFor(string $fileRelativePath): array { $fileSectionHeaderStart = sprintf('>>> %s >>>', $fileRelativePath); $fileSectionHeaderEnd = sprintf('<<< %s <<<', $fileRelativePath); - return array($fileSectionHeaderStart, $fileSectionHeaderEnd); + return [$fileSectionHeaderStart, $fileSectionHeaderEnd]; } /** @@ -113,6 +115,7 @@ protected function getFileSectionHeadersFor(string $fileRelativePath): array * * @throws ReflectionException */ + #[\Override] protected function assertData(mixed $data): void { $currentIterator = $this->buildIterator($this->current); @@ -120,9 +123,7 @@ protected function assertData(mixed $data): void $root = rtrim($this->current, '\\/'); /** @var SplFileInfo[] $files */ $files = iterator_to_array($currentIterator, false); - $currentFiles = array_map(static function (SplFileInfo $file) use ($root): string { - return '/' . ltrim(str_replace($root, '', $file->getPathname()), '/'); - }, $files); + $currentFiles = array_map(static fn(SplFileInfo $file): string => '/' . ltrim(str_replace($root, '', $file->getPathname()), '/'), $files); usort($currentFiles, 'strcasecmp'); @@ -136,9 +137,7 @@ protected function assertData(mixed $data): void $multiIterator->attachIterator(new ArrayIterator($snapshotFiles)); $sortedFiles = $files; $sortedFiles = array_combine( - array_map(static function (SplFileInfo $f): string { - return $f->getPathname(); - }, $sortedFiles), + array_map(static fn(SplFileInfo $f): string => $f->getPathname(), $sortedFiles), $sortedFiles ); diff --git a/src/HtmlSnapshot.php b/src/HtmlSnapshot.php index a377b1d..7f191e4 100644 --- a/src/HtmlSnapshot.php +++ b/src/HtmlSnapshot.php @@ -36,6 +36,7 @@ public function __construct($current = null) /** * {@inheritDoc} */ + #[\Override] public function fileExtension(): string { return 'snapshot.html'; @@ -51,6 +52,7 @@ public function fileExtension(): string * * @throws RuntimeException If there's an issue during the HTML string parsing. */ + #[\Override] protected function assertData($data): void { if ($this->dataVisitor !== null) { diff --git a/src/JsonSnapshot.php b/src/JsonSnapshot.php index fe2a036..1c66fc4 100644 --- a/src/JsonSnapshot.php +++ b/src/JsonSnapshot.php @@ -17,6 +17,7 @@ class JsonSnapshot extends AbstractSnapshot /** * {@inheritDoc} */ + #[\Override] public function fileExtension(): string { return 'snapshot.json'; diff --git a/src/SnapshotAssertions.php b/src/SnapshotAssertions.php index 73b9033..1139d26 100644 --- a/src/SnapshotAssertions.php +++ b/src/SnapshotAssertions.php @@ -28,7 +28,7 @@ trait SnapshotAssertions * * @throws ReflectionException */ - public function assertMatchesStringSnapshot(string $current, callable $dataVisitor = null): void + public function assertMatchesStringSnapshot(string $current, ?callable $dataVisitor = null): void { $stringSnapshot = new StringSnapshot($current); $showSnapshotDiff = !property_exists($this, 'showSnapshotDiff') || $this->showSnapshotDiff; @@ -51,7 +51,7 @@ public function assertMatchesStringSnapshot(string $current, callable $dataVisit * @throws InvalidArgumentException If the HTML is not valid. * @throws ReflectionException */ - public function assertMatchesHtmlSnapshot(string $current, callable $dataVisitor = null): void + public function assertMatchesHtmlSnapshot(string $current, ?callable $dataVisitor = null): void { $htmlSnapshot = new HtmlSnapshot($current); $showSnapshotDiff = !property_exists($this, 'showSnapshotDiff') || $this->showSnapshotDiff; @@ -73,7 +73,7 @@ public function assertMatchesHtmlSnapshot(string $current, callable $dataVisitor * * @throws ReflectionException */ - public function assertMatchesJsonSnapshot(string $current, callable $dataVisitor = null): void + public function assertMatchesJsonSnapshot(string $current, ?callable $dataVisitor = null): void { $jsonSnapshot = new JsonSnapshot($current); $showSnapshotDiff = !property_exists($this, 'showSnapshotDiff') || $this->showSnapshotDiff; @@ -99,7 +99,7 @@ public function assertMatchesJsonSnapshot(string $current, callable $dataVisitor public function assertMatchesCodeSnapshot( string $current, string $extension = 'php', - callable $dataVisitor = null + ?callable $dataVisitor = null ): void { $codeSnapshot = new CodeSnapshot($current, $extension); $showSnapshotDiff = !property_exists($this, 'showSnapshotDiff') || $this->showSnapshotDiff; @@ -120,7 +120,7 @@ public function assertMatchesCodeSnapshot( * * @throws ReflectionException */ - public function assertMatchesDirectorySnapshot(string $current, callable $dataVisitor = null): void + public function assertMatchesDirectorySnapshot(string $current, ?callable $dataVisitor = null): void { $dirSnapshot = new DirectorySnapshot($current); $showSnapshotDiff = !property_exists($this, 'showSnapshotDiff') || $this->showSnapshotDiff; diff --git a/src/StringSnapshot.php b/src/StringSnapshot.php index 312685c..91f1290 100644 --- a/src/StringSnapshot.php +++ b/src/StringSnapshot.php @@ -17,6 +17,7 @@ class StringSnapshot extends AbstractSnapshot /** * {@inheritDoc} */ + #[\Override] public function fileExtension(): string { return 'snapshot.txt'; @@ -38,6 +39,7 @@ public function __construct($current = null) * * @return string The string representation of the current value. */ + #[\Override] protected function fetchData(): string { return $this->stringify($this->current); From a7351ee83afdbee7a357507d7d1db650ad6ace47 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 5 Dec 2024 10:04:33 +0100 Subject: [PATCH 2/4] style(src) cs issues --- src/AbstractSnapshot.php | 5 ++++- src/DirectorySnapshot.php | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/AbstractSnapshot.php b/src/AbstractSnapshot.php index 5c09106..c5d75c9 100644 --- a/src/AbstractSnapshot.php +++ b/src/AbstractSnapshot.php @@ -145,7 +145,10 @@ protected static function getTraitMethods(): array } $reflection = new ReflectionClass(SnapshotAssertions::class); - static::$traitMethods = array_map(static fn(ReflectionMethod $method): string => $method->name, $reflection->getMethods()); + static::$traitMethods = array_map( + static fn(ReflectionMethod $method): string => $method->name, + $reflection->getMethods() + ); return static::$traitMethods; } diff --git a/src/DirectorySnapshot.php b/src/DirectorySnapshot.php index 826f8d2..30c8715 100644 --- a/src/DirectorySnapshot.php +++ b/src/DirectorySnapshot.php @@ -123,7 +123,12 @@ protected function assertData(mixed $data): void $root = rtrim($this->current, '\\/'); /** @var SplFileInfo[] $files */ $files = iterator_to_array($currentIterator, false); - $currentFiles = array_map(static fn(SplFileInfo $file): string => '/' . ltrim(str_replace($root, '', $file->getPathname()), '/'), $files); + $currentFiles = array_map( + static function (SplFileInfo $file) use ( $root ): string { + return '/' . ltrim( str_replace( $root, '', $file->getPathname() ), '/' ); + }, + $files + ); usort($currentFiles, 'strcasecmp'); From d74032dc6178201396efb246268e65dfeb2d5beb Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 7 Dec 2024 11:21:59 +0100 Subject: [PATCH 3/4] style(src) cs issues --- src/AbstractSnapshot.php | 4 ++-- src/DirectorySnapshot.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AbstractSnapshot.php b/src/AbstractSnapshot.php index c5d75c9..c50b0d6 100644 --- a/src/AbstractSnapshot.php +++ b/src/AbstractSnapshot.php @@ -146,8 +146,8 @@ protected static function getTraitMethods(): array $reflection = new ReflectionClass(SnapshotAssertions::class); static::$traitMethods = array_map( - static fn(ReflectionMethod $method): string => $method->name, - $reflection->getMethods() + static fn(ReflectionMethod $method): string => $method->name, + $reflection->getMethods() ); return static::$traitMethods; diff --git a/src/DirectorySnapshot.php b/src/DirectorySnapshot.php index 30c8715..4cf9a04 100644 --- a/src/DirectorySnapshot.php +++ b/src/DirectorySnapshot.php @@ -124,10 +124,10 @@ protected function assertData(mixed $data): void /** @var SplFileInfo[] $files */ $files = iterator_to_array($currentIterator, false); $currentFiles = array_map( - static function (SplFileInfo $file) use ( $root ): string { - return '/' . ltrim( str_replace( $root, '', $file->getPathname() ), '/' ); - }, - $files + static function (SplFileInfo $file) use ($root): string { + return '/' . ltrim(str_replace($root, '', $file->getPathname()), '/'); + }, + $files ); usort($currentFiles, 'strcasecmp'); From 0003a07d8a6fa1b57319033107e310ebef7eadef Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 7 Dec 2024 12:07:36 +0100 Subject: [PATCH 4/4] build(.github) refresh CI workflow --- .github/workflows/ci.yaml | 62 --------------------------------------- .github/workflows/ci.yml | 59 +++++++++++++++++++++++++++++++++++++ Makefile | 3 ++ 3 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml deleted file mode 100644 index e290455..0000000 --- a/.github/workflows/ci.yaml +++ /dev/null @@ -1,62 +0,0 @@ -name: CI - -on: push - -jobs: - sniff: - name: Code validation - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.0/install@master - - uses: docker://php:8.0-alpine - with: - args: vendor/bin/phpcs --colors -p --standard=PSR2 -s src - - uses: docker://php:8.0-alpine - with: - args: vendor/bin/phpstan analyze src --level=9 - php80: - name: Test on PHP 8.0 - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.0/install@master - - uses: docker://php:8.0-alpine - with: - args: vendor/bin/codecept run - php81: - name: Test on PHP 8.1 - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.1/install@master - - uses: docker://php:8.1-alpine - with: - args: vendor/bin/codecept run - php82: - name: Test on PHP 8.2 - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.2/install@master - - uses: docker://php:8.2-alpine - with: - args: vendor/bin/codecept run - php83: - name: Test on PHP 8.3 - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.3/install@master - - uses: docker://php:8.3-alpine - with: - args: vendor/bin/codecept run - php84: - name: Test on PHP 8.4 - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - uses: MilesChou/composer-action/8.4/install@master - - uses: docker://php:8.4-alpine - with: - args: vendor/bin/codecept run diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8bbfbc7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,59 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow +name: CI + +on: push + +jobs: + sniff: + name: Code validation + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@master + - name: Setup PHP {{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + tools: composer + - name: Get Composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + - name: Install Composer dependencies + run: composer install --prefer-dist + - name: Run PHPCodeSniffer + run: vendor/bin/phpcs --colors -p --standard=PSR2 -s src + - name: Run PHPStan + run: vendor/bin/phpstan analyze src --level=9 + test: + strategy: + matrix: + php-versions: [8.0, 8.1, 8.2, 8.3, 8.4] + name: Test on PHP ${{ matrix.php-versions }} + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@master + - name: Setup PHP {{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: '${{ matrix.php-versions }}' + tools: composer + - name: Get Composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + - name: Install Composer dependencies + run: composer install --prefer-dist + - name: Run tests + run: vendor/bin/codecept run diff --git a/Makefile b/Makefile index da7a6f8..ebc7862 100644 --- a/Makefile +++ b/Makefile @@ -14,3 +14,6 @@ fix_n_sniff: fix sniff # Runs phpstan on the source files. phpstan: phpstan analyze src --level=9 + +act: + act -P ubuntu-22.04=shivammathur/node:2204 -W '.github/workflows/ci.yml'