Skip to content

Commit 84e4723

Browse files
tigitznicolas-grekas
authored andcommitted
Leverage arrow function syntax for closure
1 parent dc9d4fa commit 84e4723

23 files changed

+69
-133
lines changed

Application.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,7 @@ public function find(string $name)
687687

688688
if ($alternatives = $this->findAlternatives($name, $allCommands)) {
689689
// remove hidden commands
690-
$alternatives = array_filter($alternatives, function ($name) {
691-
return !$this->get($name)->isHidden();
692-
});
690+
$alternatives = array_filter($alternatives, fn ($name) => !$this->get($name)->isHidden());
693691

694692
if (1 == \count($alternatives)) {
695693
$message .= "\n\nDid you mean this?\n ";
@@ -840,9 +838,7 @@ protected function doRenderThrowable(\Throwable $e, OutputInterface $output): vo
840838
}
841839

842840
if (str_contains($message, "@anonymous\0")) {
843-
$message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
844-
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
845-
}, $message);
841+
$message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', fn ($m) => class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0], $message);
846842
}
847843

848844
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : \PHP_INT_MAX;
@@ -1163,7 +1159,7 @@ private function findAlternatives(string $name, iterable $collection): array
11631159
}
11641160
}
11651161

1166-
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
1162+
$alternatives = array_filter($alternatives, fn ($lev) => $lev < 2 * $threshold);
11671163
ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
11681164

11691165
return array_keys($alternatives);

Command/CompleteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155155

156156
$this->log('<info>Suggestions:</>');
157157
if ($options = $suggestions->getOptionSuggestions()) {
158-
$this->log(' --'.implode(' --', array_map(function ($o) { return $o->getName(); }, $options)));
158+
$this->log(' --'.implode(' --', array_map(fn ($o) => $o->getName(), $options)));
159159
} elseif ($values = $suggestions->getValueSuggestions()) {
160160
$this->log(' '.implode(' ', $values));
161161
} else {

Command/DumpCompletionCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
141141
*/
142142
private function getSupportedShells(): array
143143
{
144-
return $this->supportedShells ??= array_map(function ($f) {
145-
return pathinfo($f, \PATHINFO_EXTENSION);
146-
}, glob(__DIR__.'/../Resources/completion.*'));
144+
return $this->supportedShells ??= array_map(fn ($f) => pathinfo($f, \PATHINFO_EXTENSION), glob(__DIR__.'/../Resources/completion.*'));
147145
}
148146
}

Command/HelpCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ protected function configure()
3434
$this
3535
->setName('help')
3636
->setDefinition([
37-
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help', function () {
38-
return array_keys((new ApplicationDescription($this->getApplication()))->getCommands());
39-
}),
40-
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', function () {
41-
return (new DescriptorHelper())->getFormats();
42-
}),
37+
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help', fn () => array_keys((new ApplicationDescription($this->getApplication()))->getCommands())),
38+
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
4339
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
4440
])
4541
->setDescription('Display help for a command')

Command/ListCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ protected function configure()
3030
$this
3131
->setName('list')
3232
->setDefinition([
33-
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name', null, function () {
34-
return array_keys((new ApplicationDescription($this->getApplication()))->getNamespaces());
35-
}),
33+
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name', null, fn () => array_keys((new ApplicationDescription($this->getApplication()))->getNamespaces())),
3634
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
37-
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', function () {
38-
return (new DescriptorHelper())->getFormats();
39-
}),
35+
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
4036
new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
4137
])
4238
->setDescription('List commands')

Descriptor/MarkdownDescriptor.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ protected function describeCommand(Command $command, array $options = [])
110110
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n"
111111
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
112112
.'### Usage'."\n\n"
113-
.array_reduce($command->getAliases(), function ($carry, $usage) {
114-
return $carry.'* `'.$usage.'`'."\n";
115-
})
113+
.array_reduce($command->getAliases(), fn ($carry, $usage) => $carry.'* `'.$usage.'`'."\n")
116114
);
117115

118116
return;
@@ -125,9 +123,7 @@ protected function describeCommand(Command $command, array $options = [])
125123
.str_repeat('-', Helper::width($command->getName()) + 2)."\n\n"
126124
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
127125
.'### Usage'."\n\n"
128-
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
129-
return $carry.'* `'.$usage.'`'."\n";
130-
})
126+
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), fn ($carry, $usage) => $carry.'* `'.$usage.'`'."\n")
131127
);
132128

133129
if ($help = $command->getProcessedHelp()) {
@@ -157,9 +153,7 @@ protected function describeApplication(Application $application, array $options
157153
}
158154

159155
$this->write("\n\n");
160-
$this->write(implode("\n", array_map(function ($commandName) use ($description) {
161-
return sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName()));
162-
}, $namespace['commands'])));
156+
$this->write(implode("\n", array_map(fn ($commandName) => sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName())), $namespace['commands'])));
163157
}
164158

