Skip to content

Commit 5736cad

Browse files
committed
Run Rector to apply Laravel-focused upgrades and improve type safety
1 parent e1471af commit 5736cad

18 files changed

+90
-55
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"require": {
2020
"php": "^8.0",
21+
"driftingly/rector-laravel": "^2.1",
2122
"illuminate/contracts": "^11.0|^12.0",
2223
"spatie/laravel-package-tools": "^1.9.2"
2324
},

rector.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,27 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Set\LaravelSetList;
7+
use RectorLaravel\Set\LaravelSetProvider;
68

79
return RectorConfig::configure()
10+
->withSetProviders(LaravelSetProvider::class)
11+
->withSets([
12+
LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL,
13+
LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL,
14+
LaravelSetList::LARAVEL_CODE_QUALITY,
15+
LaravelSetList::LARAVEL_COLLECTION,
16+
LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME,
17+
LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER,
18+
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
19+
LaravelSetList::LARAVEL_FACTORIES,
20+
LaravelSetList::LARAVEL_IF_HELPERS,
21+
LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES,
22+
])
23+
->withImportNames(
24+
removeUnusedImports: true,
25+
)
26+
->withComposerBased(laravel: true)
827
->withPaths([
928
__DIR__ . '/config',
1029
__DIR__ . '/src',
@@ -18,5 +37,4 @@
1837
typeDeclarations: true,
1938
privatization: true,
2039
earlyReturn: true,
21-
strictBooleans: true,
2240
);

src/Concerns/HasDrafts.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Oddvalue\LaravelDrafts\Concerns;
44

5-
use Illuminate\Contracts\Database\Query\Builder as QueryBuilder;
5+
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -339,12 +339,12 @@ public function save(array $options = []): bool
339339
return parent::save($options);
340340
}
341341

342-
public static function savingAsDraft(string|\Closure $callback): void
342+
public static function savingAsDraft(string|Closure $callback): void
343343
{
344344
static::registerModelEvent('savingAsDraft', $callback);
345345
}
346346

