Skip to content

Commit 48df568

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 24936f6 commit 48df568

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

CurlHttpClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function request(string $method, string $url, array $options = []): Respo
269269
if ($options['bindto']) {
270270
if (file_exists($options['bindto'])) {
271271
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
272-
} elseif (0 !== strpos($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
272+
} elseif (!str_starts_with($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
273273
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
274274
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
275275
} else {
@@ -305,7 +305,7 @@ public function request(string $method, string $url, array $options = []): Respo
305305
foreach ($curlopts as $opt => $value) {
306306
if (null !== $value && !curl_setopt($ch, $opt, $value) && \CURLOPT_CERTINFO !== $opt) {
307307
$constants = array_filter(get_defined_constants(), static function ($v, $k) use ($opt) {
308-
return $v === $opt && 'C' === $k[0] && (0 === strpos($k, 'CURLOPT_') || 0 === strpos($k, 'CURLINFO_'));
308+
return $v === $opt && 'C' === $k[0] && (str_starts_with($k, 'CURLOPT_') || str_starts_with($k, 'CURLINFO_'));
309309
}, \ARRAY_FILTER_USE_BOTH);
310310

311311
throw new TransportException(sprintf('Curl option "%s" is not supported.', key($constants) ?? $opt));
@@ -399,7 +399,7 @@ private function handlePush($parent, $pushed, array $requestHeaders, int $maxPen
399399
// curl before 7.65 doesn't validate the pushed ":authority" header,
400400
// but this is a MUST in the HTTP/2 RFC; let's restrict pushes to the original host,
401401
// ignoring domains mentioned as alt-name in the certificate for now (same as curl).
402-
if (0 !== strpos($origin, $url.'/')) {
402+
if (!str_starts_with($origin, $url.'/')) {
403403
$this->logger && $this->logger->debug(sprintf('Rejecting pushed response from "%s": server is not authoritative for "%s"', $origin, $url));
404404

405405
return \CURL_PUSH_DENY;

Exception/HttpExceptionTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(ResponseInterface $response)
3232
$httpCodeFound = false;
3333
$isJson = false;
3434
foreach (array_reverse($response->getInfo('response_headers')) as $h) {
35-
if (0 === strpos($h, 'HTTP/')) {
35+
if (str_starts_with($h, 'HTTP/')) {
3636
if ($httpCodeFound) {
3737
break;
3838
}

HttpClientTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
215215
$alternatives = [];
216216

217217
foreach ($defaultOptions as $key => $v) {
218-
if (levenshtein($name, $key) <= \strlen($name) / 3 || false !== strpos($key, $name)) {
218+
if (levenshtein($name, $key) <= \strlen($name) / 3 || str_contains($key, $name)) {
219219
$alternatives[] = $key;
220220
}
221221
}
@@ -465,7 +465,7 @@ private static function parseUrl(string $url, array $query = [], array $allowedS
465465
continue;
466466
}
467467

468-
if (false !== strpos($parts[$part], '%')) {
468+
if (str_contains($parts[$part], '%')) {
469469
// https://tools.ietf.org/html/rfc3986#section-2.3
470470
$parts[$part] = preg_replace_callback('/%(?:2[DE]|3[0-9]|[46][1-9A-F]|5F|[57][0-9A]|7E)++/i', function ($m) { return rawurldecode($m[0]); }, $parts[$part]);
471471
}
@@ -493,11 +493,11 @@ private static function removeDotSegments(string $path)
493493
$result = '';
494494

495495
while (!\in_array($path, ['', '.', '..'], true)) {
496-
if ('.' === $path[0] && (0 === strpos($path, $p = '../') || 0 === strpos($path, $p = './'))) {
496+
if ('.' === $path[0] && (str_starts_with($path, $p = '../') || str_starts_with($path, $p = './'))) {
497497
$path = substr($path, \strlen($p));
498-
} elseif ('/.' === $path || 0 === strpos($path, '/./')) {
498+
} elseif ('/.' === $path || str_starts_with($path, '/./')) {
499499
$path = substr_replace($path, '/', 0, 3);
500-
} elseif ('/..' === $path || 0 === strpos($path, '/../')) {
500+
} elseif ('/..' === $path || str_starts_with($path, '/../')) {
501501
$i = strrpos($result, '/');
502502
$result = $i ? substr($result, 0, $i) : '';
503503
$path = substr_replace($path, '/', 0, 4);

NativeHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public function request(string $method, string $url, array $options = []): Respo
7171
if (file_exists($options['bindto'])) {
7272
throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
7373
}
74-
if (0 === strpos($options['bindto'], 'if!')) {
74+
if (str_starts_with($options['bindto'], 'if!')) {
7575
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
7676
}
77-
if (0 === strpos($options['bindto'], 'host!')) {
77+
if (str_starts_with($options['bindto'], 'host!')) {
7878
$options['bindto'] = substr($options['bindto'], 5);
7979
}
8080
}

Response/CurlResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
335335
return \strlen($data);
336336
}
337337

338-
if (0 !== strpos($data, 'HTTP/')) {
338+
if (!str_starts_with($data, 'HTTP/')) {
339339
if (0 === stripos($data, 'Location:')) {
340340
$location = trim(substr($data, 9));
341341
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"php-http/async-client-implementation": "*",
1919
"php-http/client-implementation": "*",
2020
"psr/http-client-implementation": "1.0",
21-
"symfony/http-client-implementation": "1.1|2.0"
21+
"symfony/http-client-implementation": "1.1|2.0",
22+
"symfony/polyfill-php80": "^1.16"
2223
},
2324
"require": {
2425
"php": ">=7.1.3",

0 commit comments

Comments
 (0)