8000 [OptionsResolver] optimized validation · symfony/symfony@37a3a29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37a3a29

Browse files
committed
[OptionsResolver] optimized validation
1 parent b07fb3c commit 37a3a29

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function setRequired(array $optionNames)
168168
*/
169169
public function setAllowedValues(array $allowedValues)
170 8000 170
{
171-
$this->validateOptionNames(array_keys($allowedValues));
171+
$this->validateOptionNames($allowedValues);
172172

173173
$this->allowedValues = array_replace($this->allowedValues, $allowedValues);
174174

@@ -191,7 +191,7 @@ public function setAllowedValues(array $allowedValues)
191191
*/
192192
public function addAllowedValues(array $allowedValues)
193193
{
194-
$this->validateOptionNames(array_keys($allowedValues));
194+
$this->validateOptionNames($allowedValues);
195195

196196
$this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
197197

@@ -243,7 +243,7 @@ public function isRequired($option)
243243
*/
244244
public function resolve(array $options)
245245
{
246-
$this->validateOptionNames(array_keys($options));
246+
$this->validateOptionNames($options);
247247

248248
// Make sure this method can be called multiple times
249249
$combinedOptions = clone $this->defaultOptions;
@@ -266,44 +266,36 @@ public function resolve(array $options)
266266
* Validates that the given option names exist and throws an exception
267267
* otherwise.
268268
*
269-
* @param array $optionNames A list of option names.
269+
* @param array $optionNames An list of option names as keys.
270270
*
271271
* @throws InvalidOptionsException If any of the options has not been
272272
* defined.
273273
* @throws MissingOptionsException If a required option is missing.
274274
*/
275275
private function validateOptionNames(array $optionNames)
276276
{
277-
ksort($this->knownOptions);
278-
279-
$knownOptions = array_keys($this->knownOptions);
280-
$diff = array_diff($optionNames, $knownOptions);
281-
282-
sort($diff);
277+
$diff = array_diff_key($optionNames, $this->knownOptions);
283278

284279
if (count($diff) > 0) {
285-
if (count($diff) > 1) {
286-
throw new InvalidOptionsException(sprintf('The options "%s" do not exist. Known options are: "%s"', implode('", "', $diff), implode('", "', $knownOptions)));
287-
}
288-
289-
throw new InvalidOptionsException(sprintf('The option "%s" does not exist. Known options are: "%s"', current($diff), implode('", "', $knownOptions)));
280+
ksort($this->knownOptions);
281+
ksort($diff);
282+
283+
throw new InvalidOptionsException(sprintf(
284+
(count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.') . ' Known options are: "%s"',
285+
implode('", "', array_keys($diff)),
286+
implode('", "', array_keys($this->knownOptions))
287+
));
290288
}
291289

292-
ksort($this->requiredOptions);
293-
294-
$requiredOptions = array_keys($this->requiredOptions);
295-
$diff = array_diff($requiredOptions, $optionNames);
296-
297-
sort($diff);
290+
$diff = array_diff_key($this->requiredOptions, $optionNames);
298291

299292
if (count($diff) > 0) {
300-
if (count($diff) > 1) {
301-
throw new MissingOptionsException(sprintf('The required options "%s" are missing.',
302-
implode('",
303-
"', $diff)));
304-
}
293+
ksort($diff);
305294

306-
throw new MissingOptionsException(sprintf('The required option "%s" is missing.', current($diff)));
295+
throw new MissingOptionsException(sprintf(
296+
count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.',
297+
implode('", "', array_keys($diff))
298+
));
307299
}
308300
}
309301

0 commit comments

Comments
 (0)
0