Skip to content

Commit f229d96

Browse files
committed
minor #1934 [CS] Optimize sprintf calls for PHP 8.4 (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [CS] Optimize `sprintf` calls for PHP 8.4 Follows * symfony/symfony#57461 * twigphp/Twig#4108 Update php-cs-fixer config + apply rule on all the repository code Commits ------- eab7db5 [CS] Optimize `sprintf` calls for PHP 8.4
2 parents fed00e2 + eab7db5 commit f229d96

File tree

106 files changed

+295
-290
lines changed

Some content is hidden

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

106 files changed

+295
-290
lines changed

.php-cs-fixer.dist.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
EOF;
2424

2525
return (new PhpCsFixer\Config())
26+
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
2627
->setRules([
2728
'@PHPUnit75Migration:risky' => true,
2829
'@Symfony' => true,
2930
'@Symfony:risky' => true,
3031
'header_comment' => ['header' => $fileHeaderComment],
32+
// TODO: Remove once the "compiler_optimized" set includes "sprintf"
33+
'native_function_invocation' => ['include' => ['@compiler_optimized', 'sprintf'], 'scope' => 'namespaced', 'strict' => true],
3134
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'match', 'parameters']],
3235
])
3336
->setRiskyAllowed(true)

src/Autocomplete/src/AutocompleteResultsExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
9898
}
9999

100100
if (!\is_callable($groupBy)) {
101-
throw new \InvalidArgumentException(sprintf('Option "group_by" must be callable, "%s" given.', get_debug_type($groupBy)));
101+
throw new \InvalidArgumentException(\sprintf('Option "group_by" must be callable, "%s" given.', get_debug_type($groupBy)));
102102
}
103103

104104
$optgroupLabels = [];

