Skip to content

Commit

Permalink
Adding assert() to forms, required a bit of refactoring here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Dec 10, 2024
1 parent b0e83f1 commit 404e8a2
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 117 deletions.
78 changes: 0 additions & 78 deletions app/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,6 @@
<code><![CDATA[$values->password]]></code>
</MixedArgument>
</file>
<file src="src/Form/InterviewFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->action]]></code>
<code><![CDATA[$values->action]]></code>
<code><![CDATA[$values->audioEmbed]]></code>
<code><![CDATA[$values->audioEmbed]]></code>
<code><![CDATA[$values->audioHref]]></code>
<code><![CDATA[$values->audioHref]]></code>
<code><![CDATA[$values->date]]></code>
<code><![CDATA[$values->date]]></code>
<code><![CDATA[$values->description]]></code>
<code><![CDATA[$values->description]]></code>
<code><![CDATA[$values->href]]></code>
<code><![CDATA[$values->href]]></code>
<code><![CDATA[$values->sourceHref]]></code>
<code><![CDATA[$values->sourceHref]]></code>
<code><![CDATA[$values->sourceName]]></code>
<code><![CDATA[$values->sourceName]]></code>
<code><![CDATA[$values->title]]></code>
<code><![CDATA[$values->title]]></code>
<code><![CDATA[$values->videoEmbed]]></code>
<code><![CDATA[$values->videoEmbed]]></code>
<code><![CDATA[$values->videoHref]]></code>
<code><![CDATA[$values->videoHref]]></code>
</MixedArgument>
</file>
<file src="src/Form/PostFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->lead === '' ? null : $values->lead]]></code>
Expand All @@ -61,52 +35,6 @@
<code><![CDATA[$values->site->new->url]]></code>
</MixedArgument>
</file>
<file src="src/Form/TalkFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->action]]></code>
<code><![CDATA[$values->action]]></code>
<code><![CDATA[$values->date]]></code>
<code><![CDATA[$values->date]]></code>
<code><![CDATA[$values->description]]></code>
<code><![CDATA[$values->description]]></code>
<code><![CDATA[$values->event]]></code>
<code><![CDATA[$values->event]]></code>
<code><![CDATA[$values->eventHref]]></code>
<code><![CDATA[$values->eventHref]]></code>
<code><![CDATA[$values->favorite]]></code>
<code><![CDATA[$values->favorite]]></code>
<code><![CDATA[$values->filenamesTalk]]></code>
<code><![CDATA[$values->filenamesTalk]]></code>
<code><![CDATA[$values->href]]></code>
<code><![CDATA[$values->href]]></code>
<code><![CDATA[$values->locale]]></code>
<code><![CDATA[$values->locale]]></code>
<code><![CDATA[$values->ogImage]]></code>
<code><![CDATA[$values->ogImage]]></code>
<code><![CDATA[$values->publishSlides]]></code>
<code><![CDATA[$values->publishSlides]]></code>
<code><![CDATA[$values->slidesEmbed]]></code>
<code><![CDATA[$values->slidesEmbed]]></code>
<code><![CDATA[$values->slidesHref]]></code>
<code><![CDATA[$values->slidesHref]]></code>
<code><![CDATA[$values->slidesNote]]></code>
<code><![CDATA[$values->slidesNote]]></code>
<code><![CDATA[$values->slidesTalk]]></code>
<code><![CDATA[$values->slidesTalk]]></code>
<code><![CDATA[$values->supersededBy]]></code>
<code><![CDATA[$values->supersededBy]]></code>
<code><![CDATA[$values->title]]></code>
<code><![CDATA[$values->title]]></code>
<code><![CDATA[$values->transcript]]></code>
<code><![CDATA[$values->transcript]]></code>
<code><![CDATA[$values->translationGroup]]></code>
<code><![CDATA[$values->translationGroup]]></code>
<code><![CDATA[$values->videoEmbed]]></code>
<code><![CDATA[$values->videoEmbed]]></code>
<code><![CDATA[$values->videoHref]]></code>
<code><![CDATA[$values->videoHref]]></code>
</MixedArgument>
</file>
<file src="src/Form/TrainingApplicationAdminFormFactory.php">
<MixedArgument>
<code><![CDATA[$dateId]]></code>
Expand Down Expand Up @@ -210,12 +138,6 @@
<code><![CDATA[$matches[2]]]></code>
</MixedArgument>
</file>
<file src="src/Media/VideoThumbnails.php">
<MixedArgument>
<code><![CDATA[$values->videoThumbnail]]></code>
<code><![CDATA[$values->videoThumbnailAlternative]]></code>
</MixedArgument>
</file>
<file src="src/Pulse/Passwords/PasswordsSorting.php">
<MixedArgument>
<code><![CDATA[$sorter]]></code>
Expand Down
22 changes: 18 additions & 4 deletions app/src/Form/InterviewFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MichalSpacekCz\Media\VideoThumbnails;
use Nette\Forms\Controls\SubmitButton;
use Nette\Forms\Form;
use Nette\Http\FileUpload;

