8000 Add union types · symfony/symfony@fc6fa3a · GitHub
[go: up one dir, main page]

Skip to content

Commit fc6fa3a

Browse files
Add union types
1 parent bf5dc51 commit fc6fa3a

38 files changed

+137
-259
lines changed

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,9 @@ public function getMetadata(): array
151151
/**
152152
* Validates a cache key according to PSR-6.
153153
*
154-
* @param string $key The key to validate
155-
*
156154
* @throws InvalidArgumentException When $key is not valid
157155
*/
158-
public static function validateKey($key): string
156+
public static function validateKey(mixed $key): string
159157
{
160158
if (!\is_string($key)) {
161159
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));

src/Symfony/Component/Cache/Messenger/EarlyExpirationMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function findCallback(ReverseContainer $reverseContainer): callable
8888
return $callback;
8989
}
9090

91-
private function __construct(CacheItem $item, string $pool, $callback)
91+
private function __construct(CacheItem $item, string $pool, string|array $callback)
9292
{
9393
$this->item = $item;
9494
$this->pool = $pool;

src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,12 @@ public function saveDeferred(CacheItemInterface $item)
291291
*
292292
* Calling this method also clears the memoized namespace version and thus forces a resynchonization of it.
293293
*
294-
* @param bool $enable
295-
*
296294
* @return bool the previous state of versioning
297295
*/
298-
public function enableVersioning($enable = true)
296+
public function enableVersioning(bool $enable = true)
299297
{
300298
$wasEnabled = $this->versioningIsEnabled;
301-
$this->versioningIsEnabled = (bool) $enable;
299+
$this->versioningIsEnabled = $enable;
302300
$this->namespaceVersion = '';
303301
$this->ids = [];
304302

@@ -356,7 +354,7 @@ private function generateItems(iterable $items, array &$keys): iterable
356354
}
357355
}
358356

