diff --git a/composer.json b/composer.json index 7108c4c..f06f713 100644 --- a/composer.json +++ b/composer.json @@ -70,6 +70,7 @@ "analyse:phpstan": "Run static analysis" }, "require-dev": { - "flarum/phpstan": "*" + "flarum/phpstan": "*", + "flarum/pusher": "*" } } diff --git a/extend.php b/extend.php index 4101862..bcc6b04 100644 --- a/extend.php +++ b/extend.php @@ -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; @@ -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)) @@ -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([ diff --git a/src/AddDiscussionData.php b/src/AddDiscussionData.php index 606d151..313cf1f 100644 --- a/src/AddDiscussionData.php +++ b/src/AddDiscussionData.php @@ -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(); diff --git a/src/AddPostData.php b/src/AddPostData.php index a5cd0c9..c2b4160 100644 --- a/src/AddPostData.php +++ b/src/AddPostData.php @@ -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(); @@ -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; diff --git a/src/Api/Controllers/ConvertLikesController.php b/src/Api/Controllers/ConvertLikesController.php index 7cfff18..4d88cea 100755 --- a/src/Api/Controllers/ConvertLikesController.php +++ b/src/Api/Controllers/ConvertLikesController.php @@ -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(); diff --git a/src/Api/Controllers/CreateRankController.php b/src/Api/Controllers/CreateRankController.php index 1c9c1a8..f588d67 100755 --- a/src/Api/Controllers/CreateRankController.php +++ b/src/Api/Controllers/CreateRankController.php @@ -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( diff --git a/src/Api/Controllers/DeleteRankController.php b/src/Api/Controllers/DeleteRankController.php index 66f2b8e..266a5d8 100755 --- a/src/Api/Controllers/DeleteRankController.php +++ b/src/Api/Controllers/DeleteRankController.php @@ -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( diff --git a/src/Api/Controllers/DeleteTopImageController.php b/src/Api/Controllers/DeleteTopImageController.php index 3a8b995..a90356a 100755 --- a/src/Api/Controllers/DeleteTopImageController.php +++ b/src/Api/Controllers/DeleteTopImageController.php @@ -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'); diff --git a/src/Api/Controllers/ListRanksController.php b/src/Api/Controllers/ListRanksController.php index c569cc7..ce8cf93 100755 --- a/src/Api/Controllers/ListRanksController.php +++ b/src/Api/Controllers/ListRanksController.php @@ -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(); diff --git a/src/Api/Controllers/OrderByPointsController.php b/src/Api/Controllers/OrderByPointsController.php index e8762ed..473f274 100755 --- a/src/Api/Controllers/OrderByPointsController.php +++ b/src/Api/Controllers/OrderByPointsController.php @@ -23,9 +23,6 @@ class OrderByPointsController extends AbstractListController { public $serializer = UserSerializer::class; - /** - * @var array - */ public $include = ['ranks']; /** @@ -33,19 +30,11 @@ class OrderByPointsController extends AbstractListController */ 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')) { diff --git a/src/Api/Controllers/UpdateRankController.php b/src/Api/Controllers/UpdateRankController.php index fc2453b..2f37c2c 100755 --- a/src/Api/Controllers/UpdateRankController.php +++ b/src/Api/Controllers/UpdateRankController.php @@ -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( diff --git a/src/Api/Controllers/UploadTopImageController.php b/src/Api/Controllers/UploadTopImageController.php index c15c966..756be8d 100755 --- a/src/Api/Controllers/UploadTopImageController.php +++ b/src/Api/Controllers/UploadTopImageController.php @@ -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; diff --git a/src/Gamification.php b/src/Gamification.php index 5f58f5d..3322855 100755 --- a/src/Gamification.php +++ b/src/Gamification.php @@ -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')); diff --git a/src/Listeners/AddVoteHandler.php b/src/Listeners/AddVoteHandler.php index 1947b06..6bfc9e8 100644 --- a/src/Listeners/AddVoteHandler.php +++ b/src/Listeners/AddVoteHandler.php @@ -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); } diff --git a/src/Listeners/SaveVotesToDatabase.php b/src/Listeners/SaveVotesToDatabase.php index 1769186..cf791fc 100755 --- a/src/Listeners/SaveVotesToDatabase.php +++ b/src/Listeners/SaveVotesToDatabase.php @@ -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; } } diff --git a/src/LoadActorVoteRelationship.php b/src/LoadActorVoteRelationship.php index 602b16a..6392efb 100644 --- a/src/LoadActorVoteRelationship.php +++ b/src/LoadActorVoteRelationship.php @@ -46,6 +46,7 @@ public static function sumRelation($controller, $data): void } if ($loadable) { + /** @phpstan-ignore-next-line */ $loadable->loadSum('actualvotes', 'value'); } } diff --git a/src/Notification/VoteBlueprint.php b/src/Notification/VoteBlueprint.php index af0b0c3..2d8e286 100755 --- a/src/Notification/VoteBlueprint.php +++ b/src/Notification/VoteBlueprint.php @@ -77,7 +77,7 @@ public static function getSubjectModel() /** * Get the name of the view to construct a notification email with. * - * @return string + * @return array */ public function getEmailView() { diff --git a/src/Rank.php b/src/Rank.php index e1ec6da..89ca304 100755 --- a/src/Rank.php +++ b/src/Rank.php @@ -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 { /** diff --git a/src/Vote.php b/src/Vote.php index c2fc34d..ee4ddcf 100755 --- a/src/Vote.php +++ b/src/Vote.php @@ -17,10 +17,10 @@ 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 */ @@ -28,9 +28,9 @@ class Vote extends AbstractModel { protected $table = 'post_votes'; - protected $dates = [ - 'created_at', - 'updated_at', + protected $casts = [ + 'created_at' => 'datetime', + 'updated_at' => 'datetime', ]; protected $fillable = [