fix(phpstan): most of the things and stupid lines of code

This commit is contained in:
veselcraft 2025-02-28 17:33:02 +03:00
parent f672e5001a
commit 92e741aeea
No known key found for this signature in database
GPG key ID: 9CF0B42766CCF7BA
30 changed files with 140 additions and 122 deletions

View file

@ -9,6 +9,7 @@ use Nette\Utils\ImageException;
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;

View file

@ -292,14 +292,14 @@ final class Users extends VKAPIRequestHandler
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);
@ -383,7 +383,8 @@ final class Users extends VKAPIRequestHandler
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");

View file

@ -53,7 +53,7 @@ final class Wall extends VKAPIRequestHandler
$this->fail(15, "Access denied: wall is disabled");
} // Don't search for logic here pls
$iteratorv;
$iteratorv = null;
switch ($filter) {
case "all":
@ -722,7 +722,7 @@ final class Wall extends VKAPIRequestHandler
$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();
}
@ -734,7 +734,7 @@ final class Wall extends VKAPIRequestHandler
$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");
}

View file

@ -351,7 +351,7 @@ class Document extends Media
return $this->getRecord()->owner;
}
public function toApiPreview(): object
public function toApiPreview(): ?object
{
$preview = $this->getPreview();
if ($preview instanceof Photo) {
@ -360,6 +360,8 @@ class Document extends Media
"sizes" => array_values($preview->getVkApiSizes()),
],
];
} else {
return null;
}
}

View file

@ -112,7 +112,6 @@ class Gift extends RowModel
public function setImage(string $file): bool
{
$imgBlob;
try {
$image = Image::fromFile($file);
$image->resize(512, 512, Image::SHRINK_ONLY);

View file

@ -33,6 +33,8 @@ class Message extends 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;
}
}
@ -49,6 +51,8 @@ class Message extends 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;
}
}
@ -147,7 +151,7 @@ class Message extends RowModel
"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(),

View file

@ -64,13 +64,15 @@ class Notification
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;
}
}

View file

@ -385,7 +385,7 @@ class Photo extends Media
}
}
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->setOwner($owner);

View file

@ -45,11 +45,7 @@ class Report extends RowModel
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

View file

@ -932,6 +932,7 @@ class User extends RowModel
case 1:
return tr('female');
case 2:
default:
return tr('neutral');
}
}
@ -1559,14 +1560,14 @@ class User extends RowModel
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);

View file

@ -9,12 +9,13 @@ use openvk\Web\Util\Shell\Exceptions\{ShellUnavailableException, UnknownCommandE
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_UNKNOWN = -1;
protected $tableName = "videos";
protected $fileExtension = "mp4";
@ -108,6 +109,7 @@ class Video extends Media
} elseif (!is_null($this->getRecord()->link)) {
return Video::TYPE_EMBED;
}
return VIDEO::TYPE_UNKNOWN;
}
public function getVideoDriver(): ?VideoDriver
@ -269,7 +271,7 @@ class Video extends Media
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);

View file

