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

Skip to content

Commit c3adead

Browse files
Add union types
1 parent f4d1e4f commit c3adead

File tree

72 files changed

+237
-430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+237
-430
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-
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/Console/Command/Command.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,9 @@ public function mergeApplicationDefinition(bool $mergeArgs = true)
371371
/**
372372
* Sets an array of argument and option instances.
373373
*
374-
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
375-
*
376374
* @return $this
377375
*/
378-
public function setDefinition($definition)
376+
public function setDefinition(array|InputDefinition $definition)
379377
{
380378
if ($definition instanceof InputDefinition) {
381379
$this->definition = $definition;
@@ -427,7 +425,7 @@ public function getNativeDefinition()
427425
*
428426
* @return $this
429427
*/
430-
public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
428+
public function addArgument(string $name, int $mode = null, string $description = '', string|array|null $default = null)
431429
{
432430
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
433431
if (null !== $this->fullDefinition) {
@@ -448,7 +446,7 @@ public function addArgument(string $name, int $mode = null, string $description
448446
*
449447
* @return $this
450448
*/
451-
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
449+
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', string|array|bool|null $default = null)
452450
{
453451
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
454452
if (null !== $this->fullDefinition) {

src/Symfony/Component/Console/Command/LazyCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function mergeApplicationDefinition(bool $mergeArgs = true): void
9090
/**
9191
* @return $this
9292
*/
93-
public function setDefinition($definition): self
93+
public function setDefinition(array|InputDefinition $definition): self
9494
{
9595
$this->getCommand()->setDefinition($definition);
9696

@@ -110,7 +110,7 @@ public function getNativeDefinition(): InputDefinition
110110
/**
111111
* @return $this
112112
*/
113-
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
113+
public function addArgument(string $name, int $mode = null, string $description = '', string|array|null $default = null): self
114114
{
115115
$this->getCommand()->addArgument($name, $mode, $description, $default);
116116

@@ -120,7 +120,7 @@ public function addArgument(string $name, int $mode = null, string $description
120120
/**
121121
* @return $this
122122
*/
123-
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self
123+
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', string|array|bool|null $default = null): self
124124
{
125125
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default);
126126

src/Symfony/Component/Console/Descriptor/Descriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class Descriptor implements DescriptorInterface
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function describe(OutputInterface $output, $object, array $options = [])
37+
public function describe(OutputInterface $output, object $object, array $options = [])
3838
{
3939
$this->output = $output;
4040

src/Symfony/Component/Console/Descriptor/DescriptorInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ interface DescriptorInterface
2222
{
2323
/**
2424
* Describes an object if supported.
25-
*
26-
* @param object $object
2725
*/
28-
public function describe(OutputInterface $output, $object, array $options = []);
26+
public function describe(OutputInterface $output, object $object, array $options = []);
2927
}

src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MarkdownDescriptor extends Descriptor
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function describe(OutputInterface $output, $object, array $options = [])
34+
public function describe(OutputInterface $output, object $object, array $options = [])
3535
{
3636
$decorated = $output->isDecorated();
3737
$output->setDecorated(false);

src/Symfony/Component/Console/Helper/FormatterHelper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ public function formatSection(string $section, string $message, string $style =
3333
/**
3434
* Formats a message as a block of text.
3535
*
36-
* @param string|array $messages The message to write in the block
37-
*
3836
* @return string The formatter message
3937
*/
40-
public function formatBlock($messages, string $style, bool $large = false)
38+
public function formatBlock(string|array $messages, string $style, bool $large = false)
4139
{
4240
if (!\is_array($messages)) {
4341
$messages = [$messages];

src/Symfony/Component/Console/Helper/ProcessHelper.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ class ProcessHelper extends Helper
3131
* @param array|Process $cmd An instance of Process or an array of the command and arguments
3232
* @param callable|null $callback A PHP callback to run whenever there is some
3333
* output available on STDOUT or STDERR
34-
*
35-
* @return Process The process that ran
3634
*/
37-
public function run(OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
35+
public function run(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
3836
{
3937
if (!class_exists(Process::class)) {
4038
throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
@@ -50,10 +48,6 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
5048
$cmd = [$cmd];
5149
}
5250

53-
if (!\is_array($cmd)) {
54-
throw new \TypeError(sprintf('The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.', __METHOD__, Process::class, get_debug_type($cmd)));
55-
}
56-
5751
if (\is_string($cmd[0] ?? null)) {
5852
$process = new Process($cmd);
5953
$cmd = [];
@@ -96,13 +90,11 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
9690
* @param callable|null $callback A PHP callback to run whenever there is some
9791
* output available on STDOUT or STDERR
9892
*
99-
* @return Process The process that ran
100-
*
10193
* @throws ProcessFailedException
10294
*
10395
* @see run()
10496
*/
105-
public function mustRun(OutputInterface $output, $cmd, string $error = null, callable $callback = null): Process
97+
public function mustRun(OutputInterface $output, string|Process $cmd, string $error = null, callable $callback = null): Process
10698
{
10799
$process = $this->run($output, $cmd, $error, $callback);
108100

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,10 @@ private function setIOCodepage(): int
552552
/**
553553
* Sets console I/O to the specified code page and converts the user input.
554554
*
555-
* @param string|false $input
556555
*
557556
* @return string|false
558557
*/
559-
private function resetIOCodepage(int $cp, $input)
558+
private function resetIOCodepage(int $cp, string|false $input)
560559
{
561560
if (0 !== $cp) {
562561
sapi_windows_cp_set($cp);

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ public static function getStyleDefinition(string $name)
133133
/**
134134
* Sets table style.
135135
*
136-
* @param TableStyle|string $name The style name or a TableStyle instance
137-
*
138136
* @return $this
139137
*/
140-
public function setStyle($name)
138+
public function setStyle(TableStyle|string $name)
141139
{
142140
$this->style = $this->resolveStyle($name);
143141

@@ -161,7 +159,7 @@ public function getStyle()
161159
*
162160
* @return $this
163161
*/
164-
public function setColumnStyle(int $columnIndex, $name)
162+
public function setColumnStyle(int $columnIndex, TableStyle|string $name)
165163
{
166164
$this->columnStyles[$columnIndex] = $this->resolveStyle($name);
167165

@@ -254,18 +252,14 @@ public function addRows(array $rows)
254252
return $this;
255253
}
256254

257-
public function addRow($row)
255+
public function addRow(TableSeparator|array $row)
258256
{
259257
if ($row instanceof TableSeparator) {
260258
$this->rows[] = $row;
261259

262260
return $this;
263261
}
264262

265-
if (!\is_array($row)) {
266-
throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.');
267-
}
268-
269263
$this->rows[] = array_values($row);
270264

271265
return $this;
@@ -274,7 +268,7 @@ public function addRow($row)
274268
/**
275269
* Adds a row to the table, and re-renders the table.
276270
*/
277-
public function appendRow($row): self
271+
public function appendRow(TableSeparator|array $row): self
278272
{
279273
if (!$this->output instanceof ConsoleSectionOutput) {
280274
throw new RuntimeException(sprintf('Output should be an instance of "%s" when calling "%s".', ConsoleSectionOutput::class, __METHOD__));
@@ -290,7 +284,7 @@ public function appendRow($row): self
290284
return $this;
291285
}
292286

293-
public function setRow($column, array $row)
287+
public function setRow(int|string $column, array $row)
294288
{
295289
$this->rows[$column] = $row;
296290

@@ -596,11 +590,11 @@ private function buildTableRows(array $rows): TableRows
596590

597591
return new TableRows(function () use ($rows, $unmergedRows): \Traversable {
598592
foreach ($rows as $rowKey => $row) {
599-
yield $this->fillCells($row);
593+
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
600594

601595
if (isset($unmergedRows[$rowKey])) {
602-
foreach ($unmergedRows[$rowKey] as $unmergedRow) {
603-
yield $this->fillCells($unmergedRow);
596+
foreach ($unmergedRows[$rowKey] as $row) {
597+
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
604598
}
605599
}
606600
}
@@ -681,7 +675,7 @@ private function fillNextRows(array $rows, int $line): array
681675
/**
682676
* fill cells for a row that contains colspan > 1.
683677
*/
684-
private function fillCells($row)
678+
private function fillCells(iterable $row)
685679
{
686680
$newRow = [];
687681

@@ -848,7 +842,7 @@ private static function initStyles(): array
848842
];
849843
}
850844

851-
private function resolveStyle($name): TableStyle
845+
private function resolveStyle(TableStyle|string $name): TableStyle
852846
{
853847
if ($name instanceof TableStyle) {
854848
return $name;

0 commit comments

Comments
 (0)
0