Skip to content

Commit

Permalink
minor #41429 Leverage Stringable (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.0 branch.

Discussion
----------

Leverage Stringable

| Q             | A
| ------------- | ---
| Branch?       | 6.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

From https://wiki.php.net/rfc/stringable

Commits
-------

941f5bb Leverage Stringable
  • Loading branch information
nicolas-grekas committed May 28, 2021
2 parents d9f0467 + 941f5bb commit 4b8256d
Show file tree
Hide file tree
Showing 40 changed files with 41 additions and 45 deletions.
Expand Up @@ -181,7 +181,7 @@ private function formatWithIdentifiers($em, $class, $value)
return $this->formatValue($value, self::PRETTY_DATE);
}

if (method_exists($value, '__toString')) {
if ($value instanceof \Stringable) {
return (string) $value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Secrets/SodiumVault.php
Expand Up @@ -33,7 +33,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
*/
public function __construct(string $secretsDir, $decryptionKey = null)
{
if (null !== $decryptionKey && !\is_string($decryptionKey) && !(\is_object($decryptionKey) && method_exists($decryptionKey, '__toString'))) {
if (null !== $decryptionKey && !\is_string($decryptionKey) && !$decryptionKey instanceof \Stringable) {
throw new \TypeError(sprintf('Decryption key should be a string or an object that implements the __toString() method, "%s" given.', get_debug_type($decryptionKey)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/CacheItem.php
Expand Up @@ -121,7 +121,7 @@ public function tag($tags): ItemInterface
$tags = [$tags];
}
foreach ($tags as $tag) {
if (!\is_string($tag) && !(\is_object($tag) && method_exists($tag, '__toString'))) {
if (!\is_string($tag) && !$tag instanceof \Stringable) {
throw new InvalidArgumentException(sprintf('Cache tag must be string or object that implements __toString(), "%s" given.', \is_object($tag) ? \get_class($tag) : \gettype($tag)));
}
$tag = (string) $tag;
Expand Down
Expand Up @@ -82,7 +82,7 @@ private static function getInputString(ConsoleEvent $event): ?string
$commandName = $event->getCommand() ? $event->getCommand()->getName() : null;
$input = $event->getInput();

if (method_exists($input, '__toString')) {
if ($input instanceof \Stringable) {
if ($commandName) {
return str_replace(["'$commandName'", "\"$commandName\""], $commandName, (string) $input);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Table.php
Expand Up @@ -631,7 +631,7 @@ private function fillNextRows(array $rows, int $line): array
{
$unmergedRows = [];
foreach ($rows[$line] as $column => $cell) {
if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !$cell instanceof \Stringable) {
throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given.', get_debug_type($cell)));
}
if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Logger/ConsoleLogger.php
Expand Up @@ -110,7 +110,7 @@ private function interpolate(string $message, array $context): string

$replacements = [];
foreach ($context as $key => $val) {
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
if (null === $val || is_scalar($val) || $val instanceof \Stringable) {
$replacements["{{$key}}"] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/HttpClientTrait.php
Expand Up @@ -248,7 +248,7 @@ private static function normalizeHeaders(array $headers): array
$normalizedHeaders = [];

foreach ($headers as $name => $values) {
if (\is_object($values) && method_exists($values, '__toString')) {
if ($values instanceof \Stringable) {
$values = (string) $values;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/InputBag.php
Expand Up @@ -29,7 +29,7 @@ final class InputBag extends ParameterBag
*/
public function get(string $key, $default = null)
{
if (null !== $default && !is_scalar($default) && !(\is_object($default) && method_exists($default, '__toString'))) {
if (null !== $default && !is_scalar($default) && !$default instanceof \Stringable) {
throw new \InvalidArgumentException(sprintf('Excepted a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public function add(array $inputs = [])
*/
public function set(string $key, $value)
{
if (null !== $value && !is_scalar($value) && !\is_array($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (null !== $value && !is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
throw new \InvalidArgumentException(sprintf('Excepted a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Log/Logger.php
Expand Up @@ -91,7 +91,7 @@ private function format(string $level, string $message, array $context, bool $pr
if (false !== strpos($message, '{')) {
$replacements = [];
foreach ($context as $key => $val) {
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
if (null === $val || is_scalar($val) || $val instanceof \Stringable) {
$replacements["{{$key}}"] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
Expand Down
Expand Up @@ -95,7 +95,7 @@ public function getUser()
*/
public function setUser($user)
{
if (!($user instanceof UserInterface || (\is_object($user) && method_exists($user, '__toString')) || \is_string($user))) {
if (!($user instanceof UserInterface || $user instanceof \Stringable || \is_string($user))) {
throw new \InvalidArgumentException('$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string.');
}

Expand Down
Expand Up @@ -88,7 +88,7 @@ public function computeSignatureHash(UserInterface $user, int $expires): string
$value = $value->format('c');
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new \InvalidArgumentException(sprintf('The property path "%s" on the user object "%s" must return a value that can be cast to a string, but "%s" was returned.', $property, \get_class($user), get_debug_type($value)));
}
$signatureFields[] = base64_encode($value);
Expand Down
Expand Up @@ -136,7 +136,7 @@ private function getCredentials(Request $request): array
$credentials['password'] = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']) ?? '';
}

if (!\is_string($credentials['username']) && (!\is_object($credentials['username']) || !method_exists($credentials['username'], '__toString'))) {
if (!\is_string($credentials['username']) && !$credentials['username'] instanceof \Stringable) {
throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.', $this->options['username_parameter'], \gettype($credentials['username'])));
}

Expand Down
Expand Up @@ -89,7 +89,7 @@ protected function attemptAuthentication(Request $request)
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
}

if (!\is_string($username) && (!\is_object($username) || !method_exists($username, '__toString'))) {
if (!\is_string($username) && !$username instanceof \Stringable) {
throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.', $this->options['username_parameter'], get_debug_type($username)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/ConstraintValidator.php
Expand Up @@ -100,7 +100,7 @@ protected function formatValue($value, int $format = 0)
}

if (\is_object($value)) {
if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
if (($format & self::OBJECT_TO_STRING) && $value instanceof \Stringable) {
return $value->__toString();
}

Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Validator/ConstraintViolation.php
Expand Up @@ -49,12 +49,8 @@ class ConstraintViolation implements ConstraintViolationInterface
* caused the violation
* @param mixed $cause The cause of the violation
*/
public function __construct($message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, string $code = null, Constraint $constraint = null, $cause = null)
public function __construct(string|\Stringable $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, string $code = null, Constraint $constraint = null, $cause = null)
{
if (!\is_string($message) && !(\is_object($message) && method_exists($message, '__toString'))) {
throw new \TypeError('Constraint violation message should be a string or an object which implements the __toString() method.');
}

$this->message = $message;
$this->messageTemplate = $messageTemplate;
$this->parameters = $parameters;
Expand Down
Expand Up @@ -68,7 +68,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -46,7 +46,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -56,7 +56,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -116,7 +116,7 @@ public function validate($value, Constraint $constraint)
}
}

if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof FileObject && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -150,7 +150,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -40,7 +40,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -36,7 +36,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -33,7 +33,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedTypeException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public function validate($value, Constraint $constraint)

// Work with strings only, because long numbers are represented as floats
// internally and don't work with strlen()
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!\is_string($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -63,7 +63,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (null !== $value && !is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -46,7 +46,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -52,7 +52,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down
Expand Up @@ -72,7 +72,7 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

Expand Down

0 comments on commit 4b8256d

Please sign in to comment.