src/Autocomplete/src/Controller/EntityAutocompleteController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __invoke(string $alias, Request $request): Response
4242
{
4343
$autocompleter = $this->autocompleteFieldRegistry->getAutocompleter($alias);
4444
if (!$autocompleter) {
45-
throw new NotFoundHttpException(sprintf('No autocompleter found for "%s". Available autocompleters are: (%s)', $alias, implode(', ', $this->autocompleteFieldRegistry->getAutocompleterNames())));
45+
throw new NotFoundHttpException(\sprintf('No autocompleter found for "%s". Available autocompleters are: (%s)', $alias, implode(', ', $this->autocompleteFieldRegistry->getAutocompleterNames())));
4646
}
4747

4848
if ($autocompleter instanceof OptionsAwareEntityAutocompleterInterface) {

src/Autocomplete/src/DependencyInjection/AutocompleteFormTypePass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private function processEntityAutocompleteFieldTag(ContainerBuilder $container)
4040
foreach ($container->findTaggedServiceIds(self::ENTITY_AUTOCOMPLETE_FIELD_TAG, true) as $serviceId => $tag) {
4141
$serviceDefinition = $container->getDefinition($serviceId);
4242
if (!$serviceDefinition->hasTag('form.type')) {
43-
throw new \LogicException(sprintf('Service "%s" has the "%s" tag, but is not tagged with "form.type". Did you add the "%s" attribute to a class that is not a form type?', $serviceId, self::ENTITY_AUTOCOMPLETE_FIELD_TAG, AsEntityAutocompleteField::class));
43+
throw new \LogicException(\sprintf('Service "%s" has the "%s" tag, but is not tagged with "form.type". Did you add the "%s" attribute to a class that is not a form type?', $serviceId, self::ENTITY_AUTOCOMPLETE_FIELD_TAG, AsEntityAutocompleteField::class));
4444
}
4545
$alias = $this->getAlias($serviceId, $serviceDefinition, $tag);
4646

@@ -61,7 +61,7 @@ private function getAlias(string $serviceId, Definition $serviceDefinition, arra
6161
$class = $serviceDefinition->getClass();
6262
$attribute = AsEntityAutocompleteField::getInstance($class);
6363
if (null === $attribute) {
64-
throw new \LogicException(sprintf('The service "%s" either needs to have the #[%s] attribute above its class or its "%s" tag needs an "alias" key.', $serviceId, self::ENTITY_AUTOCOMPLETE_FIELD_TAG, AsEntityAutocompleteField::class));
64+
throw new \LogicException(\sprintf('The service "%s" either needs to have the #[%s] attribute above its class or its "%s" tag needs an "alias" key.', $serviceId, self::ENTITY_AUTOCOMPLETE_FIELD_TAG, AsEntityAutocompleteField::class));
6565
}
6666

6767
return $attribute->getAlias() ?: AsEntityAutocompleteField::shortName($class);
@@ -72,7 +72,7 @@ private function processEntityAutocompleterTag(ContainerBuilder $container)
7272
$servicesMap = [];
7373
foreach ($container->findTaggedServiceIds(self::ENTITY_AUTOCOMPLETER_TAG, true) as $serviceId => $tag) {
7474
if (!isset($tag[0]['alias'])) {
75-
throw new \LogicException(sprintf('The "%s" tag of the "%s" service needs "alias" key.', self::ENTITY_AUTOCOMPLETER_TAG, $serviceId));
75+
throw new \LogicException(\sprintf('The "%s" tag of the "%s" service needs "alias" key.', self::ENTITY_AUTOCOMPLETER_TAG, $serviceId));
7676
}
7777

7878
$servicesMap[$tag[0]['alias']] = new Reference($serviceId);

src/Autocomplete/src/Doctrine/EntityMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getFieldMetadata(string $propertyName): array
6565
return (array) $this->metadata->fieldMappings[$propertyName];
6666
}
6767

68-
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
68+
throw new \InvalidArgumentException(\sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
6969
}
7070

7171
/**
@@ -86,7 +86,7 @@ public function getAssociationMetadata(string $propertyName): array
8686
return $associationMapping;
8787
}
8888

89-
throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
89+
throw new \InvalidArgumentException(\sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
9090
}
9191

9292
public function getPropertyDataType(string $propertyName): string

src/Autocomplete/src/Doctrine/EntityMetadataFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private function getEntityMetadata(string $entityFqcn): ClassMetadata
3737
$entityMetadata = $entityManager->getClassMetadata($entityFqcn);
3838

3939
if (1 !== \count($entityMetadata->getIdentifierFieldNames())) {
40-
throw new \RuntimeException(sprintf('Autocomplete does not support Doctrine entities with composite primary keys (such as the ones used in the "%s" entity).', $entityFqcn));
40+
throw new \RuntimeException(\sprintf('Autocomplete does not support Doctrine entities with composite primary keys (such as the ones used in the "%s" entity).', $entityFqcn));
4141
}
4242

4343
return $entityMetadata;
@@ -46,7 +46,7 @@ private function getEntityMetadata(string $entityFqcn): ClassMetadata
4646
private function getEntityManager(string $entityFqcn): ObjectManager
4747
{
4848
if (null === $entityManager = $this->doctrine->getManagerForClass($entityFqcn)) {
49-
throw new \RuntimeException(sprintf('There is no Doctrine Entity Manager defined for the "%s" class', $entityFqcn));
49+
throw new \RuntimeException(\sprintf('There is no Doctrine Entity Manager defined for the "%s" class', $entityFqcn));
5050
}
5151

5252
return $entityManager;

src/Autocomplete/src/Doctrine/EntitySearchUtil.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function addSearchClause(QueryBuilder $queryBuilder, string $query, strin
5656
$numAssociatedProperties = \count($associatedProperties);
5757

5858
if (1 === $numAssociatedProperties) {
59-
throw new \InvalidArgumentException(sprintf('The "%s" property included in the setSearchFields() method is not a valid search field. When using associated properties in search, you must also define the exact field used in the search (e.g. \'%s.id\', \'%s.name\', etc.)', $propertyName, $propertyName, $propertyName));
59+
throw new \InvalidArgumentException(\sprintf('The "%s" property included in the setSearchFields() method is not a valid search field. When using associated properties in search, you must also define the exact field used in the search (e.g. \'%s.id\', \'%s.name\', etc.)', $propertyName, $propertyName, $propertyName));
6060
}
6161

6262
$originalPropertyName = $associatedProperties[0];
@@ -102,19 +102,19 @@ public function addSearchClause(QueryBuilder $queryBuilder, string $query, strin
102102
|| ($isIntegerProperty && $isIntegerQuery)
103103
|| ($isNumericProperty && $isNumericQuery)
104104
) {
105-
$expressions[] = $queryBuilder->expr()->eq(sprintf('%s.%s', $entityName, $propertyName), ':query_for_numbers');
105+
$expressions[] = $queryBuilder->expr()->eq(\sprintf('%s.%s', $entityName, $propertyName), ':query_for_numbers');
106106
$queryBuilder->setParameter('query_for_numbers', $dqlParameters['numeric_query']);
107107
} elseif ($isGuidProperty && $isUuidQuery) {
108-
$expressions[] = $queryBuilder->expr()->eq(sprintf('%s.%s', $entityName, $propertyName), ':query_for_uuids');
108+
$expressions[] = $queryBuilder->expr()->eq(\sprintf('%s.%s', $entityName, $propertyName), ':query_for_uuids');
109109
$queryBuilder->setParameter('query_for_uuids', $dqlParameters['uuid_query'], 'uuid' === $propertyDataType ? 'uuid' : null);
110110
} elseif ($isUlidProperty && $isUlidQuery) {
111-
$expressions[] = $queryBuilder->expr()->eq(sprintf('%s.%s', $entityName, $propertyName), ':query_for_uuids');
111+
$expressions[] = $queryBuilder->expr()->eq(\sprintf('%s.%s', $entityName, $propertyName), ':query_for_uuids');
112112
$queryBuilder->setParameter('query_for_uuids', $dqlParameters['uuid_query'], 'ulid');
113113
} elseif ($isTextProperty) {
114-
$expressions[] = $queryBuilder->expr()->like(sprintf('LOWER(%s.%s)', $entityName, $propertyName), ':query_for_text');
114+
$expressions[] = $queryBuilder->expr()->like(\sprintf('LOWER(%s.%s)', $entityName, $propertyName), ':query_for_text');
115115
$queryBuilder->setParameter('query_for_text', $dqlParameters['text_query']);
116116

117-
$expressions[] = $queryBuilder->expr()->in(sprintf('LOWER(%s.%s)', $entityName, $propertyName), ':query_as_words');
117+
$expressions[] = $queryBuilder->expr()->in(\sprintf('LOWER(%s.%s)', $entityName, $propertyName), ':query_as_words');
118118
$queryBuilder->setParameter('query_as_words', $dqlParameters['words_query']);
119119
}
120120
}

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function getUrlWithExtraOptions(string $url, array $extraOptions): strin
108108
$extraOptions[self::CHECKSUM_KEY] = $this->checksumCalculator->calculateForArray($extraOptions);
109109
$extraOptions = base64_encode(json_encode($extraOptions));
110110

111-
return sprintf(
111+
return \sprintf(
112112
'%s%s%s',
113113
$url,
114114
$this->hasUrlParameters($url) ? '&' : '?',
@@ -127,7 +127,7 @@ private function validateExtraOptions(array $extraOptions): void
127127
{
128128
foreach ($extraOptions as $optionKey => $option) {
129129
if (!\is_scalar($option) && !\is_array($option) && null !== $option) {
130-
throw new \InvalidArgumentException(sprintf('Extra option with key "%s" must be a scalar value, an array or null. Got "%s".', $optionKey, get_debug_type($option)));
130+
throw new \InvalidArgumentException(\sprintf('Extra option with key "%s" must be a scalar value, an array or null. Got "%s".', $optionKey, get_debug_type($option)));
131131
}
132132

133133
if (\is_array($option)) {

src/Autocomplete/src/Form/AutocompleteEntityTypeSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @internal
2525
*
26-
* @deprecated since 2.13
26+
* @deprecated since UX 2.13
2727
*/
2828
final class AutocompleteEntityTypeSubscriber implements EventSubscriberInterface
2929
{
@@ -77,7 +77,7 @@ public function preSubmit(FormEvent $event)
7777

7878
if ($params) {
7979
$queryBuilder
80-
->andWhere(sprintf("$rootAlias.$idField IN (%s)", implode(', ', array_keys($params))))
80+
->andWhere(\sprintf("$rootAlias.$idField IN (%s)", implode(', ', array_keys($params))))
8181
;
8282
foreach ($params as $key => $param) {
8383
$queryBuilder->setParameter($key, $param[0], $param[1]);

src/Autocomplete/src/Form/BaseEntityAutocompleteType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function getAutocompleteUrl(FormBuilderInterface $builder, array $option
9898
$attribute = AsEntityAutocompleteField::getInstance($formType::class);
9999

100100
if (!$attribute) {
101-
throw new \LogicException(sprintf('You must either provide your own autocomplete_url, or add #[AsEntityAutocompleteField] attribute to %s.', $formType::class));
101+
throw new \LogicException(\sprintf('You must either provide your own autocomplete_url, or add #[AsEntityAutocompleteField] attribute to "%s".', $formType::class));
102102
}
103103

104104
return $this->urlGenerator->generate($attribute->getRoute(), [

src/Autocomplete/src/Form/ParentEntityAutocompleteType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* All form types that want to expose autocomplete functionality should use this for its getParent().
2626
*
27-
* @deprecated since 2.13, use "Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType" instead
27+
* @deprecated since UX 2.13, use "Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType" instead
2828
*/
2929
final class ParentEntityAutocompleteType extends AbstractType implements DataMapperInterface
3030
{
@@ -39,7 +39,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3939
$attribute = AsEntityAutocompleteField::getInstance($formType::class);
4040

4141
if (!$attribute && empty($options['autocomplete_url'])) {
42-
throw new \LogicException(sprintf('You must either provide your own autocomplete_url, or add #[AsEntityAutocompleteField] attribute to %s.', $formType::class));
42+
throw new \LogicException(\sprintf('You must either provide your own autocomplete_url, or add #[AsEntityAutocompleteField] attribute to "%s".', $formType::class));
4343
}
4444

4545
// Use the provided URL, or auto-generate from the provided alias

src/Autocomplete/src/Maker/MakeAutocompleteField.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
8787

8888
$this->entityClass = $io->askQuestion($question);
8989

90-
$defaultClass = Str::asClassName(sprintf('%s AutocompleteField', $this->entityClass));
90+
$defaultClass = Str::asClassName(\sprintf('%s AutocompleteField', $this->entityClass));
9191
$this->className = $io->ask(
92-
sprintf('Choose a name for your entity field class (e.g. <fg=yellow>%s</>)', $defaultClass),
92+
\sprintf('Choose a name for your entity field class (e.g. <fg=yellow>%s</>)', $defaultClass),
9393
$defaultClass
9494
);
9595
}
@@ -146,7 +146,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
146146
'',
147147
' <comment>$builder</comment>',
148148
' <comment>// ...</comment>',
149-
sprintf(' <comment>->add(\'%s\', %s::class)</comment>', Str::asLowerCamelCase($entityClassDetails->getShortName()), $classDetails->getShortName()),
149+
\sprintf(' <comment>->add(\'%s\', %s::class)</comment>', Str::asLowerCamelCase($entityClassDetails->getShortName()), $classDetails->getShortName()),
150150
' <comment>;</>',
151151
]);
152152
}

src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ public function testItReturnsErrorWhenSendingMalformedExtraOptions(): void
164164
$extraOptionsWithValidChecksum = $this->encodeData(['foo' => 'bar', '@checksum' => 'O2nYjcGr/l8GmUuYUSfE52hoyEL0NtDhBzUbn17KVHQ=']);
165165

166166
$this->browser()
167-
->post(sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithoutChecksum))
167+
->post(\sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithoutChecksum))
168168
->assertStatus(400)
169-
->post(sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithInvalidChecksum))
169+
->post(\sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithInvalidChecksum))
170170
->assertStatus(400)
171-
->post(sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithValidChecksum))
171+
->post(\sprintf('/test/autocomplete/category_autocomplete_type?extra_options=%s', $extraOptionsWithValidChecksum))
172172
->assertStatus(200)
173173
;
174174
}

src/Chartjs/src/Twig/ChartExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public function renderChart(Chart $chart, array $attributes = []): string
7373
}
7474
}
7575

