From 53b0f63bc3c302fc2a3118cf9141eb668ef46330 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 13 Mar 2020 11:54:27 +0100 Subject: [PATCH] [String] leverage Stringable from PHP 8 --- composer.json | 1 + src/Symfony/Component/String/AbstractString.php | 2 +- src/Symfony/Component/String/LazyString.php | 11 +++++++---- src/Symfony/Component/String/composer.json | 3 ++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 499ead25ec126..4d92d53ce4eed 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "symfony/polyfill-intl-normalizer": "~1.0", "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php73": "^1.11", + "symfony/polyfill-php80": "^1.15", "symfony/polyfill-uuid": "^1.15" }, "replace": { diff --git a/src/Symfony/Component/String/AbstractString.php b/src/Symfony/Component/String/AbstractString.php index c11a93062815e..84b96c5ccb04a 100644 --- a/src/Symfony/Component/String/AbstractString.php +++ b/src/Symfony/Component/String/AbstractString.php @@ -27,7 +27,7 @@ * * @throws ExceptionInterface */ -abstract class AbstractString implements \JsonSerializable +abstract class AbstractString implements \Stringable, \JsonSerializable { public const PREG_PATTERN_ORDER = PREG_PATTERN_ORDER; public const PREG_SET_ORDER = PREG_SET_ORDER; diff --git a/src/Symfony/Component/String/LazyString.php b/src/Symfony/Component/String/LazyString.php index bb55baefe17aa..51680e65534ae 100644 --- a/src/Symfony/Component/String/LazyString.php +++ b/src/Symfony/Component/String/LazyString.php @@ -16,7 +16,7 @@ * * @author Nicolas Grekas */ -class LazyString implements \JsonSerializable +class LazyString implements \Stringable, \JsonSerializable { private $value; @@ -50,14 +50,14 @@ public static function fromCallable($callback, ...$arguments): self } /** - * @param object|string|int|float|bool $value A scalar or an object that implements the __toString() magic method + * @param string|int|float|bool|\Stringable $value * * @return static */ public static function fromStringable($value): self { if (!self::isStringable($value)) { - throw new \TypeError(sprintf('Argument 1 passed to %s() must be a scalar or an object that implements the __toString() magic method, %s given.', __METHOD__, \is_object($value) ? \get_class($value) : \gettype($value))); + throw new \TypeError(sprintf('Argument 1 passed to %s() must be a scalar or a stringable object, %s given.', __METHOD__, \is_object($value) ? \get_class($value) : \gettype($value))); } if (\is_object($value)) { @@ -75,7 +75,7 @@ public static function fromStringable($value): self */ final public static function isStringable($value): bool { - return \is_string($value) || $value instanceof self || (\is_object($value) ? \is_callable([$value, '__toString']) : is_scalar($value)); + return \is_string($value) || $value instanceof self || (\is_object($value) ? method_exists($value, '__toString') : is_scalar($value)); } /** @@ -90,6 +90,9 @@ final public static function resolve($value): string return $value; } + /** + * @return string + */ public function __toString() { if (\is_string($this->value)) { diff --git a/src/Symfony/Component/String/composer.json b/src/Symfony/Component/String/composer.json index b44b8e0b7f7d8..ec936e019d727 100644 --- a/src/Symfony/Component/String/composer.json +++ b/src/Symfony/Component/String/composer.json @@ -19,7 +19,8 @@ "php": "^7.2.5", "symfony/polyfill-intl-grapheme": "~1.0", "symfony/polyfill-intl-normalizer": "~1.0", - "symfony/polyfill-mbstring": "~1.0" + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php80": "~1.15" }, "require-dev": { "symfony/error-handler": "^4.4|^5.0",