Skip to content
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

Using sprintf() is preferred over string concatenation #6062

Merged
merged 1 commit into from
Jul 3, 2020
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
5 changes: 1 addition & 4 deletions src/Action/GetShortObjectDescriptionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function __invoke(Request $request): Response
$admin = $this->pool->getInstance($code);

if (!$admin) {
throw new NotFoundHttpException(sprintf(
'Could not find admin for code "%s"',
$code
));
throw new NotFoundHttpException(sprintf('Could not find admin for code "%s"', $code));
}

$admin->setRequest($request);
Expand Down
15 changes: 10 additions & 5 deletions src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public function __construct(Environment $twig, Pool $pool, $validator)
{
// NEXT_MAJOR: Move ValidatorInterface check to method signature
if (!($validator instanceof ValidatorInterface)) {
throw new \InvalidArgumentException(
'Argument 3 is an instance of '.\get_class($validator).', expecting an instance of'
.' \Symfony\Component\Validator\Validator\ValidatorInterface'
);
throw new \InvalidArgumentException(sprintf(
'Argument 3 is an instance of %s, expecting an instance of %s',
\get_class($validator),
ValidatorInterface::class
));
}
$this->pool = $pool;
$this->twig = $twig;
Expand All @@ -74,7 +75,11 @@ public function __invoke(Request $request): JsonResponse
}

if (Request::METHOD_POST !== $request->getMethod()) {
return new JsonResponse(sprintf('Invalid request method given "%s", %s expected', $request->getMethod(), Request::METHOD_POST), Response::HTTP_METHOD_NOT_ALLOWED);
return new JsonResponse(sprintf(
'Invalid request method given "%s", %s expected',
$request->getMethod(),
Request::METHOD_POST
), Response::HTTP_METHOD_NOT_ALLOWED);
}

$rootObject = $object = $admin->getObject($objectId);
Expand Down
247 changes: 136 additions & 111 deletions src/Admin/AbstractAdmin.php

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

namespace Sonata\AdminBundle\Admin;

@trigger_error(
'The '.__NAMESPACE__.'\Admin class is deprecated since version 3.1 and will be removed in 4.0.'
.' Use '.__NAMESPACE__.'\AbstractAdmin instead.',
E_USER_DEPRECATED
);
@trigger_error(sprintf(
'The %1$s\Admin class is deprecated since version 3.1 and will be removed in 4.0. Use %1$s\AbstractAdmin instead.',
__NAMESPACE__
), E_USER_DEPRECATED);

/**
* NEXT_MAJOR: remove this class.
Expand Down
10 changes: 5 additions & 5 deletions src/Admin/AdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

namespace Sonata\AdminBundle\Admin;

@trigger_error(
'The '.__NAMESPACE__.'\AdminExtension class is deprecated since version 3.1 and will be removed in 4.0.'
.' Use '.__NAMESPACE__.'\AbstractAdminExtension instead.',
E_USER_DEPRECATED
);
@trigger_error(sprintf(
'The %1$s\AdminExtension class is deprecated since version 3.1 and will be removed in 4.0.'
.' Use %1$s\AbstractAdminExtension instead.',
__NAMESPACE__
), E_USER_DEPRECATED);

/**
* NEXT_MAJOR: remove this class.
Expand Down
38 changes: 21 additions & 17 deletions src/Admin/AdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\ODM\MongoDB\PersistentCollection;
use Doctrine\ORM\PersistentCollection as DoctrinePersistentCollection;
Expand Down Expand Up @@ -236,9 +237,11 @@ public function addNewInstance($object, FieldDescriptionInterface $fieldDescript
/*
* NEXT_MAJOR: Use BadMethodCallException instead
*/
throw new \RuntimeException(
sprintf('Method %s::%s() does not exist.', ClassUtils::getClass($object), $method)
);
throw new \RuntimeException(sprintf(
'Method %s::%s() does not exist.',
ClassUtils::getClass($object),
$method
));
}

$object = $object->$method();
Expand All @@ -256,9 +259,11 @@ public function addNewInstance($object, FieldDescriptionInterface $fieldDescript
/*
* NEXT_MAJOR: Use BadMethodCallException instead
*/
throw new \RuntimeException(
sprintf('Method %s::%s() does not exist.', ClassUtils::getClass($object), $method)
);
throw new \RuntimeException(sprintf(
'Method %s::%s() does not exist.',
ClassUtils::getClass($object),
$method
));
}
}
}
Expand All @@ -283,14 +288,11 @@ public function addNewInstance($object, FieldDescriptionInterface $fieldDescript
*/
public function camelize($property)
{
@trigger_error(
sprintf(
'The %s method is deprecated since 3.1 and will be removed in 4.0. '.
'Use \Doctrine\Inflector\Inflector::classify() instead.',
__METHOD__
),
E_USER_DEPRECATED
);
@trigger_error(sprintf(
'The %s method is deprecated since 3.1 and will be removed in 4.0. Use %s::classify() instead.',
__METHOD__,
Inflector::class
), E_USER_DEPRECATED);

return InflectorFactory::create()->build()->classify($property);
}
Expand Down Expand Up @@ -328,9 +330,11 @@ public function getElementAccessPath($elementId, $model)
}

