PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | vendor_prefixed/guzzlehttp/psr7/src/Query.php +39 -16 18.2 → trunk View file →
@@ -1,6 +1,7 @@
1 1 <?php
2 2
3 +declare (strict_types=1);
3 4 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
4 5
5 6 final class Query
6 7 {
@@ -13,12 +14,10 @@
13 14 * will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`.
14 15 *
15 16 * @param string $str Query string to parse
16 17 * @param int|bool $urlEncoding How the query string is encoded
17 - *
18 - * @return array
19 18 */
20 - public static function parse($str, $urlEncoding = \true)
19 + public static function parse(string $str, $urlEncoding = \true) : array
21 20 {
22 21 $result = [];
23 22 if ($str === '') {
24 23 return $result;
@@ -24,9 +23,9 @@
24 23 return $result;
25 24 }
26 25 if ($urlEncoding === \true) {
27 26 $decoder = function ($value) {
28 - return \rawurldecode(\str_replace('+', ' ', $value));
27 + return \rawurldecode(\str_replace('+', ' ', (string) $value));
29 28 };
30 29 } elseif ($urlEncoding === \PHP_QUERY_RFC3986) {
31 30 $decoder = 'rawurldecode';
32 31 } elseif ($urlEncoding === \PHP_QUERY_RFC1738) {
@@ -39,9 +38,9 @@
39 38 foreach (\explode('&', $str) as $kvp) {
40 39 $parts = \explode('=', $kvp, 2);
41 40 $key = $decoder($parts[0]);
42 41 $value = isset($parts[1]) ? $decoder($parts[1]) : null;
43 - if (!isset($result[$key])) {
42 + if (!\array_key_exists($key, $result)) {
44 43 $result[$key] = $value;
45 44 } else {
46 45 if (!\is_array($result[$key])) {
47 46 $result[$key] = [$result[$key]];
@@ -57,22 +56,23 @@
57 56 * This function can use the return value of `parse()` to build a query
58 57 * string. This function does not modify the provided keys when an array is
59 58 * encountered (like `http_build_query()` would).
60 59 *
61 - * @param array $params Query string parameters.
62 - * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
63 - * to encode using RFC3986, or PHP_QUERY_RFC1738
64 - * to encode using RFC1738.
65 - *
66 - * @return string
60 + * @param array $params Query string parameters.
61 + * @param int|false $encoding Set to false to not encode,
62 + * PHP_QUERY_RFC3986 to encode using
63 + * RFC3986, or PHP_QUERY_RFC1738 to
64 + * encode using RFC1738.
65 + * @param bool $treatBoolsAsInts Set to true to encode as 0/1, and
66 + * false as false/true.
67 67 */
68 - public static function build(array $params, $encoding = \PHP_QUERY_RFC3986)
68 + public static function build(array $params, $encoding = \PHP_QUERY_RFC3986, bool $treatBoolsAsInts = \true) : string
69 69 {
70 70 if (!$params) {
71 71 return '';
72 72 }
73 73 if ($encoding === \false) {
74 - $encoder = function ($str) {
74 + $encoder = function (string $str) : string {
75 75 return $str;
76 76 };
77 77 } elseif ($encoding === \PHP_QUERY_RFC3986) {
78 78 $encoder = 'rawurlencode';
@@ -80,22 +80,29 @@
80 80 $encoder = 'urlencode';
81 81 } else {
82 82 throw new \InvalidArgumentException('Invalid type');
83 83 }
84 + $castBool = $treatBoolsAsInts ? static function ($v) {
85 + return (int) $v;
86 + } : static function ($v) {
87 + return $v ? 'true' : 'false';
88 + };
84 89 $qs = '';
85 90 foreach ($params as $k => $v) {
86 - $k = $encoder($k);
91 + $k = $encoder((string) $k);
87 92 if (!\is_array($v)) {
88 93 $qs .= $k;
94 + $v = \is_bool($v) ? $castBool($v) : self::normalizeNonFiniteFloat($v);
89 95 if ($v !== null) {
90 - $qs .= '=' . $encoder($v);
96 + $qs .= '=' . $encoder((string) $v);
91 97 }
92 98 $qs .= '&';
93 99 } else {
94 100 foreach ($v as $vv) {
95 101 $qs .= $k;
102 + $vv = \is_bool($vv) ? $castBool($vv) : self::normalizeNonFiniteFloat($vv);
96 103 if ($vv !== null) {
97 - $qs .= '=' . $encoder($vv);
104 + $qs .= '=' . $encoder((string) $vv);
98 105 }
99 106 $qs .= '&';
100 107 }
101 108 }
@@ -100,6 +107,22 @@
100 107 }
101 108 }
102 109 }
103 110 return $qs ? (string) \substr($qs, 0, -1) : '';
111 + }
112 + /**
113 + * Converts non-finite floats to the strings PHP coerces them to, as
114 + * implicit coercion of NAN emits a warning on PHP 8.5.
115 + *
116 + * @param mixed $value
117 + *
118 + * @return mixed
119 + */
120 + private static function normalizeNonFiniteFloat($value)
121 + {
122 + if (\is_float($value) && !\is_finite($value)) {
123 + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.12', 'Passing a non-finite float to Query::build() is deprecated; guzzlehttp/psr7 3.0 rejects non-finite floats.');
124 + return \is_nan($value) ? 'NAN' : ($value > 0 ? 'INF' : '-INF');
125 + }
126 + return $value;
104 127 }
105 128 }