From ec2be5ce279938fb867869916bb8166ca955d7b6 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Mon, 1 Sep 2025 23:16:21 +0200 Subject: [PATCH 1/5] Update InteractsWithData::string to return native string --- .../Support/Traits/InteractsWithData.php | 19 ++++++-- tests/Http/HttpRequestTest.php | 48 ++++++++++++++----- tests/Support/SupportFluentTest.php | 22 ++++----- tests/Support/ValidatedInputTest.php | 24 +++++----- tests/View/ViewComponentAttributeBagTest.php | 10 ++-- 5 files changed, 79 insertions(+), 44 deletions(-) diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index 35365b99c3b6..d39e39969cb8 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Date; use Illuminate\Support\Str; +use InvalidArgumentException; use stdClass; use function Illuminate\Support\enum_value; @@ -231,19 +232,27 @@ protected function isEmptyString($key) */ public function str($key, $default = null) { - return $this->string($key, $default); + return Str::of($this->data($key, $default)); } /** - * Retrieve data from the instance as a Stringable instance. + * Retrieve data from the instance as a string. * * @param string $key * @param mixed $default - * @return \Illuminate\Support\Stringable - */ + * @return string + */ public function string($key, $default = null) { - return Str::of($this->data($key, $default)); + $value = $this->data($key, $default); + + if (! is_scalar($value) && ! is_null($value)) { + throw new InvalidArgumentException( + sprintf('Value for key [%s] must be a string, %s given.', $key, gettype($value)) + ); + } + + return (string) $value; } /** diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 08ab80bc6d80..9607e4e8e01c 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -604,17 +604,43 @@ public function testStringMethod() 'empty_str' => '', 'null' => null, ]); - $this->assertTrue($request->string('int') instanceof Stringable); - $this->assertTrue($request->string('unknown_key') instanceof Stringable); - $this->assertSame('123', $request->string('int')->value()); - $this->assertSame('456', $request->string('int_str')->value()); - $this->assertSame('123.456', $request->string('float')->value()); - $this->assertSame('123.456', $request->string('float_str')->value()); - $this->assertSame('0', $request->string('float_zero')->value()); - $this->assertSame('0.000', $request->string('float_str_zero')->value()); - $this->assertSame('', $request->string('empty_str')->value()); - $this->assertSame('', $request->string('null')->value()); - $this->assertSame('', $request->string('unknown_key')->value()); + $this->assertIsString($request->string('int')); + $this->assertIsString($request->string('unknown_key')); + $this->assertSame('123', $request->string('int')); + $this->assertSame('456', $request->string('int_str')); + $this->assertSame('123.456', $request->string('float')); + $this->assertSame('123.456', $request->string('float_str')); + $this->assertSame('0', $request->string('float_zero')); + $this->assertSame('0.000', $request->string('float_str_zero')); + $this->assertSame('', $request->string('empty_str')); + $this->assertSame('', $request->string('null')); + $this->assertSame('', $request->string('unknown_key')); + } + + public function testStrMethod() + { + $request = Request::create('/', 'GET', [ + 'int' => 123, + 'int_str' => '456', + 'float' => 123.456, + 'float_str' => '123.456', + 'float_zero' => 0.000, + 'float_str_zero' => '0.000', + 'str' => 'abc', + 'empty_str' => '', + 'null' => null, + ]); + $this->assertTrue($request->str('int') instanceof Stringable); + $this->assertTrue($request->str('unknown_key') instanceof Stringable); + $this->assertSame('123', $request->str('int')->value()); + $this->assertSame('456', $request->str('int_str')->value()); + $this->assertSame('123.456', $request->str('float')->value()); + $this->assertSame('123.456', $request->str('float_str')->value()); + $this->assertSame('0', $request->str('float_zero')->value()); + $this->assertSame('0.000', $request->str('float_str_zero')->value()); + $this->assertSame('', $request->str('empty_str')->value()); + $this->assertSame('', $request->str('null')->value()); + $this->assertSame('', $request->str('unknown_key')->value()); } public function testBooleanMethod() diff --git a/tests/Support/SupportFluentTest.php b/tests/Support/SupportFluentTest.php index fe0edb1aedb7..96b19fdded59 100755 --- a/tests/Support/SupportFluentTest.php +++ b/tests/Support/SupportFluentTest.php @@ -168,17 +168,17 @@ public function testStringMethod() 'empty_str' => '', 'null' => null, ]); - $this->assertTrue($fluent->string('int') instanceof Stringable); - $this->assertTrue($fluent->string('unknown_key') instanceof Stringable); - $this->assertSame('123', $fluent->string('int')->value()); - $this->assertSame('456', $fluent->string('int_str')->value()); - $this->assertSame('123.456', $fluent->string('float')->value()); - $this->assertSame('123.456', $fluent->string('float_str')->value()); - $this->assertSame('0', $fluent->string('float_zero')->value()); - $this->assertSame('0.000', $fluent->string('float_str_zero')->value()); - $this->assertSame('', $fluent->string('empty_str')->value()); - $this->assertSame('', $fluent->string('null')->value()); - $this->assertSame('', $fluent->string('unknown_key')->value()); + $this->assertIsString($fluent->string('int')); + $this->assertIsString($fluent->string('unknown_key')); + $this->assertSame('123', $fluent->string('int')); + $this->assertSame('456', $fluent->string('int_str')); + $this->assertSame('123.456', $fluent->string('float')); + $this->assertSame('123.456', $fluent->string('float_str')); + $this->assertSame('0', $fluent->string('float_zero')); + $this->assertSame('0.000', $fluent->string('float_str_zero')); + $this->assertSame('', $fluent->string('empty_str')); + $this->assertSame('', $fluent->string('null')); + $this->assertSame('', $fluent->string('unknown_key')); } public function testBooleanMethod() diff --git a/tests/Support/ValidatedInputTest.php b/tests/Support/ValidatedInputTest.php index fe821604ba1b..78008fbe3286 100644 --- a/tests/Support/ValidatedInputTest.php +++ b/tests/Support/ValidatedInputTest.php @@ -352,18 +352,18 @@ public function test_string_method() 'null' => null, ]); - $this->assertTrue($input->string('int') instanceof Stringable); - $this->assertTrue($input->string('int') instanceof Stringable); - $this->assertTrue($input->string('unknown_key') instanceof Stringable); - $this->assertSame('123', $input->string('int')->value()); - $this->assertSame('456', $input->string('int_str')->value()); - $this->assertSame('123.456', $input->string('float')->value()); - $this->assertSame('123.456', $input->string('float_str')->value()); - $this->assertSame('0', $input->string('float_zero')->value()); - $this->assertSame('0.000', $input->string('float_str_zero')->value()); - $this->assertSame('', $input->string('empty_str')->value()); - $this->assertSame('', $input->string('null')->value()); - $this->assertSame('', $input->string('unknown_key')->value()); + $this->assertIsString($input->string('int')); + $this->assertIsString($input->string('int')); + $this->assertIsString($input->string('unknown_key')); + $this->assertSame('123', $input->string('int')); + $this->assertSame('456', $input->string('int_str')); + $this->assertSame('123.456', $input->string('float')); + $this->assertSame('123.456', $input->string('float_str')); + $this->assertSame('0', $input->string('float_zero')); + $this->assertSame('0.000', $input->string('float_str_zero')); + $this->assertSame('', $input->string('empty_str')); + $this->assertSame('', $input->string('null')); + $this->assertSame('', $input->string('unknown_key')); } public function test_boolean_method() diff --git a/tests/View/ViewComponentAttributeBagTest.php b/tests/View/ViewComponentAttributeBagTest.php index a2293e6e5f19..17a6ee5db09f 100644 --- a/tests/View/ViewComponentAttributeBagTest.php +++ b/tests/View/ViewComponentAttributeBagTest.php @@ -346,11 +346,11 @@ public function testString() 'number' => 123, ]); - $this->assertInstanceOf(\Illuminate\Support\Stringable::class, $bag->string('name')); - $this->assertEquals('test', (string) $bag->string('name')); - $this->assertEquals('', (string) $bag->string('empty')); - $this->assertEquals('123', (string) $bag->string('number')); - $this->assertEquals('default', (string) $bag->string('missing', 'default')); + $this->assertIsString($bag->string('name')); + $this->assertEquals('test', $bag->string('name')); + $this->assertEquals('', $bag->string('empty')); + $this->assertEquals('123', $bag->string('number')); + $this->assertEquals('default', $bag->string('missing', 'default')); } public function testBoolean() From 655f8f71382454396a8d07495b56b0fa937cfe1f Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Mon, 1 Sep 2025 23:32:42 +0200 Subject: [PATCH 2/5] Add native return type --- src/Illuminate/Support/Traits/InteractsWithData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index d39e39969cb8..8f7a03cceef2 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -242,7 +242,7 @@ public function str($key, $default = null) * @param mixed $default * @return string */ - public function string($key, $default = null) + public function string($key, $default = null): string { $value = $this->data($key, $default); From dc8f3be2ac3fa28b47c2b56c01786b9cbf755d6e Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Mon, 1 Sep 2025 23:40:03 +0200 Subject: [PATCH 3/5] Fix coding style issues --- src/Illuminate/Support/Traits/InteractsWithData.php | 2 +- tests/Support/SupportFluentTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index 8f7a03cceef2..bc37a8e45531 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -241,7 +241,7 @@ public function str($key, $default = null) * @param string $key * @param mixed $default * @return string - */ + */ public function string($key, $default = null): string { $value = $this->data($key, $default); diff --git a/tests/Support/SupportFluentTest.php b/tests/Support/SupportFluentTest.php index 96b19fdded59..760e156cf6e7 100755 --- a/tests/Support/SupportFluentTest.php +++ b/tests/Support/SupportFluentTest.php @@ -6,7 +6,6 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Fluent; -use Illuminate\Support\Stringable; use InvalidArgumentException; use IteratorAggregate; use PHPUnit\Framework\TestCase; From 44c036fa1da6d31cf9499eb9480817c16922697f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 11 Sep 2025 12:13:32 +0800 Subject: [PATCH 4/5] Update src/Illuminate/Support/Traits/InteractsWithData.php --- src/Illuminate/Support/Traits/InteractsWithData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index b6671c9d03cc..0e1df5e9b9a1 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -241,7 +241,7 @@ public function str($key, $default = null) * @param string $key * @param mixed $default * @return string - */ + */ public function string($key, $default = null): string { $value = $this->data($key, $default); From a430ee7cd99df2219fb8b0f27220f72ffaa519ca Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Mon, 15 Sep 2025 11:59:43 +0200 Subject: [PATCH 5/5] Fix coding style issue --- src/Illuminate/Support/Traits/InteractsWithData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index b6671c9d03cc..0e1df5e9b9a1 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -241,7 +241,7 @@ public function str($key, $default = null) * @param string $key * @param mixed $default * @return string - */ + */ public function string($key, $default = null): string { $value = $this->data($key, $default);