@ -13,6 +13,8 @@ class ChandlerGroups
{
private $context;
private $groups;
private $members;
private $perms;
public function __construct()
{

View file

@ -91,7 +91,7 @@ class Clubs
return (clone $this->clubs)->count('*');
}
public function getPopularClubs(): \Traversable
public function getPopularClubs(): ?\Traversable
{
// TODO rewrite
@ -106,6 +106,8 @@ class Clubs
"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

View file

@ -152,7 +152,7 @@ class Documents
switch ($paramName) {
case "type":
if ($paramValue < 1 || $paramValue > 8) {
continue;
break;
}
$result->where("type", $paramValue);
break;

View file

@ -11,7 +11,7 @@ use openvk\Web\Models\Entities\{User, SupportAgent};
class SupportAgents
{
private $context;
private $tickets;
private $agents;
public function __construct()
{

View file

@ -57,7 +57,7 @@ class Tickets
{
$requests = $this->tickets->where(["id" => $requestId])->fetch();
if (!is_null($requests)) {
return new Req($requests);
return new Ticket($requests);
} else {
return null;
}

View file

@ -255,7 +255,7 @@ final class AdminPresenter extends OpenVKPresenter
{
$this->warnIfNoCommerce();
$cat;
$cat = null;
$gen = false;
if ($id !== 0) {
$cat = $this->gifts->getCat($id);

View file

@ -34,7 +34,8 @@ final class BlobPresenter extends OpenVKPresenter
}
if (isset($_SERVER["HTTP_IF_NONE_MATCH"])) {
exit(header("HTTP/1.1 304 Not Modified"));
header("HTTP/1.1 304 Not Modified");
exit();
}
header("Content-Type: " . mime_content_type($path));

View file

@ -7,6 +7,7 @@ namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Comment, Notifications\MentionNotification, Photo, Video, User, Topic, Post};
use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\{Comments, Clubs, Videos, Photos, Audios};
use Nette\InvalidStateException as ISE;
final class CommentPresenter extends OpenVKPresenter
{

View file

@ -18,7 +18,7 @@ final class ContentSearchPresenter extends OpenVKPresenter
public function renderIndex(): void
{
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$this->template->results = $repo->find([
$this->template->results = $this->$repo->find([
"query" => $this->postParam("query"),
]);
}

View file

@ -9,6 +9,7 @@ use Nette\InvalidStateException;
use openvk\Web\Models\Entities\Notifications\ClubModeratorNotification;
use openvk\Web\Models\Repositories\{Clubs, Users, Albums, Managers, Topics, Audios, Posts, Documents};
use Chandler\Security\Authenticator;
use Nette\InvalidStateException as ISE;
final class GroupPresenter extends OpenVKPresenter
{
@ -288,7 +289,6 @@ final class GroupPresenter extends OpenVKPresenter
(new Albums())->getClubAvatarAlbum($club)->addPhoto($photo);
} catch (ISE $ex) {
$name = $album->getName();
$this->flashFail("err", tr("error"), tr("error_when_uploading_photo"));
}
}

View file

@ -93,9 +93,11 @@ final class MessengerPresenter extends OpenVKPresenter
header("Content-Type: application/json");
if ($this->queryParam("act") !== "a_check") {
exit(header("HTTP/1.1 400 Bad Request"));
header("HTTP/1.1 400 Bad Request");
exit();
} elseif (!$this->queryParam("key")) {
exit(header("HTTP/1.1 403 Forbidden"));
header("HTTP/1.1 403 Forbidden");
exit();
}
$key = $this->queryParam("key");
@ -158,7 +160,8 @@ final class MessengerPresenter extends OpenVKPresenter
$sel = $this->getCorrespondent($sel);
if ($sel->getId() !== $this->user->id && !$sel->getPrivacyPermission('messages.write', $this->user->identity)) {
exit(header("HTTP/1.1 403 Forbidden"));
header("HTTP/1.1 403 Forbidden");
exit();
}
$cor = new Correspondence($this->user->identity, $sel);

View file

@ -151,77 +151,6 @@ final class NoSpamPresenter extends OpenVKPresenter
$this->assertNoCSRF();
$this->willExecuteWriteAction();
function searchByAdditionalParams(?string $table = null, ?string $where = null, ?string $ip = null, ?string $useragent = null, ?int $ts = null, ?int $te = null, $user = null)
{
$db = DatabaseConnection::i()->getContext();
if ($table && ($ip || $useragent || $ts || $te || $user)) {
$conditions = [];
if ($ip) {
$conditions[] = "`ip` REGEXP '$ip'";
}
if ($useragent) {
$conditions[] = "`useragent` REGEXP '$useragent'";
}
if ($ts) {
$conditions[] = "`ts` < $ts";
}
if ($te) {
$conditions[] = "`ts` > $te";
}
if ($user) {
$users = new Users();
$_user = $users->getByChandlerUser((new ChandlerUsers())->getById($user))
?? $users->get((int) $user)
?? $users->getByAddress($user)
?? null;
if ($_user) {
$conditions[] = "`user` = '" . $_user->getChandlerGUID() . "'";
}
}
$whereStart = "WHERE `object_table` = '$table'";
if ($table === "profiles") {
$whereStart .= "AND `type` = 0";
}
$conditions = count($conditions) > 0 ? "AND (" . implode(" AND ", $conditions) . ")" : "";
$response = [];
if ($conditions) {
$logs = $db->query("SELECT * FROM `ChandlerLogs` $whereStart $conditions GROUP BY `object_id`, `object_model`");
foreach ($logs as $log) {
$log = (new Logs())->get($log->id);
$object = $log->getObject()->unwrap();
if (!$object) {
continue;
}
if ($where) {
if (str_starts_with($where, " AND")) {
$where = substr_replace($where, "", 0, strlen(" AND"));
}
$a = $db->query("SELECT * FROM `$table` WHERE $where")->fetchAll();
foreach ($a as $o) {
if ($object->id == $o["id"]) {
$response[] = $object;
}
}
} else {
$response[] = $object;
}
}
}
return $response;
}
}
try {
$response = [];
$processed = 0;
@ -290,7 +219,7 @@ final class NoSpamPresenter extends OpenVKPresenter
}
if ($ip || $useragent || $ts || $te || $user) {
$rows = searchByAdditionalParams($table, $where, $ip, $useragent, $ts, $te, $user);
$rows = $this->searchByAdditionalParams($table, $where, $ip, $useragent, $ts, $te, $user);
} else {
if (!$where) {
$rows = [];
@ -408,4 +337,75 @@ final class NoSpamPresenter extends OpenVKPresenter
$this->returnJson(["success" => false, "error" => $e->getMessage()]);
}
}
private function searchByAdditionalParams(?string $table = null, ?string $where = null, ?string $ip = null, ?string $useragent = null, ?int $ts = null, ?int $te = null, $user = null)
{
$db = DatabaseConnection::i()->getContext();
if ($table && ($ip || $useragent || $ts || $te || $user)) {
$conditions = [];
if ($ip) {
$conditions[] = "`ip` REGEXP '$ip'";
}
if ($useragent) {
$conditions[] = "`useragent` REGEXP '$useragent'";
}
if ($ts) {
$conditions[] = "`ts` < $ts";
}
if ($te) {
$conditions[] = "`ts` > $te";
}
if ($user) {
$users = new Users();
$_user = $users->getByChandlerUser((new ChandlerUsers())->getById($user))
?? $users->get((int) $user)
?? $users->getByAddress($user)
?? null;
if ($_user) {
$conditions[] = "`user` = '" . $_user->getChandlerGUID() . "'";
}
}
$whereStart = "WHERE `object_table` = '$table'";
if ($table === "profiles") {
$whereStart .= "AND `type` = 0";
}
$conditions = count($conditions) > 0 ? "AND (" . implode(" AND ", $conditions) . ")" : "";
$response = [];
if ($conditions) {
$logs = $db->query("SELECT * FROM `ChandlerLogs` $whereStart $conditions GROUP BY `object_id`, `object_model`");
foreach ($logs as $log) {
$log = (new Logs())->get($log->id);
$object = $log->getObject()->unwrap();
if (!$object) {
continue;
}
if ($where) {
if (str_starts_with($where, " AND")) {
$where = substr_replace($where, "", 0, strlen(" AND"));
}
$a = $db->query("SELECT * FROM `$table` WHERE $where")->fetchAll();
foreach ($a as $o) {
if ($object->id == $o["id"]) {
$response[] = $object;
}
}
} else {
$response[] = $object;
}
}
}
return $response;
}
}
}

View file

@ -74,7 +74,6 @@ abstract class OpenVKPresenter extends SimplePresenter
protected function logInUserWithToken(): void
{
$header = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
$token;
preg_match("%Bearer (.*)$%", $header, $matches);
$token = $matches[1] ?? "";

View file

@ -70,7 +70,7 @@ final class SearchPresenter extends OpenVKPresenter
case 'marital_status':
case 'polit_views':
if ((int) $param_value == 0) {
continue;
break;
}
$parameters[$param_name] = $param_value;
@ -96,7 +96,7 @@ final class SearchPresenter extends OpenVKPresenter
# дай бог работал этот case
case 'from_me':
if ((int) $param_value != 1) {
continue;
break;
}
$parameters['from_me'] = $this->user->id;

View file

@ -314,17 +314,20 @@ final class SupportPresenter extends OpenVKPresenter
$comment = $this->comments->get($id);
if ($this->user->id !== $comment->getTicket()->getUser()->getId()) {
exit(header("HTTP/1.1 403 Forbidden"));
header("HTTP/1.1 403 Forbidden");
exit();
}
if ($mark !== 1 && $mark !== 2) {
exit(header("HTTP/1.1 400 Bad Request"));
header("HTTP/1.1 400 Bad Request");
exit();
}
$comment->setMark($mark);
$comment->save();
exit(header("HTTP/1.1 200 OK"));
header("HTTP/1.1 200 OK");
exit();
}
public function renderQuickBanInSupport(int $id): void

View file

@ -6,6 +6,7 @@ namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Topic, Club, Comment, Photo, Video};
use openvk\Web\Models\Repositories\{Topics, Clubs};
use Nette\InvalidStateException as ISE;
final class TopicsPresenter extends OpenVKPresenter
{
@ -112,9 +113,6 @@ final class TopicsPresenter extends OpenVKPresenter
$video = null;
if ($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
$album = null;
if ($wall > 0 && $wall === $this->user->id) {
$album = (new Albums())->getUserWallAlbum($wallOwner);
}
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
}

View file

@ -273,7 +273,7 @@ final class VKAPIPresenter extends OpenVKPresenter
}
}
define("VKAPI_DECL_VER", $this->requestParam("v") ?? "4.100", false);
define("VKAPI_DECL_VER", $this->requestParam("v") ?? "4.100");
try {
$res = $handler->{$method}(...$params);

View file

@ -66,6 +66,7 @@ class DateTime
case static::RELATIVE_FORMAT_LOWER:
return $this->zmdate();
case static::RELATIVE_FORMAT_SHORT:
default:
return "";
}
}

View file

@ -188,9 +188,9 @@ class Makima
$tries = [];
$firstLine;
$secondLine;
$thirdLine;
$firstLine = null;
$secondLine = null;
$thirdLine = null;
# Try one line:
$tries[$firstLine = $count] = [$this->calculateMultiThumbsHeight($ratiosCropped, $maxWidth, $marginWidth)];
@ -234,7 +234,7 @@ class Makima
}
}
if (!$optimalConfiguration || $confDigff < $optimalDifference) {
if (!$optimalConfiguration || $confDiff < $optimalDifference) {
$optimalConfiguration = $config;
$optimalDifference = $confDiff;
}