Skip to content

Leverage class name literal on object #47752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testFixManagersAutoMappingsWithTwoAutomappings()
'SecondBundle' => 'My\SecondBundle',
];

$reflection = new \ReflectionClass(\get_class($this->extension));
$reflection = new \ReflectionClass($this->extension);
$method = $reflection->getMethod('fixManagersAutoMappings');

$method->invoke($this->extension, $emConfigs, $bundles);
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testFixManagersAutoMappings(array $originalEm1, array $originalE
'SecondBundle' => 'My\SecondBundle',
];

$reflection = new \ReflectionClass(\get_class($this->extension));
$reflection = new \ReflectionClass($this->extension);
$method = $reflection->getMethod('fixManagersAutoMappings');

$newEmConfigs = $method->invoke($this->extension, $emConfigs, $bundles);
Expand All @@ -182,7 +182,7 @@ public function testMappingTypeDetection()
{
$container = $this->createContainer();

$reflection = new \ReflectionClass(\get_class($this->extension));
$reflection = new \ReflectionClass($this->extension);
$method = $reflection->getMethod('detectMappingType');

// The ordinary fixtures contain annotation
Expand Down Expand Up @@ -326,7 +326,7 @@ public function testBundleAutoMapping(string $bundle, string $expectedType, stri

$container = $this->createContainer([], [$bundle => $bundleClassName]);

$reflection = new \ReflectionClass(\get_class($this->extension));
$reflection = new \ReflectionClass($this->extension);
$method = $reflection->getMethod('getMappingDriverBundleConfigDefaults');

$this->assertSame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private function getCallableData(mixed $callable): array

if (\is_object($callable[0])) {
$data['name'] = $callable[1];
$data['class'] = \get_class($callable[0]);
$data['class'] = $callable[0]::class;
} else {
if (!str_starts_with($callable[1], 'parent::')) {
$data['name'] = $callable[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ protected function describeCallable(mixed $callable, array $options = [])

if (\is_object($callable[0])) {
$string .= "\n".sprintf('- Name: `%s`', $callable[1]);
$string .= "\n".sprintf('- Class: `%s`', \get_class($callable[0]));
$string .= "\n".sprintf('- Class: `%s`', $callable[0]::class);
} else {
if (!str_starts_with($callable[1], 'parent::')) {
$string .= "\n".sprintf('- Name: `%s`', $callable[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private function formatCallable(mixed $callable): string
{
if (\is_array($callable)) {
if (\is_object($callable[0])) {
return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]);
return sprintf('%s::%s()', $callable[0]::class, $callable[1]);
}

return sprintf('%s::%s()', $callable[0], $callable[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private function getCallableDocument(mixed $callable): \DOMDocument

if (\is_object($callable[0])) {
$callableXML->setAttribute('name', $callable[1]);
$callableXML->setAttribute('class', \get_class($callable[0]));
$callableXML->setAttribute('class', $callable[0]::class);
} else {
if (!str_starts_with($callable[1], 'parent::')) {
$callableXML->setAttribute('name', $callable[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function formatCallable(mixed $callable): string
{
if (\is_array($callable)) {
if (\is_object($callable[0])) {
return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]);
return sprintf('%s::%s()', $callable[0]::class, $callable[1]);
}

return sprintf('%s::%s()', $callable[0], $callable[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function collect(Request $request, Response $response, \Throwable $except
foreach ($decisionLog as $key => $log) {
$decisionLog[$key]['voter_details'] = [];
foreach ($log['voterDetails'] as $voterDetail) {
$voterClass = \get_class($voterDetail['voter']);
$voterClass = $voterDetail['voter']::class;
$classData = $this->hasVarDumper ? new ClassStub($voterClass) : $voterClass;
$decisionLog[$key]['voter_details'][] = [
'class' => $classData,
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Command/CompleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$completionInput->bind($command->getDefinition());

if (CompletionInput::TYPE_OPTION_NAME === $completionInput->getCompletionType()) {
$this->log(' Completing option names for the <comment>'.\get_class($command instanceof LazyCommand ? $command->getCommand() : $command).'</> command.');
$this->log(' Completing option names for the <comment>'.($command instanceof LazyCommand ? $command->getCommand() : $command)::class.'</> command.');

$suggestions->suggestOptions($command->getDefinition()->getOptions());
} else {
$this->log([
' Completing using the <comment>'.\get_class($command instanceof LazyCommand ? $command->getCommand() : $command).'</> class.',
' Completing using the <comment>'.($command instanceof LazyCommand ? $command->getCommand() : $command)::class.'</> class.',
' Completing <comment>'.$completionInput->getCompletionType().'</> for <comment>'.$completionInput->getCompletionName().'</>',
]);
if (null !== $compval = $completionInput->getCompletionValue()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public function testRenderAnonymousException()
$application = new Application();
$application->setAutoExit(false);
$application->register('foo')->setCode(function () {
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() { })));
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', (new class() { })::class));
});
$tester = new ApplicationTester($application);

Expand All @@ -948,7 +948,7 @@ public function testRenderExceptionStackTraceContainsRootException()
$application = new Application();
$application->setAutoExit(false);
$application->register('foo')->setCode(function () {
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() { })));
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', (new class() { })::class));
});
$tester = new ApplicationTester($application);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private function findNodes(): array
}

if (!$container->hasDefinition($id)) {
$nodes[$id] = ['class' => str_replace('\\', '\\\\', \get_class($container->get($id))), 'attributes' => $this->options['node.instance']];
$nodes[$id] = ['class' => str_replace('\\', '\\\\', $container->get($id)::class), 'attributes' => $this->options['node.instance']];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function createNotFoundException(string $id): NotFoundExceptionInterface
}

$class = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 4);
$class = isset($class[3]['object']) ? \get_class($class[3]['object']) : null;
$class = isset($class[3]['object']) ? $class[3]['object']::class : null;
$externalId = $this->externalId ?: $class;

$msg = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,23 @@ public function testDeepDefinitionsResolving()
$this->process($container);

$configurator = $container->getDefinition('sibling')->getConfigurator();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($configurator[0]));
$this->assertSame('Symfony\Component\DependencyInjection\Definition', $configurator[0]::class);
$this->assertSame('parentClass', $configurator[0]->getClass());

$factory = $container->getDefinition('sibling')->getFactory();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($factory[0]));
$this->assertSame('Symfony\Component\DependencyInjection\Definition', $factory[0]::class);
$this->assertSame('parentClass', $factory[0]->getClass());

$argument = $container->getDefinition('sibling')->getArgument(0);
$this->assertSame('Symfony\Component\DependencyInjection\Definition', $argument::class);
$this->assertSame('parentClass', $argument->getClass());

$properties = $container->getDefinition('sibling')->getProperties();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($properties['prop']));
$this->assertSame('Symfony\Component\DependencyInjection\Definition', $properties['prop']::class);
$this->assertSame('parentClass', $properties['prop']->getClass());

$methodCalls = $container->getDefinition('sibling')->getMethodCalls();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($methodCalls[0][1][0]));
$this->assertSame('Symfony\Component\DependencyInjection\Definition', $methodCalls[0][1][0]::class);
$this->assertSame('parentClass', $methodCalls[0][1][0]->getClass());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function testProcessValue()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(CustomDefinition::class, \get_class($locator('bar')));
$this->assertSame(CustomDefinition::class, \get_class($locator('baz')));
$this->assertSame(CustomDefinition::class, \get_class($locator('some.service')));
$this->assertSame(CustomDefinition::class, $locator('bar')::class);
$this->assertSame(CustomDefinition::class, $locator('baz')::class);
$this->assertSame(CustomDefinition::class, $locator('some.service')::class);
}

public function testServiceWithKeyOverwritesPreviousInheritedKey()
Expand All @@ -104,7 +104,7 @@ public function testServiceWithKeyOverwritesPreviousInheritedKey()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition2::class, \get_class($locator('bar')));
$this->assertSame(TestDefinition2::class, $locator('bar')::class);
}

public function testInheritedKeyOverwritesPreviousServiceWithKey()
Expand All @@ -128,8 +128,8 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
$this->assertSame(TestDefinition2::class, \get_class($locator(16)));
$this->assertSame(TestDefinition1::class, $locator('bar')::class);
$this->assertSame(TestDefinition2::class, $locator(16)::class);
}

public function testBindingsAreCopied()
Expand Down Expand Up @@ -164,8 +164,8 @@ public function testTaggedServices()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
$this->assertSame(TestDefinition2::class, \get_class($locator('baz')));
$this->assertSame(TestDefinition1::class, $locator('bar')::class);
$this->assertSame(TestDefinition2::class, $locator('baz')::class);
}

public function testIndexedByServiceIdWithDecoration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ public function handleExceptionProvider(): array
['Uncaught Exception: foo', new \Exception('foo')],
['Uncaught Exception: foo', new class('foo') extends \RuntimeException {
}],
['Uncaught Exception: foo stdClass@anonymous bar', new \RuntimeException('foo '.\get_class(new class() extends \stdClass {
}).' bar')],
['Uncaught Exception: foo stdClass@anonymous bar', new \RuntimeException('foo '.(new class() extends \stdClass {
})::class.' bar')],
['Uncaught Error: bar', new \Error('bar')],
['Uncaught ccc', new \ErrorException('ccc')],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ public function testAnonymousClass()

$this->assertSame('RuntimeException@anonymous', $flattened->getClass());

$flattened->setClass(\get_class(new class('Oops') extends NotFoundHttpException {
}));
$flattened->setClass((new class('Oops') extends NotFoundHttpException {
})::class);

$this->assertSame('Symfony\Component\HttpKernel\Exception\NotFoundHttpException@anonymous', $flattened->getClass());

$flattened = FlattenException::createFromThrowable(new \Exception(sprintf('Class "%s" blah.', \get_class(new class() extends \RuntimeException {
}))));
$flattened = FlattenException::createFromThrowable(new \Exception(sprintf('Class "%s" blah.', (new class() extends \RuntimeException {
})::class)));

$this->assertSame('Class "RuntimeException@anonymous" blah.', $flattened->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function parseListener(array $listener): array
}

if (\is_object($listener[0])) {
return [get_debug_type($listener[0]), \get_class($listener[0])];
return [get_debug_type($listener[0]), $listener[0]::class];
}

return [$listener[0], $listener[0]];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$object = $resolvedType->getOptionsResolver();

if (!$object->isDefined($option)) {
$message = sprintf('Option "%s" is not defined in "%s".', $option, \get_class($resolvedType->getInnerType()));
$message = sprintf('Option "%s" is not defined in "%s".', $option, $resolvedType->getInnerType()::class);

if ($alternatives = $this->findAlternatives($option, $object->getDefinedOptions())) {
if (1 === \count($alternatives)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function filterOptionsByDeprecated(ResolvedFormTypeInterface $type)

private function getParentOptionsResolver(ResolvedFormTypeInterface $type): OptionsResolver
{
$this->parents[$class = \get_class($type->getInnerType())] = [];
$this->parents[$class = $type->getInnerType()::class] = [];

if (null !== $type->getParent()) {
$optionsResolver = clone $this->getParentOptionsResolver($type->getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
$this->sortOptions($formOptions);

$data = [
'class' => \get_class($resolvedFormType->getInnerType()),
'class' => $resolvedFormType->getInnerType()::class,
'block_prefix' => $resolvedFormType->getInnerType()->getBlockPrefix(),
'options' => $formOptions,
'parent_types' => $this->parents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
'extension' => 'Extension options',
], $formOptions);

$this->output->title(sprintf('%s (Block prefix: "%s")', \get_class($resolvedFormType->getInnerType()), $resolvedFormType->getInnerType()->getBlockPrefix()));
$this->output->title(sprintf('%s (Block prefix: "%s")', $resolvedFormType->getInnerType()::class, $resolvedFormType->getInnerType()->getBlockPrefix()));

if ($formOptions) {
$this->output->table($tableHeaders, $this->buildTableRows($tableHeaders, $formOptions));
Expand Down Expand Up @@ -135,7 +135,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
}
array_pop($rows);

$this->output->title(sprintf('%s (%s)', \get_class($options['type']), $options['option']));
$this->output->title(sprintf('%s (%s)', $options['type']::class, $options['option']));
$this->output->table([], $rows);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function configureOptions(OptionsResolver $resolver)

// Derive "data_class" option from passed "data" object
$dataClass = function (Options $options) {
return isset($options['data']) && \is_object($options['data']) ? \get_class($options['data']) : null;
return isset($options['data']) && \is_object($options['data']) ? $options['data']::class : null;
};

// Derive "empty_data" closure from "data_class" option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->addEventSubscriber(new CsrfValidationListener(
$options['csrf_field_name'],
$options['csrf_token_manager'],
$options['csrf_token_id'] ?: ($builder->getName() ?: \get_class($builder->getType()->getInnerType())),
$options['csrf_token_id'] ?: ($builder->getName() ?: $builder->getType()->getInnerType()::class),
$options['csrf_message'],
$this->translator,
$this->translationDomain,
Expand All @@ -74,7 +74,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
$factory = $form->getConfig()->getFormFactory();
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: \get_class($form->getConfig()->getType()->getInnerType()));
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: $form->getConfig()->getType()->getInnerType()::class);
$data = (string) $options['csrf_token_manager']->getToken($tokenId);

$csrfForm = $factory->createNamed($options['csrf_field_name'], HiddenType::class, $data, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function getCasters(): array
FormInterface::class => function (FormInterface $f, array $a) {
return [
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub($f->getConfig()->getType()->getInnerType()::class),
];
},
FormView::class => StubCaster::cutInternals(...),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function extractConfiguration(FormInterface $form): array
$data = [
'id' => $this->buildId($form),
'name' => $form->getName(),
'type_class' => \get_class($form->getConfig()->getType()->getInnerType()),
'type_class' => $form->getConfig()->getType()->getInnerType()::class,
'synchronized' => $form->isSynchronized(),
'passed_options' => [],
'resolved_options' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(FormTypeGuesserInterface $guesser)

public function addType(FormTypeInterface $type)
{
$this->types[\get_class($type)] = $type;
$this->types[$type::class] = $type;
}

public function getType($name): FormTypeInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testAddType()
$extensions = $registry->getExtensions();

$this->assertCount(1, $extensions);
$this->assertTrue($extensions[0]->hasType(\get_class($this->type)));
$this->assertTrue($extensions[0]->hasType($this->type::class));
$this->assertNull($extensions[0]->getTypeGuesser());
}

Expand Down
Loading