Skip to content

Commit dea7897

Browse files
chore: use mago for linting and formating (#54)
BREAKING CHANGE: the `BackupPipe` abstract now requires return types to be specified
1 parent 97ed5bb commit dea7897

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+793
-567
lines changed

.github/workflows/build-docs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
name: Deploy VitePress site to Pages
44

55
on:
6-
# TODO: Change to tag?
76
push:
87
tags:
98
- "v*"

.github/workflows/run-lint.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
name: Lint
22
on: push
33
jobs:
4-
pint:
4+
mago:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v1
8-
- name: "laravel-pint"
9-
uses: aglipanci/[email protected]
7+
- name: "checkout"
8+
uses: "actions/checkout@v4"
9+
10+
- name: "installing PHP"
11+
uses: "shivammathur/setup-php@v2"
1012
with:
11-
verboseMode: true
12-
testMode: true
13+
php-version: "8.3"
14+
15+
- name: "installing dependencies"
16+
run: composer install --no-interaction --prefer-dist --optimize-autoloader
17+
18+
- name: "formatting"
19+
run: php vendor/bin/mago fmt --dry-run
20+
21+
- name: "linting"
22+
run: php vendor/bin/mago lint --reporting-format=github

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@v3
18+
uses: actions/checkout@v4
1919

2020
- name: Setup PHP
2121
uses: shivammathur/setup-php@v2

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
"pixelfear/composer-dist-plugin": "^0.1.6"
1515
},
1616
"require-dev": {
17-
"laravel/pint": "^1.13",
1817
"orchestra/testbench": "^9.0",
1918
"pestphp/pest": "^3.0",
20-
"pestphp/pest-plugin-laravel": "^3.0"
19+
"pestphp/pest-plugin-laravel": "^3.0",
20+
"carthage-software/mago": "^0.10.0"
2121
},
2222
"autoload": {
2323
"psr-4": {
@@ -48,12 +48,16 @@
4848
"@php vendor/bin/testbench serve"
4949
],
5050
"lint": [
51-
"@php vendor/bin/pint"
51+
"@php vendor/bin/mago lint"
52+
],
53+
"format": [
54+
"@php vendor/bin/mago format"
5255
],
5356
"test": [
5457
"XDEBUG_MODE=coverage vendor/bin/pest --coverage"
5558
],
5659
"qa": [
60+
"@format",
5761
"@lint",
5862
"@test"
5963
]
@@ -76,7 +80,8 @@
7680
"config": {
7781
"allow-plugins": {
7882
"pixelfear/composer-dist-plugin": true,
79-
"pestphp/pest-plugin": true
83+
"pestphp/pest-plugin": true,
84+
"carthage-software/mago": true
8085
}
8186
}
8287
}

composer.lock

Lines changed: 54 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mago.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[source]
2+
paths = ["src", "tests"]
3+
includes = ["vendor"]
4+
5+
[format]
6+
method_chain_break_threshold = 2
7+
null_type_hint = "question"
8+
9+
[[linter.rules]]
10+
name = "strictness/require-return-type"
11+
ignore_arrow_function = true

pint.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Abstracts/BackupPipe.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ abstract public static function getKey(): string;
1818
* Run the restore process.
1919
*
2020
* @param string $path The path to the root of the backup file.
21+
* @param Closure(string $path):string $next The next pipe in the chain.
2122
*/
22-
abstract public function restore(string $path, Closure $next);
23+
abstract public function restore(string $path, Closure $next): string;
2324

2425
/**
2526
* Run the backup process.
27+
*
28+
* @param Zipper $zip The zipper instance.
29+
* @param Closure(Zipper $zip):Zipper $next The next pipe in the chain.
2630
*/
27-
abstract public function backup(Zipper $zip, Closure $next);
31+
abstract public function backup(Zipper $zip, Closure $next): Zipper;
2832

