Skip to content

Commit

Permalink
Add assert()s and tests to test those assert()s
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Dec 1, 2024
1 parent c5a9454 commit 5f1a2ab
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 193 deletions.
87 changes: 0 additions & 87 deletions app/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@
<code><![CDATA[$values->videoHref]]></code>
</MixedArgument>
</file>
<file src="src/Form/TalkSlidesFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->deleteReplaced]]></code>
</MixedArgument>
</file>
<file src="src/Form/TrainingApplicationAdminFormFactory.php">
<MixedArgument>
<code><![CDATA[$dateId]]></code>
Expand Down Expand Up @@ -161,12 +156,6 @@
<code><![CDATA[$values->status]]></code>
</MixedArgument>
</file>
<file src="src/Form/TrainingApplicationPreliminaryFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->email]]></code>
<code><![CDATA[$values->name]]></code>
</MixedArgument>
</file>
<file src="src/Form/TrainingApplicationStatusesFormFactory.php">
<MixedArgument>
<code><![CDATA[$id]]></code>
Expand Down Expand Up @@ -226,26 +215,6 @@
<code><![CDATA[$id]]></code>
</MixedArgument>
</file>
<file src="src/Form/TrainingReviewFormFactory.php">
<MixedArgument>
<code><![CDATA[$values->company]]></code>
<code><![CDATA[$values->company]]></code>
<code><![CDATA[$values->hidden]]></code>
<code><![CDATA[$values->hidden]]></code>
<code><![CDATA[$values->href ?: null]]></code>
<code><![CDATA[$values->href ?: null]]></code>
<code><![CDATA[$values->jobTitle ?: null]]></code>
<code><![CDATA[$values->jobTitle ?: null]]></code>
<code><![CDATA[$values->name]]></code>
<code><![CDATA[$values->name]]></code>
<code><![CDATA[$values->note ?: null]]></code>
<code><![CDATA[$values->note ?: null]]></code>
<code><![CDATA[$values->ranking ?: null]]></code>
<code><![CDATA[$values->ranking ?: null]]></code>
<code><![CDATA[$values->review]]></code>
<code><![CDATA[$values->review]]></code>
</MixedArgument>
</file>
<file src="src/Formatter/TexyFormatter.php">
<MixedArgument>
<code><![CDATA[$matches[2]]]></code>
Expand All @@ -254,21 +223,9 @@
<file src="src/Media/VideoThumbnails.php">
<MixedArgument>
<code><![CDATA[$values->videoThumbnail]]></code>
<code><![CDATA[$values->videoThumbnail]]></code>
<code><![CDATA[$values->videoThumbnailAlternative]]></code>
<code><![CDATA[$values->videoThumbnailAlternative]]></code>
</MixedArgument>
</file>
<file src="src/Net/DnsResolver.php">
<MixedArgument>
<code><![CDATA[$record]]></code>
<code><![CDATA[$record]]></code>
<code><![CDATA[$record]]></code>
<code><![CDATA[$record]]></code>
<code><![CDATA[$record]]></code>
<code><![CDATA[$record]]></code>
</MixedArgument>
</file>
<file src="src/Pulse/Passwords/PasswordsSorting.php">
<MixedArgument>
<code><![CDATA[$sorter]]></code>
Expand All @@ -278,57 +235,13 @@
<MixedArgument>
<code><![CDATA[$replace ?? $slide->filename ?? '']]></code>
<code><![CDATA[$replaceAlternative ?? $slide->filenameAlternative ?? '']]></code>
<code><![CDATA[$slide->alias]]></code>
<code><![CDATA[$slide->number]]></code>
<code><![CDATA[$slide->number]]></code>
<code><![CDATA[$slide->replace]]></code>
<code><![CDATA[$slide->replace]]></code>
<code><![CDATA[$slide->replaceAlternative]]></code>
<code><![CDATA[$slide->replaceAlternative]]></code>
<code><![CDATA[$slide->speakerNotes]]></code>
<code><![CDATA[$slide->title]]></code>
</MixedArgument>
</file>
<file src="src/Tls/CertificateGatherer.php">
<MixedArgument>
<code><![CDATA[$ipAddress]]></code>
</MixedArgument>
</file>
<file src="src/Training/ApplicationForm/TrainingApplicationFormSuccess.php">
<MixedArgument>
<code><![CDATA[$values->city]]></code>
<code><![CDATA[$values->city]]></code>
<code><![CDATA[$values->city]]></code>
<code><![CDATA[$values->company]]></code>
<code><![CDATA[$values->company]]></code>
<code><![CDATA[$values->company]]></code>
<code><![CDATA[$values->companyId]]></code>
<code><![CDATA[$values->companyId]]></code>
<code><![CDATA[$values->companyId]]></code>
<code><![CDATA[$values->companyTaxId]]></code>
<code><![CDATA[$values->companyTaxId]]></code>
<code><![CDATA[$values->companyTaxId]]></code>
<code><![CDATA[$values->country]]></code>
<code><![CDATA[$values->country]]></code>
<code><![CDATA[$values->country]]></code>
<code><![CDATA[$values->email]]></code>
<code><![CDATA[$values->email]]></code>
<code><![CDATA[$values->email]]></code>
<code><![CDATA[$values->name]]></code>
<code><![CDATA[$values->name]]></code>
<code><![CDATA[$values->name]]></code>
<code><![CDATA[$values->note]]></code>
<code><![CDATA[$values->note]]></code>
<code><![CDATA[$values->note]]></code>
<code><![CDATA[$values->street]]></code>
<code><![CDATA[$values->street]]></code>
<code><![CDATA[$values->street]]></code>
<code><![CDATA[$values->trainingId]]></code>
<code><![CDATA[$values->zip]]></code>
<code><![CDATA[$values->zip]]></code>
<code><![CDATA[$values->zip]]></code>
</MixedArgument>
</file>
<file src="src/Training/Reviews/TrainingReviews.php">
<MixedArgument>
<code><![CDATA[$row->ranking]]></code>
Expand Down
2 changes: 1 addition & 1 deletion app/src/Application/Routing/BlogPostRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
* Maps HTTP request to a Request object.
*
* @param IRequest $httpRequest
* @return array<string, string>|null
* @return array<array-key, mixed>|null
*/
#[Override]
public function match(IRequest $httpRequest): ?array
Expand Down
9 changes: 4 additions & 5 deletions app/src/EasterEgg/NetteCve202015227.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Nette\Application\BadRequestException;
use Nette\Application\Routers\RouteList;
use Nette\Application\UI\Component;

