Skip to content

Commit

Permalink
chore: enable phpstan, apply fixes (#51)
Browse files Browse the repository at this point in the history
* chore: enable phpstan

* fix: phpstan errors

* Apply fixes from StyleCI

* fix: php 7.3 compat

* Apply fixes from StyleCI

* fix: php 7.x compat

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 13, 2023
1 parent abf9b4c commit bc4c7d1
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 40 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: .
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"flarum/core": "^1.8.1",
"flarum/core": "^1.8.3",
"rinvex/countries": "^6.1.2 || ^7.0.1 || ^8.1.0",
"league/csv": "^9.7",
"ianm/iso-639": "^1.0",
Expand Down Expand Up @@ -69,7 +69,8 @@
"require-dev": {
"fof/byobu": "*",
"fof/follow-tags": "*",
"flarum/testing": "^1.0.0"
"flarum/testing": "^1.0.0",
"flarum/phpstan": "*"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -83,12 +84,15 @@
],
"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"
}
}
27 changes: 19 additions & 8 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Flarum\Extend;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Event\Creating as TagCreating;
use Flarum\Tags\Tag;
use Flarum\Tags\TagState;
use FoF\DiscussionLanguage\Api\Serializers\DiscussionLanguageSerializer;
use FoF\DiscussionLanguage\Api\Serializers\TagLocalizedLastDiscussionSerializer;
use FoF\FollowTags\Event\SubscriptionChanging;
Expand All @@ -48,8 +50,15 @@
->add(Middleware\AddLanguageFilter::class),

(new Extend\Model(Discussion::class))
->cast('language_id', 'int')
->hasOne('language', DiscussionLanguage::class, 'id', 'language_id'),

(new Extend\Model(Tag::class))
->cast('localised_last_discussion', 'string'),

(new Extend\Model(TagState::class))
->cast('dl_language_id', 'int'),

(new Extend\Event())
->listen(Saving::class, Listener\AddDiscussionLanguage::class)
->listen(TagCreating::class, Listener\TagCreating::class)
Expand Down Expand Up @@ -104,14 +113,16 @@
->attributes(TagLocalizedLastDiscussionSerializer::class),

(new Extend\Conditional())
->whenExtensionEnabled('fof-follow-tags', [
(new Extend\ApiSerializer(TagSerializer::class))
->attributes(AddTagSerializerAttributes::class),
->whenExtensionEnabled('fof-follow-tags', function () {
return [
(new Extend\ApiSerializer(TagSerializer::class))
->attributes(AddTagSerializerAttributes::class),

(new Extend\Event())
->listen(SubscriptionChanging::class, Listener\SaveLanguageOnSubscription::class),
(new Extend\Event())
->listen(SubscriptionChanging::class, Listener\SaveLanguageOnSubscription::class),

(new Extend\Notification())
->beforeSending(CheckNotificationRecipients::class),
]),
(new Extend\Notification())
->beforeSending(CheckNotificationRecipients::class),
];
}),
];
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']
4 changes: 2 additions & 2 deletions src/Api/Serializers/DiscussionLanguageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(SettingsRepositoryInterface $settings, ISO639 $iso,
}

/**
* {@inheritdoc}
* @param \FoF\DiscussionLanguage\DiscussionLanguage $model
*/
protected function getDefaultAttributes($model)
{
Expand Down Expand Up @@ -92,7 +92,7 @@ protected function getLanguageName(string $code, bool $native)
return $this->translator->trans('fof-discussion-language.forum.index_language.any');
}

