Skip to content
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

Code health updates #151

Merged
merged 7 commits into from
Dec 30, 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
12 changes: 2 additions & 10 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.2, 8.1, 8.0]
laravel: ['8.*', '9.*', '10.*', '11.*']
php: [8.4, 8.3, 8.2, 8.1]
laravel: ['10.*', '11.*']
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: ^6.23
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
}
],
"require": {
"php": "^8.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0",
"league/flysystem": "^1.0.41|^2.0|^3.0",
"php": "^8.1",
"illuminate/support": "^10.0|^11.0",
"league/flysystem": "^3.0",
"spatie/db-dumper": "^3.3",
"spatie/laravel-package-tools": "^1.6",
"spatie/temporary-directory": "^2.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0",
"pestphp/pest-plugin-laravel": "^1.3|^2.3",
"phpunit/phpunit": "^9.5|^10.5"
"orchestra/testbench": "^8.22.3|^9.0.4",
"pestphp/pest-plugin-laravel": "^2.4",
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\DbSnapshots\Commands;

use Illuminate\Console\Command;
use Spatie\DbSnapshots\Snapshot;
use Spatie\DbSnapshots\SnapshotRepository;

class Cleanup extends Command
Expand All @@ -23,6 +24,6 @@ public function handle()
return;
}

$snapshots->splice($keep)->each(fn ($snapshot) => $snapshot->delete());
$snapshots->splice($keep)->each(fn (Snapshot $snapshot) => $snapshot->delete());
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/CannotCreateDisk.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public static function diskNotDefined(string $diskName): static

$existingDiskNames = implode(', ', array_keys($disks));

return new static("Cannot create a disk `{$diskName}`. Known disknames are {$existingDiskNames}.");
return new static("Cannot create a disk `{$diskName}`. Known disk names are {$existingDiskNames}.");
}
}
16 changes: 8 additions & 8 deletions src/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function __construct(Disk $disk, string $fileName)
$this->name = pathinfo($fileName, PATHINFO_FILENAME);
}

public function useStream()
public function useStream(): self
{
$this->useStream = true;

return $this;
}

public function load(string $connectionName = null, bool $dropTables = true): void
public function load(?string $connectionName = null, bool $dropTables = true): void
{
event(new LoadingSnapshot($this));

Expand All @@ -65,7 +65,7 @@ public function load(string $connectionName = null, bool $dropTables = true): vo
event(new LoadedSnapshot($this));
}

protected function loadAsync(string $connectionName = null)
protected function loadAsync(?string $connectionName = null): void
{
$dbDumpContents = $this->disk->get($this->fileName);

Expand All @@ -78,7 +78,7 @@ protected function loadAsync(string $connectionName = null)

protected function isASqlComment(string $line): bool
{
return substr($line, 0, 2) === '--';
return str_starts_with($line, '--');
}

protected function shouldIgnoreLine(string $line): bool
Expand All @@ -88,7 +88,7 @@ protected function shouldIgnoreLine(string $line): bool
return empty($line) || $this->isASqlComment($line);
}

protected function loadStream(string $connectionName = null)
protected function loadStream(?string $connectionName = null): void
{
LazyCollection::make(function () {
$stream = $this->compressionExtension === 'gz'
Expand Down Expand Up @@ -116,14 +116,14 @@ protected function loadStream(string $connectionName = null)
break;
}

if (substr(trim($statement), -1, 1) === ';') {
if (str_ends_with(trim($statement), ';')) {
yield $statement;
$statement = '';
}
}
}

if (substr(trim($statement), -1, 1) === ';') {
if (str_ends_with(trim($statement), ';')) {
yield $statement;
}
})->each(function (string $statement) use ($connectionName) {
Expand All @@ -150,7 +150,7 @@ public function createdAt(): Carbon
return Carbon::createFromTimestamp($this->disk->lastModified($this->fileName));
}

protected function dropAllCurrentTables()
protected function dropAllCurrentTables(): void
{
DB::connection(DB::getDefaultConnection())
->getSchemaBuilder()
Expand Down
5 changes: 4 additions & 1 deletion src/SnapshotRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function __construct(
//
}

/**
* @return Collection<Snapshot>
*/
public function getAll(): Collection
{
return collect($this->disk->allFiles())
Expand All @@ -29,7 +32,7 @@ public function getAll(): Collection
->sortByDesc(fn (Snapshot $snapshot) => $snapshot->createdAt()->toDateTimeString());
}

public function findByName(string $name)
public function findByName(string $name): ?Snapshot
{
return $this->getAll()->first(fn (Snapshot $snapshot) => $snapshot->name === $name);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Commands/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
});

it('can create a snapshot with specific name')
->tap(fn () => Artisan::call('snapshot:create', ['name' => 'test']))
->defer(fn () => Artisan::call('snapshot:create', ['name' => 'test']))
->expect('test.sql')
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');

Expand Down
Loading