Skip to content

Replace get_class() calls by ::class #47401

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
Sep 1, 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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => true],
'header_comment' => ['header' => $fileHeaderComment],
'modernize_strpos' => true,
'get_class_to_class_keyword' => true,
])
->setRiskyAllowed(true)
->setFinder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(object $object, ?\Throwable $error)
$this->object = $object;
$this->error = $error;
$this->stringable = \is_callable([$object, '__toString']);
$this->class = \get_class($object);
$this->class = $object::class;
}

public function getObject(): object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testSupportProxy()
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');

$user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\User', ['id1' => 1, 'id2' => 1]);
$this->assertTrue($provider->supportsClass(\get_class($user2)));
$this->assertTrue($provider->supportsClass($user2::class));
}

public function testLoadUserByUserNameShouldLoadUserWhenProperInterfaceProvided()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public function validate(mixed $entity, Constraint $constraint)
throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em));
}
} else {
$em = $this->registry->getManagerForClass(\get_class($entity));
$em = $this->registry->getManagerForClass($entity::class);

if (!$em) {
throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_debug_type($entity)));
}
}

$class = $em->getClassMetadata(\get_class($entity));
$class = $em->getClassMetadata($entity::class);

$criteria = [];
$hasNullValue = false;
Expand Down Expand Up @@ -136,7 +136,7 @@ public function validate(mixed $entity, Constraint $constraint)
throw new ConstraintDefinitionException(sprintf('The "%s" entity repository does not support the "%s" entity. The entity should be an instance of or extend "%s".', $constraint->entityClass, $class->getName(), $supportedClass));
}
} else {
$repository = $em->getRepository(\get_class($entity));
$repository = $em->getRepository($entity::class);
}

$arguments = [$criteria];
Expand Down Expand Up @@ -203,7 +203,7 @@ private function formatWithIdentifiers(ObjectManager $em, ClassMetadata $class,
return (string) $value;
}

if ($class->getName() !== $idClass = \get_class($value)) {
if ($class->getName() !== $idClass = $value::class) {
// non unique value might be a composite PK that consists of other entity objects
if ($em->getMetadataFactory()->hasMetadataFor($idClass)) {
$identifiers = $em->getClassMetadata($idClass)->getIdentifierValues($value);
Expand All @@ -224,7 +224,7 @@ private function formatWithIdentifiers(ObjectManager $em, ClassMetadata $class,
if (!\is_object($id) || $id instanceof \DateTimeInterface) {
$idAsString = $this->formatValue($id, self::PRETTY_DATE);
} else {
$idAsString = sprintf('object("%s")', \get_class($id));
$idAsString = sprintf('object("%s")', $id::class);
}

$id = sprintf('%s => %s', $field, $idAsString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function __construct(ManagerRegistry $registry)

public function initialize(object $object)
{
$this->registry->getManagerForClass(\get_class($object))?->initializeObject($object);
$this->registry->getManagerForClass($object::class)?->initializeObject($object);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Mime/NotificationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function getExceptionAsString(\Throwable|FlattenException $exception): s
return $exception->getAsString();
}

$message = \get_class($exception);
$message = $exception::class;
if ('' !== $exception->getMessage()) {
$message .= ': '.$exception->getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function warmUp(string $cacheDir): array
try {
$this->dumpExtension($extension, $generator);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate ConfigBuilder for extension {extensionClass}.', ['exception' => $e, 'extensionClass' => \get_class($extension)]);
$this->logger?->warning('Failed to generate ConfigBuilder for extension {extensionClass}.', ['exception' => $e, 'extensionClass' => $extension::class]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
new TableSeparator(),
['<info>Kernel</>'],
new TableSeparator(),
['Type', \get_class($kernel)],
['Type', $kernel::class],
['Environment', $kernel->getEnvironment()],
['Debug', $kernel->isDebug() ? 'true' : 'false'],
['Charset', $kernel->getCharset()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function getContainerBuilder(KernelInterface $kernel): ContainerBuilde
$this->initializeBundles();

return $this->buildContainer();
}, $kernel, \get_class($kernel));
}, $kernel, $kernel::class);
$container = $buildContainer();
$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function getContainerBuilder(): ContainerBuilder
$this->initializeBundles();

return $this->buildContainer();
}, $kernel, \get_class($kernel));
}, $kernel, $kernel::class);
$container = $buildContainer();

$skippedIds = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function formatValue(mixed $value): string
}