359-
private function getId($key)
357+
private function getId(string|int $key)
360358
{
361359
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
362360
$this->ids = [];

src/Symfony/Component/Cache/Traits/MemcachedTrait.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ private function init(\Memcached $client, string $namespace, int $defaultLifetim
8484
*
8585
* @throws \ErrorException When invalid options or servers are provided
8686
*/
87-
public static function createConnection($servers, array $options = [])
87+
public static function createConnection(string|array $servers, array $options = [])
8888
{
8989
if (\is_string($servers)) {
9090
$servers = [$servers];
91-
} elseif (!\is_array($servers)) {
92-
throw new InvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, "%s" given.', \gettype($servers)));
9391
}
9492
if (!static::isSupported()) {
9593
throw new CacheException('Memcached >= 2.2.0 is required.');
@@ -273,7 +271,7 @@ protected function doFetch(array $ids)
273271
/**
274272
* {@inheritdoc}
275273
*/
276-
protected function doHave($id)
274+
protected function doHave(string $id)
277275
{
278276
return false !== $this->getClient()->get(self::encodeKey($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
279277
}
@@ -298,12 +296,12 @@ protected function doDelete(array $ids)
298296
/**
299297
* {@inheritdoc}
300298
*/
301-
protected function doClear($namespace)
299+
protected function doClear(string $namespace)
302300
{
303301
return '' === $namespace && $this->getClient()->flush();
304302
}
305303

306-
private function checkResultCode($result)
304+
private function checkResultCode(mixed $result)
307305
{
308306
$code = $this->client->getResultCode();
309307

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ trait RedisTrait
4747
private $redis;
4848
private $marshaller;
4949

50-
/**
51-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient
52-
*/
53-
F438 private function init($redisClient, string $namespace, int $defaultLifetime, ?MarshallerInterface $marshaller)
50+
private function init(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redisClient, string $namespace, int $defaultLifetime, ?MarshallerInterface $marshaller)
5451
{
5552
parent::__construct($namespace, $defaultLifetime);
5653

5754
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
5855
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
5956
}
6057

61-
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\ClientInterface && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
62-
throw new InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($redisClient)));
63-
}
64-
6558
if ($redisClient instanceof \Predis\ClientInterface && $redisClient->getOptions()->exceptions) {
6659
$options = clone $redisClient->getOptions();
6760
\Closure::bind(function () { $this->options['exceptions'] = false; }, $options, $options)();
@@ -82,14 +75,13 @@ private function init($redisClient, string $namespace, int $defaultLifetime, ?Ma
8275
* - redis:///var/run/redis.sock
8376
* - redis://secret@/var/run/redis.sock/13
8477
*
85-
* @param string $dsn
86-
* @param array $options See self::$defaultConnectionOptions
78+
* @param array $options See self::$defaultConnectionOptions
8779
*
8880
* @throws InvalidArgumentException when the DSN is invalid
8981
*
9082
* @return \Redis|\RedisCluster|RedisClusterProxy|RedisProxy|\Predis\ClientInterface According to the "class" option
9183
*/
92-
public static function createConnection($dsn, array $options = [])
84+
public static function createConnection(string $dsn, array $options = [])
9385
{
9486
if (0 === strpos($dsn, 'redis:')) {
9587
$scheme = 'redis';
@@ -498,7 +490,7 @@ protected function doSave(array $values, int $lifetime)
498490
return $failed;
499491
}
500492

501-
private function pipeline(\Closure $generator, $redis = null): \Generator
493+
private function pipeline(\Closure $generator, object $redis = null): \Generator
502494
{
503495
$ids = [];
504496
$redis = $redis ?? $this->redis;

src/Symfony/Component/Form/ChoiceList/ChoiceList.php

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,117 +35,107 @@ final class ChoiceList
3535
/**
3636
* Creates a cacheable loader from any callable providing iterable choices.
3737
*
38-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
39-
* @param callable $choices A callable that must return iterable choices or grouped choices
40-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
38+
* @param callable $choices A callable that must return iterable choices or grouped choices
39+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
4140
*/
42-
public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader
41+
public static function lazy(FormTypeInterface|FormTypeExtensionInterface $formType, callable $choices, mixed $vary = null): ChoiceLoader
4342
{
4443
return self::loader($formType, new CallbackChoiceLoader($choices), $vary);
4544
}
4645

4746
/**
4847
* Decorates a loader to make it cacheable.
4948
*
50-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
51-
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
52-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
49+
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
50+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the loader
5351
*/
54-
public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader
52+
public static function loader(FormTypeInterface|FormTypeExtensionInterface $formType, ChoiceLoaderInterface $loader, mixed $vary = null): ChoiceLoader
5553
{
5654
return new ChoiceLoader($formType, $loader, $vary);
5755
}
5856

5957
/**
6058
* Decorates a "choice_value" callback to make it cacheable.
6159
*
62-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
63-
* @param callable $value Any pseudo callable to create a unique string value from a choice
64-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
60+
* @param callable $value Any pseudo callable to create a unique string value from a choice
61+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
6562
*/
66-
public static function value($formType, $value, $vary = null): ChoiceValue
63+
public static function value(FormTypeInterface|FormTypeExtensionInterface $formType, callable $value, mixed $vary = null): ChoiceValue
6764
{
6865
return new ChoiceValue($formType, $value, $vary);
6966
}
7067

7168
/**
72-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
73-
* @param callable $filter Any pseudo callable to filter a choice list
74-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
69+
* @param callable $filter Any pseudo callable to filter a choice list
70+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
7571
*/
76-
public static function filter($formType, $filter, $vary = null): ChoiceFilter
72+
public static function filter(FormTypeInterface|FormTypeExtensionInterface $formType, callable $filter, mixed $vary = null): ChoiceFilter
7773
{
7874
return new ChoiceFilter($formType, $filter, $vary);
7975
}
8076

8177
/**
8278
* Decorates a "choice_label" option to make it cacheable.
8379
*
84-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
85-
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
86-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
80+
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
81+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
8782
*/
88-
public static function label($formType, $label, $vary = null): ChoiceLabel
83+
public static function label(FormTypeInterface|FormTypeExtensionInterface $formType, callable|false $label, mixed $vary = null): ChoiceLabel
8984
{
9085
return new ChoiceLabel($formType, $label, $vary);
9186
}
9287

9388
/**
9489
* Decorates a "choice_name" callback to make it cacheable.
9590
*
96-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
97-
* @param callable $fieldName Any pseudo callable to create a field name from a choice
98-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
91+
* @param callable $fieldName Any pseudo callable to create a field name from a choice
92+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
9993
*/
100-
public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName
94+
public static function fieldName(FormTypeInterface|FormTypeExtensionInterface $formType, $fieldName, mixed $vary = null): ChoiceFieldName
10195
{
10296
return new ChoiceFieldName($formType, $fieldName, $vary);
10397
}
10498

10599
/**
106100
* Decorates a "choice_attr" option to make it cacheable.
107101
*
108-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
109-
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
110-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
102+
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
103+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
111104
*/
112-
public static function attr($formType, $attr, $vary = null): ChoiceAttr
105+
public static function attr(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $attr, mixed $vary = null): ChoiceAttr
113106
{
114107
return new ChoiceAttr($formType, $attr, $vary);
115108
}
116109

117110
/**
118111
* Decorates a "choice_translation_parameters" option to make it cacheable.
119112
*
120-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
121-
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
122-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
113+
* @param callable|array $translationParameters Any pseudo callable or array to create translation parameters from a choice
114+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
123115
*/
124-
public static function translationParameters($formType, $translationParameters, $vary = null): ChoiceTranslationParameters
116+
public static function translationParameters(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $translationParameters, mixed $vary = null): ChoiceTranslationParameters
125117
{
126118
return new ChoiceTranslationParameters($formType, $translationParameters, $vary);
127119
}
128120

129121
/**
130122
* Decorates a "group_by" callback to make it cacheable.
131123
*
132-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
133-
* @param callable $groupBy Any pseudo callable to return a group name from a choice
134-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
124+
* @param callable $groupBy Any pseudo callable to return a group name from a choice
125+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the callback
135126
*/
136-
public static function groupBy($formType, $groupBy, $vary = null): GroupBy
127+
public static function groupBy(FormTypeInterface|FormTypeExtensionInterface $formType, callable $groupBy, mixed $vary = null): GroupBy
137128
{
138129
return new GroupBy($formType, $groupBy, $vary);
139130
}
140131

141132
/**
142133
* Decorates a "preferred_choices" option to make it cacheable.
143134
*
144-
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
145-
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
146-
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
135+
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
136+
* @param mixed $vary Dynamic data used to compute a unique hash when caching the option
147137
*/
148-
public static function preferred($formType, $preferred, $vary = null): PreferredChoice
138+
public static function preferred(FormTypeInterface|FormTypeExtensionInterface $formType, callable|array $preferred, mixed $vary = null): PreferredChoice
149139
{
150140
return new PreferredChoice($formType, $preferred, $vary);
151141
}

src/Symfony/Component/Form/DataAccessorInterface.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,36 @@ interface DataAccessorInterface
2121
/**
2222
* Returns the value at the end of the property of the object graph.
2323
*
24-
* @param object|array $viewData The view data of the compound form
25-
* @param FormInterface $form The {@link FormInterface()} instance to check
26-
*
2724
* @return mixed The value at the end of the property
2825
*
2926
* @throws Exception\AccessException If unable to read from the given form data
3027
*/
31-
public function getValue($viewData, FormInterface $form);
28+
public function getValue(object|array $viewData, FormInterface $form);
3229

3330
/**
3431
* Sets the value at the end of the property of the object graph.
3532
*
36-
* @param object|array $viewData The view data of the compound form
37-
* @param mixed $value The value to set at the end of the object graph
38-
* @param FormInterface $form The {@link FormInterface()} instance to check
39-
*
4033
* @throws Exception\AccessException If unable to write the given value
4134
*/
42-
public function setValue(&$viewData, $value, FormInterface $form): void;
35+
public function setValue(object|array &$viewData, mixed $value, FormInterface $form): void;
4336

4437
/**
4538
* Returns whether a value can be read from an object graph.
4639
*
4740
* Whenever this method returns true, {@link getValue()} is guaranteed not
4841
* to throw an exception when called with the same arguments.
4942
*
50-
* @param object|array $viewData The view data of the compound form
51-
* @param FormInterface $form The {@link FormInterface()} instance to check
52-
*
5343
* @return bool Whether the value can be read
5444
*/
55-
public function isReadable($viewData, FormInterface $form): bool;
45+
public function isReadable(object|array $viewData, FormInterface $form): bool;
5646

5747
/**
5848
* Returns whether a value can be written at a given object graph.
5949
*
6050
* Whenever this method returns true, {@link setValue()} is guaranteed not
6151
* to throw an exception when called with the same arguments.
6252
*
63-
* @param object|array $viewData The view data of the compound form
64-
* @param FormInterface $form The {@link FormInterface()} instance to check
65-
*
6653
* @return bool Whether the value can be set
6754
*/
68-
public function isWritable($viewData, FormInterface $form): bool;
55+
public function isWritable(object|array $viewData, FormInterface $form): bool;
6956
}

src/Symfony/Component/Form/Extension/Core/DataAccessor/CallbackAccessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CallbackAccessor implements DataAccessorInterface
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function getValue($data, FormInterface $form)
28+
public function getValue(object|array $data, FormInterface $form)
2929
{
3030
if (null === $getter = $form->getConfig()->getOption('getter')) {
3131
throw new AccessException('Unable to read from the given form data as no getter is defined.');
@@ -37,7 +37,7 @@ public function getValue($data, FormInterface $form)
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function setValue(&$data, $value, FormInterface $form): void
40+
public function setValue(object|array &$data, mixed $value, FormInterface $form): void
4141
{
4242
if (null === $setter = $form->getConfig()->getOption('setter')) {
4343
throw new AccessException('Unable to write the given value as no setter is defined.');
@@ -49,15 +49,15 @@ public function setValue(&$data, $value, FormInterface $form): void
4949
/**
5050
* {@inheritdoc}
5151
*/
52-
public function isReadable($data, FormInterface $form): bool
52+
public function isReadable(object|array $data, FormInterface $form): bool
5353
{
5454
return null !== $form->getConfig()->getOption('getter');
5555
}
5656

5757
/**
5858
* {@inheritdoc}
5959
*/
60-
public function isWritable($data, FormInterface $form): bool
60+
public function isWritable(object|array $data, FormInterface $form): bool
6161
{
6262
return null !== $form->getConfig()->getOption('setter');
6363
}

0 commit comments

Comments
 (0)
0