Skip to content

Commit

Permalink
Add phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Jun 1, 2022
1 parent 96053d5 commit 96c8f3c
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 61 deletions.
7 changes: 4 additions & 3 deletions app/components/Chat/ChatControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ public function chatFormSucceeded(Nette\Application\UI\Form $form): void {

$chat = new Chat;
$chat->content = $formatter->format($values->content);
$chat->ip = $this->request->remoteAddress;
$chat->user = $this->users->getById($presenter->getUser()->getIdentity()->id);
$chat->ip = $this->request->getRemoteAddress();
$chat->user = $this->users->getById($presenter->getUser()->getIdentity()->getId());
$this->chats->persistAndFlush($chat);
try {
$this->telegramNotifier->chatMessage($presenter->getUser()->getIdentity()->username, trim(preg_replace('/\{#([0-9]+)\}/', '', $values->content)));
$username = $presenter->getUser()->getIdentity()->getData()['username'] ?? throw new \Exception('Unexpected: missing username');
$this->telegramNotifier->chatMessage($username, trim(preg_replace('/\{#([0-9]+)\}/', '', $values->content)));
} catch (\Exception) {
}
if (!$presenter->isAjax()) {
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/formatting/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function replaceGalleries($text) {
return $size;
};
$text = preg_replace_callback('/<gallery type="carousel">(.+?)<\/gallery>/s', function($match) use ($imageSize) {
$temp = sha1(random_int(0, mt_getrandmax()));
$temp = sha1((string) random_int(0, mt_getrandmax()));
$maxWidth = $maxHeight = 0;
$images = preg_replace_callback('/!\[(.*?)\]\(([^"]+?)(?: "([^"]+)")?\)/', function($match) use ($imageSize, &$maxWidth, &$maxHeight) {
$alt = $match[1];
Expand Down
4 changes: 1 addition & 3 deletions app/model/TelegramNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use Longman\TelegramBot\Telegram;

class TelegramNotifier {
private Telegram $telegram;

public function __construct(string $apiKey, private int $chatId, string $botName) {
$this->telegram = new Telegram($apiKey, $botName);
new Telegram($apiKey, $botName);
}

public function chatMessage($username, $message) {
Expand Down
1 change: 1 addition & 0 deletions app/model/orm/post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use Nette;
use Nextras\Orm\Entity\Entity;
use Nextras\Orm\Relationships\OneHasMany;

/**
* Post
Expand Down
11 changes: 4 additions & 7 deletions app/presenters/BasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ abstract class BasePresenter extends Nette\Application\UI\Presenter {
#[Nette\DI\Attributes\Inject]
public App\Model\TelegramNotifier $telegramNotifier;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

#[Nette\DI\Attributes\Inject]
public App\Helpers\Formatting\ChatFormatter $chatFormatter;

Expand All @@ -50,7 +47,7 @@ protected function createComponentPaginator($name): \VisualPaginator {

protected function startup(): void {
if ($this->getUser()->isLoggedIn()) {
$user = $this->users->getById($this->getUser()->getIdentity()->id);
$user = $this->users->getById($this->getUser()->getIdentity()->getId());
$user->lastActivity = new DateTimeImmutable();
$this->users->persistAndFlush($user);
}
Expand All @@ -68,7 +65,7 @@ public function beforeRender(): void {
parent::beforeRender();
$template = $this->getTemplate();
if ($this->getUser()->isLoggedIn()) {
$user = $this->users->getById($this->getUser()->getIdentity()->id);
$user = $this->users->getById($this->getUser()->getIdentity()->getId());
$template->unreadMails = $user->receivedMail->get()->findBy(['read' => false])->countStored();
$template->upcomingMeetings = $this->meetings->findUpcoming()->countStored();
}
Expand Down Expand Up @@ -113,7 +110,7 @@ public function beforeRender(): void {
}

public function allowed($resource, $action): bool {
$user = $this->getUser()->isLoggedIn() ? $this->users->getById($this->getUser()->getIdentity()->id) : null;
$user = $this->getUser()->isLoggedIn() ? $this->users->getById($this->getUser()->getIdentity()->getId()) : null;

return $this->authorizator->isAllowed($user, $resource, $action);
}
Expand All @@ -127,7 +124,7 @@ protected function createComponentChat(): \App\Components\ChatControl {
$this->users,
$this->telegramNotifier,
$this->helperLoader,
$this->request,
$this->getHttpRequest(),
$this->chatFormatter,
);

Expand Down
22 changes: 10 additions & 12 deletions app/presenters/MailPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class MailPresenter extends BasePresenter {
#[Nette\DI\Attributes\Inject]
public App\Model\UserRepository $users;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

private int $itemsPerPage = 25;

public function renderList($sent = false): void {
Expand All @@ -43,7 +40,7 @@ public function renderList($sent = false): void {
$template->sent = $sent;
$paginator = $this['paginator']->getPaginator();
$paginator->itemsPerPage = $this->itemsPerPage;
$mails = $this->mails->findBy([$sent ? 'sender' : 'recipient' => $this->getUser()->getIdentity()->id]);
$mails = $this->mails->findBy([$sent ? 'sender' : 'recipient' => $this->getUser()->getIdentity()->getId()]);
$paginator->itemCount = $mails->countStored();
$template->mails = $mails->orderBy(['timestamp' => 'DESC'])->limitBy($paginator->itemsPerPage, $paginator->offset);
}
Expand All @@ -69,7 +66,7 @@ public function actionShow($id, $tree = false): void {
$this->error('Toto není tvá zpráva', Nette\Http\IResponse::S403_FORBIDDEN);
}

if (!$mail->read && $mail->recipient->id === $this->getUser()->getIdentity()->id) {
if (!$mail->read && $mail->recipient->id === $this->getUser()->getIdentity()->getId()) {
$mail->read = true;
$this->mails->persistAndFlush($mail);
}
Expand All @@ -80,7 +77,8 @@ public function actionShow($id, $tree = false): void {
protected function createComponentMailForm(): Nette\Application\UI\Form {
$form = new Nette\Application\UI\Form();
$form->addProtection();
$form->setRenderer(new Renderers\Bs3FormRenderer());
$renderer = new Renderers\Bs3FormRenderer();
$form->setRenderer($renderer);

$subject = $form->addText('subject', 'Předmět:')->setRequired();
$subject->getControlPrototype()->autofocus = true;
Expand All @@ -97,7 +95,7 @@ protected function createComponentMailForm(): Nette\Application\UI\Form {

$submitButton = $form->addSubmit('send', 'Odeslat');
$submitButton->onClick[] = [$this, 'mailFormSucceeded'];
$form->getRenderer()->primaryButton = $submitButton;
$renderer->primaryButton = $submitButton;

return $form;
}
Expand All @@ -118,8 +116,8 @@ public function mailFormSucceeded(SubmitButton $button): void {
$mail->subject = $values->subject;
$mail->markdown = $values->markdown;
$mail->content = $formatted['text'];
$mail->sender = $this->users->getById($this->getUser()->getIdentity()->id);
$mail->ip = $this->request->remoteAddress;
$mail->sender = $this->users->getById($this->getUser()->getIdentity()->getId());
$mail->ip = $this->getHttpRequest()->getRemoteAddress()();

if ($this->getAction() === 'reply') {
$original_id = $this->getParameter('id');
Expand All @@ -132,7 +130,7 @@ public function mailFormSucceeded(SubmitButton $button): void {
$this->error('Zpráva s tímto id neexistuje.');
}

if ($original->recipient->id !== $this->getUser()->getIdentity()->id) {
if ($original->recipient->id !== $this->getUser()->getIdentity()->getId()) {
$this->error('Zpráva, na kterou chceš odpovědět není určena do tvých rukou.', Nette\Http\IResponse::S403_FORBIDDEN);
}

Expand Down Expand Up @@ -198,7 +196,7 @@ protected function notifyByMail(Mail $mail): void {
$message->setFrom($messageTemplate->sender . ' <neodpovidat@fan-club-penguin.cz>');
$message->setSubject('Nová zpráva ' . $mail->subject . ' (fan-club-penguin.cz)');
$message->addTo($mail->recipient->email);
$message->setBody($messageTemplate);
$message->setBody((string) $messageTemplate);

$mailer = new SendmailMailer();
$mailer->send($message);
Expand Down Expand Up @@ -239,7 +237,7 @@ public function actionReply($id): void {
$this->error('Zpráva s tímto id neexistuje.');
}

if ($original->recipient->id !== $this->getUser()->getIdentity()->id) {
if ($original->recipient->id !== $this->getUser()->getIdentity()->getId()) {
$this->error('Zpráva, na kterou chceš odpovědět není určena do tvých rukou.', Nette\Http\IResponse::S403_FORBIDDEN);
}
$this->getTemplate()->original = $original;
Expand Down
16 changes: 6 additions & 10 deletions app/presenters/MeetingPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class MeetingPresenter extends BasePresenter {
#[Nette\DI\Attributes\Inject]
public App\Model\UserRepository $users;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

#[Nette\DI\Attributes\Inject]
public App\Model\HelperLoader $helperLoader;

Expand Down Expand Up @@ -126,8 +123,8 @@ public function meetingFormSucceeded(SubmitButton $submit): void {
$meeting->date = $values->date->setTime((int) $hour, (int) $minute);

if ($this->getAction() === 'create') {
$meeting->ip = $this->request->remoteAddress;
$meeting->user = $this->users->getById($this->getUser()->getIdentity()->id);
$meeting->ip = $this->getHttpRequest()->getRemoteAddress();
$meeting->user = $this->users->getById($this->getUser()->getIdentity()->getId());
}
$this->meetings->persistAndFlush($meeting);

Expand Down Expand Up @@ -163,7 +160,7 @@ public function meetingFormPreview(SubmitButton $button): void {
[$hour, $minute] = explode(':', $program[0]['time']);
$meeting->program = Json::encode($program);
$meeting->date = $values->date->setTime((int) $hour, (int) $minute);
$meeting->user = $this->users->getById($this->getUser()->getIdentity()->id);
$meeting->user = $this->users->getById($this->getUser()->getIdentity()->getId());

$this->getTemplate()->preview = $meeting;

Expand All @@ -186,9 +183,8 @@ public function actionEdit($id): void {
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in', ['backlink' => $this->storeRequest()]);
}
/** @var Meeting $meeting */
$meeting = $this->meetings->getById($id);
if (!$meeting) {
if ($meeting === null) {
$this->error('Sraz nenalezen.');
}
if (!$this->allowed($meeting, 'edit')) {
Expand Down Expand Up @@ -249,7 +245,7 @@ public function actionDelete($id): void {
*/
protected function createComponentParticipator(): Multiplier {
return new Multiplier(function($meetingId) {
$userId = $this->getUser()->getIdentity()->id;
$userId = $this->getUser()->getIdentity()->getId();
$meeting = $this->meetings->getById($meetingId);
$youParticipate = array_reduce(iterator_to_array($meeting->visitors->get()), function($carry, $visitor) use ($userId) {
if ($visitor->id === $userId) {
Expand All @@ -269,7 +265,7 @@ public function participatorClicked(Nette\Application\UI\Form $form, $values): v
}

$meeting = $this->meetings->getById($values->id);
$userId = $this->getUser()->getIdentity()->id;
$userId = $this->getUser()->getIdentity()->getId();
$youParticipate = $values->action === 'unparticipate';
try {
if ($youParticipate) {
Expand Down
14 changes: 6 additions & 8 deletions app/presenters/PagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class PagePresenter extends BasePresenter {
#[Nette\DI\Attributes\Inject]
public Nette\Http\Response $response;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

#[Nette\DI\Attributes\Inject]
public Nette\Caching\Storage $storage;

Expand Down Expand Up @@ -127,7 +124,8 @@ public function renderLinks(): void {
protected function createComponentPageForm(): Nette\Application\UI\Form {
$form = new Nette\Application\UI\Form;
$form->addProtection();
$form->setRenderer(new Renderers\Bs3FormRenderer());
$renderer = new Renderers\Bs3FormRenderer();
$form->setRenderer($renderer);
$form->addText('title', 'Nadpis:')->setRequired()->getControlPrototype()->autofocus = true;
$slug = $form->addText('slug', 'Adresa:')->setRequired()->setType('url');
if ($this->getAction() == 'edit') {
Expand All @@ -141,7 +139,7 @@ protected function createComponentPageForm(): Nette\Application\UI\Form {

$submitButton = $form->addSubmit('send', 'Odeslat a zveřejnit');
$submitButton->onClick[] = [$this, 'pageFormSucceeded'];
$form->getRenderer()->primaryButton = $submitButton;
$renderer->primaryButton = $submitButton;

return $form;
}
Expand Down Expand Up @@ -176,7 +174,7 @@ public function pageFormSucceeded(SubmitButton $button): void {

if ($this->getAction() === 'create') {
$page->slug = $values->slug;
$page->user = $this->users->getById($this->getUser()->getIdentity()->id);
$page->user = $this->users->getById($this->getUser()->getIdentity()->getId());
}

try {
Expand All @@ -186,8 +184,8 @@ public function pageFormSucceeded(SubmitButton $button): void {
$revision->markdown = $values->markdown;
$revision->page = $page;
$revision->content = $formatted['text'];
$revision->user = $this->users->getById($this->getUser()->getIdentity()->id);
$revision->ip = $this->request->remoteAddress;
$revision->user = $this->users->getById($this->getUser()->getIdentity()->getId());
$revision->ip = $this->getHttpRequest()->getRemoteAddress();
$page->revisions->add($revision);

$this->pages->persistAndFlush($page);
Expand Down
14 changes: 6 additions & 8 deletions app/presenters/PostPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class PostPresenter extends BasePresenter {
#[Nette\DI\Attributes\Inject]
public App\Model\UserRepository $users;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

#[Nette\DI\Attributes\Inject]
public Nette\Caching\Storage $storage;

Expand Down Expand Up @@ -66,7 +63,8 @@ public function renderList(): void {
protected function createComponentPostForm() {
$form = new Nette\Application\UI\Form;
$form->addProtection();
$form->setRenderer(new Renderers\Bs3FormRenderer());
$renderer = new Renderers\Bs3FormRenderer();
$form->setRenderer($renderer);
$form->addText('title', 'Nadpis:')->setRequired()->getControlPrototype()->autofocus = true;
$form->addTextArea('markdown', 'Obsah:')->setRequired()->getControlPrototype()->addRows(15)->addClass('editor');
$form->addCheckbox('published', 'Zveřejnit')->setDefaultValue(true);
Expand All @@ -77,7 +75,7 @@ protected function createComponentPostForm() {

$submitButton = $form->addSubmit('save', 'Uložit');
$submitButton->onClick[] = [$this, 'postFormSucceeded'];
$form->getRenderer()->primaryButton = $submitButton;
$renderer->primaryButton = $submitButton;

return $form;
}
Expand Down Expand Up @@ -111,7 +109,7 @@ public function postFormSucceeded(SubmitButton $button): void {
}

if ($this->getAction() === 'create') {
$post->user = $this->users->getById($this->getUser()->getIdentity()->id);
$post->user = $this->users->getById($this->getUser()->getIdentity()->getId());
}

$this->posts->persistAndFlush($post);
Expand All @@ -121,8 +119,8 @@ public function postFormSucceeded(SubmitButton $button): void {
$revision->title = $values->title;
$revision->post = $post;
$revision->content = $formatted['text'];
$revision->user = $this->getUser()->getIdentity()->id;
$revision->ip = $this->request->remoteAddress;
$revision->user = $this->getUser()->getIdentity()->getId();
$revision->ip = $this->getHttpRequest()->getRemoteAddress();
$post->revisions->add($revision);

$cache = new Cache($this->storage, 'posts');
Expand Down
13 changes: 5 additions & 8 deletions app/presenters/ProfilePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class ProfilePresenter extends BasePresenter {
#[Nette\DI\Attributes\Inject]
public Nette\Security\Passwords $passwords;

#[Nette\DI\Attributes\Inject]
public Nette\Http\IRequest $request;

public function renderList(): void {
$template = $this->getTemplate();
$template->profiles = $this->users->findAll()->orderBy('username');
Expand All @@ -61,10 +58,10 @@ public function renderShow($id): void {
}
$template = $this->getTemplate();
if (file_exists($this->context->parameters['avatarStorage'] . '/' . $this->profile->id . 'm.png')) {
$template->avatar = str_replace('♥basePath♥', $this->request->url->baseUrl, $this->context->parameters['avatarStoragePublic']) . '/' . $this->profile->id . 'm.png';
$template->avatar = str_replace('♥basePath♥', $this->getHttpRequest()->getUrl()->getBaseUrl(), $this->context->parameters['avatarStoragePublic']) . '/' . $this->profile->id . 'm.png';
}

$template->ipAddress = $this->request->remoteAddress;
$template->ipAddress = $this->getHttpRequest()->getRemoteAddress();
$template->profile = $this->profile;
$template->stamps = $this->stamps->findAll();
}
Expand Down Expand Up @@ -151,7 +148,7 @@ public function profileFormSucceeded(Form $form): void {
$values->avatar->move($original);
try {
$resized = Image::fromFile($original)->resize(100, 100);
Image::fromBlank('100', '100', Image::rgb(0, 0, 0, 127))->place($resized, '50%', '50%')->save($medium);
Image::fromBlank(100, 100, Image::rgb(0, 0, 0, 127))->place($resized, '50%', '50%')->save($medium);
} catch (Exception $e) {
$form->addError('Chyba při zpracování avataru.');
Debugger::log($e);
Expand Down Expand Up @@ -271,7 +268,7 @@ public function passwordResetRequestFormSucceeded(Form $form): void {
$t = Random::generate();
$token = new Token;
$token->token = $this->passwords->hash($t);
$token->ip = $this->request->remoteAddress;
$token->ip = $this->getHttpRequest()->getRemoteAddress();
$token->expiration = (new DateTimeImmutable())->add(\DateInterval::createFromDateString('2 day'));
$token->type = Token::PASSWORD;
$user->tokens->add($token);
Expand All @@ -285,7 +282,7 @@ public function passwordResetRequestFormSucceeded(Form $form): void {
$mailTemplate->url = $this->link('//Profile:resetPassword', ['tid' => $token->id, 'token' => $t]);
$mailTemplate->username = $user->username;
$mail = new Message;
$mail->setFrom('admin@fan-club-penguin.cz')->addTo($user->email)->setHtmlBody($mailTemplate);
$mail->setFrom('admin@fan-club-penguin.cz')->addTo($user->email)->setHtmlBody((string) $mailTemplate);

$mailer = new SendmailMailer;
$mailer->send($mail);
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"mockery/mockery": "^1.5",
"nette/tester": "^2.4",
"nextras/migrations": "^3.0",
"nextras/orm-phpstan": "^1.0",
"phpstan/phpstan": "^1.7",
"phpstan/phpstan-nette": "^1.0",
"symfony/console": "^3.2"
},
"autoload": {
Expand Down
Loading

0 comments on commit 96c8f3c

Please # to comment.