Skip to content

Commit a6a3c9f

Browse files
committed
Assert form values
1 parent 75d36ac commit a6a3c9f

7 files changed

+115
-105
lines changed

app/psalm-baseline.xml

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,6 @@
161161
<code><![CDATA[$values->status]]></code>
162162
</MixedArgument>
163163
</file>
164-
<file src="src/Form/TrainingApplicationPreliminaryFormFactory.php">
165-
<MixedArgument>
166-
<code><![CDATA[$values->email]]></code>
167-
<code><![CDATA[$values->name]]></code>
168-
</MixedArgument>
169-
</file>
170164
<file src="src/Form/TrainingApplicationStatusesFormFactory.php">
171165
<MixedArgument>
172166
<code><![CDATA[$id]]></code>
@@ -294,41 +288,6 @@
294288
<code><![CDATA[$ipAddress]]></code>
295289
</MixedArgument>
296290
</file>
297-
<file src="src/Training/ApplicationForm/TrainingApplicationFormSuccess.php">
298-
<MixedArgument>
299-
<code><![CDATA[$values->city]]></code>
300-
<code><![CDATA[$values->city]]></code>
301-
<code><![CDATA[$values->city]]></code>
302-
<code><![CDATA[$values->company]]></code>
303-
<code><![CDATA[$values->company]]></code>
304-
<code><![CDATA[$values->company]]></code>
305-
<code><![CDATA[$values->companyId]]></code>
306-
<code><![CDATA[$values->companyId]]></code>
307-
<code><![CDATA[$values->companyId]]></code>
308-
<code><![CDATA[$values->companyTaxId]]></code>
309-
<code><![CDATA[$values->companyTaxId]]></code>
310-
<code><![CDATA[$values->companyTaxId]]></code>
311-
<code><![CDATA[$values->country]]></code>
312-
<code><![CDATA[$values->country]]></code>
313-
<code><![CDATA[$values->country]]></code>
314-
<code><![CDATA[$values->email]]></code>
315-
<code><![CDATA[$values->email]]></code>
316-
<code><![CDATA[$values->email]]></code>
317-
<code><![CDATA[$values->name]]></code>
318-
<code><![CDATA[$values->name]]></code>
319-
<code><![CDATA[$values->name]]></code>
320-
<code><![CDATA[$values->note]]></code>
321-
<code><![CDATA[$values->note]]></code>
322-
<code><![CDATA[$values->note]]></code>
323-
<code><![CDATA[$values->street]]></code>
324-
<code><![CDATA[$values->street]]></code>
325-
<code><![CDATA[$values->street]]></code>
326-
<code><![CDATA[$values->trainingId]]></code>
327-
<code><![CDATA[$values->zip]]></code>
328-
<code><![CDATA[$values->zip]]></code>
329-
<code><![CDATA[$values->zip]]></code>
330-
</MixedArgument>
331-
</file>
332291
<file src="src/Training/Reviews/TrainingReviews.php">
333292
<MixedArgument>
334293
<code><![CDATA[$row->ranking]]></code>