/**
* Nette CVE-2020-15227, here to easter-egg some bots
Expand All @@ -19,10 +20,7 @@
class NetteCve202015227
{

/**
* @param array<string, string> $params
*/
public function rce(string $callback, array $params): NetteCve202015227Rce
public function rce(string $callback, Component $component): NetteCve202015227Rce
{
$callback = strtolower($callback);
$paramNames = [
Expand All @@ -40,7 +38,8 @@ public function rce(string $callback, array $params): NetteCve202015227Rce

$data = [];

$param = $params[$paramNames[$callback]] ?? null;
$param = $component->getParameters()[$paramNames[$callback]] ?? null;
assert(is_string($param) || $param === null);
if ($param === null) {
throw new BadRequestException(sprintf("[%s] Empty param '%s' for callback '%s'", __CLASS__, $paramNames[$callback], $callback));
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/EasterEgg/Presenters/NettePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
public function actionMicro(string $callback): void
{
sleep(random_int(5, 20));
$rce = $this->cve202015227->rce($callback, $this->getParameters());
$rce = $this->cve202015227->rce($callback, $this);
$this->setView($rce->view->value);
$this->template->eth0RxPackets = $rce->eth0RxPackets;
$this->template->eth1RxPackets = $rce->eth1RxPackets;
Expand Down
3 changes: 0 additions & 3 deletions app/src/Form/InterviewFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ public function create(callable $onSuccess, ?Interview $interview = null): UiFor
}
$onSuccess();
};

$this->videoThumbnails->addOnValidateUploads($form, $videoThumbnailFormFields);

return $form;
}

Expand Down
3 changes: 0 additions & 3 deletions app/src/Form/TalkFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ public function create(callable $onSuccess, ?Talk $talk = null): UiForm
$message->addHtml(Html::el('a')->href($this->linkGenerator->link('Www:Talks:talk', [$values->action]))->setText('Zobrazit'));
$onSuccess($message);
};

$this->videoThumbnails->addOnValidateUploads($form, $videoThumbnailFormFields);

return $form;
}

Expand Down
5 changes: 4 additions & 1 deletion app/src/Form/TalkSlidesFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Nette\Application\Request;
use Nette\Forms\Container;
use Nette\Forms\Form;
use Nette\Utils\ArrayHash;
use Nette\Utils\Html;