165159
foreach ($description->getCommands() as $command) {

Descriptor/TextDescriptor.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ protected function describeApplication(Application $application, array $options
193193
}
194194

195195
// calculate max. width based on available commands per namespace
196-
$width = $this->getColumnWidth(array_merge(...array_values(array_map(function ($namespace) use ($commands) {
197-
return array_intersect($namespace['commands'], array_keys($commands));
198-
}, array_values($namespaces)))));
196+
$width = $this->getColumnWidth(array_merge(...array_values(array_map(fn ($namespace) => array_intersect($namespace['commands'], array_keys($commands)), array_values($namespaces)))));
199197

200198
if ($describedNamespace) {
201199
$this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
@@ -204,9 +202,7 @@ protected function describeApplication(Application $application, array $options
204202
}
205203

206204
foreach ($namespaces as $namespace) {
207-
$namespace['commands'] = array_filter($namespace['commands'], function ($name) use ($commands) {
208-
return isset($commands[$name]);
209-
});
205+
$namespace['commands'] = array_filter($namespace['commands'], fn ($name) => isset($commands[$name]));
210206

211207
if (!$namespace['commands']) {
212208
continue;

Helper/Dumper.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@ public function __construct(OutputInterface $output, CliDumper $dumper = null, C
4040
return rtrim($dumper->dump(($this->cloner ??= new VarCloner())->cloneVar($var)->withRefHandles(false), true));
4141
};
4242
} else {
43-
$this->handler = function ($var): string {
44-
return match (true) {
45-
null === $var => 'null',
46-
true === $var => 'true',
47-
false === $var => 'false',
48-
\is_string($var) => '"'.$var.'"',
49-
default => rtrim(print_r($var, true)),
50-
};
43+
$this->handler = fn ($var): string => match (true) {
44+
null === $var => 'null',
45+
true === $var => 'true',
46+
false === $var => 'false',
47+
\is_string($var) => '"'.$var.'"',
48+
default => rtrim(print_r($var, true)),
5149
};
5250
}
5351
}

Helper/ProgressBar.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,7 @@ private static function initPlaceholderFormatters(): array
539539

540540
return $display;
541541
},
542-
'elapsed' => function (self $bar) {
543-
return Helper::formatTime(time() - $bar->getStartTime());
544-
},
542+
'elapsed' => fn (self $bar) => Helper::formatTime(time() - $bar->getStartTime()),
545543
'remaining' => function (self $bar) {
546544
if (!$bar->getMaxSteps()) {
547545
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
@@ -556,18 +554,10 @@ private static function initPlaceholderFormatters(): array
556554

557555
return Helper::formatTime($bar->getEstimated());
558556
},
559-
'memory' => function (self $bar) {
560-
return Helper::formatMemory(memory_get_usage(true));
561-
},
562-
'current' => function (self $bar) {
563-
return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', \STR_PAD_LEFT);
564-
},
565-
'max' => function (self $bar) {
566-
return $bar->getMaxSteps();
567-
},
568-
'percent' => function (self $bar) {
569-
return floor($bar->getProgressPercent() * 100);
570-
},
557+
'memory' => fn (self $bar) => Helper::formatMemory(memory_get_usage(true)),
558+
'current' => fn (self $bar) => str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', \STR_PAD_LEFT),
559+
'max' => fn (self $bar) => $bar->getMaxSteps(),
560+
'percent' => fn (self $bar) => floor($bar->getProgressPercent() * 100),
571561
];
572562
}
573563

@@ -611,9 +601,7 @@ private function buildLine(): string
611601
$line = preg_replace_callback($regex, $callback, $this->format);
612602

613603
// gets string length for each sub line with multiline format
614-
$linesLength = array_map(function ($subLine) {
615-
return Helper::width(Helper::removeDecoration($this->output->getFormatter(), rtrim($subLine, "\r")));
616-
}, explode("\n", $line));
604+
$linesLength = array_map(fn ($subLine) => Helper::width(Helper::removeDecoration($this->output->getFormatter(), rtrim($subLine, "\r"))), explode("\n", $line));
617605

618606
$linesWidth = max($linesLength);
619607

Helper/ProgressIndicator.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,10 @@ private function getCurrentTimeInMilliseconds(): float
218218
private static function initPlaceholderFormatters(): array
219219
{
220220
return [
221-
'indicator' => function (self $indicator) {
222-
return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
223-
},
224-
'message' => function (self $indicator) {
225-
return $indicator->message;
226-
},
227-
'elapsed' => function (self $indicator) {
228-
return Helper::formatTime(time() - $indicator->startTime);
229-
},
230-
'memory' => function () {
231-
return Helper::formatMemory(memory_get_usage(true));
232-
},
221+
'indicator' => fn (self $indicator) => $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)],
222+
'message' => fn (self $indicator) => $indicator->message,
223+
'elapsed' => fn (self $indicator) => Helper::formatTime(time() - $indicator->startTime),
224+
'memory' => fn () => Helper::formatMemory(memory_get_usage(true)),
233225
];
234226
}
235227
}

0 commit comments

Comments
 (0)