app/src/Form/TrainingApplicationPreliminaryFormFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public function create(callable $onSuccess, callable $onError, int $trainingId,
2727
$form->addSubmit('signUp', 'Odeslat');
2828
$form->onSuccess[] = function (UiForm $form) use ($onSuccess, $onError, $trainingId, $action): void {
2929
$values = $form->getFormValues();
30+
assert(is_string($values->name));
31+
assert(is_string($values->email));
3032
try {
31-
$this->formSpam->check($values);
33+
$this->formSpam->check($values->name);
3234
$this->trainingApplicationStorage->addPreliminaryInvitation($trainingId, $values->name, $values->email);
3335
$onSuccess($action);
3436
} catch (SpammyApplicationException) {

app/src/Training/ApplicationForm/TrainingApplicationFormDataLogger.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
namespace MichalSpacekCz\Training\ApplicationForm;
55

66
use MichalSpacekCz\Training\Applications\TrainingApplicationSessionSection;
7-
use stdClass;
87
use Tracy\Debugger;
98

109
class TrainingApplicationFormDataLogger
1110
{
1211

13-
public function log(stdClass $values, string $name, int $dateId, ?TrainingApplicationSessionSection $sessionSection): void
12+
/**
13+
* @param array<array-key, mixed> $values
14+
*/
15+
public function log(array $values, string $name, int $dateId, ?TrainingApplicationSessionSection $sessionSection): void
1416
{
1517
$applicationId = $sessionSection?->getApplicationIdByDateId($name, $dateId);
1618
$logSession = $applicationId !== null ? "id => '{$applicationId}', dateId => '{$dateId}'" : null;
1719
$logValues = [];
18-
foreach ((array)$values as $key => $value) {
20+
foreach ($values as $key => $value) {
1921
$logValues[] = sprintf('%s => %s', $key, is_string($value) ? "'{$value}'" : get_debug_type($value));
2022
}
2123
$message = sprintf(

app/src/Training/ApplicationForm/TrainingApplicationFormSpam.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use Composer\Pcre\Regex;
77
use MichalSpacekCz\Training\Exceptions\SpammyApplicationException;
8-
use stdClass;
98

109
class TrainingApplicationFormSpam
1110
{
@@ -16,15 +15,18 @@ class TrainingApplicationFormSpam
1615
private const string FIELD_MISSING_VALUE = 'missing';
1716

1817

19-
public function check(stdClass $values): void
18+
/**
19+
* @throws SpammyApplicationException
20+
*/
21+
public function check(string $name, ?string $company = null, ?string $companyId = null, ?string $companyTaxId = null, ?string $note = null): void
2022
{
21-
if (Regex::isMatch('~\s+href="\s*https?://~', $values->note ?? '')) {
23+
if (Regex::isMatch('~\s+href="\s*https?://~', $note ?? self::FIELD_MISSING_VALUE)) {
2224
throw new SpammyApplicationException();
2325
} elseif (
24-
ctype_lower($values->name ?? self::FIELD_MISSING_VALUE)
25-
&& ctype_lower($values->company ?? self::FIELD_MISSING_VALUE)
26-
&& ctype_lower($values->companyId ?? self::FIELD_MISSING_VALUE)
27-
&& ctype_lower($values->companyTaxId ?? self::FIELD_MISSING_VALUE)
26+
ctype_lower($name)
27+
&& ctype_lower($company ?? self::FIELD_MISSING_VALUE)
28+
&& ctype_lower($companyId ?? self::FIELD_MISSING_VALUE)
29+
&& ctype_lower($companyTaxId ?? self::FIELD_MISSING_VALUE)
2830
) {
2931
throw new SpammyApplicationException();
3032
}

app/src/Training/ApplicationForm/TrainingApplicationFormSuccess.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use ParagonIE\Halite\Alerts\HaliteAlert;
2222
use PDOException;
2323
use SodiumException;
24-
use stdClass;
2524
use Tracy\Debugger;
2625

2726
readonly class TrainingApplicationFormSuccess
@@ -57,11 +56,22 @@ public function success(
5756
TrainingApplicationSessionSection $sessionSection,
5857
): void {
5958
$values = $form->getFormValues();
59+
assert(is_string($values->name));
60+
assert(is_string($values->email));
61+
assert(is_string($values->company));
62+
assert(is_string($values->street));
63+
assert(is_string($values->city));
64+
assert(is_string($values->zip));
65+
assert(is_string($values->country));
66+
assert(is_string($values->companyId));
67+
assert(is_string($values->companyTaxId));
68+
assert(is_string($values->note));
6069
try {
61-
$this->formSpam->check($values);
70+
$this->formSpam->check($values->name, $values->company, $values->companyId, $values->companyTaxId, $values->note);
6271
if ($multipleDates) {
63-
$this->checkTrainingDate($values, $action, $dates, $sessionSection);
64-
$date = $dates[$values->trainingId] ?? false;
72+
assert(is_int($values->trainingId));
73+
$this->checkTrainingDate((array)$values, $action, $values->trainingId, $dates, $sessionSection);
74+
$date = $dates[$values->trainingId];
6575
} else {
6676
$date = reset($dates);
6777
}
@@ -153,14 +163,15 @@ public function success(
153163

154164

155165
/**
166+
* @param array<array-key, mixed> $values
156167
* @param array<int, TrainingDate> $dates
157168
* @throws TrainingDateNotUpcomingException
158169
*/
159-
private function checkTrainingDate(stdClass $values, string $name, array $dates, TrainingApplicationSessionSection $sessionSection): void
170+
private function checkTrainingDate(array $values, string $name, int $dateId, array $dates, TrainingApplicationSessionSection $sessionSection): void
160171
{
161-
if (!isset($dates[$values->trainingId])) {
162-
$this->formDataLogger->log($values, $name, $values->trainingId, $sessionSection);
163-
throw new TrainingDateNotUpcomingException($values->trainingId, $dates);
172+
if (!isset($dates[$dateId])) {
173+
$this->formDataLogger->log($values, $name, $dateId, $sessionSection);
174+
throw new TrainingDateNotUpcomingException($dateId, $dates);
164175
}
165176
}
166177

app/tests/Training/ApplicationForm/TrainingApplicationFormDataLoggerTest.phpt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use MichalSpacekCz\Training\Files\TrainingFiles;
1515
use MichalSpacekCz\Training\Mails\TrainingMailMessageFactory;
1616
use Nette\Utils\Html;
1717
use Override;
18-
use stdClass;
1918
use Tester\Assert;
2019
use Tester\TestCase;
2120

@@ -50,37 +49,40 @@ class TrainingApplicationFormDataLoggerTest extends TestCase
5049

5150
public function testLogNoValuesNoSession(): void
5251
{
53-
$this->formDataLogger->log(new stdClass(), 'foo', self::DATE_ID, null);
52+
$this->formDataLogger->log([], 'foo', self::DATE_ID, null);
5453
Assert::same(['Application session data for foo: undefined, form values: empty'], $this->logger->getLogged());
5554
}
5655

5756

5857
public function testLogNoSession(): void
5958
{
60-
$values = new stdClass();
61-
$values->key1 = 'value1';
62-
$values->key2 = 'value2';
59+
$values = [
60+
'key1' => 'value1',
61+
'key2' => 'value2',
62+
];
6363
$this->formDataLogger->log($values, 'foo', self::DATE_ID, null);
6464
Assert::same(["Application session data for foo: undefined, form values: key1 => 'value1', key2 => 'value2'"], $this->logger->getLogged());
6565
}
6666

6767

6868
public function testLogEmptySession(): void
6969
{
70-
$values = new stdClass();
71-
$values->key1 = 'value1';
72-
$values->key2 = 'value2';
70+
$values = [
71+
'key1' => 'value1',
72+
'key2' => 'value2',
73+
];
7374
$this->formDataLogger->log($values, 'foo', self::DATE_ID, $this->getTrainingSessionSection());
7475
Assert::same(["Application session data for foo: empty, form values: key1 => 'value1', key2 => 'value2'"], $this->logger->getLogged());
7576
}
7677

7778

7879
public function testLog(): void
7980
{
80-
$values = new stdClass();
81-
$values->key1 = 'value1';
82-
$values->key2 = 'value2';
83-
$values->key3 = 1336;
81+
$values = [
82+
'key1' => 'value1',
83+
'key2' => 'value2',
84+
'key3' => 1336,
85+
];
8486
$trainingName = 'foo';
8587

8688
$session = $this->getTrainingSessionSection();

app/tests/Training/ApplicationForm/TrainingApplicationFormSpamTest.phpt

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ declare(strict_types = 1);
55

66
namespace MichalSpacekCz\Training\ApplicationForm;
77

8-
use Generator;
98
use MichalSpacekCz\Test\NullLogger;
109
use MichalSpacekCz\Test\TestCaseRunner;
1110
use MichalSpacekCz\Training\Exceptions\SpammyApplicationException;
12-
use stdClass;
1311
use Tester\Assert;
1412
use Tester\TestCase;
1513

@@ -26,45 +24,79 @@ class TrainingApplicationFormSpamTest extends TestCase
2624
}
2725

2826

29-
public function getValues(): Generator
27+
/**
28+
* @return list<array{name:string, companyId:string|null, companyTaxId:string|null, company:string|null, note:string|null, isNice:bool}>
29+
*/
30+
public function getValues(): array
3031
{
31-
$values = new stdClass();
32-
$values->note = 'foo href="https:// example" bar baz';
33-
yield [$values, false];
34-
35-
$values = new stdClass();
36-
$values->name = 'zggnbijhah';
37-
$values->companyId = 'vwetyeofcx';
38-
$values->companyTaxId = 'tyqvukaims';
39-
$values->company = 'qzpormrfcq';
40-
yield [$values, false];
41-
42-
$values = new stdClass();
43-
$values->name = 'zggnbijhah';
44-
yield [$values, false];
45-
46-
yield [new stdClass(), false];
47-
$values = new stdClass();
48-
$values->name = 'foo bar';
49-
yield [$values, true];
50-
51-
$values = new stdClass();
52-
$values->companyId = 'foobar1';
53-
yield [$values, true];
54-
55-
$values = new stdClass();
56-
$values->companyTaxId = 'foobar1';
57-
yield [$values, true];
32+
return [
33+
[
34+
'name' => 'foo bar',
35+
'companyId' => null,
36+
'companyTaxId' => null,
37+
'company' => null,
38+
'note' => 'foo href="https:// example" bar baz',
39+
'isNice' => false,
40+
],
41+
[
42+
'name' => 'zggnbijhah',
43+
'companyId' => 'vwetyeofcx',
44+
'companyTaxId' => 'tyqvukaims',
45+
'company' => 'qzpormrfcq',
46+
'note' => null,
47+
'isNice' => false,
48+
],
49+
[
50+
'name' => 'zggnbijhah',
51+
'companyId' => null,
52+
'companyTaxId' => null,
53+
'company' => null,
54+
'note' => null,
55+
'isNice' => false,
56+
],
57+
[
58+
'name' => 'foo bar',
59+
'companyId' => null,
60+
'companyTaxId' => null,
61+
'company' => null,
62+
'note' => null,
63+
'isNice' => true,
64+
],
65+
[
66+
'name' => '',
67+
'companyId' => 'foobar1',
68+
'companyTaxId' => null,
69+
'company' => null,
70+
'note' => null,
71+
'isNice' => true,
72+
],
73+
[
74+
'name' => '',
75+
'companyId' => null,
76+
'companyTaxId' => 'foobar1',
77+
'company' => null,
78+
'note' => null,
79+
'isNice' => true,
80+
],
81+
[
82+
'name' => '',
83+
'companyId' => null,
84+
'companyTaxId' => null,
85+
'company' => 'comp any',
86+
'note' => null,
87+
'isNice' => true,
88+
],
89+
];
5890
}
5991

6092

6193
/**
6294
* @dataProvider getValues
6395
*/
64-
public function testIsSpam(stdClass $values, bool $isNice): void
96+
public function testIsSpam(string $name, ?string $companyId, ?string $companyTaxId, ?string $company, ?string $note, bool $isNice): void
6597
{
66-
$check = function () use ($values): void {
67-
$this->formSpam->check($values);
98+
$check = function () use ($name, $company, $companyId, $companyTaxId, $note): void {
99+
$this->formSpam->check($name, $company, $companyId, $companyTaxId, $note);
68100
};
69101
if ($isNice) {
70102
Assert::noError($check);

0 commit comments

Comments
 (0)