76-
return sprintf('<canvas %s></canvas>', $stimulusAttributes);
76+
return \sprintf('<canvas %s></canvas>', $stimulusAttributes);
7777
}
7878
}

src/Icons/src/Command/ImportIconCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5757

5858
foreach ($names as $name) {
5959
if (!preg_match('#^([\w-]+):([\w-]+)$#', $name, $matches)) {
60-
$io->error(sprintf('Invalid icon name "%s".', $name));
60+
$io->error(\sprintf('Invalid icon name "%s".', $name));
6161
$result = Command::FAILURE;
6262

6363
continue;
6464
}
6565

6666
[$fullName, $prefix, $name] = $matches;
6767

68-
$io->comment(sprintf('Importing %s...', $fullName));
68+
$io->comment(\sprintf('Importing %s...', $fullName));
6969

7070
try {
7171
$svg = $this->iconify->fetchSvg($prefix, $name);
@@ -79,11 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7979
$cursor = new Cursor($output);
8080
$cursor->moveUp(2);
8181

82-
$this->registry->add(sprintf('%s/%s', $prefix, $name), $svg);
82+
$this->registry->add(\sprintf('%s/%s', $prefix, $name), $svg);
8383

8484
$license = $this->iconify->metadataFor($prefix)['license'];
8585

86-
$io->text(sprintf(
86+
$io->text(\sprintf(
8787
" <fg=bright-green;options=bold>✓</> Imported <fg=bright-white;bg=black>%s:</><fg=bright-magenta;bg=black;options>%s</> (License: <href=%s>%s</>). Render with: <comment>{{ ux_icon('%s') }}</comment>",
8888
$prefix,
8989
$name,

src/Icons/src/Command/LockIconsCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7979
continue;
8080
}
8181

82-
$this->registry->add(sprintf('%s/%s', $prefix, $name), $svg);
82+
$this->registry->add(\sprintf('%s/%s', $prefix, $name), $svg);
8383

8484
$license = $this->iconify->metadataFor($prefix)['license'];
8585
++$count;
8686

87-
$io->text(sprintf(
87+
$io->text(\sprintf(
8888
" <fg=bright-green;options=bold>✓</> Imported <fg=bright-white;bg=black>%s:</><fg=bright-magenta;bg=black;options>%s</> (License: <href=%s>%s</>). Render with: <comment>{{ ux_icon('%s') }}</comment>",
8989
$prefix,
9090
$name,
@@ -94,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9494
));
9595
}
9696

97-
$io->success(sprintf('Imported %d icons.', $count));
97+
$io->success(\sprintf('Imported %d icons.', $count));
9898

9999
return Command::SUCCESS;
100100
}

0 commit comments

Comments
 (0)