Set up PHPStan

This commit is contained in:
myrmidex 2025-07-06 10:53:37 +02:00
parent e6f9c2eea8
commit ad572148f0
4 changed files with 26 additions and 14 deletions

View file

@ -63,11 +63,11 @@ private function syncLemmyChannelPosts(): void
$token = $this->getAuthToken($api, $account); $token = $this->getAuthToken($api, $account);
// Get community ID from channel_id or resolve from name // Get community ID from channel_id or resolve from name
$communityId = $this->channel->channel_id $platformChannelId = $this->channel->channel_id
? (int) $this->channel->channel_id ? (int) $this->channel->channel_id
: $api->getCommunityId($this->channel->name); : $api->getCommunityId($this->channel->name, $token);
$api->syncChannelPosts($token, $communityId, $this->channel->name); $api->syncChannelPosts($token, $platformChannelId, $this->channel->name);
LogSaver::info('Channel posts synced successfully', $this->channel); LogSaver::info('Channel posts synced successfully', $this->channel);

View file

@ -5,7 +5,6 @@
use App\Enums\PlatformEnum; use App\Enums\PlatformEnum;
use App\Models\PlatformChannelPost; use App\Models\PlatformChannelPost;
use App\Modules\Lemmy\LemmyRequest; use App\Modules\Lemmy\LemmyRequest;
use App\Services\Auth\LemmyAuthService;
use Exception; use Exception;
class LemmyApiService class LemmyApiService
@ -42,10 +41,9 @@ public function login(string $username, string $password): ?string
} }
} }
public function getCommunityId(string $communityName): int public function getCommunityId(string $communityName, string $token): int
{ {
try { try {
$token = LemmyAuthService::getToken();
$request = new LemmyRequest($this->instance, $token); $request = new LemmyRequest($this->instance, $token);
$response = $request->get('community', ['name' => $communityName]); $response = $request->get('community', ['name' => $communityName]);
@ -61,12 +59,12 @@ public function getCommunityId(string $communityName): int
} }
} }
public function syncChannelPosts(string $token, int $communityId, string $communityName): void public function syncChannelPosts(string $token, int $platformChannelId, string $communityName): void
{ {
try { try {
$request = new LemmyRequest($this->instance, $token); $request = new LemmyRequest($this->instance, $token);
$response = $request->get('post/list', [ $response = $request->get('post/list', [
'community_id' => $communityId, 'community_id' => $platformChannelId,
'limit' => 50, 'limit' => 50,
'sort' => 'New' 'sort' => 'New'
]); ]);
@ -74,7 +72,7 @@ public function syncChannelPosts(string $token, int $communityId, string $commun
if (!$response->successful()) { if (!$response->successful()) {
logger()->warning('Failed to sync channel posts', [ logger()->warning('Failed to sync channel posts', [
'status' => $response->status(), 'status' => $response->status(),
'community_id' => $communityId 'platform_channel_id' => $platformChannelId
]); ]);
return; return;
} }
@ -87,7 +85,7 @@ public function syncChannelPosts(string $token, int $communityId, string $commun
PlatformChannelPost::storePost( PlatformChannelPost::storePost(
PlatformEnum::LEMMY, PlatformEnum::LEMMY,
(string) $communityId, (string) $platformChannelId,
$communityName, $communityName,
(string) $post['id'], (string) $post['id'],
$post['url'] ?? null, $post['url'] ?? null,
@ -97,19 +95,19 @@ public function syncChannelPosts(string $token, int $communityId, string $commun
} }
logger()->info('Synced channel posts', [ logger()->info('Synced channel posts', [
'community_id' => $communityId, 'platform_channel_id' => $platformChannelId,
'posts_count' => count($posts) 'posts_count' => count($posts)
]); ]);
} catch (Exception $e) { } catch (Exception $e) {
logger()->error('Exception while syncing channel posts', [ logger()->error('Exception while syncing channel posts', [
'error' => $e->getMessage(), 'error' => $e->getMessage(),
'community_id' => $communityId 'platform_channel_id' => $platformChannelId
]); ]);
} }
} }
public function createPost(string $token, string $title, string $body, int $communityId, ?string $url = null, ?string $thumbnail = null, ?int $languageId = null): array public function createPost(string $token, string $title, string $body, int $platformChannelId, ?string $url = null, ?string $thumbnail = null, ?int $languageId = null): array
{ {
try { try {
$request = new LemmyRequest($this->instance, $token); $request = new LemmyRequest($this->instance, $token);
@ -117,7 +115,7 @@ public function createPost(string $token, string $title, string $body, int $comm
$postData = [ $postData = [
'name' => $title, 'name' => $title,
'body' => $body, 'body' => $body,
'community_id' => $communityId, 'community_id' => $platformChannelId,
]; ];
if ($url) { if ($url) {

View file

@ -19,11 +19,13 @@
}, },
"require-dev": { "require-dev": {
"fakerphp/faker": "^1.23", "fakerphp/faker": "^1.23",
"larastan/larastan": "^3.5",
"laravel/pail": "^1.2.2", "laravel/pail": "^1.2.2",
"laravel/pint": "^1.18", "laravel/pint": "^1.18",
"laravel/sail": "^1.43", "laravel/sail": "^1.43",
"mockery/mockery": "^1.6", "mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6", "nunomaduro/collision": "^8.6",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5.3" "phpunit/phpunit": "^11.5.3"
}, },
"autoload": { "autoload": {

12
phpstan.neon Normal file
View file

@ -0,0 +1,12 @@
includes:
- vendor/larastan/larastan/extension.neon
parameters:
level: 0
paths:
- app/
- tests/
excludePaths:
- bootstrap/*.php
- storage/*