Skip to content

Commit dd284af

Browse files
committed
minor #18439 Add type-hints and return types (alexandre-daubois)
This PR was merged into the 6.2 branch. Discussion ---------- Add type-hints and return types Here we go 🙂 Commits ------- 8728589 Add type-hints and return types
2 parents 17c2bd1 + 8728589 commit dd284af

File tree

102 files changed

+312
-305
lines changed

Some content is hidden

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

102 files changed

+312
-305
lines changed

bundles/configuration.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ can add some configuration that looks like this:
8585
// config/packages/acme_social.php
8686
use Symfony\Config\AcmeSocialConfig;
8787
88-
return static function (AcmeSocialConfig $acmeSocial) {
88+
return static function (AcmeSocialConfig $acmeSocial): void {
8989
$acmeSocial->twitter()
9090
->clientId(123)
9191
->clientSecret('your_secret');
@@ -394,7 +394,7 @@ logic to the bundle class directly::
394394
// config/definition.php
395395
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
396396
397-
return static function (DefinitionConfigurator $definition) {
397+
return static function (DefinitionConfigurator $definition): void {
398398
$definition->rootNode()
399399
->children()
400400
->scalarNode('foo')->defaultValue('bar')->end()

cache.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following example shows a typical usage of the cache::
1010
use Symfony\Contracts\Cache\ItemInterface;
1111

1212
// The callable will only be executed on a cache miss.
13-
$value = $pool->get('my_cache_key', function (ItemInterface $item) {
13+
$value = $pool->get('my_cache_key', function (ItemInterface $item): string {
1414
$item->expiresAfter(3600);
1515

1616
// ... do some HTTP request or heavy computations
@@ -557,13 +557,13 @@ the same key could be invalidated with one function call::
557557

558558
public function someMethod()
559559
{
560-
$value0 = $this->myCachePool->get('item_0', function (ItemInterface $item) {
560+
$value0 = $this->myCachePool->get('item_0', function (ItemInterface $item): string {
561561
$item->tag(['foo', 'bar']);
562562

563563
return 'debug';
564564
});
565565

566-
$value1 = $this->myCachePool->get('item_1', function (ItemInterface $item) {
566+
$value1 = $this->myCachePool->get('item_1', function (ItemInterface $item): string {
567567
$item->tag('foo');
568568

569569
return 'debug';

components/cache.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ generate and return the value::
6565
use Symfony\Contracts\Cache\ItemInterface;
6666

6767
// The callable will only be executed on a cache miss.
68-
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
68+
$value = $cache->get('my_cache_key', function (ItemInterface $item): string {
6969
$item->expiresAfter(3600);
7070

7171
// ... do some HTTP request or heavy computations
@@ -115,7 +115,7 @@ recompute::
115115
use Symfony\Contracts\Cache\ItemInterface;
116116

117117
$beta = 1.0;
118-
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
118+
$value = $cache->get('my_cache_key', function (ItemInterface $item): string {
119119
$item->expiresAfter(3600);
120120
$item->tag(['tag_0', 'tag_1']);
121121

components/cache/cache_invalidation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To attach tags to cached items, you need to use the
2424
:method:`Symfony\\Contracts\\Cache\\ItemInterface::tag` method that is implemented by
2525
cache items::
2626

27-
$item = $cache->get('cache_key', function (ItemInterface $item) {
27+
$item = $cache->get('cache_key', function (ItemInterface $item): string {
2828
// [...]
2929
// add one or more tags
3030
$item->tag('tag_1');

components/cache/cache_items.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The only way to create cache items is via cache pools. When using the Cache
2727
Contracts, they are passed as arguments to the recomputation callback::
2828

2929
// $cache pool object was created before
30-
$productsCount = $cache->get('stats.products_count', function (ItemInterface $item) {
30+
$productsCount = $cache->get('stats.products_count', function (ItemInterface $item): string {
3131
// [...]
3232
});
3333

components/cache/cache_pools.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ and deleting cache items using only two methods and a callback::
3838
$cache = new FilesystemAdapter();
3939

4040
// The callable will only be executed on a cache miss.
41-
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
41+
$value = $cache->get('my_cache_key', function (ItemInterface $item): string {
4242
$item->expiresAfter(3600);
4343

4444
// ... do some HTTP request or heavy computations

components/config/definition.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ By changing a string value into an associative array with ``name`` as the key::
737737
->arrayNode('connection')
738738
->beforeNormalization()
739739
->ifString()
740-
->then(function ($v) { return ['name' => $v]; })
740+
->then(function (string $v): array { return ['name' => $v]; })
741741
->end()
742742
->children()
743743
->scalarNode('name')->isRequired()->end()

components/console/events.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dispatched. Listeners receive a
3333
use Symfony\Component\Console\ConsoleEvents;
3434
use Symfony\Component\Console\Event\ConsoleCommandEvent;
3535

36-
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
36+
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event): void {
3737
// gets the input instance
3838
$input = $event->getInput();
3939

@@ -64,7 +64,7 @@ C/C++ standard::
6464
use Symfony\Component\Console\ConsoleEvents;
6565
use Symfony\Component\Console\Event\ConsoleCommandEvent;
6666

67-
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
67+
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event): void {
6868
// gets the command to be executed
6969
$command = $event->getCommand();
7070

@@ -97,7 +97,7 @@ Listeners receive a
9797
use Symfony\Component\Console\ConsoleEvents;
9898
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9999

100-
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
100+
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event): void {
101101
$output = $event->getOutput();
102102

103103
$command = $event->getCommand();
@@ -131,7 +131,7 @@ Listeners receive a
131131
use Symfony\Component\Console\ConsoleEvents;
132132
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
133133

134-
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
134+
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event): void {
135135
// gets the output
136136
$output = $event->getOutput();
137137

@@ -170,11 +170,11 @@ Listeners receive a
170170
use Symfony\Component\Console\ConsoleEvents;
171171
use Symfony\Component\Console\Event\ConsoleSignalEvent;
172172

173-
$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
174-
173+
$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event): void {
174+
175175
// gets the signal number
176176
$signal = $event->getHandlingSignal();
177-
177+
178178
if (\SIGINT === $signal) {
179179
echo "bye bye!";
180180
}

components/console/helpers/debug_formatter.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ using
7878
// ...
7979
$process = new Process(...);
8080

81-
$process->run(function ($type, $buffer) use ($output, $debugFormatter, $process) {
81+
$process->run(function (string $type, string $buffer) use ($output, $debugFormatter, $process): void {
8282
$output->writeln(
8383
$debugFormatter->progress(
8484
spl_object_hash($process),

components/console/helpers/processhelper.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ A custom process callback can be passed as the fourth argument. Refer to the
6969

7070
use Symfony\Component\Process\Process;
7171

72-
$helper->run($output, $process, 'The process failed :(', function ($type, $data) {
72+
$helper->run($output, $process, 'The process failed :(', function (string $type, string $data): void {
7373
if (Process::ERR === $type) {
7474
// ... do something with the stderr output
7575
} else {

components/console/helpers/progressbar.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ that displays the number of remaining steps::
336336

337337
ProgressBar::setPlaceholderFormatterDefinition(
338338
'remaining_steps',
339-
function (ProgressBar $progressBar, OutputInterface $output) {
339+
function (ProgressBar $progressBar, OutputInterface $output): int {
340340
return $progressBar->getMaxSteps() - $progressBar->getProgress();
341341
}
342342
);

components/console/helpers/questionhelper.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ if you want to know a bundle name, you can add this to your command::
8282
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
8383

8484
$bundleName = $helper->ask($input, $output, $question);
85-
85+
8686
// ... do something with the bundleName
87-
87+
8888
return Command::SUCCESS;
8989
}
9090

@@ -120,7 +120,7 @@ from a predefined list::
120120
$output->writeln('You have just selected: '.$color);
121121

122122
// ... do something with the color
123-
123+
124124
return Command::SUCCESS;
125125
}
126126

@@ -158,7 +158,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
158158

159159
$colors = $helper->ask($input, $output, $question);
160160
$output->writeln('You have just selected: ' . implode(', ', $colors));
161-
161+
162162
return Command::SUCCESS;
163163
}
164164

@@ -187,9 +187,9 @@ will be autocompleted as the user types::
187187
$question->setAutocompleterValues($bundles);
188188

189189
$bundleName = $helper->ask($input, $output, $question);
190-
190+
191191
// ... do something with the bundleName
192-
192+
193193
return Command::SUCCESS;
194194
}
195195

@@ -217,7 +217,7 @@ provide a callback function to dynamically generate suggestions::
217217
// where files and dirs can be found
218218
$foundFilesAndDirs = @scandir($inputPath) ?: [];
219219

220-
return array_map(function ($dirOrFile) use ($inputPath) {
220+
return array_map(function (string $dirOrFile) use ($inputPath): void {
221221
return $inputPath.$dirOrFile;
222222
}, $foundFilesAndDirs);
223223
};
@@ -226,9 +226,9 @@ provide a callback function to dynamically generate suggestions::
226226
$question->setAutocompleterCallback($callback);
227227

228228
$filePath = $helper->ask($input, $output, $question);
229-
229+
230230
// ... do something with the filePath
231-
231+
232232
return Command::SUCCESS;
233233
}
234234

@@ -250,9 +250,9 @@ You can also specify if you want to not trim the answer by setting it directly w
250250
$question->setTrimmable(false);
251251
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
252252
$name = $helper->ask($input, $output, $question);
253-
253+
254254
// ... do something with the name
255-
255+
256256
return Command::SUCCESS;
257257
}
258258

@@ -276,9 +276,9 @@ the response to a question should allow multiline answers by passing ``true`` to
276276
$question->setMultiline(true);
277277

278278
$answer = $helper->ask($input, $output, $question);
279-
279+
280280
// ... do something with the answer
281-
281+
282282
return Command::SUCCESS;
283283
}
284284

@@ -304,9 +304,9 @@ convenient for passwords::
304304
$question->setHiddenFallback(false);
305305

306306
$password = $helper->ask($input, $output, $question);
307-
307+
308308
// ... do something with the password
309-
309+
310310
return Command::SUCCESS;
311311
}
312312

@@ -338,7 +338,7 @@ convenient for passwords::
338338
QuestionHelper::disableStty();
339339

340340
// ...
341-
341+
342342
return Command::SUCCESS;
343343
}
344344

@@ -361,15 +361,15 @@ method::
361361
$helper = $this->getHelper('question');
362362

363363
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
364-
$question->setNormalizer(function ($value) {
364+
$question->setNormalizer(function (string $value): string {
365365
// $value can be null here
366366
return $value ? trim($value) : '';
367367
});
368368

369369
$bundleName = $helper->ask($input, $output, $question);
370-
370+
371371
// ... do something with the bundleName
372-
372+
373373
return Command::SUCCESS;
374374
}
375375

@@ -399,7 +399,7 @@ method::
399399
$helper = $this->getHelper('question');
400400

401401
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
402-
$question->setValidator(function ($answer) {
402+
$question->setValidator(function (string $answer): string {
403403
if (!is_string($answer) || 'Bundle' !== substr($answer, -6)) {
404404
throw new \RuntimeException(
405405
'The name of the bundle should be suffixed with \'Bundle\''
@@ -411,9 +411,9 @@ method::
411411
$question->setMaxAttempts(2);
412412

413413
$bundleName = $helper->ask($input, $output, $question);
414-
414+
415415
// ... do something with the bundleName
416-
416+
417417
return Command::SUCCESS;
418418
}
419419

@@ -459,10 +459,10 @@ You can also use a validator with a hidden question::
459459
$helper = $this->getHelper('question');
460460

461461
$question = new Question('Please enter your password');
462-
$question->setNormalizer(function ($value) {
462+
$question->setNormalizer(function (?string $value): string {
463463
return $value ?? '';
464464
});
465-
$question->setValidator(function ($value) {
465+
$question->setValidator(function (string $value): void {
466466
if ('' === trim($value)) {
467467
throw new \Exception('The password cannot be empty');
468468
}
@@ -473,9 +473,9 @@ You can also use a validator with a hidden question::
473473
$question->setMaxAttempts(20);
474474

475475
$password = $helper->ask($input, $output, $question);
476-
476+
477477
// ... do something with the password
478-
478+
479479
return Command::SUCCESS;
480480
}
481481

components/console/single_command_tool.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ it is possible to remove this need by declaring a single command application::
2020
->setVersion('1.0.0') // Optional
2121
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
2222
->addOption('bar', null, InputOption::VALUE_REQUIRED)
23-
->setCode(function (InputInterface $input, OutputInterface $output) {
23+
->setCode(function (InputInterface $input, OutputInterface $output): int {
2424
// output arguments and options
2525
})
2626
->run();

components/dom_crawler.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ An anonymous function can be used to filter with more complex criteria::
8989

9090
$crawler = $crawler
9191
->filter('body > p')
92-
->reduce(function (Crawler $node, $i) {
92+
->reduce(function (Crawler $node, $i): bool {
9393
// filters every other node
94-
return ($i % 2) == 0;
94+
return ($i % 2) === 0;
9595
});
9696

9797
To remove a node, the anonymous function must return ``false``.
@@ -242,7 +242,7 @@ Call an anonymous function on each node of the list::
242242
use Symfony\Component\DomCrawler\Crawler;
243243
// ...
244244

245-
$nodeValues = $crawler->filter('p')->each(function (Crawler $node, $i) {
245+
$nodeValues = $crawler->filter('p')->each(function (Crawler $node, $i): string {
246246
return $node->text();
247247
});
248248

@@ -252,7 +252,7 @@ The result is an array of values returned by the anonymous function calls.
252252
When using nested crawler, beware that ``filterXPath()`` is evaluated in the
253253
context of the crawler::
254254

255-
$crawler->filterXPath('parent')->each(function (Crawler $parentCrawler, $i) {
255+
$crawler->filterXPath('parent')->each(function (Crawler $parentCrawler, $i): avoid {
256256
// DON'T DO THIS: direct child can not be found
257257
$subCrawler = $parentCrawler->filterXPath('sub-tag/sub-child-tag');
258258

components/event_dispatcher.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ The ``addListener()`` method takes up to three arguments:
147147

148148
use Symfony\Contracts\EventDispatcher\Event;
149149

150-
$dispatcher->addListener('acme.foo.action', function (Event $event) {
150+
$dispatcher->addListener('acme.foo.action', function (Event $event): void {
151151
// will be executed when the acme.foo.action event is dispatched
152152
});
153153

0 commit comments

Comments
 (0)