if (!empty($currentPath)) {
throw new \Exception(
sprintf('Could not get element id from %s Failing part: %s', $elementId, $currentPath)
);
throw new \Exception(sprintf(
'Could not get element id from %s Failing part: %s',
$elementId,
$currentPath
));
}

return $totalPath;
Expand Down
31 changes: 16 additions & 15 deletions src/Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Admin;

use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Sonata\AdminBundle\Exception\NoValueException;

Expand Down Expand Up @@ -211,7 +212,8 @@ public function getTemplate()
{
if (null !== $this->template && !\is_string($this->template) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'Returning other type than string or null in method %s() is deprecated since sonata-project/admin-bundle 3.65. It will return only those types in version 4.0.',
'Returning other type than string or null in method %s() is deprecated since'
.' sonata-project/admin-bundle 3.65. It will return only those types in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}
Expand Down Expand Up @@ -284,8 +286,9 @@ public function getAssociationAdmin()
if (!$this->hasAssociationAdmin()) {
@trigger_error(
sprintf(
'Calling %s() when there is no association admin is deprecated since sonata-project/admin-bundle 3.69'
.' and will throw an exception in 4.0. Use %s::hasAssociationAdmin() to know if there is an association admin.',
'Calling %s() when there is no association admin is deprecated since'
.' sonata-project/admin-bundle 3.69 and will throw an exception in 4.0.'
.' Use %s::hasAssociationAdmin() to know if there is an association admin.',
__METHOD__,
__CLASS__
),
Expand Down Expand Up @@ -328,9 +331,9 @@ public function getFieldValue($object, $fieldName)

$camelizedFieldName = InflectorFactory::create()->build()->classify($fieldName);

$getters[] = 'get'.$camelizedFieldName;
$getters[] = 'is'.$camelizedFieldName;
$getters[] = 'has'.$camelizedFieldName;
$getters[] = sprintf('get%s', $camelizedFieldName);
$getters[] = sprintf('is%s', $camelizedFieldName);
$getters[] = sprintf('has%s', $camelizedFieldName);
}

foreach ($getters as $getter) {
Expand Down Expand Up @@ -433,14 +436,11 @@ public function getMappingType()
*/
public static function camelize($property)
{
@trigger_error(
sprintf(
'The %s method is deprecated since 3.1 and will be removed in 4.0. '.
'Use \Doctrine\Inflector\Inflector::classify() instead.',
__METHOD__
),
E_USER_DEPRECATED
);
@trigger_error(sprintf(
'The %s method is deprecated since 3.1 and will be removed in 4.0. Use %s::classify() instead.',
__METHOD__,
Inflector::class
), E_USER_DEPRECATED);

return InflectorFactory::create()->build()->classify($property);
}
Expand All @@ -465,7 +465,8 @@ public function getLabel()
$label = $this->getOption('label');
if (null !== $label && false !== $label && !\is_string($label) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'Returning other type than string, false or null in method %s() is deprecated since sonata-project/admin-bundle 3.65. It will return only those types in version 4.0.',
'Returning other type than string, false or null in method %s() is deprecated since'
.' sonata-project/admin-bundle 3.65. It will return only those types in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}
Expand Down
19 changes: 13 additions & 6 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ public function getAdminByAdminCode($adminCode)
{
if (!\is_string($adminCode)) {
@trigger_error(sprintf(
'Passing a non string value as argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.51 and will cause a \TypeError in 4.0.',
__METHOD__
'Passing a non string value as argument 1 for %s() is deprecated since'
.' sonata-project/admin-bundle 3.51 and will cause a %s in 4.0.',
__METHOD__,
\TypeError::class
), E_USER_DEPRECATED);

return false;
Expand All @@ -269,15 +271,18 @@ public function getAdminByAdminCode($adminCode)
$code = trim(array_shift($codes));

if ('' === $code) {
throw new \InvalidArgumentException('Root admin code must contain a valid admin reference, empty string given.');
throw new \InvalidArgumentException(
'Root admin code must contain a valid admin reference, empty string given.'
);
}

$admin = $this->getInstance($code);

foreach ($codes as $code) {
if (!\in_array($code, $this->adminServiceIds, true)) {
@trigger_error(sprintf(
'Passing an invalid admin code as argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.',
'Passing an invalid admin code as argument 1 for %s() is deprecated since'
.' sonata-project/admin-bundle 3.50 and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

Expand All @@ -286,13 +291,15 @@ public function getAdminByAdminCode($adminCode)

if (!$admin->hasChild($code)) {
@trigger_error(sprintf(
'Passing an invalid admin hierarchy inside argument 1 for %s() is deprecated since sonata-project/admin-bundle 3.51 and will throw an exception in 4.0.',
'Passing an invalid admin hierarchy inside argument 1 for %s() is deprecated since'
.' sonata-project/admin-bundle 3.51 and will throw an exception in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

// NEXT_MAJOR : remove the previous `trigger_error()` call, uncomment the following exception and declare AdminInterface as return type
// throw new \InvalidArgumentException(sprintf(
// 'Argument 1 passed to %s() must contain a valid admin hierarchy, "%s" is not a valid child for "%s"',
// 'Argument 1 passed to %s() must contain a valid admin hierarchy,'
// .' "%s" is not a valid child for "%s"',
// __METHOD__,
// $code,
// $admin->getCode()
Expand Down
8 changes: 4 additions & 4 deletions src/Command/CreateClassCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

@trigger_error(
'The '.__NAMESPACE__.'\CreateClassCacheCommand class is deprecated since version 3.39.0 and will be removed in 4.0.',
E_USER_DEPRECATED
);
@trigger_error(sprintf(
'The %s\CreateClassCacheCommand class is deprecated since version 3.39.0 and will be removed in 4.0.',
__NAMESPACE__
), E_USER_DEPRECATED);

/**
* NEXT_MAJOR: Remove this class.
Expand Down
39 changes: 25 additions & 14 deletions src/Command/GenerateObjectAclCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function __construct(Pool $pool, array $aclObjectManipulators, $registry
if (null !== $registry && (!$registry instanceof RegistryInterface && !$registry instanceof ManagerRegistry)) {
if (!$registry instanceof ManagerRegistry) {
@trigger_error(sprintf(
"Passing an object that doesn't implement %s as argument 3 to %s() is deprecated since sonata-project/admin-bundle 3.56.",
'Passing an object that doesn\'t implement %s as argument 3 to %s() is deprecated since'
.' sonata-project/admin-bundle 3.56.',
ManagerRegistry::class,
__METHOD__
), E_USER_DEPRECATED);
Expand Down Expand Up @@ -102,18 +103,19 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Welcome to the AdminBundle object ACL generator');
$output->writeln([
'',
'This command helps you to generate ACL entities for the objects handled by the AdminBundle.',
'',
'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.',
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
'',
'',
'This command helps you to generate ACL entities for the objects handled by the AdminBundle.',
'',
'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.',
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
'',
]);

if (!$this->registry) {
$msg = sprintf('The command "%s" has a dependency on a non-existent service "doctrine".', static::$defaultName);

throw new ServiceNotFoundException('doctrine', static::class, null, [], $msg);
throw new ServiceNotFoundException('doctrine', static::class, null, [], sprintf(
'The command "%s" has a dependency on a non-existent service "doctrine".',
static::$defaultName
));
}

if ($input->getOption('user_model')) {
Expand Down Expand Up @@ -224,16 +226,25 @@ protected function getUserEntityClass(InputInterface $input, OutputInterface $ou

if ('' === $this->userEntityClass) {
if ($input->getOption('user_model')) {
list($userBundle, $userModel) = Validators::validateEntityName($input->getOption('user_model'));
[$userBundle, $userModel] = Validators::validateEntityName($input->getOption('user_model'));
} else {
list($userBundle, $userModel) = $this->askAndValidate($input, $output, 'Please enter the User model shortcut name: ', '', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
[$userBundle, $userModel] = $this->askAndValidate(
$input,
$output,
'Please enter the User Entity shortcut name: ',
'',
'Sonata\AdminBundle\Command\Validators::validateEntityName'
);
}

// Entity exists?
if ($this->registry instanceof RegistryInterface) {
$this->userEntityClass = $this->registry->getEntityNamespace($userBundle).'\\'.$userModel;
$namespace = $this->registry->getEntityNamespace($userBundle);
} else {
$this->userEntityClass = $this->registry->getAliasNamespace($userBundle).'\\'.$userModel;
$namespace = $this->registry->getAliasNamespace($userBundle);
}

$this->userEntityClass = sprintf('%s\%s', $namespace, $userModel);
}

return $this->userEntityClass;
Expand Down
12 changes: 6 additions & 6 deletions src/Command/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static function validateEntityName($shortcut)

if (false === $pos = strpos($model, ':')) {
throw new \InvalidArgumentException(sprintf(
'The entity name must contain a ":" (colon sign) '
.'("%s" given, expecting something like AcmeBlogBundle:Post)',
'The entity name must contain a ":" (colon sign)'
.' ("%s" given, expecting something like AcmeBlogBundle:Post)',
$model
));
}
Expand Down Expand Up @@ -97,8 +97,8 @@ public static function validateAdminClassBasename($adminClassBasename)

if (false !== strpos($adminClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf(
'The admin class name must not contain a ":" (colon sign) '
.'("%s" given, expecting something like PostAdmin")',
'The admin class name must not contain a ":" (colon sign)'
.' ("%s" given, expecting something like PostAdmin")',
$adminClassBasename
));
}
Expand All @@ -121,8 +121,8 @@ public static function validateControllerClassBasename($controllerClassBasename)

if (false !== strpos($controllerClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf(
'The controller class name must not contain a ":" (colon sign) ("%s" given, '
.'expecting something like PostAdminController")',
'The controller class name must not contain a ":" (colon sign)'
.' ("%s" given, expecting something like PostAdminController")',
$controllerClassBasename
));
}
Expand Down
Loading