Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add phpstan for static analysis #1223

Merged
merged 17 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/analyse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Static analysis

on:
push:
pull_request:

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-20.04

# 'push' runs on inner branches, 'pull_request' will run only on outer PRs
if: >
github.event_name == 'push'
|| (github.event_name == 'pull_request'
&& github.event.pull_request.head.repo.full_name != github.repository)

steps:
- name: Code Checkout
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and start Docker container
working-directory: install/automated/docker
run: |
docker build -t openvk ../../.. -f openvk.Dockerfile

- name: Run Docker container with PHPStan
working-directory: install/automated/docker
run: |
docker container run --rm \
-v ./chandler.example.yml:/opt/chandler/chandler.yml \
-v ./openvk.example.yml:/opt/chandler/extensions/available/openvk/openvk.yml \
openvk vendor/bin/phpstan analyse --memory-limit 1G
1 change: 1 addition & 0 deletions CLI/FetchToncoinTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class FetchToncoinTransactions extends Command
{
private $images;
private $transactions;

protected static $defaultName = "fetch-ton";

Expand Down
1 change: 1 addition & 0 deletions ServiceAPI/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Wall implements Handler
protected $user;
protected $posts;
protected $notes;
protected $videos;

public function __construct(?User $user)
{
Expand Down
3 changes: 2 additions & 1 deletion VKAPI/Handlers/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ public function deleteTopic(int $group_id, int $topic_id)
return 1;
}

public function editComment(int $comment_id, int $group_id = 0, int $topic_id = 0, string $message, string $attachments)
public function editComment(string $message, string $attachments, int $comment_id, int $group_id = 0, int $topic_id = 0)
{
# FIXME
/*
$this->requireUser();
$this->willExecuteWriteAction();
Expand Down
2 changes: 1 addition & 1 deletion VKAPI/Handlers/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get(int $user_id = 0, string $fields = "", int $offset = 0, int
$clbsCount = $user->getClubCount();
}

$rClubs;
$rClubs = [];

$ic = sizeof($clbs);
if (sizeof($clbs) > $count) {
Expand Down
2 changes: 1 addition & 1 deletion VKAPI/Handlers/Newsfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function get(string $fields = "", int $start_from = 0, int $start_time =
return $response;
}

public function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $rss = 0)
public function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $rss = 0, int $return_banned = 0)
{
$this->requireUser();

Expand Down
11 changes: 7 additions & 4 deletions VKAPI/Handlers/Notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,14 @@ public function get(int $user_id, string $note_ids = "", int $offset = 0, int $c
$this->fail(15, "Access denied");
}

$nodez = (object) [
"count" => 0,
"notes" => [],
];
if (empty($note_ids)) {
$nodez->count = (new NotesRepo())->getUserNotesCount($user);

$notes = array_slice(iterator_to_array((new NotesRepo())->getUserNotes($user, 1, $count + $offset, $sort == 0 ? "ASC" : "DESC")), $offset);
$nodez = (object) [
"count" => (new NotesRepo())->getUserNotesCount((new UsersRepo())->get($user_id)),
"notes" => [],
];

foreach ($notes as $note) {
if ($note->isDeleted()) {
Expand All @@ -210,6 +212,7 @@ public function get(int $user_id, string $note_ids = "", int $offset = 0, int $c
$note = (new NotesRepo())->getNoteById((int) $id[0], (int) $id[1]);
if ($note && !$note->isDeleted()) {
$nodez->notes[] = $note->toVkApiStruct();
$nodez->count++;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions VKAPI/Handlers/Photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use openvk\Web\Models\Entities\{Photo, Album, Comment};
use openvk\Web\Models\Repositories\Albums;
use openvk\Web\Models\Repositories\Photos as PhotosRepo;
use openvk\Web\Models\Repositories\Videos as VideosRepo;
use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Comments as CommentsRepo;
Expand Down
7 changes: 4 additions & 3 deletions VKAPI/Handlers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ public function get(string $user_ids = "0", string $fields = "", int $offset = 0
break;
case 'blacklisted_by_me':
if (!$authuser) {
continue;
break;
}

$response[$i]->blacklisted_by_me = (int) $usr->isBlacklistedBy($this->getUser());
break;
case 'blacklisted':
if (!$authuser) {
continue;
break;
}

$response[$i]->blacklisted = (int) $this->getUser()->isBlacklistedBy($usr);
Expand Down Expand Up @@ -383,7 +383,8 @@ public function search(
string $fav_music = "",
string $fav_films = "",
string $fav_shows = "",
string $fav_books = ""
string $fav_books = "",
string $interests = ""
) {
if ($count > 100) {
$this->fail(100, "One of the parameters specified was missing or invalid: count should be less or equal to 100");
Expand Down
2 changes: 1 addition & 1 deletion VKAPI/Handlers/VKAPIRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(?User $user = null, ?string $platform = null)
$this->platform = $platform;
}

protected function fail(int $code, string $message): void
protected function fail(int $code, string $message): never
{
throw new APIErrorException($message, $code);
}
Expand Down
6 changes: 3 additions & 3 deletions VKAPI/Handlers/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function get(int $owner_id, string $domain = "", int $offset = 0, int $co
$this->fail(15, "Access denied: wall is disabled");
} // Don't search for logic here pls

$iteratorv;
$iteratorv = null;

switch ($filter) {
case "all":
Expand Down Expand Up @@ -722,7 +722,7 @@ public function post(
$post->attach($attachment);
}

if ($wall > 0 && $wall !== $this->user->identity->getId()) {
if ($owner_id > 0 && $owner_id !== $this->user->identity->getId()) {
(new WallPostNotification($wallOwner, $post, $this->user->identity))->emit();
}

Expand All @@ -734,7 +734,7 @@ public function repost(string $object, string $message = "", string $attachments
$this->requireUser();
$this->willExecuteWriteAction();

$postArray;
$postArray = [];
if (preg_match('/(wall|video|photo)((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0) {
$this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect");
}
Expand Down
4 changes: 3 additions & 1 deletion Web/Models/Entities/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function getOwnerID(): int
return $this->getRecord()->owner;
}

public function toApiPreview(): object
public function toApiPreview(): ?object
{
$preview = $this->getPreview();
if ($preview instanceof Photo) {
Expand All @@ -360,6 +360,8 @@ public function toApiPreview(): object
"sizes" => array_values($preview->getVkApiSizes()),
],
];
} else {
return null;
}
}

Expand Down
1 change: 0 additions & 1 deletion Web/Models/Entities/Gift.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public function setName(string $name): void

public function setImage(string $file): bool
{
$imgBlob;
try {
$image = Image::fromFile($file);
$image->resize(512, 512, Image::SHRINK_ONLY);
Expand Down
6 changes: 5 additions & 1 deletion Web/Models/Entities/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function getSender(): ?RowModel
return (new Users())->get($this->getRecord()->sender_id);
} elseif ($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\Club') {
return (new Clubs())->get($this->getRecord()->sender_id);
} else {
return null;
}
}

Expand All @@ -49,6 +51,8 @@ public function getRecipient(): ?RowModel
return (new Users())->get($this->getRecord()->recipient_id);
} elseif ($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\Club') {
return (new Clubs())->get($this->getRecord()->recipient_id);
} else {
return null;
}
}

Expand Down Expand Up @@ -147,7 +151,7 @@ public function simplify(): array
"id" => $author->getId(),
"link" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $author->getURL(),
"avatar" => $author->getAvatarUrl(),
"name" => $author->getFirstName() . $unreadmsg,
"name" => $author->getFirstName(),
],
"timing" => [
"sent" => (string) $this->getSendTimeHumanized(),
Expand Down
4 changes: 3 additions & 1 deletion Web/Models/Entities/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ public function getRecipient(): User
return $this->recipient;
}

public function getModel(int $index): RowModel
public function getModel(int $index): ?RowModel
{
switch ($index) {
case 0:
return $this->originModel;
case 1:
return $this->targetModel;
default:
return null;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Web/Models/Entities/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ public function canBeViewedBy(?User $user = null): bool
}
}

public static function fastMake(int $owner, string $description = "", array $file, ?Album $album = null, bool $anon = false): Photo
public static function fastMake(int $owner, string $description, array $file, ?Album $album = null, bool $anon = false): Photo
{
$photo = new static();
$photo = new Photo();
$photo->setOwner($owner);
$photo->setDescription(iconv_substr($description, 0, 36) . "...");
$photo->setAnonymous($anon);
Expand Down
6 changes: 1 addition & 5 deletions Web/Models/Entities/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public function getTime(): DateTime

public function isDeleted(): bool
{
if ($this->getRecord()->deleted === 0) {
return false;
} elseif ($this->getRecord()->deleted === 1) {
return true;
}
return $this->getRecord()->deleted === 1;
}

public function authorId(): int
Expand Down
5 changes: 3 additions & 2 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ public function getLocalizedPronouns(): string
case 1:
return tr('female');
case 2:
default:
return tr('neutral');
}
}
Expand Down Expand Up @@ -1559,14 +1560,14 @@ public function toVkApiStruct(?User $user = null, string $fields = ''): object
break;
case "blacklisted_by_me":
if (!$user) {
continue;
break;
}

$res->blacklisted_by_me = (int) $this->isBlacklistedBy($user);
break;
case "blacklisted":
if (!$user) {
continue;
break;
}

$res->blacklisted = (int) $user->isBlacklistedBy($this);
Expand Down
12 changes: 7 additions & 5 deletions Web/Models/Entities/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
use openvk\Web\Models\VideoDrivers\VideoDriver;
use Nette\InvalidStateException as ISE;

define("VIDEOS_FRIENDLY_ERROR", "Uploads are disabled on this instance :<", false);
define("VIDEOS_FRIENDLY_ERROR", "Uploads are disabled on this instance :<");

class Video extends Media
{
public const TYPE_DIRECT = 0;
public const TYPE_EMBED = 1;
public const TYPE_DIRECT = 0;
public const TYPE_EMBED = 1;
public const TYPE_UNKNOWN = -1;

protected $tableName = "videos";
protected $fileExtension = "mp4";
Expand Down Expand Up @@ -108,6 +109,7 @@ public function getType(): int
} elseif (!is_null($this->getRecord()->link)) {
return Video::TYPE_EMBED;
}
return Video::TYPE_UNKNOWN;
}

public function getVideoDriver(): ?VideoDriver
Expand Down Expand Up @@ -238,7 +240,7 @@ public function deleteVideo(): void
$this->save();
}

public static function fastMake(int $owner, string $name = "Unnamed Video.ogv", string $description = "", array $file, bool $unlisted = true, bool $anon = false): Video
public static function fastMake(int $owner, string $name, string $description, array $file, bool $unlisted = true, bool $anon = false): Video
{
if (OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading']) {
exit(VIDEOS_FRIENDLY_ERROR);
Expand Down Expand Up @@ -269,7 +271,7 @@ public function fillDimensions()
return false;
}

$streams = Shell::ffprobe("-i", $path, "-show_streams", "-select_streams v", "-loglevel error")->execute($error);
$streams = Shell::ffprobe("-i", $path, "-show_streams", "-select_streams v", "-loglevel error")->execute();
$durations = [];
preg_match_all('%duration=([0-9\.]++)%', $streams, $durations);

Expand Down
2 changes: 2 additions & 0 deletions Web/Models/Repositories/ChandlerGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ChandlerGroups
{
private $context;
private $groups;
private $members;
private $perms;

public function __construct()
{
Expand Down
4 changes: 3 additions & 1 deletion Web/Models/Repositories/Clubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getCount(): int
return (clone $this->clubs)->count('*');
}

public function getPopularClubs(): \Traversable
public function getPopularClubs(): ?\Traversable
{
// TODO rewrite

Expand All @@ -106,6 +106,8 @@ public function getPopularClubs(): \Traversable
"subscriptions" => $entry["subscriptions"],
];
*/
trigger_error("Clubs::getPopularClubs() is currently commented out and returns null", E_USER_WARNING);
return null;
}

public function getWriteableClubs(int $id): \Traversable
Expand Down
Loading
Loading