Skip to content

Commit 99b38b4

Browse files
Add union types
1 parent 9b50f78 commit 99b38b4

File tree

3 files changed

+21
-68
lines changed

3 files changed

+21
-68
lines changed

OptionConfigurator.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function __construct(string $name, OptionsResolver $resolver)
2828
/**
2929
* Adds allowed types for this option.
3030
*
31-
* @param string ...$types One or more accepted types
32-
*
3331
* @return $this
3432
*
3533
* @throws AccessException If called from a lazy option or normalizer
@@ -50,7 +48,7 @@ public function allowedTypes(string ...$types): self
5048
*
5149
* @throws AccessException If called from a lazy option or normalizer
5250
*/
53-
public function allowedValues(...$values): self
51+
public function allowedValues(mixed ...$values): self
5452
{
5553
$this->resolver->setAllowedValues($this->name, $values);
5654

@@ -60,13 +58,11 @@ public function allowedValues(...$values): self
6058
/**
6159
* Sets the default value for this option.
6260
*
63-
* @param mixed $value The default value of the option
64-
*
6561
* @return $this
6662
*
6763
* @throws AccessException If called from a lazy option or normalizer
6864
*/
69-
public function default($value): self
65+
public function default(mixed $value): self
7066
{
7167
$this->resolver->setDefault($this->name, $value);
7268

@@ -90,7 +86,7 @@ public function define(string $option): self
9086
*
9187
* @return $this
9288
*/
93-
public function deprecated(string $package, string $version, $message = 'The option "%name%" is deprecated.'): self
89+
public function deprecated(string $package, string $version, string|\Closure $message = 'The option "%name%" is deprecated.'): self
9490
{
9591
$this->resolver->setDeprecated($this->name, $package, $version, $message);
9692

@@ -100,8 +96,6 @@ public function deprecated(string $package, string $version, $message = 'The opt
10096
/**
10197
* Sets the normalizer for this option.
10298
*
103-
* @param \Closure $normalizer The normalizer
104-
*
10599
* @return $this
106100
*
107101
* @throws AccessException If called from a lazy option or normalizer

OptionsResolver.php

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,11 @@ class OptionsResolver implements Options
187187
* // 'default' === $parent['connection']
188188
* }
189189
*
190-
* @param string $option The name of the option
191-
* @param mixed $value The default value of the option
192-
*
193190
* @return $this
194191
*
195192
* @throws AccessException If called from a lazy option or normalizer
196193
*/
197-
public function setDefault(string $option, $value)
194+
public function setDefault(string $option, mixed $value)
198195
{
199196
// Setting is not possible once resolving starts, because then lazy
200197
// options could manipulate the state of the object, leading to
@@ -263,8 +260,6 @@ public function setDefault(string $option, $value)
263260
/**
264261
* Sets a list of default values.
265262
*
266-
* @param array $defaults The default values to set
267-
*
268263
* @return $this
269264
*
270265
* @throws AccessException If called from a lazy option or normalizer
@@ -284,8 +279,6 @@ public function setDefaults(array $defaults)
284279
* Returns true if {@link setDefault()} was called for this option.
285280
* An option is also considered set if it was set to null.
286281
*
287-
* @param string $option The option name
288-
*
289282
* @return bool Whether a default value is set
290283
*/
291284
public function hasDefault(string $option)
@@ -302,7 +295,7 @@ public function hasDefault(string $option)
302295
*
303296
* @throws AccessException If called from a lazy option or normalizer
304297
*/
305-
public function setRequired($optionNames)
298+
public function setRequired(string|array $optionNames)
306299
{
307300
if ($this->locked) {
308301
throw new AccessException('Options cannot be made required from a lazy option or normalizer.');
@@ -321,8 +314,6 @@ public function setRequired($optionNames)
321314
*
322315
* An option is required if it was passed to {@link setRequired()}.
323316
*
324-
* @param string $option The name of the option
325-
*
326317
* @return bool Whether the option is required
327318
*/
328319
public function isRequired(string $option)
@@ -349,8 +340,6 @@ public function getRequiredOptions()
349340
* to {@link setDefault()}. This option must be passed explicitly to
350341
* {@link resolve()}, otherwise an exception will be thrown.
351342
*
352-
* @param string $option The name of the option
353-
*
354343
* @return bool Whether the option is missing
355344
*/
356345
public function isMissing(string $option)
@@ -383,7 +372,7 @@ public function getMissingOptions()
383372
*
384373
* @throws AccessException If called from a lazy option or normalizer
385374
*/
386-
public function setDefined($optionNames)
375+
public function setDefined(string|array $optionNames)
387376
{
388377
if ($this->locked) {
389378
throw new AccessException('Options cannot be defined from a lazy option or normalizer.');
@@ -402,8 +391,6 @@ public function setDefined($optionNames)
402391
* Returns true for any option passed to {@link setDefault()},
403392
* {@link setRequired()} or {@link setDefined()}.
404393
*
405-
* @param string $option The option name
406-
*
407394
* @return bool Whether the option is defined
408395
*/
409396
public function isDefined(string $option)
@@ -449,7 +436,7 @@ public function isNested(string $option): bool
449436
* @param string $version The version of the package that introduced the deprecation
450437
* @param string|\Closure $message The deprecation message to use
451438
*/
452-
public function setDeprecated(string $option, string $package, string $version, $message = 'The option "%name%" is deprecated.'): self
439+
public function setDeprecated(string $option, string $package, string $version, string|\Closure $message = 'The option "%name%" is deprecated.'): self
453440
{
454441
if ($this->locked) {
455442
throw new AccessException('Options cannot be deprecated from a lazy option or normalizer.');
@@ -503,9 +490,6 @@ public function isDeprecated(string $option): bool
503490
*
504491
* The resolved option value is set to the return value of the closure.
505492
*
506-
* @param string $option The option name
507-
* @param \Closure $normalizer The normalizer
508-
*
509493
* @return $this
510494
*
511495
* @throws UndefinedOptionsException If the option is undefined
@@ -547,10 +531,6 @@ public function setNormalizer(string $option, \Closure $normalizer)
547531
*
548532
* The resolved option value is set to the return value of the closure.
549533
*
550-
* @param string $option The option name
551-
* @param \Closure $normalizer The normalizer
552-
* @param bool $forcePrepend If set to true, prepend instead of appending
553-
*
554534
* @return $this
555535
*
556536
* @throws UndefinedOptionsException If the option is undefined
@@ -592,15 +572,14 @@ public function addNormalizer(string $option, \Closure $normalizer, bool $forceP
592572
* The closure receives the value as argument and should return true to
593573
* accept the value and false to reject the value.
594574
*
595-
* @param string $option The option name
596-
* @param mixed $allowedValues One or more acceptable values/closures
575+
* @param mixed $allowedValues One or more acceptable values/closures
597576
*
598577
* @return $this
599578
*
600579
* @throws UndefinedOptionsException If the option is undefined
601580
* @throws AccessException If called from a lazy option or normalizer
602581
*/
603-
public function setAllowedValues(string $option, $allowedValues)
582+
public function setAllowedValues(string $option, mixed $allowedValues)
604583
{
605584
if ($this->locked) {
606585
throw new AccessException('Allowed values cannot be set from a lazy option or normalizer.');
@@ -633,15 +612,14 @@ public function setAllowedValues(string $option, $allowedValues)
633612
* The closure receives the value as argument and should return true to
634613
* accept the value and false to reject the value.
635614
*
636-
* @param string $option The option name
637-
* @param mixed $allowedValues One or more acceptable values/closures
615+
* @param mixed $allowedValues One or more acceptable values/closures
638616
*
639617
* @return $this
640618
*
641619
* @throws UndefinedOptionsException If the option is undefined
642620
* @throws AccessException If called from a lazy option or normalizer
643621
*/
644-
public function addAllowedValues(string $option, $allowedValues)
622+
public function addAllowedValues(string $option, mixed $allowedValues)
645623
{
646624
if ($this->locked) {
647625
throw new AccessException('Allowed values cannot be added from a lazy option or normalizer.');
@@ -674,15 +652,14 @@ public function addAllowedValues(string $option, $allowedValues)
674652
* acceptable. Additionally, fully-qualified class or interface names may
675653
* be passed.
676654
*
677-
* @param string $option The option name
678655
* @param string|string[] $allowedTypes One or more accepted types
679656
*
680657
* @return $this
681658
*
682659
* @throws UndefinedOptionsException If the option is undefined
683660
* @throws AccessException If called from a lazy option or normalizer
684661
*/
685-
public function setAllowedTypes(string $option, $allowedTypes)
662+
public function setAllowedTypes(string $option, string|array $allowedTypes)
686663
{
687664
if ($this->locked) {
688665
throw new AccessException('Allowed types cannot be set from a lazy option or normalizer.');
@@ -709,15 +686,14 @@ public function setAllowedTypes(string $option, $allowedTypes)
709686
* acceptable. Additionally, fully-qualified class or interface names may
710687
* be passed.
711688
*
712-
* @param string $option The option name
713689
* @param string|string[] $allowedTypes One or more accepted types
714690
*
715691
* @return $this
716692
*
717693
* @throws UndefinedOptionsException If the option is undefined
718694
* @throws AccessException If called from a lazy option or normalizer
719695
*/
720-
public function addAllowedTypes(string $option, $allowedTypes)
696+
public function addAllowedTypes(string $option, string|array $allowedTypes)
721697
{
722698
if ($this->locked) {
723699
throw new AccessException('Allowed types cannot be added from a lazy option or normalizer.');
@@ -824,7 +800,7 @@ public function isPrototype(): bool
824800
*
825801
* @throws AccessException If called from a lazy option or normalizer
826802
*/
827-
public function remove($optionNames)
803+
public function remove(string|array $optionNames)
828804
{
829805
if ($this->locked) {
830806
throw new AccessException('Options cannot be removed from a lazy option or normalizer.');
@@ -877,8 +853,6 @@ public function clear()
877853
* - Options have invalid types;
878854
* - Options have invalid values.
879855
*
880-
* @param array $options A map of option names to values
881-
*
882856
* @return array The merged and validated options
883857
*
884858
* @throws UndefinedOptionsException If an option name is undefined
@@ -940,8 +914,7 @@ public function resolve(array $options = [])
940914
/**
941915
* Returns the resolved value of an option.
942916
*
943-
* @param string $option The option name
944-
* @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default)
917+
* @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default)
945918
*
946919
* @return mixed The option value
947920
*
@@ -953,7 +926,7 @@ public function resolve(array $options = [])
953926
* @throws OptionDefinitionException If there is a cyclic dependency between
954927
* lazy options and/or normalizers
955928
*/
956-
public function offsetGet($option, bool $triggerDeprecation = true)
929+
public function offsetGet(mixed $option, bool $triggerDeprecation = true)
957930
{
958931
if (!$this->locked) {
959932
throw new AccessException('Array access is only supported within closures of lazy options and normalizers.');
@@ -1173,7 +1146,7 @@ public function offsetGet($option, bool $triggerDeprecation = true)
11731146
return $value;
11741147
}
11751148

1176-
private function verifyTypes(string $type, $value, array &$invalidTypes, int $level = 0): bool
1149+
private function verifyTypes(string $type, mixed $value, array &$invalidTypes, int $level = 0): bool
11771150
{
11781151
if (\is_array($value) && '[]' === substr($type, -2)) {
11791152
$type = substr($type, 0, -2);
@@ -1202,15 +1175,13 @@ private function verifyTypes(string $type, $value, array &$invalidTypes, int $le
12021175
/**
12031176
* Returns whether a resolved option with the given name exists.
12041177
*
1205-
* @param string $option The option name
1206-
*
12071178
* @return bool Whether the option is set
12081179
*
12091180
* @throws AccessException If accessing this method outside of {@link resolve()}
12101181
*
12111182
* @see \ArrayAccess::offsetExists()
12121183
*/
1213-
public function offsetExists($option)
1184+
public function offsetExists(mixed $option)
12141185
{
12151186
if (!$this->locked) {
12161187
throw new AccessException('Array access is only supported within closures of lazy options and normalizers.');
@@ -1224,7 +1195,7 @@ public function offsetExists($option)
12241195
*
12251196
* @throws AccessException
12261197
*/
1227-
public function offsetSet($option, $value)
1198+
public function offsetSet(mixed $option, mixed $value)
12281199
{
12291200
throw new AccessException('Setting options via array access is not supported. Use setDefault() instead.');
12301201
}
@@ -1234,7 +1205,7 @@ public function offsetSet($option, $value)
12341205
*
12351206
* @throws AccessException
12361207
*/
1237-
public function offsetUnset($option)
1208+
public function offsetUnset(mixed $option)
12381209
{
12391210
throw new AccessException('Removing options via array access is not supported. Use remove() instead.');
12401211
}
@@ -1265,10 +1236,8 @@ public function count()
12651236
* This method returns the equivalent PHP tokens for most scalar types
12661237
* (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
12671238
* in double quotes (").
1268-
*
1269-
* @param mixed $value The value to format as string
12701239
*/
1271-
private function formatValue($value): string
1240+
private function formatValue(mixed $value): string
12721241
{
12731242
if (\is_object($value)) {
12741243
return \get_class($value);

Tests/OptionsResolverTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,16 +464,6 @@ public function testSetDeprecatedFailsIfUnknownOption()
464464
$this->resolver->setDeprecated('foo', 'vendor/package', '1.1');
465465
}
466466

467-
public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
468-
{
469-
$this->expectException(InvalidArgumentException::class);
470-
$this->expectExceptionMessage('Invalid type for deprecation message argument, expected string or \Closure, but got "bool".');
471-
$this->resolver
472-
->setDefined('foo')
473-
->setDeprecated('foo', 'vendor/package', '1.1', true)
474-
;
475-
}
476-
477467
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
478468
{
479469
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)