Skip to content

Commit

Permalink
Add type-hints and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Jun 20, 2023
1 parent b3b88d2 commit 8728589
Show file tree
Hide file tree
Showing 102 changed files with 312 additions and 305 deletions.
4 changes: 2 additions & 2 deletions bundles/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ can add some configuration that looks like this:
// config/packages/acme_social.php
use Symfony\Config\AcmeSocialConfig;
return static function (AcmeSocialConfig $acmeSocial) {
return static function (AcmeSocialConfig $acmeSocial): void {
$acmeSocial->twitter()
->clientId(123)
->clientSecret('your_secret');
Expand Down Expand Up @@ -394,7 +394,7 @@ logic to the bundle class directly::
// config/definition.php
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
return static function (DefinitionConfigurator $definition) {
return static function (DefinitionConfigurator $definition): void {
$definition->rootNode()
->children()
->scalarNode('foo')->defaultValue('bar')->end()
Expand Down
6 changes: 3 additions & 3 deletions cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The following example shows a typical usage of the cache::
use Symfony\Contracts\Cache\ItemInterface;

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

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

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

return 'debug';
});

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

return 'debug';
Expand Down
4 changes: 2 additions & 2 deletions components/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ generate and return the value::
use Symfony\Contracts\Cache\ItemInterface;

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion components/cache/cache_invalidation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To attach tags to cached items, you need to use the
:method:`Symfony\\Contracts\\Cache\\ItemInterface::tag` method that is implemented by
cache items::

$item = $cache->get('cache_key', function (ItemInterface $item) {
$item = $cache->get('cache_key', function (ItemInterface $item): string {
// [...]
// add one or more tags
$item->tag('tag_1');
Expand Down
2 changes: 1 addition & 1 deletion components/cache/cache_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The only way to create cache items is via cache pools. When using the Cache
Contracts, they are passed as arguments to the recomputation callback::

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

Expand Down
2 changes: 1 addition & 1 deletion components/cache/cache_pools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ and deleting cache items using only two methods and a callback::
$cache = new FilesystemAdapter();

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

// ... do some HTTP request or heavy computations
Expand Down
2 changes: 1 addition & 1 deletion components/config/definition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ By changing a string value into an associative array with ``name`` as the key::
->arrayNode('connection')
->beforeNormalization()
->ifString()
->then(function ($v) { return ['name' => $v]; })
->then(function (string $v): array { return ['name' => $v]; })
->end()
->children()
->scalarNode('name')->isRequired()->end()
Expand Down
14 changes: 7 additions & 7 deletions components/console/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dispatched. Listeners receive a
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

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

Expand Down Expand Up @@ -64,7 +64,7 @@ C/C++ standard::
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

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

Expand Down Expand Up @@ -97,7 +97,7 @@ Listeners receive a
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;

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

$command = $event->getCommand();
Expand Down Expand Up @@ -131,7 +131,7 @@ Listeners receive a
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;

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

Expand Down Expand Up @@ -170,11 +170,11 @@ Listeners receive a
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;

$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event): void {

// gets the signal number
$signal = $event->getHandlingSignal();

if (\SIGINT === $signal) {
echo "bye bye!";
}
Expand Down
2 changes: 1 addition & 1 deletion components/console/helpers/debug_formatter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ using
// ...
$process = new Process(...);

$process->run(function ($type, $buffer) use ($output, $debugFormatter, $process) {
$process->run(function (string $type, string $buffer) use ($output, $debugFormatter, $process): void {
$output->writeln(
$debugFormatter->progress(
spl_object_hash($process),
Expand Down
2 changes: 1 addition & 1 deletion components/console/helpers/processhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ A custom process callback can be passed as the fourth argument. Refer to the

use Symfony\Component\Process\Process;

$helper->run($output, $process, 'The process failed :(', function ($type, $data) {
$helper->run($output, $process, 'The process failed :(', function (string $type, string $data): void {
if (Process::ERR === $type) {
// ... do something with the stderr output
} else {
Expand Down
2 changes: 1 addition & 1 deletion components/console/helpers/progressbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ that displays the number of remaining steps::

ProgressBar::setPlaceholderFormatterDefinition(
'remaining_steps',
function (ProgressBar $progressBar, OutputInterface $output) {
function (ProgressBar $progressBar, OutputInterface $output): int {
return $progressBar->getMaxSteps() - $progressBar->getProgress();
}
);
Expand Down
52 changes: 26 additions & 26 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ if you want to know a bundle name, you can add this to your command::
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Command::SUCCESS;
}

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

// ... do something with the color

return Command::SUCCESS;
}

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

$colors = $helper->ask($input, $output, $question);
$output->writeln('You have just selected: ' . implode(', ', $colors));

return Command::SUCCESS;
}

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

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Command::SUCCESS;
}

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

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

$filePath = $helper->ask($input, $output, $question);

// ... do something with the filePath

return Command::SUCCESS;
}

Expand All @@ -250,9 +250,9 @@ You can also specify if you want to not trim the answer by setting it directly w
$question->setTrimmable(false);
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
$name = $helper->ask($input, $output, $question);

// ... do something with the name

return Command::SUCCESS;
}

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

$answer = $helper->ask($input, $output, $question);

// ... do something with the answer

return Command::SUCCESS;
}

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

$password = $helper->ask($input, $output, $question);

// ... do something with the password

return Command::SUCCESS;
}

Expand Down Expand Up @@ -338,7 +338,7 @@ convenient for passwords::
QuestionHelper::disableStty();

// ...

return Command::SUCCESS;
}

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

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

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Command::SUCCESS;
}

Expand Down Expand Up @@ -399,7 +399,7 @@ method::
$helper = $this->getHelper('question');

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

$bundleName = $helper->ask($input, $output, $question);

// ... do something with the bundleName

return Command::SUCCESS;
}

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

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

$password = $helper->ask($input, $output, $question);

// ... do something with the password

return Command::SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion components/console/single_command_tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ it is possible to remove this need by declaring a single command application::
->setVersion('1.0.0') // Optional
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
->addOption('bar', null, InputOption::VALUE_REQUIRED)
->setCode(function (InputInterface $input, OutputInterface $output) {
->setCode(function (InputInterface $input, OutputInterface $output): int {
// output arguments and options
})
->run();
Expand Down
8 changes: 4 additions & 4 deletions components/dom_crawler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ An anonymous function can be used to filter with more complex criteria::

$crawler = $crawler
->filter('body > p')
->reduce(function (Crawler $node, $i) {
->reduce(function (Crawler $node, $i): bool {
// filters every other node
return ($i % 2) == 0;
return ($i % 2) === 0;
});

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ The ``addListener()`` method takes up to three arguments:

use Symfony\Contracts\EventDispatcher\Event;

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

Expand Down
Loading

0 comments on commit 8728589

Please sign in to comment.