Skip to content

Commit 9edac31

Browse files
authored
don't early exit (#13)
* don't early exit * simplify * remove unused
1 parent b44c157 commit 9edac31

File tree

3 files changed

+47
-69
lines changed

3 files changed

+47
-69
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"user errors"
1313
],
1414
"require": {
15+
"php": "^8",
1516
"ext-json": "*",
1617
"ext-mbstring": "*",
1718
"ext-intl": "*"

src/Type/Definition/ValidatedFieldDefinition.php

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -101,57 +101,6 @@ protected function _createUserErrorsType(string $name, array $args, array $confi
101101
], [$name], false, \ucfirst($name) . 'Result');
102102
}
103103

104-
private function _noop(mixed $value): int
105-
{
106-
// this is just a no-op validation function to fallback to when no validation function is provided
107-
return 0;
108-
}
109-
110-
/**
111-
* @param mixed[] $arr
112-
*/
113-
protected function _isAssoc(array $arr): bool
114-
{
115-
if ($arr === []) {
116-
return false;
117-
}
118-
119-
return \array_keys($arr) !== \range(0, \count($arr) - 1);
120-
}
121-
122-
/**
123-
* @param array<string, mixed> $config
124-
* @param mixed[] $value
125-
* @param Array<string|int> $path
126-
*
127-
* @throws ValidateItemsError
128-
*/
129-
protected function _validateItems(array $config, array $value, array $path, callable $validate): void
130-
{
131-
foreach ($value as $idx => $subValue) {
132-
if (\is_array($subValue) && ! $this->_isAssoc($subValue)) {
133-
$path[\count($path) - 1] = $idx;
134-
$newPath = $path;
135-
$newPath[] = 0;
136-
$this->_validateItems($config, $subValue, $newPath, $validate);
137-
} else {
138-
$path[\count($path) - 1] = $idx;
139-
$err = $validate($subValue);
140-
141-
if (empty($err)) {
142-
$wrappedType = $config['type']->getInnermostType();
143-
$err = $this->_validate([
144-
'type' => $wrappedType,
145-
], $subValue, $config['type'] instanceof ListOfType);
146-
}
147-
148-
if ($err) {
149-
throw new ValidateItemsError($path, $err);
150-
}
151-
}
152-
}
153-
}
154-
155104
/**
156105
* @param mixed[] $arg
157106
* @param mixed $value
@@ -191,23 +140,46 @@ protected function _validate(array $arg, $value, bool $isParentList = false): ar
191140
}
192141

193142
/**
194-
* @param array<string, mixed> $config
195-
* @param array<mixed> $res
143+
* @param array<string, mixed> $config
144+
* @param mixed[] $value
145+
* @param array<mixed> $res
146+
* @param Array<string|int> $path
147+
*
148+
* @throws ValidateItemsError
196149
*/
197-
protected function _validateListOfType(array $config, mixed $value, array &$res): void
150+
protected function _validateListOfType(array $config, array $value, array &$res, array $path=[0]): void
198151
{
199-
try {
200-
$this->_validateItems($config, $value, [0], $config['validate'] ?? [$this, '_noop']);
201-
} catch (ValidateItemsError $e) {
202-
if (isset($e->error['suberrors'])) {
203-
$err = $e->error;
152+
$validate = $config['validate'] ?? null;
153+
$wrappedType = $config['type']->getWrappedType();
154+
foreach ($value as $idx => $subValue) {
155+
if ($wrappedType instanceof ListOfType) {
156+
$path[\count($path) - 1] = $idx;
157+
$newPath = $path;
158+
$newPath[] = 0;
159+
$this->_validateListOfType(["type"=>$wrappedType, "validate" => $validate], $subValue, $res, $newPath );
204160
} else {
205-
$err = [
206-
'error' => $e->error,
207-
];
161+
$path[\count($path) - 1] = $idx;
162+
$err = $validate != null ? $validate($subValue): 0;
163+
164+
if (empty($err)) {
165+
$wrappedType = $config['type']->getInnermostType();
166+
$err = $this->_validate([
167+
'type' => $wrappedType,
168+
], $subValue, $config['type'] instanceof ListOfType);
169+
}
170+
171+
if ($err) {
172+
if (isset($err['suberrors'])) {
173+
$err = $err;
174+
} else {
175+
$err = [
176+
'error' => $err,
177+
];
178+
}
179+
$err['path'] = $path;
180+
$res[] = $err;
181+
}
208182
}
209-
$err['path'] = $e->path;
210-
$res[] = $err;
211183
}
212184
}
213185

@@ -223,11 +195,7 @@ protected function _validateInputObject(mixed $arg, mixed $value, array &$res, b
223195
$type = $arg['type'];
224196
if (isset($arg['validate'])) {
225197
$err = $arg['validate']($value) ?? [];
226-
if ($err != 0) {
227-
$res['error'] = $err;
228-
229-
return;
230-
}
198+
$res['error'] = $err;
231199
}
232200

233201
$this->_validateInputObjectFields($type, $arg, $value, $res, $isParentList);

tests/Type/ValidatedFieldDefinition/ListOfScalarValidationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function testItemsValidationOnWrappedTypeFail(): void
8686
[
8787
'123-4567',
8888
'xxx456-7890xxx',
89+
'555-whoops',
8990
],
9091
],
9192
]
@@ -104,6 +105,14 @@ public function testItemsValidationOnWrappedTypeFail(): void
104105
'code' => 'invalidPhoneNumber',
105106
'msg' => 'That does not seem to be a valid phone number',
106107
],
108+
[
109+
'path' => [
110+
0,
111+
2,
112+
],
113+
'code' => 'invalidPhoneNumber',
114+
'msg' => 'That does not seem to be a valid phone number',
115+
],
107116
],
108117
],
109118
'result' => null,

0 commit comments

Comments
 (0)