2933
/**
3034
* Get the directory path for the current pipe.
@@ -37,7 +41,7 @@ protected function getDirectoryPath(string $path): string
3741
/**
3842
* Mark pipe as skipped.
3943
*/
40-
protected function skip(string $reason, Closure $next, Zipper $zip)
44+
protected function skip(string $reason, Closure $next, Zipper $zip): Zipper
4145
{
4246
$zip->addMeta(static::class, ['skipped' => $reason]);
4347
return $next($zip);

src/Backuper.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Itiden\Backup;
66

77
use Exception;
8+
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Facades\File;
810
use Illuminate\Support\Facades\Pipeline;
911
use Itiden\Backup\Contracts\Repositories\BackupRepository;
1012
use Itiden\Backup\Support\Zipper;
@@ -15,7 +17,7 @@
1517
final class Backuper
1618
{
1719
public function __construct(
18-
protected BackupRepository $repository
20+
protected BackupRepository $repository,
1921
) {
2022
}
2123

@@ -36,7 +38,9 @@ public function backup(): BackupDto
3638
->through(config('backup.pipeline'))
3739
->thenReturn();
3840

39-
if ($password = config('backup.password')) {
41+
$password = config('backup.password');
42+
43+
if ($password) {
4044
$zipper->encrypt($password);
4145
}
4246

@@ -50,17 +54,23 @@ public function backup(): BackupDto
5054

5155
$metadata = $backup->getMetadata();
5256

53-
if ($user = auth()->user()) {
57+
$user = auth()->user();
58+
59+
if ($user) {
5460
$metadata->setCreatedBy($user);
5561
}
5662

57-
$zipMeta->each(fn ($meta, $key) => match ($key) {
58-
'skipped' => $meta->each(fn (string $reason, string $pipe) => $metadata->addSkippedPipe($pipe, $reason)),
59-
});
63+
$zipMeta->each(
64+
static fn(Collection $meta, string $key): mixed => match ($key) {
65+
'skipped' => $meta->each(function (string $reason, string $pipe) use ($metadata): void {
66+
$metadata->addSkippedPipe(pipe: $pipe, reason: $reason);
67+
}),
68+
},
69+
);
6070

6171
event(new BackupCreated($backup));
6272

63-
@unlink($temp_zip_path);
73+
File::delete($temp_zip_path);
6474

6575
$this->enforceMaxBackups();
6676

@@ -76,17 +86,22 @@ public function backup(): BackupDto
7686
}
7787
}
7888

79-
private function resolveMetaFromZip(Zipper $zip)
89+
/**
90+
* @return Collection<string, Collection<string|int, mixed>>
91+
*/
92+
private function resolveMetaFromZip(Zipper $zip): Collection
8093
{
81-
$metadata = collect([
82-
'skipped' => collect(),
83-
]);
84-
85-
$zip->getMeta()->each(function ($meta, $key) use ($metadata) {
86-
if (isset($meta['skipped'])) {
87-
$metadata->get('skipped')->put($key, $meta['skipped']);
88-
}
89-
});
94+
$metadata = collect(['skipped' => collect()]);
95+
96+
$zip
97+
->getMeta()
98+
->each(static function (array|string $meta, string $key) use ($metadata): void {
99+
if (is_array($meta) && isset($meta['skipped'])) {
100+
$metadata
101+
->get('skipped')
102+
->put($key, $meta['skipped']);
103+
}
104+
});
90105

91106
return $metadata;
92107
}
@@ -96,16 +111,17 @@ private function resolveMetaFromZip(Zipper $zip)
96111
*/
97112
private function enforceMaxBackups(): void
98113
{
99-
if (!$max_backups = config('backup.max_backups', false)) {
114+
$maxBackups = config('backup.max_backups', false);
115+
if (!$maxBackups) {
100116
return;
101117
}
102118

103119
$backups = $this->repository->all();
104120

105-
if ($backups->count() > $max_backups) {
106-
$backups->slice($max_backups)->each(function ($backup) {
107-
$this->repository->remove($backup->timestamp);
108-
});
121+
if ($backups->count() > $maxBackups) {
122+
$backups
123+
->slice($maxBackups)
124+
->each(fn(BackupDto $backup): ?BackupDto => $this->repository->remove($backup->timestamp));
109125
}
110126
}
111127
}

0 commit comments

Comments
 (0)