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 Nov 14, 2023
1 parent 95f337e commit 38795f8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 51 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Flarumite Simple Discussion Views PHP

on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
with:
enable_backend_testing: false
enable_phpstan: true

backend_directory: .
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Javascript
name: Flarumite Simple Discussion Views JS

on: [workflow_dispatch, push, pull_request]

Expand All @@ -8,11 +8,12 @@ jobs:
with:
enable_bundlewatch: false
enable_prettier: true
enable_typescript: false
enable_typescript: true

frontend_directory: ./js
backend_directory: .
js_package_manager: npm
main_git_branch: master

secrets:
bundlewatch_github_token: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }}
91 changes: 53 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
{
"name": "flarumite/simple-discussion-views",
"description": "Add view counts to discussions",
"type": "flarum-extension",
"require": {
"flarum/core": "^1.7.0",
"jaybizzle/crawler-detect": "^1.2"
},
"license": "MIT",
"authors": [
{
"name": "IanM",
"homepage": "https://discuss.flarum.org/u/ianm",
"role": "Developer"
"name": "flarumite/simple-discussion-views",
"description": "Add view counts to discussions",
"type": "flarum-extension",
"require": {
"flarum/core": "^1.8.0",
"jaybizzle/crawler-detect": "^1.2"
},
"license": "MIT",
"authors": [
{
"name": "IanM",
"homepage": "https://discuss.flarum.org/u/ianm",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/flarumite/simple-discussion-views/issues",
"source": "https://github.com/flarumite/simple-discussion-views",
"forum": "https://discuss.flarum.org/d/24002"
},
"autoload": {
"psr-4": {
"Flarumite\\DiscussionViews\\": "src/"
}
},
"extra": {
"flarum-extension": {
"title": "Flarumite Simple Discussion Views",
"category": "feature",
"icon": {
"name": "far fa-eye",
"backgroundColor": "#008000",
"color": "#fff"
}
},
"flarum-cli": {
"modules": {
"githubActions": true
}
}
},
"conflict": {
"michaelbelgium/flarum-discussion-views": "*"
},
"require-dev": {
"flarum/phpstan": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
},
"scripts-descriptions": {
"analyse:phpstan": "Run static analysis"
}
],
"support": {
"issues": "https://github.com/flarumite/simple-discussion-views/issues",
"source": "https://github.com/flarumite/simple-discussion-views",
"forum": "https://discuss.flarum.org/d/24002"
},
"autoload": {
"psr-4": {
"Flarumite\\DiscussionViews\\": "src/"
}
},
"extra": {
"flarum-extension": {
"title": "Flarumite Simple Discussion Views",
"category": "feature",
"icon": {
"name": "far fa-eye",
"backgroundColor": "#008000",
"color": "#fff"
}
}
},
"conflict": {
"michaelbelgium/flarum-discussion-views": "*"
}
}
7 changes: 5 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Flarum\Api\Controller;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Database\AbstractModel;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
Expand All @@ -29,11 +29,14 @@

new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Model(Discussion::class))
->cast('view_count', 'int'),

(new Extend\Event())
->listen(Saving::class, Listeners\SaveDiscussionFromModal::class),

(new Extend\ApiSerializer(DiscussionSerializer::class))
->attribute('views', function (DiscussionSerializer $serializer, AbstractModel $discussion) {
->attribute('views', function (DiscussionSerializer $serializer, Discussion $discussion) {
return $discussion->view_count;
})
->attributes(AddAttributesBasedOnPermission::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']
2 changes: 1 addition & 1 deletion src/Events/DiscussionWasViewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DiscussionWasViewed
/**
* DiscussionWasViewed constructor.
*
* @param User $viewer
* @param User $actor
* @param Discussion $discussion
*/
public function __construct(User $actor, Discussion $discussion, ?string $ip, ?string $userAgent, DateTime $timeStamp)
Expand Down
18 changes: 10 additions & 8 deletions src/Listeners/AddDiscussionViewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ class AddDiscussionViewHandler
public $settings;

/**
* @var CrawlerDetect
*/
public $crawler;

/**
* Allows disabling the handler ahead of any internal API calls.
*
* @var bool
*
* @info Allows disabling the handler ahead of any internal API calls.
*/
public static $enabled = true;

public function __construct(Dispatcher $bus, SettingsRepositoryInterface $settings)
public function __construct(Dispatcher $bus, SettingsRepositoryInterface $settings, CrawlerDetect $crawler)
{
$this->bus = $bus;
$this->settings = $settings;
$this->crawler = $crawler;
}

public function __invoke(ShowDiscussionController $controller, &$data, ServerRequestInterface $request)
Expand All @@ -52,9 +58,6 @@ public function __invoke(ShowDiscussionController $controller, &$data, ServerReq
return;
}

/**
* @var \Flarum\User\User
*/
$actor = RequestUtil::getActor($request);

if ($this->settings->get('fsdv.ignore-crawlers') && $this->isCrawler($request->getHeader('User-Agent'))) {
Expand All @@ -73,14 +76,13 @@ public function __invoke(ShowDiscussionController $controller, &$data, ServerReq
private function isCrawler(array $agents): bool
{
$detected = false;
$crawler = new CrawlerDetect();

foreach ($agents as $agent) {
if (empty($agent)) {
continue;
}

if ($crawler->isCrawler($agent)) {
if ($this->crawler->isCrawler($agent)) {
$detected = true;
}
}
Expand Down

0 comments on commit 38795f8

Please sign in to comment.