Skip to content

Commit afd1428

Browse files
committed
Form PHPStan fix
1 parent 0cf2530 commit afd1428

File tree

1 file changed

+91
-27
lines changed

1 file changed

+91
-27
lines changed

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Codeception\Module\Symfony;
66

77
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
8+
use Symfony\Component\VarDumper\Cloner\Data;
89
use function array_key_exists;
910
use function in_array;
11+
use function is_array;
1012
use function is_int;
1113
use function sprintf;
1214

@@ -22,10 +24,15 @@ trait FormAssertionsTrait
2224
*/
2325
public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
2426
{
25-
$node = $this->getCLient()->getCrawler()->filter($formSelector);
27+
$node = $this->getClient()->getCrawler()->filter($formSelector);
2628
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
29+
2730
$values = $node->form()->getValues();
28-
$this->assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector));
31+
$this->assertArrayHasKey(
32+
$fieldName,
33+
$values,
34+
$message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector)
35+
);
2936
$this->assertSame($value, $values[$fieldName]);
3037
}
3138

@@ -39,10 +46,15 @@ public function assertFormValue(string $formSelector, string $fieldName, string
3946
*/
4047
public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
4148
{
42-
$node = $this->getCLient()->getCrawler()->filter($formSelector);
49+
$node = $this->getClient()->getCrawler()->filter($formSelector);
4350
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
51+
4452
$values = $node->form()->getValues();
45-
$this->assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector));
53+
$this->assertArrayNotHasKey(
54+
$fieldName,
55+
$values,
56+
$message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector)
57+
);
4658
}
4759

4860
/**
@@ -56,14 +68,9 @@ public function assertNoFormValue(string $formSelector, string $fieldName, strin
5668
public function dontSeeFormErrors(): void
5769
{
5870
$formCollector = $this->grabFormCollector(__FUNCTION__);
71+
$errors = $this->extractFormCollectorScalar($formCollector, 'nb_errors');
5972

60-
$errors = (int)$formCollector->getData()->offsetGet('nb_errors');
61-
62-
$this->assertSame(
63-
0,
64-
$errors,
65-
'Expecting that the form does not have errors, but there were!'
66-
);
73+
$this->assertSame(0, $errors, 'Expecting that the form does not have errors, but there were!');
6774
}
6875

6976
/**
@@ -79,38 +86,51 @@ public function dontSeeFormErrors(): void
7986
public function seeFormErrorMessage(string $field, ?string $message = null): void
8087
{
8188
$formCollector = $this->grabFormCollector(__FUNCTION__);
89+
/** @var list<array<string, mixed>> $forms */
90+
$forms = $this->extractFormCollectorArray($formCollector, 'forms');
8291

83-
if (!$forms = $formCollector->getData()->getValue(true)['forms']) {
92+
if ($forms === []) {
8493
$this->fail('No forms found on the current page.');
8594
}
8695

8796
$fields = [];
8897
$errors = [];
8998

9099
foreach ($forms as $form) {
100+
if (!isset($form['children']) || !is_array($form['children'])) {
101+
continue;
102+
}
91103
foreach ($form['children'] as $child) {
104+
if (!is_array($child) || !array_key_exists('name', $child) || !is_string($child['name'])) {
105+
continue;
106+
}
92107
$fieldName = $child['name'];
93-
$fields[] = $fieldName;
108+
$fields[] = $fieldName;
94109

95-
if (!array_key_exists('errors', $child)) {
110+
if (!isset($child['errors']) || !is_array($child['errors'])) {
96111
continue;
97112
}
98113

99114
foreach ($child['errors'] as $error) {
100-
$errors[$fieldName] = $error['message'];
115+
if (is_array($error)
116+
&& array_key_exists('message', $error)
117+
&& is_string($error['message'])
118+
) {
119+
$errors[$fieldName] = $error['message'];
120+
}
101121
}
102122
}
103123
}
104124

105-
if (!in_array($field, $fields)) {
125+
if (!in_array($field, $fields, true)) {
106126
$this->fail("The field '{$field}' does not exist in the form.");
107127
}
108128

109129
if (!array_key_exists($field, $errors)) {
110130
$this->fail("No form error message for field '{$field}'.");
111131
}
112132

113-
if (!$message) {
133+
if ($message === null) {
114134
return;
115135
}
116136

@@ -164,15 +184,15 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
164184
* ]);
165185
* ```
166186
*
167-
* @param string[] $expectedErrors
187+
* @param array<int|string, string|null> $expectedErrors
168188
*/
169189
public function seeFormErrorMessages(array $expectedErrors): void
170190
{
171-
foreach ($expectedErrors as $field => $message) {
191+
foreach ($expectedErrors as $field => $msg) {
172192
if (is_int($field)) {
173-
$this->seeFormErrorMessage($message);
193+
$this->seeFormErrorMessage((string) $msg);
174194
} else {
175-
$this->seeFormErrorMessage($field, $message);
195+
$this->seeFormErrorMessage($field, $msg);
176196
}
177197
}
178198
}
@@ -188,16 +208,60 @@ public function seeFormErrorMessages(array $expectedErrors): void
188208
public function seeFormHasErrors(): void
189209
{
190210
$formCollector = $this->grabFormCollector(__FUNCTION__);
211+
$errors = $this->extractFormCollectorScalar($formCollector, 'nb_errors');
191212

192-
$this->assertGreaterThan(
193-
0,
194-
$formCollector->getData()->offsetGet('nb_errors'),
195-
'Expecting that the form has errors, but there were none!'
196-
);
213+
$this->assertGreaterThan(0, $errors, 'Expecting that the form has errors, but there were none!');
214+
}
215+
216+
/** @return list<array<string, mixed>> */
217+
private function extractFormCollectorArray(FormDataCollector $collector, string $key): array
218+
{
219+
$data = $collector->getData();
220+
221+
if ($data instanceof Data) {
222+
$rawData = $data->getValue(true);
223+
} else {
224+
$rawData = $data;
225+
}
226+
227+
if (is_array($rawData)
228+
&& array_key_exists($key, $rawData)
229+
&& is_array($rawData[$key])
230+
) {
231+
/** @var array<string, mixed> $slice */
232+
$slice = $rawData[$key];
233+
} else {
234+
$slice = [];
235+
}
236+
237+
/** @var list<array<string, mixed>> $forms */
238+
$forms = array_values($slice);
239+
240+
return $forms;
241+
}
242+
243+
private function extractFormCollectorScalar(FormDataCollector $collector, string $key): int
244+
{
245+
$data = $collector->getData();
246+
$valueRaw = null;
247+
248+
if ($data instanceof Data) {
249+
$valueRaw = $data->offsetGet($key);
250+
} elseif (array_key_exists($key, $data)) {
251+
$valueRaw = $data[$key];
252+
}
253+
254+
if (is_numeric($valueRaw)) {
255+
return (int) $valueRaw;
256+
}
257+
258+
return 0;
197259
}
198260

199261
protected function grabFormCollector(string $function): FormDataCollector
200262
{
201-
return $this->grabCollector('form', $function);
263+
/** @var FormDataCollector $collector */
264+
$collector = $this->grabCollector('form', $function);
265+
return $collector;
202266
}
203267
}

0 commit comments

Comments
 (0)