Skip to content

Commit d31d5e5

Browse files
Update: Improved code quality.
1 parent 0ee660c commit d31d5e5

File tree

8 files changed

+51
-38
lines changed

8 files changed

+51
-38
lines changed

database/factories/TestModelFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codelabmw\InfiniteScroll\Database\Factories;
46

57
use Codelabmw\Tests\Fixtures\TestModel;
@@ -8,7 +10,7 @@
810
/**
911
* @extends Factory<TestModel>
1012
*/
11-
class TestModelFactory extends Factory
13+
final class TestModelFactory extends Factory
1214
{
1315
/**
1416
* The name of the factory's corresponding model.
@@ -24,4 +26,4 @@ public function definition(): array
2426
{
2527
return [];
2628
}
27-
}
29+
}

database/migrations/create_test_table.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use Illuminate\Database\Migrations\Migration;
46
use Illuminate\Database\Schema\Blueprint;
57
use Illuminate\Support\Facades\Schema;
68

7-
return new class () extends Migration
9+
return new class() extends Migration
810
{
911
public function up(): void
1012
{
@@ -18,4 +20,4 @@ public function down(): void
1820
{
1921
Schema::dropIfExists('test_models');
2022
}
21-
};
23+
};

src/Enums/PaginationType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codelabmw\InfiniteScroll\Enums;
46

57
enum PaginationType: string
68
{
79
case CURSOR = 'cursor';
810
case PAGED = 'paged';
9-
}
11+
}

src/Facades/InfiniteScroll.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codelabmw\InfiniteScroll\Facades;
46

57
use Illuminate\Support\Facades\Facade;
68

7-
class InfiniteScroll extends Facade
9+
/**
10+
* @codeCoverageIgnore
11+
*/
12+
final class InfiniteScroll extends Facade
813
{
914
/**
1015
* Get the registered name of the component.
@@ -15,4 +20,4 @@ protected static function getFacadeAccessor()
1520
{
1621
return 'infinite-scroll';
1722
}
18-
}
23+
}

src/InfiniteScroll.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codelabmw\InfiniteScroll;
46

7+
use Codelabmw\InfiniteScroll\Enums\PaginationType;
58
use Illuminate\Contracts\Pagination\CursorPaginator;
69
use Illuminate\Contracts\Pagination\Paginator;
710
use Illuminate\Database\Eloquent\Builder;
811
use Inertia\Inertia;
9-
use Codelabmw\InfiniteScroll\Enums\PaginationType;
1012

11-
class InfiniteScroll
13+
final class InfiniteScroll
1214
{
1315
/**
1416
* Sets up infinite scrolling props for a given query.
15-
*
16-
* @param string $key
17-
* @param Builder|CursorPaginator|Paginator $paginator
18-
* @param int $perPage
19-
* @param array<int, string> $columns
20-
*
17+
*
18+
* @param array<int, string> $columns
2119
* @return array<string, mixed>
20+
*
21+
* @phpstan-ignore-next-line missingType.generics
2222
*/
2323
public function make(string $key, Builder|CursorPaginator|Paginator $paginator, int $perPage = 15, array $columns = ['*']): array
2424
{
2525
if ($paginator instanceof Paginator) {
2626
return [
27-
$key => Inertia::defer(fn() => $paginator->items())->deepMerge(),
28-
'type' => fn() => PaginationType::PAGED,
29-
'page' => fn() => $paginator->currentPage(),
30-
'hasMore' => fn() => $paginator->hasMorePages(),
31-
'perPage' => fn() => $perPage,
27+
$key => Inertia::defer(fn () => $paginator->items())->deepMerge(),
28+
'type' => fn (): PaginationType => PaginationType::PAGED,
29+
'page' => fn () => $paginator->currentPage(),
30+
'hasMore' => fn () => $paginator->hasMorePages(),
31+
'perPage' => fn (): int => $perPage,
3232
];
3333
}
3434

@@ -37,11 +37,11 @@ public function make(string $key, Builder|CursorPaginator|Paginator $paginator,
3737
}
3838

3939
return [
40-
$key => Inertia::defer(fn() => $paginator->items())->deepMerge(),
41-
'type' => fn() => PaginationType::CURSOR,
42-
'cursor' => fn() => $paginator->nextCursor()?->encode(),
43-
'hasMore' => fn() => $paginator->hasMorePages(),
44-
'perPage' => fn() => $perPage,
40+
$key => Inertia::defer(fn () => $paginator->items())->deepMerge(),
41+
'type' => fn (): PaginationType => PaginationType::CURSOR,
42+
'cursor' => fn () => $paginator->nextCursor()?->encode(),
43+
'hasMore' => fn () => $paginator->hasMorePages(), // @phpstan-ignore-line method.notFound
44+
'perPage' => fn (): int => $perPage,
4545
];
4646
}
47-
}
47+
}

