From f968b45077af95a2af1095cc59a2500500a5c361 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Tue, 31 Oct 2023 08:44:42 +0000 Subject: [PATCH] chore: enable phpstan, apply fixes --- .github/workflows/backend.yml | 1 + composer.json | 10 +++++++--- extend.php | 4 ++-- phpstan.neon | 13 +++++++++++++ src/Commands/BanUserHandler.php | 10 +--------- src/Commands/DeleteBannedIPHandler.php | 5 ----- src/Commands/EditBannedIPHandler.php | 10 +--------- src/Commands/UnbanUserHandler.php | 20 ++------------------ src/Listeners/RemoveAccessToBannedUsers.php | 9 +-------- src/Repositories/BannedIPRepository.php | 8 +++----- src/Search/NxGambit.php | 1 + 11 files changed, 32 insertions(+), 59 deletions(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index b8ba340..589f4ff 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -7,5 +7,6 @@ jobs: uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main with: enable_backend_testing: true + enable_phpstan: true backend_directory: . diff --git a/composer.json b/composer.json index 77086a2..4ca9288 100644 --- a/composer.json +++ b/composer.json @@ -76,15 +76,19 @@ ], "test:unit": "phpunit -c tests/phpunit.unit.xml", "test:integration": "phpunit -c tests/phpunit.integration.xml", - "test:setup": "@php tests/integration/setup.php" + "test:setup": "@php tests/integration/setup.php", + "analyse:phpstan": "phpstan analyse", + "clear-cache:phpstan": "phpstan clear-result-cache" }, "scripts-descriptions": { "test": "Runs all tests.", "test:unit": "Runs all unit tests.", "test:integration": "Runs all integration tests.", - "test:setup": "Sets up a database for use with integration tests. Execute this only once." + "test:setup": "Sets up a database for use with integration tests. Execute this only once.", + "analyse:phpstan": "Run static analysis" }, "require-dev": { - "flarum/testing": "^1.0.0" + "flarum/testing": "^1.0.0", + "flarum/phpstan": "*" } } diff --git a/extend.php b/extend.php index b6313a3..6804109 100644 --- a/extend.php +++ b/extend.php @@ -61,7 +61,7 @@ ->modelPolicy(User::class, Access\UserPolicy::class), (new Extend\ApiSerializer(Serializer\PostSerializer::class)) - ->attributes(function (AbstractSerializer $serializer, AbstractModel $post, array $attributes): array { + ->attributes(function (AbstractSerializer $serializer, Post $post, array $attributes): array { $attributes['canBanIP'] = $serializer->getActor()->can('banIP', $post->user); return $attributes; @@ -95,7 +95,7 @@ (new Extend\ApiController(Controller\ListPostsController::class)) ->addInclude(['banned_ip', 'banned_ip.user']), - (new Extend\ApiController(Controller\ShowPostsController::class)) + (new Extend\ApiController(Controller\ShowPostController::class)) ->addInclude(['banned_ip', 'banned_ip.user']), (new Extend\ApiController(Controller\CreatePostController::class)) diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..03cf261 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,13 @@ +includes: + - vendor/flarum/phpstan/extension.neon + +parameters: + # The level will be increased in Flarum 2.0 + level: 5 + paths: + - extend.php + - src + excludePaths: + - *.blade.php + checkMissingIterableValueType: false + databaseMigrationsPath: ['migrations'] diff --git a/src/Commands/BanUserHandler.php b/src/Commands/BanUserHandler.php index 4c8b16c..e07da8a 100644 --- a/src/Commands/BanUserHandler.php +++ b/src/Commands/BanUserHandler.php @@ -16,17 +16,11 @@ use FoF\BanIPs\Events\IPWasBanned; use FoF\BanIPs\Repositories\BannedIPRepository; use FoF\BanIPs\Validators\BannedIPValidator; -use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Events\Dispatcher as DispatcherEvents; use Illuminate\Support\Arr; class BanUserHandler { - /** - * @var Dispatcher - */ - private $bus; - /** * @var DispatcherEvents */ @@ -43,14 +37,12 @@ class BanUserHandler private $validator; /** - * @param Dispatcher $bus * @param DispatcherEvents $events * @param BannedIPRepository $bannedIPs * @param BannedIPValidator $validator */ - public function __construct(Dispatcher $bus, DispatcherEvents $events, BannedIPRepository $bannedIPs, BannedIPValidator $validator) + public function __construct(DispatcherEvents $events, BannedIPRepository $bannedIPs, BannedIPValidator $validator) { - $this->bus = $bus; $this->events = $events; $this->bannedIPs = $bannedIPs; $this->validator = $validator; diff --git a/src/Commands/DeleteBannedIPHandler.php b/src/Commands/DeleteBannedIPHandler.php index 293cfa6..c9f6d13 100644 --- a/src/Commands/DeleteBannedIPHandler.php +++ b/src/Commands/DeleteBannedIPHandler.php @@ -26,11 +26,6 @@ public function __construct(BannedIPRepository $bannedIPs) $this->bannedIPs = $bannedIPs; } - /** - * @param DeleteBannedIP $command - * - * @return BanIP - */ public function handle(DeleteBannedIP $command) { /** diff --git a/src/Commands/EditBannedIPHandler.php b/src/Commands/EditBannedIPHandler.php index 660ad28..b478393 100644 --- a/src/Commands/EditBannedIPHandler.php +++ b/src/Commands/EditBannedIPHandler.php @@ -15,29 +15,21 @@ use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\User; use FoF\BanIPs\BannedIP; -use FoF\BanIPs\Repositories\BannedIPRepository; use FoF\BanIPs\Validators\BannedIPValidator; use Illuminate\Support\Arr; class EditBannedIPHandler { - /** - * @var BannedIPRepository - */ - private $bannedIPs; - /** * @var BannedIPValidator */ private $validator; /** - * @param BannedIPRepository $bannedIPs * @param BannedIPValidator $validator */ - public function __construct(BannedIPRepository $bannedIPs, BannedIPValidator $validator) + public function __construct(BannedIPValidator $validator) { - $this->bannedIPs = $bannedIPs; $this->validator = $validator; } diff --git a/src/Commands/UnbanUserHandler.php b/src/Commands/UnbanUserHandler.php index bf35742..b0912f5 100644 --- a/src/Commands/UnbanUserHandler.php +++ b/src/Commands/UnbanUserHandler.php @@ -14,17 +14,10 @@ use Flarum\User\User; use FoF\BanIPs\Events\IPWasUnbanned; use FoF\BanIPs\Repositories\BannedIPRepository; -use FoF\BanIPs\Validators\BannedIPValidator; -use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Events\Dispatcher as DispatcherEvents; class UnbanUserHandler { - /** - * @var Dispatcher - */ - private $bus; - /** * @var DispatcherEvents */ @@ -36,26 +29,17 @@ class UnbanUserHandler private $bannedIPs; /** - * @var BannedIPValidator - */ - private $validator; - - /** - * @param Dispatcher $bus * @param DispatcherEvents $events * @param BannedIPRepository $bannedIPs - * @param BannedIPValidator $validator */ - public function __construct(Dispatcher $bus, DispatcherEvents $events, BannedIPRepository $bannedIPs, BannedIPValidator $validator) + public function __construct(DispatcherEvents $events, BannedIPRepository $bannedIPs) { - $this->bus = $bus; $this->events = $events; $this->bannedIPs = $bannedIPs; - $this->validator = $validator; } /** - * @param BanUser $command + * @param UnbanUser $command * * @return mixed */ diff --git a/src/Listeners/RemoveAccessToBannedUsers.php b/src/Listeners/RemoveAccessToBannedUsers.php index ed4bdba..97cacdd 100644 --- a/src/Listeners/RemoveAccessToBannedUsers.php +++ b/src/Listeners/RemoveAccessToBannedUsers.php @@ -11,7 +11,6 @@ namespace FoF\BanIPs\Listeners; -use Flarum\Http\SessionAuthenticator; use Flarum\User\User; use FoF\BanIPs\Events\IPWasBanned; use FoF\BanIPs\Repositories\BannedIPRepository; @@ -23,14 +22,8 @@ class RemoveAccessToBannedUsers */ private $bannedIPs; - /** - * @var SessionAuthenticator - */ - private $authenticator; - - public function __construct(SessionAuthenticator $authenticator, BannedIPRepository $bannedIPs) + public function __construct(BannedIPRepository $bannedIPs) { - $this->authenticator = $authenticator; $this->bannedIPs = $bannedIPs; } diff --git a/src/Repositories/BannedIPRepository.php b/src/Repositories/BannedIPRepository.php index 0d17a66..1593bb0 100644 --- a/src/Repositories/BannedIPRepository.php +++ b/src/Repositories/BannedIPRepository.php @@ -31,7 +31,7 @@ class BannedIPRepository private static $ips = []; /** - * Get a new query builder for the pages table. + * Get a new query builder for the banned IP table. * * @return Builder */ @@ -47,8 +47,6 @@ public function query() * @param User $actor * * @throws ModelNotFoundException - * - * @return BannedIP */ public function findOrFail($id, User $actor = null) { @@ -111,7 +109,7 @@ public function findUsers($ips) */ public function isUserBanned(User $user) { - if (Arr::has(self::$bans, $user->id)) { + if (Arr::has(self::$bans, [$user->id])) { return (bool) self::$bans[$user->id]; } @@ -120,7 +118,7 @@ public function isUserBanned(User $user) public function getUserIPs(User $user): Collection { - if (Arr::has(self::$ips, $user->id)) { + if (Arr::has(self::$ips, [$user->id])) { return self::$ips[$user->id]; } diff --git a/src/Search/NxGambit.php b/src/Search/NxGambit.php index 98a7d65..a4e7e99 100644 --- a/src/Search/NxGambit.php +++ b/src/Search/NxGambit.php @@ -35,5 +35,6 @@ public function filter(FilterState $filterState, string $filterValue, bool $nega public function apply(SearchState $search, $bit) { // Does nothing + return false; } }