347-
public static function savedAsDraft(string|\Closure $callback): void
347+
public static function savedAsDraft(string|Closure $callback): void
348348
{
349349
static::registerModelEvent('drafted', $callback);
350350
}
@@ -491,7 +491,7 @@ public function publisher(): MorphTo
491491
/**
492492
* @param Builder<Model> $query
493493
*/
494-
public function scopeCurrent(Builder $query): void
494+
protected function scopeCurrent(Builder $query): void
495495
{
496496
/** @phpstan-ignore method.notFound, method.nonObject */
497497
$query->withDrafts()->where($this->getIsCurrentColumn(), true);
@@ -500,15 +500,15 @@ public function scopeCurrent(Builder $query): void
500500
/**
501501
* @param Builder<Model> $query
502502
*/
503-
public function scopeWithoutCurrent(Builder $query): void
503+
protected function scopeWithoutCurrent(Builder $query): void
504504
{
505505
$query->where($this->getIsCurrentColumn(), false);
506506
}
507507

508508
/**
509509
* @param Builder<Model> $query
510510
*/
511-
public function scopeExcludeRevision(Builder $query, int | Model $exclude): void
511+
protected function scopeExcludeRevision(Builder $query, int | Model $exclude): void
512512
{
513513
$query->where($this->getKeyName(), '!=', is_int($exclude) ? $exclude : $exclude->getKey());
514514
}
@@ -517,7 +517,7 @@ public function scopeExcludeRevision(Builder $query, int | Model $exclude): void
517517
* @deprecated This doesn't actually work, will be removed in next version
518518
* @param Builder<Model> $query
519519
*/
520-
public function scopeWithoutSelf(Builder $query): void
520+
protected function scopeWithoutSelf(Builder $query): void
521521
{
522522
/** @phpstan-ignore argument.type */
523523
$query->where('id', '!=', $this->id);
@@ -532,7 +532,7 @@ public function scopeWithoutSelf(Builder $query): void
532532
/**
533533
* @return static|null
534534
*/
535-
public function getDraftAttribute(): ?self
535+
protected function getDraftAttribute(): ?self
536536
{
537537
if ($this->relationLoaded('drafts')) {
538538
/** @phpstan-ignore return.type */

src/Concerns/Publishes.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Oddvalue\LaravelDrafts\Concerns;
44

5+
use Closure;
6+
use Illuminate\Database\Eloquent\Builder;
57
use Illuminate\Database\Eloquent\Model;
68
use Oddvalue\LaravelDrafts\Scopes\PublishingScope;
79

810
/**
9-
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Illuminate\Database\Query\Builder withPublished(bool $withPublished = true)
10-
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Illuminate\Database\Query\Builder onlyPublished()
11-
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Illuminate\Database\Query\Builder withoutPublished()
11+
* @method static Builder<static>|\Illuminate\Database\Query\Builder withPublished(bool $withPublished = true)
12+
* @method static Builder<static>|\Illuminate\Database\Query\Builder onlyPublished()
13+
* @method static Builder<static>|\Illuminate\Database\Query\Builder withoutPublished()
1214
*/
1315
trait Publishes
1416
{
@@ -70,15 +72,15 @@ public function isPublished(): bool
7072
/**
7173
* Register a "published" model event callback with the dispatcher.
7274
*/
73-
public static function publishing(string|\Closure $callback): void
75+
public static function publishing(string|Closure $callback): void
7476
{
7577
static::registerModelEvent('publishing', $callback);
7678
}
7779

7880
/**
7981
* Register a "softDeleted" model event callback with the dispatcher.
8082
*/
81-
public static function published(string|\Closure $callback): void
83+
public static function published(string|Closure $callback): void
8284
{
8385
static::registerModelEvent('published', $callback);
8486
}

src/Facades/LaravelDrafts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Oddvalue\LaravelDrafts\Facades;
44

5+
use Illuminate\Contracts\Auth\Authenticatable;
56
use Illuminate\Support\Facades\Facade;
67

78
/**
8-
* @method static \Illuminate\Contracts\Auth\Authenticatable getCurrentUser()
9+
* @method static Authenticatable getCurrentUser()
910
* @method static void previewMode(bool $previewMode = true)
1011
* @method static void disablePreviewMode()
1112
* @method static bool isPreviewModeEnabled()

src/LaravelDraftsServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oddvalue\LaravelDrafts;
44

5+
use Closure;
56
use Illuminate\Contracts\Http\Kernel;
67
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Facades\Route;
@@ -28,8 +29,7 @@ public function packageRegistered(): void
2829
{
2930
$this->app->singleton(LaravelDrafts::class, fn (): LaravelDrafts => new LaravelDrafts());
3031

31-
/** @phpstan-ignore offsetAccess.nonOffsetAccessible, method.nonObject */
32-
$this->app[Kernel::class]->prependToMiddlewarePriority(WithDraftsMiddleware::class);
32+
$this->app->make(Kernel::class)->prependToMiddlewarePriority(WithDraftsMiddleware::class);
3333

3434
Blueprint::macro('drafts', function (
3535
?string $uuid = null,
@@ -87,7 +87,7 @@ public function packageRegistered(): void
8787
]);
8888
});
8989

90-
Route::macro('withDrafts', function (\Closure $routes): void {
90+
Route::macro('withDrafts', function (Closure $routes): void {
9191
Route::middleware(WithDraftsMiddleware::class)->group($routes);
9292
});
9393
}

src/Scopes/PublishingScope.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Oddvalue\LaravelDrafts\Scopes;
44

5-
use Illuminate\Contracts\Database\Query\Builder as QueryBuilder;
65
use Illuminate\Database\Eloquent\Builder;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Scope;

tests/ConfigTest.php

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

33

4+
use Illuminate\Database\Eloquent\Factories\HasFactory;
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Oddvalue\LaravelDrafts\Concerns\HasDrafts;
48
use Oddvalue\LaravelDrafts\Tests\app\Models\Post;
59

610
it('can override columns via config', function (): void {
@@ -13,7 +17,7 @@
1317
'publisher_morph_name' => 'publisher_override',
1418
],
1519
]);
16-
$post = Post::make();
20+
$post = Post::query()->make();
1721

1822
expect($post->getPublishedAtColumn())->toBe('published_at_override')
1923
->and($post->getIsPublishedColumn())->toBe('is_published_override')
@@ -48,7 +52,7 @@
4852

4953
it('honors column name overrides', function (): void {
5054

51-
$post = OverridePost::make([
55+
$post = OverridePost::query()->make([
5256

5357
]);
5458
invade($post)->newRevision();
@@ -60,10 +64,11 @@
6064
->and($post->getPublisherColumns())->toBe(['id' => 'publisher_override_id', 'type' => 'publisher_override_type']);
6165
});
6266

63-
class OverridePost extends \Illuminate\Database\Eloquent\Model
67+
class OverridePost extends Model
6468
{
65-
use \Oddvalue\LaravelDrafts\Concerns\HasDrafts;
66-
use \Illuminate\Database\Eloquent\SoftDeletes;
69+
use HasFactory;
70+
use HasDrafts;
71+
use SoftDeletes;
6772

6873
public const PUBLISHED_AT = 'published_at_override';
6974

tests/DraftableRelationsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
expect($draft->fresh()->sections)->toHaveCount(2)
2525
->and($post->fresh()->sections)->toHaveCount(2)
26-
->and(PostSection::count())->toBe(4);
26+
->and(PostSection::query()->count())->toBe(4);
2727
});
2828

2929
it('can draft BelongsToMany relations', function (): void {
@@ -44,7 +44,7 @@
4444

4545
expect($draft->fresh()->tags)->toHaveCount(2)
4646
->and($post->fresh()->tags)->toHaveCount(2)
47-
->and(Tag::count())->toBe(2)
47+
->and(Tag::query()->count())->toBe(2)
4848
->and(DB::table('post_tag')->count())->toBe(4);
4949
});
5050

@@ -66,7 +66,7 @@
6666

6767
expect($draft->fresh()->morphToTags)->toHaveCount(2)
6868
->and($post->fresh()->morphToTags)->toHaveCount(2)
69-
->and(Tag::count())->toBe(2)
69+
->and(Tag::query()->count())->toBe(2)
7070
->and(DB::table('taggables')->count())->toBe(4);
7171
});
7272

@@ -88,5 +88,5 @@
8888

8989
expect($draft->fresh()->section)->toBeInstanceOf(PostSection::class)
9090
->and($post->fresh()->section)->toBeInstanceOf(PostSection::class)
91-
->and(PostSection::count())->toBe(2);
91+
->and(PostSection::query()->count())->toBe(2);
9292
});

tests/DraftsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@
118118
$post->updateAsDraft(['title' => 'Baz']);
119119
$post->updateAsDraft(['title' => 'Qux']);
120120

121-
expect(Post::find($originalId)->title)->toBe('Foo')
121+
expect(Post::query()->find($originalId)->title)->toBe('Foo')
122122
->and(DB::table('posts')->count())->toBe(4);
123123

124124
$post->publish()->save();
125125

126-
expect(Post::find($originalId)->title)->toBe('Qux');
126+
expect(Post::query()->find($originalId)->title)->toBe('Qux');
127127
});
128128

129129
it('can get all revisions excluding self', function (): void {

0 commit comments

Comments
 (0)