if (\is_object($value)) {
return sprintf('object(%s)', \get_class($value));
return sprintf('object(%s)', $value::class);
}

if (\is_string($value)) {
Expand Down Expand Up @@ -335,7 +335,7 @@ private function getContainerEnvVars(ContainerBuilder $container): array
$getDefaultParameter = function (string $name) {
return parent::get($name);
};
$getDefaultParameter = $getDefaultParameter->bindTo($bag, \get_class($bag));
$getDefaultParameter = $getDefaultParameter->bindTo($bag, $bag::class);

$getEnvReflection = new \ReflectionMethod($container, 'getEnv');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function describeContainerService(object $service, array $options = []
} elseif ($service instanceof Definition) {
$this->writeData($this->getContainerDefinitionData($service, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $builder, $options['id']), $options);
} else {
$this->writeData(\get_class($service), $options);
$this->writeData($service::class, $options);
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
} elseif ($service instanceof Definition) {
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments, $builder, $serviceId);
} else {
$data['services'][$serviceId] = \get_class($service);
$data['services'][$serviceId] = $service::class;
}
}

Expand Down Expand Up @@ -204,7 +204,7 @@ protected function getRouteData(Route $route): array
'hostRegex' => '' !== $route->getHost() ? $route->compile()->getHostRegex() : '',
'scheme' => $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'method' => $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
'class' => \get_class($route),
'class' => $route::class,
'defaults' => $route->getDefaults(),
'requirements' => $route->getRequirements() ?: 'NO CUSTOM',
'options' => $route->getOptions(),
Expand Down Expand Up @@ -382,7 +382,7 @@ private function getCallableData(mixed $callable): array

if (method_exists($callable, '__invoke')) {
$data['type'] = 'object';
$data['name'] = \get_class($callable);
$data['name'] = $callable::class;

return $data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function describeRoute(Route $route, array $options = [])
."\n".'- Host Regex: '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')
."\n".'- Scheme: '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')
."\n".'- Method: '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')
."\n".'- Class: '.\get_class($route)
."\n".'- Class: '.$route::class
."\n".'- Defaults: '.$this->formatRouterConfig($route->getDefaults())
."\n".'- Requirements: '.($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM')
."\n".'- Options: '.$this->formatRouterConfig($route->getOptions());
Expand Down Expand Up @@ -101,7 +101,7 @@ protected function describeContainerService(object $service, array $options = []
} elseif ($service instanceof Definition) {
$this->describeContainerDefinition($service, $childOptions, $builder);
} else {
$this->write(sprintf('**`%s`:** `%s`', $options['id'], \get_class($service)));
$this->write(sprintf('**`%s`:** `%s`', $options['id'], $service::class));
}
}

Expand Down Expand Up @@ -188,7 +188,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$this->write("\n\nServices\n--------\n");
foreach ($services['services'] as $id => $service) {
$this->write("\n");
$this->write(sprintf('- `%s`: `%s`', $id, \get_class($service)));
$this->write(sprintf('- `%s`: `%s`', $id, $service::class));
}
}
}
Expand Down Expand Up @@ -399,7 +399,7 @@ protected function describeCallable(mixed $callable, array $options = [])

