Skip to content

Commit 8aba377

Browse files
Extract method
1 parent ba650a6 commit 8aba377

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/Metadata/Api/DataProvider.php

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,40 +62,10 @@ public function providedData(string $className, string $methodName): ?array
6262
$this->triggerWarningForMixingOfDataProviderAndTestWith($testMethod);
6363
}
6464

65-
$data = $this->dataProvidedByMethods($testMethod, $dataProvider);
66-
} else {
67-
$data = $this->dataProvidedByMetadata($testWith);
65+
return $this->dataProvidedByMethods($testMethod, $dataProvider);
6866
}
6967

70-
$testMethodNumberOfParameters = $testMethod->getNumberOfParameters();
71-
$testMethodIsNonVariadic = !$testMethod->isVariadic();
72-
73-
foreach ($data as $key => $providedData) {
74-
$value = $providedData->value();
75-
76-
if (!is_array($value)) {
77-
throw new InvalidDataProviderException(
78-
sprintf(
79-
'Data set %s provided by %s is invalid, expected array but got %s',
80-
$this->formatKey($key),
81-
$providedData->label(),
82-
get_debug_type($value),
83-
),
84-
);
85-
}
86-
87-
if ($testMethodIsNonVariadic && $testMethodNumberOfParameters < count($value)) {
88-
$this->triggerWarningForArgumentCount(
89-
$testMethod,
90-
$this->formatKey($key),
91-
$providedData->label(),
92-
count($value),
93-
$testMethodNumberOfParameters,
94-
);
95-
}
96-
}
97-
98-
return $data;
68+
return $this->dataProvidedByMetadata($testMethod, $testWith);
9969
}
10070

10171
/**
@@ -219,13 +189,15 @@ private function dataProvidedByMethods(ReflectionMethod $testMethod, MetadataCol
219189
);
220190
}
221191

192+
$this->validate($testMethod, $result);
193+
222194
return $result;
223195
}
224196

225197
/**
226198
* @return array<ProvidedData>
227199
*/
228-
private function dataProvidedByMetadata(MetadataCollection $testWith): array
200+
private function dataProvidedByMetadata(ReflectionMethod $testMethod, MetadataCollection $testWith): array
229201
{
230202
$result = [];
231203

@@ -253,9 +225,47 @@ private function dataProvidedByMetadata(MetadataCollection $testWith): array
253225
}
254226
}
255227

228+
$this->validate($testMethod, $result);
229+
256230
return $result;
257231
}
258232

233+
/**
234+
* @param array<ProvidedData> $data
235+
*
236+
* @throws InvalidDataProviderException
237+
*/
238+
private function validate(ReflectionMethod $testMethod, array $data): void
239+
{
240+
$testMethodNumberOfParameters = $testMethod->getNumberOfParameters();
241+
$testMethodIsNonVariadic = !$testMethod->isVariadic();
242+
243+
foreach ($data as $key => $providedData) {
244+
$value = $providedData->value();
245+
246+
if (!is_array($value)) {
247+
throw new InvalidDataProviderException(
248+
sprintf(
249+
'Data set %s provided by %s is invalid, expected array but got %s',
250+
$this->formatKey($key),
251+
$providedData->label(),
252+
get_debug_type($value),
253+
),
254+
);
255+
}
256+
257+
if ($testMethodIsNonVariadic && $testMethodNumberOfParameters < count($value)) {
258+
$this->triggerWarningForArgumentCount(
259+
$testMethod,
260+
$this->formatKey($key),
261+
$providedData->label(),
262+
count($value),
263+
$testMethodNumberOfParameters,
264+
);
265+
}
266+
}
267+
}
268+
259269
/**
260270
* @param int|non-empty-string $key
261271
*

0 commit comments

Comments
 (0)