if (!$this->records) {
if ($this->records === null) {
$csv = Reader::createFromPath(__DIR__.'/../../../resources/wikipedia-iso-639-2-codes.csv');
$csv->setHeaderOffset(0);

Expand Down
8 changes: 6 additions & 2 deletions src/CheckNotificationRecipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function __invoke(BlueprintInterface $blueprint, array $users): array
return $users;
}

/** @var Tag[] */
/**
* @var Tag[] $tags
*
* @phpstan-ignore-next-line
*/
$tags = $discussion->tags()->get();

// Build a map of users and an array of their respective dl_language_id values
Expand Down Expand Up @@ -69,6 +73,6 @@ public function __invoke(BlueprintInterface $blueprint, array $users): array
*/
protected function isFollowTagsBlueprint(BlueprintInterface $blueprint): bool
{
return strpos($blueprint::class, 'FoF\FollowTags\Notification') === 0;
return strpos(get_class($blueprint), 'FoF\FollowTags\Notification') === 0;
}
}
9 changes: 1 addition & 8 deletions src/Commands/CreateLanguageCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@

use FoF\DiscussionLanguage\DiscussionLanguage;
use FoF\DiscussionLanguage\Validators\DiscussionLanguageValidator;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;

class CreateLanguageCommandHandler
{
/**
* @var Dispatcher
*/
private $events;

/**
* @var DiscussionLanguageValidator
*/
private $validator;

public function __construct(Dispatcher $events, DiscussionLanguageValidator $validator)
public function __construct(DiscussionLanguageValidator $validator)
{
$this->events = $events;
$this->validator = $validator;
}

Expand Down
9 changes: 1 addition & 8 deletions src/Commands/UpdateLanguageCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@

use FoF\DiscussionLanguage\DiscussionLanguage;
use FoF\DiscussionLanguage\Validators\DiscussionLanguageValidator;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;

class UpdateLanguageCommandHandler
{
/**
* @var Dispatcher
*/
private $events;

/**
* @var DiscussionLanguageValidator
*/
private $validator;

public function __construct(Dispatcher $events, DiscussionLanguageValidator $validator)
public function __construct(DiscussionLanguageValidator $validator)
{
$this->events = $events;
$this->validator = $validator;
}

Expand Down
6 changes: 3 additions & 3 deletions src/DiscussionLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Flarum\Discussion\Discussion;

/**
* @property $id string
* @property $code string
* @property $country string
* @property string $id
* @property string $code
* @property string $country
*/
class DiscussionLanguage extends AbstractModel
{
Expand Down
4 changes: 3 additions & 1 deletion src/Listener/SaveLanguageOnSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function handle(SubscriptionChanging $event): void
}

// Fetch the language ID using the provided language string
$languageId = DiscussionLanguage::where('code', $language)->pluck('id')->first();
$languageId = DiscussionLanguage::query()
->where('code', $language)
->value('id');

$event->state->dl_language_id = $languageId;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Listener/UpdateTagMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Flarum\Tags\Event\DiscussionWasTagged;
use Flarum\Tags\Tag;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;

class UpdateTagMetadata
Expand Down Expand Up @@ -70,6 +71,7 @@ public function whenDiscussionIsDeleted(Deleted $event)
{
$this->updateTags($event->discussion, -1);

/** @phpstan-ignore-next-line */
$event->discussion->tags()->detach();
}

Expand Down Expand Up @@ -124,12 +126,13 @@ public function whenPostIsRestored(PostRestored $event)
/**
* @param \Flarum\Discussion\Discussion $discussion
* @param int $delta
* @param Tag[]|null $tags
* @param Post $post: This is only used when a post has been hidden
* @param Collection<Tag>|null $tags
* @param \Flarum\Post\Post $post: This is only used when a post has been hidden
*/
protected function updateTags(Discussion $discussion, $delta = 0, $tags = null, $post = null)
{
if (!$tags) {
/** @phpstan-ignore-next-line */
$tags = $discussion->tags;
}

Expand Down
2 changes: 1 addition & 1 deletion src/LoadForumDiscussionLanguageRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace FoF\DiscussionLanguage;

use Flarum\Api\Controller\ShowForumController;
use Flarum\Database\Eloquent\Collection;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand Down
1 change: 0 additions & 1 deletion src/Middleware/AddLanguageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ protected function determineLanguageFromBrowserRequest(string $acceptLangs): str
if ($this->locales->hasLocale($lang)) {
// Once we find a match, return it
return $lang;
break;
}
}

Expand Down

0 comments on commit bc4c7d1

Please sign in to comment.