if (method_exists($callable, '__invoke')) {
$string .= "\n- Type: `object`";
$string .= "\n".sprintf('- Name: `%s`', \get_class($callable));
$string .= "\n".sprintf('- Name: `%s`', $callable::class);

return $this->write($string."\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function describeRoute(Route $route, array $options = [])
['Scheme', $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'],
['Method', $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'],
['Requirements', $route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM'],
['Class', \get_class($route)],
['Class', $route::class],
['Defaults', $this->formatRouterConfig($defaults)],
['Options', $this->formatRouterConfig($route->getOptions())],
];
Expand Down Expand Up @@ -156,7 +156,7 @@ protected function describeContainerService(object $service, array $options = []
$options['output']->table(
['Service ID', 'Class'],
[
[$options['id'] ?? '-', \get_class($service)],
[$options['id'] ?? '-', $service::class],
]
);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$alias = $definition;
$tableRows[] = array_merge([$styledServiceId, sprintf('alias for "%s"', $alias)], $tagsCount ? array_fill(0, $tagsCount, '') : []);
} else {
$tableRows[] = array_merge([$styledServiceId, \get_class($definition)], $tagsCount ? array_fill(0, $tagsCount, '') : []);
$tableRows[] = array_merge([$styledServiceId, $definition::class], $tagsCount ? array_fill(0, $tagsCount, '') : []);
}
}

Expand Down Expand Up @@ -625,7 +625,7 @@ private function formatCallable(mixed $callable): string
}

if (method_exists($callable, '__invoke')) {
return sprintf('%s::__invoke()', \get_class($callable));
return sprintf('%s::__invoke()', $callable::class);
}

throw new \InvalidArgumentException('Callable is not describable.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function getRouteDocument(Route $route, string $name = null): \DOMDocume
$routeXML->setAttribute('name', $name);
}

$routeXML->setAttribute('class', \get_class($route));
$routeXML->setAttribute('class', $route::class);

$routeXML->appendChild($pathXML = $dom->createElement('path'));
$pathXML->setAttribute('regex', $route->compile()->getRegex());
Expand Down Expand Up @@ -269,7 +269,7 @@ private function getContainerServiceDocument(object $service, string $id, Contai
} else {
$dom->appendChild($serviceXML = $dom->createElement('service'));
$serviceXML->setAttribute('id', $id);
$serviceXML->setAttribute('class', \get_class($service));
$serviceXML->setAttribute('class', $service::class);
}

return $dom;
Expand Down Expand Up @@ -577,7 +577,7 @@ private function getCallableDocument(mixed $callable): \DOMDocument

if (method_exists($callable, '__invoke')) {
$callableXML->setAttribute('type', 'object');
$callableXML->setAttribute('name', \get_class($callable));
$callableXML->setAttribute('name', $callable::class);

return $dom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testCacheIsWarmedWithOldContainer()
\Closure::bind(function (Container $class) {
unset($class->loadedDynamicParameters['kernel.build_dir']);
unset($class->parameters['kernel.build_dir']);
}, null, \get_class($container))($container);
}, null, $container::class)($container);

$input = new ArrayInput(['cache:clear']);
$application = new Application($kernel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ public function testValidCollectorWithTemplateUsingAutoconfigure(TemplateAwareDa
$profilerDefinition = $container->register('profiler', 'ProfilerClass');

$container->registerForAutoconfiguration(DataCollectorInterface::class)->addTag('data_collector');
$container->register('mydatacollector', \get_class($dataCollector))->setAutoconfigured(true);
$container->register('mydatacollector', $dataCollector::class)->setAutoconfigured(true);

(new ResolveInstanceofConditionalsPass())->process($container);
(new ProfilerPass())->process($container);

$idForTemplate = \get_class($dataCollector);
$idForTemplate = $dataCollector::class;
$this->assertSame(['mydatacollector' => [$idForTemplate, 'foo']], $container->getParameter('data_collector.templates'));

// grab the method calls off of the "profiler" definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnnotatedController
*/
public function requestDefaultNullAction(Request $request = null)
{
return new Response($request ? \get_class($request) : null);
return new Response($request ? $request::class : null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private function displayAuthenticators(string $name, SymfonyStyle $io): void
array_map(
static function ($authenticator) {
return [
\get_class($authenticator),
$authenticator::class,
];
},
$authenticators
Expand Down Expand Up @@ -253,7 +253,7 @@ private function formatCallable(mixed $callable): string
}

if (method_exists($callable, '__invoke')) {
return sprintf('%s::__invoke()', \get_class($callable));
return sprintf('%s::__invoke()', $callable::class);
}

throw new \InvalidArgumentException('Callable is not describable.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function collect(Request $request, Response $response, \Throwable $except
'impersonator_user' => $impersonatorUser,
'impersonation_exit_path' => null,
'token' => $token,
'token_class' => $this->hasVarDumper ? new ClassStub(\get_class($token)) : \get_class($token),
'token_class' => $this->hasVarDumper ? new ClassStub($token::class) : $token::class,
'logout_url' => $logoutUrl,
'user' => $token->getUserIdentifier(),
'roles' => $assignedRoles,
Expand All @@ -137,7 +137,7 @@ public function collect(Request $request, Response $response, \Throwable $except
$voter = $voter->getDecoratedVoter();
}

$this->data['voters'][] = $this->hasVarDumper ? new ClassStub(\get_class($voter)) : \get_class($voter);
$this->data['voters'][] = $this->hasVarDumper ? new ClassStub($voter::class) : $voter::class;
}

// collect voter details
Expand Down
Loading