Skip to content

refactor(src) PHP 8.4 compatibility #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions .github/workflows/ci.yaml

This file was deleted.

59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
29 changes: 17 additions & 12 deletions src/AbstractSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AbstractSnapshot extends Snapshot
* @param array<int|string,mixed>|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());
}
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -146,9 +145,10 @@ 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;
}
Expand Down Expand Up @@ -214,6 +214,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);
Expand Down Expand Up @@ -245,6 +246,7 @@ public function prepareSnapshotForDump(): string|false
* @throws Exception
* @throws Exception
*/
#[\Override]
public function assert(): void
{
// Fetch data.
Expand Down Expand Up @@ -299,6 +301,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)) {
Expand All @@ -313,6 +316,7 @@ protected function fetchData(): string|false
*
* @throws ReflectionException
*/
#[\Override]
protected function load(): void
{
$filename = $this->getFileName();
Expand All @@ -335,7 +339,7 @@ protected function load(): void
*/
protected function printDebug(string $message): void
{
Debug::debug($this::class . ': ' . $message);
Debug::debug(static::class . ': ' . $message);
}

/**
Expand Down Expand Up @@ -373,6 +377,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) {
Expand Down
1 change: 1 addition & 0 deletions src/CodeSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct($current = null, string $extension = 'php')
/**
* {@inheritDoc}
*/
#[\Override]
public function fileExtension(): string
{
return 'snapshot.'.$this->extension;
Expand Down
18 changes: 11 additions & 7 deletions src/DirectorySnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct($current = null)
/**
* {@inheritDoc}
*/
#[\Override]
protected function fetchData(): string|false
{
return $this->prepareSnapshotForDump();
Expand Down Expand Up @@ -69,6 +70,7 @@ protected function buildIterator(string $dir): RecursiveIteratorIterator
/**
* {@inheritDoc}
*/
#[\Override]
public function prepareSnapshotForDump(): string
{
if ($this->preparedSnapshot !== null) {
Expand Down Expand Up @@ -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];
}

/**
Expand All @@ -113,16 +115,20 @@ protected function getFileSectionHeadersFor(string $fileRelativePath): array
*
* @throws ReflectionException
*/
#[\Override]
protected function assertData(mixed $data): void
{
$currentIterator = $this->buildIterator($this->current);
$snapshotFiles = $this->readFileListFromSnapshot($this->getFileName());
$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 function (SplFileInfo $file) use ($root): string {
return '/' . ltrim(str_replace($root, '', $file->getPathname()), '/');
},
$files
);

usort($currentFiles, 'strcasecmp');

Expand All @@ -136,9 +142,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
);

Expand Down
2 changes: 2 additions & 0 deletions src/HtmlSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct($current = null)
/**
* {@inheritDoc}
*/
#[\Override]
public function fileExtension(): string
{
return 'snapshot.html';
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/JsonSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class JsonSnapshot extends AbstractSnapshot
/**
* {@inheritDoc}
*/
#[\Override]
public function fileExtension(): string
{
return 'snapshot.json';
Expand Down
10 changes: 5 additions & 5 deletions src/SnapshotAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/StringSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StringSnapshot extends AbstractSnapshot
/**
* {@inheritDoc}
*/
#[\Override]
public function fileExtension(): string
{
return 'snapshot.txt';
Expand All @@ -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);
Expand Down