readonly class InterviewFormFactory
{
Expand Down Expand Up @@ -71,8 +72,21 @@ public function create(callable $onSuccess, ?Interview $interview = null): UiFor

$form->onSuccess[] = function (UiForm $form) use ($interview, $onSuccess, $videoThumbnailFormFields): void {
$values = $form->getFormValues();
$videoThumbnailBasename = $this->videoThumbnails->getUploadedMainFileBasename($values);
$videoThumbnailBasenameAlternative = $this->videoThumbnails->getUploadedAlternativeFileBasename($values);
assert($values->videoThumbnail instanceof FileUpload);
assert($values->videoThumbnailAlternative instanceof FileUpload);
assert(is_string($values->action));
assert(is_string($values->title));
assert(is_string($values->description));
assert(is_string($values->date));
assert(is_string($values->href));
assert(is_string($values->audioHref));
assert(is_string($values->audioEmbed));
assert(is_string($values->videoHref));
assert(is_string($values->videoEmbed));
assert(is_string($values->sourceName));
assert(is_string($values->sourceHref));
$videoThumbnailBasename = $this->videoThumbnails->getUploadedMainFileBasename($values->videoThumbnail);
$videoThumbnailBasenameAlternative = $this->videoThumbnails->getUploadedAlternativeFileBasename($values->videoThumbnailAlternative);
if ($interview) {
$removeVideoThumbnail = $videoThumbnailFormFields->hasVideoThumbnail() && $values->removeVideoThumbnail;
$removeVideoThumbnailAlternative = $videoThumbnailFormFields->hasAlternativeVideoThumbnail() && $values->removeVideoThumbnailAlternative;
Expand All @@ -94,7 +108,7 @@ public function create(callable $onSuccess, ?Interview $interview = null): UiFor
$values->sourceName,
$values->sourceHref,
);
$this->videoThumbnails->saveVideoThumbnailFiles($interview->getId(), $values);
$this->videoThumbnails->saveVideoThumbnailFiles($interview->getId(), $values->videoThumbnail, $values->videoThumbnailAlternative);
if ($removeVideoThumbnail && $thumbnailFilename !== null) {
$this->videoThumbnails->deleteFile($interview->getId(), $thumbnailFilename);
}
Expand All @@ -117,7 +131,7 @@ public function create(callable $onSuccess, ?Interview $interview = null): UiFor
$values->sourceName,
$values->sourceHref,
);
$this->videoThumbnails->saveVideoThumbnailFiles($interviewId, $values);
$this->videoThumbnails->saveVideoThumbnailFiles($interviewId, $values->videoThumbnail, $values->videoThumbnailAlternative);
}
$onSuccess();
};
Expand Down
2 changes: 2 additions & 0 deletions app/src/Form/SignInHoneypotFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function create(): UiForm
$this->controlsFactory->addSignIn($form);
$form->onSuccess[] = function (UiForm $form): void {
$values = $form->getFormValues();
assert(is_string($values->username));
assert(is_string($values->password));
Debugger::log("Sign-in attempt: {$values->username}, {$values->password}, {$this->httpRequest->getRemoteAddress()}", 'honeypot');
$creds = $values->username . ':' . $values->password;
if (Regex::isMatch('~\slimit\s~i', $creds)) {
Expand Down
33 changes: 29 additions & 4 deletions app/src/Form/TalkFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MichalSpacekCz\Talks\Talks;
use Nette\Forms\Controls\SubmitButton;
use Nette\Forms\Form;
use Nette\Http\FileUpload;
use Nette\Utils\Html;
use Nette\Utils\Strings;

Expand Down Expand Up @@ -107,8 +108,32 @@ public function create(callable $onSuccess, ?Talk $talk = null): UiForm

$form->onSuccess[] = function (UiForm $form) use ($talk, $onSuccess, $videoThumbnailFormFields): void {
$values = $form->getFormValues();
$videoThumbnailBasename = $this->videoThumbnails->getUploadedMainFileBasename($values);
$videoThumbnailBasenameAlternative = $this->videoThumbnails->getUploadedAlternativeFileBasename($values);
assert($values->videoThumbnail instanceof FileUpload);
assert($values->videoThumbnailAlternative instanceof FileUpload);
assert(is_int($values->locale));
assert(is_int($values->translationGroup) || $values->translationGroup === null);
assert(is_string($values->action));
assert(is_string($values->title));
assert(is_string($values->description));
assert(is_string($values->date));
assert(is_string($values->duration));
assert(is_string($values->href));
assert(is_int($values->slidesTalk) || $values->slidesTalk === null);
assert(is_int($values->filenamesTalk) || $values->filenamesTalk === null);
assert(is_string($values->slidesHref));
assert(is_string($values->slidesEmbed));
assert(is_string($values->slidesNote));
assert(is_string($values->videoHref));
assert(is_string($values->videoEmbed));
assert(is_string($values->event));
assert(is_string($values->eventHref));
assert(is_string($values->ogImage));
assert(is_string($values->transcript));
assert(is_string($values->favorite));
assert(is_int($values->supersededBy) || $values->supersededBy === null);
assert(is_bool($values->publishSlides));
$videoThumbnailBasename = $this->videoThumbnails->getUploadedMainFileBasename($values->videoThumbnail);
$videoThumbnailBasenameAlternative = $this->videoThumbnails->getUploadedAlternativeFileBasename($values->videoThumbnailAlternative);
if ($talk) {
$removeVideoThumbnail = $videoThumbnailFormFields->hasVideoThumbnail() && $values->removeVideoThumbnail;
$removeVideoThumbnailAlternative = $videoThumbnailFormFields->hasAlternativeVideoThumbnail() && $values->removeVideoThumbnailAlternative;
Expand Down Expand Up @@ -141,7 +166,7 @@ public function create(callable $onSuccess, ?Talk $talk = null): UiForm
$values->supersededBy,
$values->publishSlides,
);
$this->videoThumbnails->saveVideoThumbnailFiles($talk->getId(), $values);
$this->videoThumbnails->saveVideoThumbnailFiles($talk->getId(), $values->videoThumbnail, $values->videoThumbnailAlternative);
if ($removeVideoThumbnail && $thumbnailFilename !== null) {
$this->videoThumbnails->deleteFile($talk->getId(), $thumbnailFilename);
}
Expand Down Expand Up @@ -176,7 +201,7 @@ public function create(callable $onSuccess, ?Talk $talk = null): UiForm
$values->supersededBy,
$values->publishSlides,
);
$this->videoThumbnails->saveVideoThumbnailFiles($talkId, $values);
$this->videoThumbnails->saveVideoThumbnailFiles($talkId, $values->videoThumbnail, $values->videoThumbnailAlternative);
$message = Html::el()->setText('Přednáška přidána ');
}
$message->addHtml(Html::el('a')->href($this->linkGenerator->link('Www:Talks:talk', [$values->action]))->setText('Zobrazit'));
Expand Down
2 changes: 1 addition & 1 deletion app/src/Form/TalkSlidesFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function create(callable $onSuccess, int $talkId, TalkSlideCollection $sl
assert($values->slides instanceof ArrayHash);
assert($values->new instanceof ArrayHash);
assert(is_bool($values->deleteReplaced));
$this->talkSlides->saveSlides($talkId, $slides, (array)$values->slides, array_values((array)$values->new), $values->deleteReplaced);
$this->talkSlides->saveSlides($talkId, $slides, $values->slides, $values->new, $values->deleteReplaced);
$message = $this->texyFormatter->translate('messages.talks.admin.slideadded');
$type = 'info';
} catch (DuplicatedSlideException $e) {
Expand Down
19 changes: 9 additions & 10 deletions app/src/Media/VideoThumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Nette\Http\FileUpload;
use Nette\Utils\Callback;
use Nette\Utils\ImageException;
use stdClass;

readonly class VideoThumbnails
{
Expand Down Expand Up @@ -114,18 +113,18 @@ public function deleteFile(int $id, string $basename): void
/**
* @throws ContentTypeException
*/
public function getUploadedMainFileBasename(stdClass $values): ?string
public function getUploadedMainFileBasename(FileUpload $thumbnail): ?string
{
return $this->getUploadedFileBasename($values->videoThumbnail, $this->supportedImageFileFormats->getMainExtensionByContentType(...));
return $this->getUploadedFileBasename($thumbnail, $this->supportedImageFileFormats->getMainExtensionByContentType(...));
}


/**
* @throws ContentTypeException
*/
public function getUploadedAlternativeFileBasename(stdClass $values): ?string
public function getUploadedAlternativeFileBasename(FileUpload $thumbnail): ?string
{
return $this->getUploadedFileBasename($values->videoThumbnailAlternative, $this->supportedImageFileFormats->getAlternativeExtensionByContentType(...));
return $this->getUploadedFileBasename($thumbnail, $this->supportedImageFileFormats->getAlternativeExtensionByContentType(...));
}


Expand All @@ -149,15 +148,15 @@ private function getUploadedFileBasename(FileUpload $thumbnail, callable $getExt
/**
* @throws ContentTypeException
*/
public function saveVideoThumbnailFiles(int $id, stdClass $values): void
public function saveVideoThumbnailFiles(int $id, FileUpload $videoThumbnail, FileUpload $videoThumbnailAlternative): void
{
$basename = $this->getUploadedMainFileBasename($values);
$basename = $this->getUploadedMainFileBasename($videoThumbnail);
if ($basename !== null) {
$values->videoThumbnail->move($this->mediaResources->getImageFilename($id, $basename));
$videoThumbnail->move($this->mediaResources->getImageFilename($id, $basename));
}
$basename = $this->getUploadedAlternativeFileBasename($values);
$basename = $this->getUploadedAlternativeFileBasename($videoThumbnailAlternative);
if ($basename !== null) {
$values->videoThumbnailAlternative->move($this->mediaResources->getImageFilename($id, $basename));
$videoThumbnailAlternative->move($this->mediaResources->getImageFilename($id, $basename));
}
}

Expand Down
17 changes: 5 additions & 12 deletions app/src/Talks/Slides/TalkSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,16 @@ private function replaceSlideImage(int $talkId, FileUpload $replace, callable $g


/**
* Insert slides.
*
* @param int $talkId
* @param list<ArrayHash<int|string|FileUpload|null>> $slides
* @throws DuplicatedSlideException
* @throws ContentTypeException
* @throws SlideImageUploadFailedException
*/
private function addSlides(int $talkId, array $slides): void
private function addSlides(int $talkId, ArrayHash $slides): void
{
$lastNumber = 0;
try {
foreach ($slides as $slide) {
assert($slide instanceof ArrayHash);
assert($slide->replace instanceof FileUpload);
assert($slide->replaceAlternative instanceof FileUpload);
assert(is_int($slide->number));
Expand Down Expand Up @@ -245,23 +242,21 @@ private function addSlides(int $talkId, array $slides): void


/**
* Update slides.
*
* @param array<int, ArrayHash<int|string|FileUpload|null>> $slides
* @param bool $removeFiles Remove old files?
* @throws DuplicatedSlideException
* @throws ContentTypeException
* @throws SlideImageUploadFailedException
* @throws TalkSlideDoesNotExistException
*/
private function updateSlides(int $talkId, TalkSlideCollection $originalSlides, array $slides, bool $removeFiles): void
private function updateSlides(int $talkId, TalkSlideCollection $originalSlides, ArrayHash $slides, bool $removeFiles): void
{
foreach ($originalSlides as $slide) {
foreach ($slide->getAllFilenames() as $filename) {
$this->incrementOtherSlides($filename);
}
}
foreach ($slides as $id => $slide) {
assert($slide instanceof ArrayHash);
assert($slide->replace instanceof FileUpload || $slide->replace === null);
assert($slide->replaceAlternative instanceof FileUpload || $slide->replaceAlternative === null);
assert(is_string($slide->alias));
Expand Down Expand Up @@ -318,14 +313,12 @@ private function updateSlidesRow(int $talkId, string $alias, int $slideNumber, s


/**
* @param array<int, ArrayHash<int|string|FileUpload|null>> $updateSlides
* @param list<ArrayHash<int|string|FileUpload|null>> $newSlides
* @throws ContentTypeException
* @throws DuplicatedSlideException
* @throws SlideImageUploadFailedException
* @throws TalkSlideDoesNotExistException
*/
public function saveSlides(int $talkId, TalkSlideCollection $originalSlides, array $updateSlides, array $newSlides, bool $deleteReplaced): void
public function saveSlides(int $talkId, TalkSlideCollection $originalSlides, ArrayHash $updateSlides, ArrayHash $newSlides, bool $deleteReplaced): void
{
$this->otherSlides = [];
$this->database->beginTransaction();
Expand Down
Loading

0 comments on commit 404e8a2

Please sign in to comment.