Skip to content

Commit 710a8ab

Browse files
Merge branch '5.2' into 5.3
* 5.2: Leverage str_contains/str_starts_with Leverage str_ends_with
2 parents 173b980 + c424d84 commit 710a8ab

15 files changed

+42
-42
lines changed

BinaryFileResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function setContentDisposition(string $disposition, string $filename = ''
160160
$filename = $this->file->getFilename();
161161
}
162162

163-
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
163+
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || str_contains($filename, '%'))) {
164164
$encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
165165

166166
for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
@@ -240,7 +240,7 @@ public function prepare(Request $request)
240240
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
241241
$range = $request->headers->get('Range');
242242

243-
if (0 === strpos($range, 'bytes=')) {
243+
if (str_starts_with($range, 'bytes=')) {
244244
[$start, $end] = explode('-', substr($range, 6), 2) + [0];
245245

246246
$end = ('' === $end) ? $fileSize - 1 : (int) $end;

File/UploadedFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ private static function parseFilesize(string $size)
240240
$size = strtolower($size);
241241

242242
$max = ltrim($size, '+');
243-
if (0 === strpos($max, '0x')) {
243+
if (str_starts_with($max, '0x')) {
244244
$max = \intval($max, 16);
245-
} elseif (0 === strpos($max, '0')) {
245+
} elseif (str_starts_with($max, '0')) {
246246
$max = \intval($max, 8);
247247
} else {
248248
$max = (int) $max;

HeaderUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ public static function makeDisposition(string $disposition, string $filename, st
176176
}
177177

178178
// percent characters aren't safe in fallback.
179-
if (false !== strpos($filenameFallback, '%')) {
179+
if (str_contains($filenameFallback, '%')) {
180180
throw new \InvalidArgumentException('The filename fallback cannot contain the "%" character.');
181181
}
182182

183183
// path separators aren't allowed in either.
184-
if (false !== strpos($filename, '/') || false !== strpos($filename, '\\') || false !== strpos($filenameFallback, '/') || false !== strpos($filenameFallback, '\\')) {
184+
if (str_contains($filename, '/') || str_contains($filename, '\\') || str_contains($filenameFallback, '/') || str_contains($filenameFallback, '\\')) {
185185
throw new \InvalidArgumentException('The filename and the fallback cannot contain the "/" and "\\" characters.');
186186
}
187187

IpUtils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function checkIp4(?string $requestIp, string $ip)
7070
return self::$checkedIps[$cacheKey] = false;
7171
}
7272

73-
if (false !== strpos($ip, '/')) {
73+
if (str_contains($ip, '/')) {
7474
[$address, $netmask] = explode('/', $ip, 2);
7575

7676
if ('0' === $netmask) {
@@ -117,7 +117,7 @@ public static function checkIp6(?string $requestIp, string $ip)
117117
throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
118118
}
119119

120-
if (false !== strpos($ip, '/')) {
120+
if (str_contains($ip, '/')) {
121121
[$address, $netmask] = explode('/', $ip, 2);
122122

123123
if ('0' === $netmask) {

JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function setData($data = [])
157157
try {
158158
$data = json_encode($data, $this->encodingOptions);
159159
} catch (\Exception $e) {
160-
if ('Exception' === \get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
160+
if ('Exception' === \get_class($e) && str_starts_with($e->getMessage(), 'Failed calling ')) {
161161
throw $e->getPrevious() ?: $e;
162162
}
163163
throw $e;

Request.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public static function createFromGlobals()
305305
{
306306
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
307307

308-
if (0 === strpos($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
308+
if (str_starts_with($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
309309
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
310310
) {
311311
parse_str($request->getContent(), $data);
@@ -1682,7 +1682,7 @@ public function getLanguages()
16821682
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
16831683
$this->languages = [];
16841684
foreach ($languages as $lang => $acceptHeaderItem) {
1685-
if (false !== strpos($lang, '-')) {
1685+
if (str_contains($lang, '-')) {
16861686
$codes = explode('-', $lang);
16871687
if ('i' === $codes[0]) {
16881688
// Language not listed in ISO 639 that are not variants
@@ -2007,7 +2007,7 @@ private function setPhpDefaultLocale(string $locale): void
20072007
*/
20082008
private function getUrlencodedPrefix(string $string, string $prefix): ?string
20092009
{
2010-
if (0 !== strpos(rawurldecode($string), $prefix)) {
2010+
if (!str_starts_with(rawurldecode($string), $prefix)) {
20112011
return null;
20122012
}
20132013

@@ -2069,7 +2069,7 @@ private function getTrustedValues(int $type, string $ip = null): array
20692069
continue;
20702070
}
20712071
if (self::HEADER_X_FORWARDED_PORT === $type) {
2072-
if (']' === substr($v, -1) || false === $v = strrchr($v, ':')) {
2072+
if (str_ends_with($v, ']') || false === $v = strrchr($v, ':')) {
20732073
$v = $this->isSecure() ? ':443' : ':80';
20742074
}
20752075
$v = '0.0.0.0'.$v;
@@ -2115,7 +2115,7 @@ private function normalizeAndFilterClientIps(array $clientIps, string $ip): arra
21152115
if ($i) {
21162116
$clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
21172117
}
2118-
} elseif (0 === strpos($clientIp, '[')) {
2118+
} elseif (str_starts_with($clientIp, '[')) {
21192119
// Strip brackets and :port from IPv6 addresses.
21202120
$i = strpos($clientIp, ']', 1);
21212121
$clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);

Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function prepare(Request $request)
324324
}
325325

326326
// Check if we need to send extra expire info headers
327-
if ('1.0' == $this->getProtocolVersion() && false !== strpos($headers->get('Cache-Control'), 'no-cache')) {
327+
if ('1.0' == $this->getProtocolVersion() && str_contains($headers->get('Cache-Control'), 'no-cache')) {
328328
$headers->set('pragma', 'no-cache');
329329
$headers->set('expires', -1);
330330
}
@@ -926,7 +926,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
926926
if (null === $etag) {
927927
$this->headers->remove('Etag');
928928
} else {
929-
if (0 !== strpos($etag, '"')) {
929+
if (!str_starts_with($etag, '"')) {
930930
$etag = '"'.$etag.'"';
931931
}
932932

ServerBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getHeaders()
2929
{
3030
$headers = [];
3131
foreach ($this->parameters as $key => $value) {
32-
if (0 === strpos($key, 'HTTP_')) {
32+
if (str_starts_with($key, 'HTTP_')) {
3333
$headers[substr($key, 5)] = $value;
3434
} elseif (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
3535
$headers[$key] = $value;

Session/Attribute/NamespacedAttributeBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function remove(string $name)
106106
protected function &resolveAttributePath(string $name, bool $writeContext = false)
107107
{
108108
$array = &$this->attributes;
109-
$name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
109+
$name = (str_starts_with($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
110110

111111
// Check if there is anything to do, else return
112112
if (!$name) {

Session/Storage/Handler/PdoSessionHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function __construct($pdoOrDsn = null, array $options = [])
179179

180180
$this->pdo = $pdoOrDsn;
181181
$this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
182-
} elseif (\is_string($pdoOrDsn) && false !== strpos($pdoOrDsn, '://')) {
182+
} elseif (\is_string($pdoOrDsn) && str_contains($pdoOrDsn, '://')) {
183183
$this->dsn = $this->buildDsnFromUrl($pdoOrDsn);
184184
} else {
185185
$this->dsn = $pdoOrDsn;
@@ -353,7 +353,7 @@ protected function doWrite(string $sessionId, string $data)
353353
$insertStmt->execute();
354354
} catch (\PDOException $e) {
355355
// Handle integrity violation SQLSTATE 23000 (or a subclass like 23505 in Postgres) for duplicate keys
356-
if (0 === strpos($e->getCode(), '23')) {
356+
if (str_starts_with($e->getCode(), '23')) {
357357
$updateStmt->execute();
358358
} else {
359359
throw $e;
@@ -487,7 +487,7 @@ private function buildDsnFromUrl(string $dsnOrUrl): string
487487
$driver = $driverAliasMap[$params['scheme']] ?? $params['scheme'];
488488

489489
// Doctrine DBAL supports passing its internal pdo_* driver names directly too (allowing both dashes and underscores). This allows supporting the same here.
490-
if (0 === strpos($driver, 'pdo_') || 0 === strpos($driver, 'pdo-')) {
490+
if (str_starts_with($driver, 'pdo_') || str_starts_with($driver, 'pdo-')) {
491491
$driver = substr($driver, 4);
492492
}
493493

@@ -681,7 +681,7 @@ protected function doRead(string $sessionId)
681681
} catch (\PDOException $e) {
682682
// Catch duplicate key error because other connection created the session already.
683683
// It would only not be the case when the other connection destroyed the session.
684-
if (0 === strpos($e->getCode(), '23')) {
684+
if (str_starts_with($e->getCode(), '23')) {
685685
// Retrieve finished session data written by concurrent connection by restarting the loop.
686686
// We have to start a new transaction as a failed query will mark the current transaction as
687687
// aborted in PostgreSQL and disallow further queries within it.

Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,38 @@ public static function createHandler($connection): AbstractSessionHandler
4747

4848
case !\is_string($connection):
4949
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', get_debug_type($connection)));
50-
case 0 === strpos($connection, 'file://'):
50+
case str_starts_with($connection, 'file://'):
5151
$savePath = substr($connection, 7);
5252

5353
return new StrictSessionHandler(new NativeFileSessionHandler('' === $savePath ? null : $savePath));
5454

55-
case 0 === strpos($connection, 'redis:'):
56-
case 0 === strpos($connection, 'rediss:'):
57-
case 0 === strpos($connection, 'memcached:'):
55+
case str_starts_with($connection, 'redis:'):
56+
case str_starts_with($connection, 'rediss:'):
57+
case str_starts_with($connection, 'memcached:'):
5858
if (!class_exists(AbstractAdapter::class)) {
5959
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
6060
}
61-
$handlerClass = 0 === strpos($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
61+
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
6262
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
6363

6464
return new $handlerClass($connection);
6565

66-
case 0 === strpos($connection, 'pdo_oci://'):
66+
case str_starts_with($connection, 'pdo_oci://'):
6767
if (!class_exists(DriverManager::class)) {
6868
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
6969
}
7070
$connection = DriverManager::getConnection(['url' => $connection])->getWrappedConnection();
7171
// no break;
7272

73-
case 0 === strpos($connection, 'mssql://'):
74-
case 0 === strpos($connection, 'mysql://'):
75-
case 0 === strpos($connection, 'mysql2://'):
76-
case 0 === strpos($connection, 'pgsql://'):
77-
case 0 === strpos($connection, 'postgres://'):
78-
case 0 === strpos($connection, 'postgresql://'):
79-
case 0 === strpos($connection, 'sqlsrv://'):
80-
case 0 === strpos($connection, 'sqlite://'):
81-
case 0 === strpos($connection, 'sqlite3://'):
73+
case str_starts_with($connection, 'mssql://'):
74+
case str_starts_with($connection, 'mysql://'):
75+
case str_starts_with($connection, 'mysql2://'):
76+
case str_starts_with($connection, 'pgsql://'):
77+
case str_starts_with($connection, 'postgres://'):
78+
case str_starts_with($connection, 'postgresql://'):
79+
case str_starts_with($connection, 'sqlsrv://'):
80+
case str_starts_with($connection, 'sqlite://'):
81+
case str_starts_with($connection, 'sqlite3://'):
8282
return new PdoSessionHandler($connection);
8383
}
8484

Session/Storage/NativeSessionStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function save()
256256

257257
// Register error handler to add information about the current save handler
258258
$previousHandler = set_error_handler(function ($type, $msg, $file, $line) use (&$previousHandler) {
259-
if (\E_WARNING === $type && 0 === strpos($msg, 'session_write_close():')) {
259+
if (\E_WARNING === $type && str_starts_with($msg, 'session_write_close():')) {
260260
$handler = $this->saveHandler instanceof SessionHandlerProxy ? $this->saveHandler->getHandler() : $this->saveHandler;
261261
$msg = sprintf('session_write_close(): Failed to write session data with "%s" handler', \get_class($handler));
262262
}

Tests/Session/Storage/Handler/PdoSessionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testReadLockedConvertsStreamToString()
156156
$insertStmt = $this->createMock(\PDOStatement::class);
157157

158158
$pdo->prepareResult = function ($statement) use ($selectStmt, $insertStmt) {
159-
return 0 === strpos($statement, 'INSERT') ? $insertStmt : $selectStmt;
159+
return str_starts_with($statement, 'INSERT') ? $insertStmt : $selectStmt;
160160
};
161161

162162
$content = 'foobar';

UrlHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(RequestStack $requestStack, RequestContext $requestC
3131

3232
public function getAbsoluteUrl(string $path): string
3333
{
34-
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
34+
if (str_contains($path, '://') || '//' === substr($path, 0, 2)) {
3535
return $path;
3636
}
3737

@@ -60,7 +60,7 @@ public function getAbsoluteUrl(string $path): string
6060

6161
public function getRelativePath(string $path): string
6262
{
63-
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
63+
if (str_contains($path, '://') || '//' === substr($path, 0, 2)) {
6464
return $path;
6565
}
6666

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=7.2.5",
2020
"symfony/deprecation-contracts": "^2.1",
2121
"symfony/polyfill-mbstring": "~1.1",
22-
"symfony/polyfill-php80": "^1.15"
22+
"symfony/polyfill-php80": "^1.16"
2323
},
2424
"require-dev": {
2525
"predis/predis": "~1.0",

0 commit comments

Comments
 (0)