readonly class TalkSlidesFormFactory
Expand Down Expand Up @@ -61,6 +62,9 @@ public function create(callable $onSuccess, int $talkId, TalkSlideCollection $sl
$form->onSuccess[] = function (UiForm $form) use ($slides, $onSuccess, $talkId): void {
try {
$values = $form->getFormValues();
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);
$message = $this->texyFormatter->translate('messages.talks.admin.slideadded');
$type = 'info';
Expand Down Expand Up @@ -96,7 +100,6 @@ private function addSlideFields(UiForm $form, Container $container, ?int $filena
->setRequired('Zadejte prosím alias')
->addRule(Form::Pattern, 'Alias musí být ve formátu [_.,a-z0-9-]+', '[_.,a-z0-9-]+');
$container->addInteger('number', 'Slajd:')
->setHtmlType('number')
->setDefaultValue(1)
->setHtmlAttribute('class', 'right slide-nr')
->setRequired('Zadejte prosím číslo slajdu');
Expand Down
4 changes: 3 additions & 1 deletion app/src/Form/TrainingApplicationPreliminaryFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public function create(callable $onSuccess, callable $onError, int $trainingId,
$form->addSubmit('signUp', 'Odeslat');
$form->onSuccess[] = function (UiForm $form) use ($onSuccess, $onError, $trainingId, $action): void {
$values = $form->getFormValues();
assert(is_string($values->name));
assert(is_string($values->email));
try {
$this->formSpam->check($values);
$this->formSpam->check($values->name);
$this->trainingApplicationStorage->addPreliminaryInvitation($trainingId, $values->name, $values->email);
$onSuccess($action);
} catch (SpammyApplicationException) {
Expand Down
24 changes: 16 additions & 8 deletions app/src/Form/TrainingReviewFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public function create(callable $onSuccess, int $dateId, ?TrainingReview $review
->setRequired(false)
->addRule(Form::MaxLength, 'Maximální délka odkazu je %d znaků', 200);
$form->addCheckbox('hidden', 'Skrýt:');
$form->addText('ranking', 'Pořadí:')
$form->addInteger('ranking', 'Pořadí:')
->setRequired(false)
->setHtmlType('number');
->addRule(Form::Min, 'Minimální hodnota pořadí je %d', 0);
$form->addText('note', 'Poznámka:')
->setRequired(false)
->addRule(Form::MaxLength, 'Maximální délka poznámky je %d znaků', 2000);
Expand All @@ -64,29 +64,37 @@ public function create(callable $onSuccess, int $dateId, ?TrainingReview $review

$form->onSuccess[] = function (UiForm $form) use ($onSuccess, $review, $dateId): void {
$values = $form->getFormValues();
assert(is_string($values->name));
assert(is_string($values->company));
assert(is_string($values->jobTitle));
assert(is_string($values->review));
assert(is_string($values->href));
assert(is_bool($values->hidden));
assert(is_int($values->ranking) || $values->ranking === null);
assert(is_string($values->note));
if ($review) {
$this->trainingReviews->updateReview(
$review->getId(),
$dateId,
$values->name,
$values->company,
$values->jobTitle ?: null,
$values->jobTitle !== '' ? $values->jobTitle : null,
$values->review,
$values->href ?: null,
$values->href !== '' ? $values->href : null,
$values->hidden,
$values->ranking ?: null,
$values->ranking !== 0 ? $values->ranking : null,
$values->note ?: null,
);
} else {
$this->trainingReviews->addReview(
$dateId,
$values->name,
$values->company,
$values->jobTitle ?: null,
$values->jobTitle !== '' ? $values->jobTitle : null,
$values->review,
$values->href ?: null,
$values->href !== '' ? $values->href : null,
$values->hidden,
$values->ranking ?: null,
$values->ranking !== 0 ? $values->ranking : null,
$values->note ?: null,
);
}
Expand Down
15 changes: 6 additions & 9 deletions app/src/Media/VideoThumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,14 @@ public function addFormFields(UiForm $form, bool $hasMainVideoThumbnail, bool $h
$videoThumbnailAlternative->addCondition(Form::Filled, true)
->toggle('#currentVideoThumbnailAlternative', false);
}
return new VideoThumbnailFileUploads($videoThumbnail, $videoThumbnailAlternative, $hasMainVideoThumbnail, $hasAlternativeVideoThumbnail);
}


public function addOnValidateUploads(UiForm $form, VideoThumbnailFileUploads $formFields): void
{
$form->onValidate[] = function (UiForm $form) use ($formFields): void {
$form->onValidate[] = function (UiForm $form) use ($videoThumbnail, $videoThumbnailAlternative): void {
$values = $form->getFormValues();
$this->validateUpload($values->videoThumbnail, $formFields->getVideoThumbnail());
$this->validateUpload($values->videoThumbnailAlternative, $formFields->getVideoThumbnailAlternative());
assert($values->videoThumbnail instanceof FileUpload);
assert($values->videoThumbnailAlternative instanceof FileUpload);
$this->validateUpload($values->videoThumbnail, $videoThumbnail);
$this->validateUpload($values->videoThumbnailAlternative, $videoThumbnailAlternative);
};
return new VideoThumbnailFileUploads($videoThumbnail, $videoThumbnailAlternative, $hasMainVideoThumbnail, $hasAlternativeVideoThumbnail);
}


Expand Down
7 changes: 6 additions & 1 deletion app/src/Net/DnsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public function getRecords(string $hostname, int $type): array
}
$result = [];
foreach ($records as $record) {
$result[] = new DnsRecord(...$record);
assert(is_string($record['host']));
assert(is_string($record['class']));
assert(is_int($record['ttl']));
assert(is_string($record['type']));
assert(is_string($record['ip']));
$result[] = new DnsRecord($record['host'], $record['class'], $record['ttl'], $record['type'], $record['ip']);
}
return $result;
}
Expand Down
13 changes: 12 additions & 1 deletion app/src/Talks/Slides/TalkSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ private function addSlides(int $talkId, array $slides): void
$lastNumber = 0;
try {
foreach ($slides as $slide) {
assert($slide->replace instanceof FileUpload);
assert($slide->replaceAlternative instanceof FileUpload);
assert(is_int($slide->number));
$width = self::SLIDE_MAX_WIDTH;
$height = self::SLIDE_MAX_HEIGHT;
$replace = $this->replaceSlideImage($talkId, $slide->replace, $this->supportedImageFileFormats->getMainExtensionByContentType(...), false, null, $width, $height);
$replaceAlternative = $this->replaceSlideImage($talkId, $slide->replaceAlternative, $this->supportedImageFileFormats->getAlternativeExtensionByContentType(...), false, null, $width, $height);
$lastNumber = (int)$slide->number;
$lastNumber = $slide->number;
$this->database->query(
'INSERT INTO talk_slides',
[
Expand Down Expand Up @@ -259,6 +262,14 @@ private function updateSlides(int $talkId, TalkSlideCollection $originalSlides,
}
}
foreach ($slides as $id => $slide) {
assert($slide->replace instanceof FileUpload || $slide->replace === null);
assert($slide->replaceAlternative instanceof FileUpload || $slide->replaceAlternative === null);
assert(is_string($slide->alias));
assert(is_int($slide->number));
assert(is_string($slide->filename));
assert(is_string($slide->filenameAlternative));
assert(is_string($slide->title));
assert(is_string($slide->speakerNotes));
$width = self::SLIDE_MAX_WIDTH;
$height = self::SLIDE_MAX_HEIGHT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
namespace MichalSpacekCz\Training\ApplicationForm;

use MichalSpacekCz\Training\Applications\TrainingApplicationSessionSection;
use stdClass;
use Tracy\Debugger;

class TrainingApplicationFormDataLogger
{

public function log(stdClass $values, string $name, int $dateId, ?TrainingApplicationSessionSection $sessionSection): void
/**
* @param array<array-key, mixed> $values
*/
public function log(array $values, string $name, int $dateId, ?TrainingApplicationSessionSection $sessionSection): void
{
$applicationId = $sessionSection?->getApplicationIdByDateId($name, $dateId);
$logSession = $applicationId !== null ? "id => '{$applicationId}', dateId => '{$dateId}'" : null;
$logValues = [];
foreach ((array)$values as $key => $value) {
foreach ($values as $key => $value) {
$logValues[] = sprintf('%s => %s', $key, is_string($value) ? "'{$value}'" : get_debug_type($value));
}
$message = sprintf(
Expand Down
Loading

0 comments on commit 5f1a2ab

Please sign in to comment.