Skip to content

Commit

Permalink
chore: enable phpstan, apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 31, 2023
1 parent a640b12 commit f968b45
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 59 deletions.
1 change: 1 addition & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ jobs:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
with:
enable_backend_testing: true
enable_phpstan: true

backend_directory: .
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
}
}
4 changes: 2 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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']
10 changes: 1 addition & 9 deletions src/Commands/BanUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/Commands/DeleteBannedIPHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public function __construct(BannedIPRepository $bannedIPs)
$this->bannedIPs = $bannedIPs;
}

/**
* @param DeleteBannedIP $command
*
* @return BanIP
*/
public function handle(DeleteBannedIP $command)
{
/**
Expand Down
10 changes: 1 addition & 9 deletions src/Commands/EditBannedIPHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 2 additions & 18 deletions src/Commands/UnbanUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
9 changes: 1 addition & 8 deletions src/Listeners/RemoveAccessToBannedUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions src/Repositories/BannedIPRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -47,8 +47,6 @@ public function query()
* @param User $actor
*
* @throws ModelNotFoundException
*
* @return BannedIP
*/
public function findOrFail($id, User $actor = null)
{
Expand Down Expand Up @@ -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];
}

Expand All @@ -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];
}

Expand Down
1 change: 1 addition & 0 deletions src/Search/NxGambit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public function filter(FilterState $filterState, string $filterValue, bool $nega
public function apply(SearchState $search, $bit)
{
// Does nothing
return false;
}
}

0 comments on commit f968b45

Please sign in to comment.