Skip to content

Commit b83089f

Browse files
authored
[12.x] Prefer Symfony PHP polyfills over function_exists calls (#56621)
* Require Symfony PHP 8.3 polyfill in Support in favor of function existence check * Require Symfony PHP 8.3 polyfill in Testing and Validation in favor of function existence check
1 parent 087ce3a commit b83089f

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

src/Illuminate/Support/Str.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Illuminate\Support\Traits\Macroable;
7-
use JsonException;
87
use League\CommonMark\Environment\Environment;
98
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
109
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
@@ -577,17 +576,7 @@ public static function isJson($value)
577576
return false;
578577
}
579578

580-
if (function_exists('json_validate')) {
581-
return json_validate($value, 512);
582-
}
583-
584-
try {
585-
json_decode($value, true, 512, JSON_THROW_ON_ERROR);
586-
} catch (JsonException) {
587-
return false;
588-
}
589-
590-
return true;
579+
return json_validate($value, 512);
591580
}
592581

593582
/**
@@ -937,17 +926,7 @@ public static function numbers($value)
937926
*/
938927
public static function padBoth($value, $length, $pad = ' ')
939928
{
940-
if (function_exists('mb_str_pad')) {
941-
return mb_str_pad($value, $length, $pad, STR_PAD_BOTH);
942-
}
943-
944-
$short = max(0, $length - mb_strlen($value));
945-
$shortLeft = floor($short / 2);
946-
$shortRight = ceil($short / 2);
947-
948-
return mb_substr(str_repeat($pad, $shortLeft), 0, $shortLeft).
949-
$value.
950-
mb_substr(str_repeat($pad, $shortRight), 0, $shortRight);
929+
return mb_str_pad($value, $length, $pad, STR_PAD_BOTH);
951930
}
952931

953932
/**
@@ -960,13 +939,7 @@ public static function padBoth($value, $length, $pad = ' ')
960939
*/
961940
public static function padLeft($value, $length, $pad = ' ')
962941
{
963-
if (function_exists('mb_str_pad')) {
964-
return mb_str_pad($value, $length, $pad, STR_PAD_LEFT);
965-
}
966-
967-
$short = max(0, $length - mb_strlen($value));
968-
969-
return mb_substr(str_repeat($pad, $short), 0, $short).$value;
942+
return mb_str_pad($value, $length, $pad, STR_PAD_LEFT);
970943
}
971944

972945
/**
@@ -979,13 +952,7 @@ public static function padLeft($value, $length, $pad = ' ')
979952
*/
980953
public static function padRight($value, $length, $pad = ' ')
981954
{
982-
if (function_exists('mb_str_pad')) {
983-
return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT);
984-
}
985-
986-
$short = max(0, $length - mb_strlen($value));
987-
988-
return $value.mb_substr(str_repeat($pad, $short), 0, $short);
955+
return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT);
989956
}
990957

991958
/**

src/Illuminate/Support/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"illuminate/contracts": "^12.0",
2525
"illuminate/macroable": "^12.0",
2626
"nesbot/carbon": "^3.8.4",
27+
"symfony/polyfill-php83": "^1.31",
2728
"voku/portable-ascii": "^2.0.2"
2829
},
2930
"conflict": {

src/Illuminate/Testing/TestResponse.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,11 +1757,9 @@ public function ddBody($key = null)
17571757
{
17581758
$content = $this->content();
17591759

1760-
if (function_exists('json_validate') && json_validate($content)) {
1761-
$this->ddJson($key);
1762-
}
1763-
1764-
dd($content);
1760+
return json_validate($content)
1761+
? $this->ddJson($key)
1762+
: dd($content);
17651763
}
17661764

17671765
/**

src/Illuminate/Testing/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"illuminate/collections": "^12.0",
2020
"illuminate/contracts": "^12.0",
2121
"illuminate/macroable": "^12.0",
22-
"illuminate/support": "^12.0"
22+
"illuminate/support": "^12.0",
23+
"symfony/polyfill-php83": "^1.31"
2324
},
2425
"autoload": {
2526
"psr-4": {

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,13 +1631,7 @@ public function validateJson($attribute, $value)
16311631
return false;
16321632
}
16331633

1634-
if (function_exists('json_validate')) {
1635-
return json_validate($value);
1636-
}
1637-
1638-
json_decode($value);
1639-
1640-
return json_last_error() === JSON_ERROR_NONE;
1634+
return json_validate($value);
16411635
}
16421636

16431637
/**

src/Illuminate/Validation/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"illuminate/support": "^12.0",
2727
"illuminate/translation": "^12.0",
2828
"symfony/http-foundation": "^7.2",
29-
"symfony/mime": "^7.2"
29+
"symfony/mime": "^7.2",
30+
"symfony/polyfill-php83": "^1.31"
3031
},
3132
"autoload": {
3233
"psr-4": {

0 commit comments

Comments
 (0)