Skip to content

Commit

Permalink
fix: phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 13, 2023
1 parent 1ce1092 commit 8aed801
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 75 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"analyse:phpstan": "Run static analysis"
},
"require-dev": {
"flarum/phpstan": "*"
"flarum/phpstan": "*",
"flarum/pusher": "*"
}
}
8 changes: 8 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Flarum\Api\Controller;
use Flarum\Api\Serializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Started;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
Expand Down Expand Up @@ -41,6 +42,9 @@
new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Model(User::class))
->cast('votes', 'int')
->cast('rank', 'string')
->cast('last_vote_time', 'datetime')
->belongsToMany('ranks', Rank::class, 'rank_users'),

(new Extend\Model(Post::class))
Expand All @@ -55,6 +59,10 @@
return $post->hasMany(Vote::class, 'post_id');
}),

(new Extend\Model(Discussion::class))
->cast('votes', 'int')
->cast('hotness', 'float'),

(new ExtensionSettings())
->setPrefix('fof-gamification.')
->addKeys([
Expand Down
1 change: 1 addition & 0 deletions src/AddDiscussionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __invoke(BasicDiscussionSerializer $serializer, Discussion $disc
$actor = $serializer->getActor();

if (!$actor->isGuest() && $actor->exists && $post) {
/** @phpstan-ignore-next-line */
$vote = $post->actualvotes->first();

$attributes['hasUpvoted'] = $vote && $vote->isUpvote();
Expand Down
2 changes: 2 additions & 0 deletions src/AddPostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __invoke(PostSerializer $serializer, Post $post, array $attribut

if ($canSeeVotes) {
if ($actor->exists) {
/** @phpstan-ignore-next-line */
$vote = $post->actualvotes->first();

$attributes['hasUpvoted'] = $vote && $vote->isUpvote();
Expand All @@ -44,6 +45,7 @@ public function __invoke(PostSerializer $serializer, Post $post, array $attribut
$attributes['hasDownvoted'] = null;
}
$attributes['canSeeVotes'] = $canSeeVotes;
/** @phpstan-ignore-next-line */
$attributes['votes'] = $post->actualvotes_sum_value;
} else {
$attributes['votes'] = null;
Expand Down
9 changes: 0 additions & 9 deletions src/Api/Controllers/ConvertLikesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,12 @@ class ConvertLikesController implements RequestHandlerInterface
*/
protected $gamification;

/**
* @param SettingsRepositoryInterface $settings
* @param Gamification $gamification
*/
public function __construct(SettingsRepositoryInterface $settings, Gamification $gamification)
{
$this->settings = $settings;
$this->gamification = $gamification;
}

/**
* @param ServerRequestInterface $request
*
* @return int
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
RequestUtil::getActor($request)->assertAdmin();
Expand Down
9 changes: 0 additions & 9 deletions src/Api/Controllers/CreateRankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ class CreateRankController extends AbstractCreateController
*/
protected $bus;

/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}

/**
* @param ServerRequestInterface $request
* @param Document $document
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
{
return $this->bus->dispatch(
Expand Down
6 changes: 0 additions & 6 deletions src/Api/Controllers/DeleteRankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@ class DeleteRankController extends AbstractDeleteController
*/
protected $bus;

/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}

/**
* @param ServerRequestInterface $request
*/
protected function delete(ServerRequestInterface $request)
{
$this->bus->dispatch(
Expand Down
6 changes: 0 additions & 6 deletions src/Api/Controllers/DeleteTopImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ class DeleteTopImageController extends AbstractDeleteController
*/
protected $paths;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths)
{
$this->settings = $settings;
$this->paths = $paths;
}

/**
* {@inheritdoc}
*/
protected function delete(ServerRequestInterface $request)
{
$id = Arr::get($request->getQueryParams(), 'id');
Expand Down
9 changes: 0 additions & 9 deletions src/Api/Controllers/ListRanksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,8 @@

class ListRanksController extends AbstractListController
{
/**
* @var RankSerializer
*/
public $serializer = RankSerializer::class;

/**
* @param ServerRequestInterface $request
* @param Document $document
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
{
return Rank::all();
Expand Down
11 changes: 0 additions & 11 deletions src/Api/Controllers/OrderByPointsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,18 @@ class OrderByPointsController extends AbstractListController
{
public $serializer = UserSerializer::class;

/**
* @var array
*/
public $include = ['ranks'];

/**
* @var Gamification
*/
protected $gamification;

/**
* @param Gamification $gamification
*/
public function __construct(Gamification $gamification)
{
$this->gamification = $gamification;
}

/**
* @param ServerRequestInterface $request
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
{
if (RequestUtil::getActor($request)->cannot('fof.gamification.viewRankingPage')) {
Expand Down
9 changes: 0 additions & 9 deletions src/Api/Controllers/UpdateRankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ class UpdateRankController extends AbstractShowController
*/
protected $bus;

/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}

/**
* @param ServerRequestInterface $request
* @param Document $document
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
{
return $this->bus->dispatch(
Expand Down
3 changes: 0 additions & 3 deletions src/Api/Controllers/UploadTopImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class UploadTopImageController extends ShowForumController
*/
protected $imageManager;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths, ImageManager $imageManager)
{
$this->settings = $settings;
Expand Down
2 changes: 1 addition & 1 deletion src/Gamification.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function calculateHotness($discussion)
/**
* @return mixed
*/
public function orderByPoints($limit, $offset)
public function orderByPoints(int $limit, int $offset)
{
$blockedUsers = explode(', ', $this->settings->get('fof-gamification.blockedUsers'));

Expand Down
4 changes: 2 additions & 2 deletions src/Listeners/AddVoteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function handle(Posted $event)
$vote->value = 1;
$vote->save();

$ranks = Rank::where('points', '<=', $actor->votes)->get();
$ranks = Rank::query()->where('points', '<=', $actor->votes)->get();

if ($ranks) {
if (count($ranks) > 0) {
$actor->ranks()->detach();
$actor->ranks()->attach($ranks);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/SaveVotesToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function vote(Post $post, bool $isDownvoted, bool $isUpvoted, User $actor
} else {
if ($isUpvoted) {
$vote->value = 1;
} elseif ($isDownvoted) {
} else {
$vote->value = -1;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/LoadActorVoteRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static function sumRelation($controller, $data): void
}

if ($loadable) {
/** @phpstan-ignore-next-line */
$loadable->loadSum('actualvotes', 'value');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/VoteBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function getSubjectModel()
/**
* Get the name of the view to construct a notification email with.
*
* @return string
* @return array<string, string>
*/
public function getEmailView()
{
Expand Down
6 changes: 6 additions & 0 deletions src/Rank.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
use Flarum\Database\AbstractModel;
use Flarum\User\User;

/**
* @property int $id
* @property string $name
* @property string $color
* @property int $points
*/
class Rank extends AbstractModel
{
/**
Expand Down
14 changes: 7 additions & 7 deletions src/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
use Flarum\User\User;

/**
* @property int id
* @property int user_id
* @property int post_id
* @property int value
* @property int $id
* @property int $user_id
* @property int $post_id
* @property int $value
* @property Post $post
* @property User $user
*/
class Vote extends AbstractModel
{
protected $table = 'post_votes';

protected $dates = [
'created_at',
'updated_at',
protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

protected $fillable = [
Expand Down

0 comments on commit 8aed801

Please sign in to comment.