src/InfiniteScrollServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Codelabmw\InfiniteScroll;
66

77
use Illuminate\Support\ServiceProvider;
8-
use Inertia\Inertia;
98

109
final class InfiniteScrollServiceProvider extends ServiceProvider
1110
{
@@ -14,7 +13,7 @@ final class InfiniteScrollServiceProvider extends ServiceProvider
1413
*/
1514
public function register(): void
1615
{
17-
$this->app->bind('infinite-scroll', fn() => new InfiniteScroll());
16+
$this->app->bind('infinite-scroll', fn (): InfiniteScroll => new InfiniteScroll());
1817
}
1918

2019
/**

tests/Fixtures/TestModel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codelabmw\Tests\Fixtures;
46

57
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Database\Eloquent\Model;
79

8-
class TestModel extends Model
10+
final class TestModel extends Model
911
{
1012
/** @uses HasFactory<\Codelabmw\InfiniteScroll\Database\Factories\TestModelFactory> */
1113
use HasFactory;
1214

1315
/** @var array<string, mixed> */
1416
protected $guarded = [];
15-
}
17+
}

tests/Unit/InfiniteScrollTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use Codelabmw\InfiniteScroll\Enums\PaginationType;
46
use Codelabmw\InfiniteScroll\Facades\InfiniteScroll;
57
use Codelabmw\Tests\Fixtures\TestModel;
68
use Inertia\DeferProp;
79

8-
beforeEach(function () {
10+
beforeEach(function (): void {
911
// Arrange
1012
TestModel::factory()->count(20)->create();
11-
})->only();
12-
13+
});
1314

14-
test('returns cursor pagination data given query', function () {
15+
test('returns cursor pagination data given query', function (): void {
1516
// Act
1617
$result = InfiniteScroll::make('test', TestModel::query());
1718

@@ -25,7 +26,7 @@
2526
expect($result['perPage']())->toBeInt();
2627
});
2728

28-
test('returns cursor pagination data given cursor object', function () {
29+
test('returns cursor pagination data given cursor object', function (): void {
2930
// Act
3031
$result = InfiniteScroll::make('test', TestModel::query()->cursorPaginate());
3132

@@ -39,7 +40,7 @@
3940
expect($result['perPage']())->toBeInt();
4041
});
4142

42-
test('returns pagination data given a pagination object', function () {
43+
test('returns pagination data given a pagination object', function (): void {
4344
// Act
4445
$result = InfiniteScroll::make('test', TestModel::query()->paginate());
4546

@@ -53,7 +54,7 @@
5354
expect($result['perPage']())->toBeInt();
5455
});
5556

56-
test('returns pagination data given a simple pagination object', function () {
57+
test('returns pagination data given a simple pagination object', function (): void {
5758
// Act
5859
$result = InfiniteScroll::make('test', TestModel::query()->simplePaginate());
5960

@@ -65,4 +66,4 @@
6566
expect($result['page']())->toBeInt();
6667
expect($result['hasMore']())->toBeTrue();
6768
expect($result['perPage']())->toBeInt();
68-
});
69+
});

0 commit comments

Comments
 (0)