diff --git a/.gitignore b/.gitignore index 74f462d..e3c3e07 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ composer.lock cghooks.lock .php-cs-fixer.cache +.phpunit.cache/ diff --git a/composer.json b/composer.json index a9ec495..4179578 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,11 @@ }, "require-dev": { "mockery/mockery": "^1.5", - "phpunit/phpunit": "^9.5|^10.0", - "orchestra/testbench": "^7.4", - "friendsofphp/php-cs-fixer": "^3.8" + "phpunit/phpunit": "^10.0", + "orchestra/testbench": "^8.0", + "friendsofphp/php-cs-fixer": "^3.8", + "brainmaestro/composer-git-hooks": "dev-master", + "laravel/pint": "^1.5" }, "extra": { "laravel": { @@ -57,8 +59,8 @@ "cghooks update" ], "cghooks": "vendor/bin/cghooks", - "check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi", - "fix-style": "php-cs-fixer fix --using-cache=no --ansi", + "check-style": "vendor/bin/pint --test", + "fix-style": "vendor/bin/pint", "test": "phpunit --colors=always" }, "scripts-descriptions": { diff --git a/config/follow.php b/config/follow.php index 0387eb6..0655fdc 100644 --- a/config/follow.php +++ b/config/follow.php @@ -1,7 +1,7 @@ false, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b2daa31..46a93e1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + src/ diff --git a/src/Events/Event.php b/src/Events/Event.php index 34c3f69..250f858 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -7,8 +7,11 @@ class Event { public int|string $followable_id; + public int|string $followable_type; + public int|string $follower_id; + public int|string $user_id; protected Followable $relation; diff --git a/src/FollowServiceProvider.php b/src/FollowServiceProvider.php index 997e4f2..1c7a6fb 100644 --- a/src/FollowServiceProvider.php +++ b/src/FollowServiceProvider.php @@ -9,11 +9,11 @@ class FollowServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - \dirname(__DIR__) . '/config/follow.php' => config_path('follow.php'), + \dirname(__DIR__).'/config/follow.php' => config_path('follow.php'), ], 'config'); $this->publishes([ - \dirname(__DIR__) . '/migrations/' => database_path('migrations'), + \dirname(__DIR__).'/migrations/' => database_path('migrations'), ], 'migrations'); } } diff --git a/src/Followable.php b/src/Followable.php index 534163c..9b4b821 100644 --- a/src/Followable.php +++ b/src/Followable.php @@ -2,21 +2,21 @@ namespace Overtrue\LaravelFollow; +use function config; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphTo; -use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Support\Str; use Overtrue\LaravelFollow\Events\Followed; use Overtrue\LaravelFollow\Events\Unfollowed; -use function config; /** * @property int|string $followable_id; * @property int|string $followable_type; * @property int|string $user_id; + * * @method HasMany of(Model $model) * @method HasMany followedBy(Model $model) * @method HasMany withType(string $type) diff --git a/src/Traits/Followable.php b/src/Traits/Followable.php index 47fbb6c..39aad95 100644 --- a/src/Traits/Followable.php +++ b/src/Traits/Followable.php @@ -2,18 +2,12 @@ namespace Overtrue\LaravelFollow\Traits; -use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder; +use function config; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Pagination\CursorPaginator; -use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Support\Enumerable; -use Illuminate\Support\LazyCollection; use Overtrue\LaravelFollow\Traits\Follower as Follower; -use function config; /** * @property Collection $followables @@ -28,7 +22,7 @@ public function needsToApproveFollowRequests(): bool public function rejectFollowRequestFrom(Model $follower): void { - if (!in_array(Follower::class, \class_uses($follower))) { + if (! in_array(Follower::class, \class_uses($follower))) { throw new \InvalidArgumentException('The model must use the Follower trait.'); } @@ -37,7 +31,7 @@ public function rejectFollowRequestFrom(Model $follower): void public function acceptFollowRequestFrom(Model $follower): void { - if (!in_array(Follower::class, \class_uses($follower))) { + if (! in_array(Follower::class, \class_uses($follower))) { throw new \InvalidArgumentException('The model must use the Follower trait.'); } @@ -46,7 +40,7 @@ public function acceptFollowRequestFrom(Model $follower): void public function isFollowedBy(Model $follower): bool { - if (!in_array(Follower::class, \class_uses($follower))) { + if (! in_array(Follower::class, \class_uses($follower))) { throw new \InvalidArgumentException('The model must use the Follower trait.'); } diff --git a/src/Traits/Follower.php b/src/Traits/Follower.php index 5cee8c3..2383efc 100644 --- a/src/Traits/Follower.php +++ b/src/Traits/Follower.php @@ -2,39 +2,36 @@ namespace Overtrue\LaravelFollow\Traits; +use function abort_if; +use function class_uses; +use function collect; use Illuminate\Contracts\Pagination\Paginator; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Pagination\CursorPaginator; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Enumerable; use Illuminate\Support\LazyCollection; -use InvalidArgumentException; -use JetBrains\PhpStorm\ArrayShape; -use Overtrue\LaravelFollow\Traits\Followable; -use function abort_if; -use function class_uses; -use function collect; use function in_array; +use InvalidArgumentException; use function is_array; use function iterator_to_array; +use JetBrains\PhpStorm\ArrayShape; /** * @property Collection $followings */ trait Follower { - #[ArrayShape(['pending' => "mixed"])] + #[ArrayShape(['pending' => 'mixed'])] public function follow(Model $followable): array { if ($followable->is($this)) { throw new InvalidArgumentException('Cannot follow yourself.'); } - if (!in_array(Followable::class, class_uses($followable))) { + if (! in_array(Followable::class, class_uses($followable))) { throw new InvalidArgumentException('The followable model must use the Followable trait.'); } @@ -45,7 +42,7 @@ public function follow(Model $followable): array 'followable_id' => $followable->getKey(), 'followable_type' => $followable->getMorphClass(), ], [ - 'accepted_at' => $isPending ? null : now() + 'accepted_at' => $isPending ? null : now(), ]); return ['pending' => $isPending]; @@ -53,7 +50,7 @@ public function follow(Model $followable): array public function unfollow(Model $followable): void { - if (!in_array(Followable::class, class_uses($followable))) { + if (! in_array(Followable::class, class_uses($followable))) { throw new InvalidArgumentException('The followable model must use the Followable trait.'); } @@ -67,7 +64,7 @@ public function toggleFollow(Model $followable): void public function isFollowing(Model $followable): bool { - if (!in_array(Followable::class, class_uses($followable))) { + if (! in_array(Followable::class, class_uses($followable))) { throw new InvalidArgumentException('The followable model must use the Followable trait.'); } @@ -84,7 +81,7 @@ public function isFollowing(Model $followable): bool public function hasRequestedToFollow(Model $followable): bool { - if (!in_array(\Overtrue\LaravelFollow\Traits\Followable::class, \class_uses($followable))) { + if (! in_array(\Overtrue\LaravelFollow\Traits\Followable::class, \class_uses($followable))) { throw new InvalidArgumentException('The followable model must use the Followable trait.'); } @@ -144,7 +141,7 @@ public function attachFollowStatus($followables, callable $resolver = null) break; } - abort_if(!($followables instanceof Enumerable), 422, 'Invalid $followables type.'); + abort_if(! ($followables instanceof Enumerable), 422, 'Invalid $followables type.'); $followed = $this->followings()->get(); diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index 693469b..6420d4c 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -3,7 +3,6 @@ namespace Tests; use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Overtrue\LaravelFollow\Events\Followed; use Overtrue\LaravelFollow\Events\Unfollowed; @@ -180,11 +179,6 @@ function () use ($user1, $user2, $user3, $user4) { $this->assertSame(0, $sqls->count()); } - /** - * @param \Closure $callback - * - * @return \Illuminate\Support\Collection - */ protected function getQueryLog(\Closure $callback): \Illuminate\Support\Collection { $sqls = \collect([]); diff --git a/tests/PrivacyTest.php b/tests/PrivacyTest.php index 2792c45..e84d754 100644 --- a/tests/PrivacyTest.php +++ b/tests/PrivacyTest.php @@ -2,11 +2,6 @@ namespace Tests; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Event; -use Overtrue\LaravelFollow\Events\Followed; -use Overtrue\LaravelFollow\Events\Unfollowed; - class PrivacyTest extends TestCase { public function setUp(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 879e929..77e9564 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,8 +9,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase /** * Load package service provider. * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return array */ protected function getPackageProviders($app) @@ -21,7 +20,7 @@ protected function getPackageProviders($app) /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app */ protected function getEnvironmentSetUp($app) { @@ -37,7 +36,7 @@ protected function getEnvironmentSetUp($app) protected function setUp(): void { parent::setUp(); - $this->loadMigrationsFrom(__DIR__ . '/migrations'); - $this->loadMigrationsFrom(dirname(__DIR__) . '/migrations'); + $this->loadMigrationsFrom(__DIR__.'/migrations'); + $this->loadMigrationsFrom(dirname(__DIR__).'/migrations'); } } diff --git a/tests/User.php b/tests/User.php index 1d46a29..a6ffb50 100644 --- a/tests/User.php +++ b/tests/User.php @@ -17,9 +17,6 @@ class User extends Model 'private' => 'boolean', ]; - /** - * @return bool - */ public function needsToApproveFollowRequests(): bool { return $this->private ?? false;