Skip to content

Commit afa486c

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent 522ad92 commit afa486c

16 files changed

+42
-42
lines changed

ApacheRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function prepareBaseUrl()
3737
{
3838
$baseUrl = $this->server->get('SCRIPT_NAME');
3939

40-
if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
40+
if (!str_contains($this->server->get('REQUEST_URI'), $baseUrl)) {
4141
// assume mod_rewrite
4242
return rtrim(\dirname($baseUrl), '/\\');
4343
}

BinaryFileResponse.php

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

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

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

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

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

File/MimeType/FileBinaryMimeTypeGuesser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function guess($path)
8585
ob_start();
8686

8787
// need to use --mime instead of -i. see #6641
88-
passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return);
88+
passthru(sprintf($this->cmd, escapeshellarg((str_starts_with($path, '-') ? './' : '').$path)), $return);
8989
if ($return > 0) {
9090
ob_end_clean();
9191

File/UploadedFile.php

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

265265
$max = ltrim($size, '+');
266-
if (0 === strpos($max, '0x')) {
266+
if (str_starts_with($max, '0x')) {
267267
$max = \intval($max, 16);
268-
} elseif (0 === strpos($max, '0')) {
268+
} elseif (str_starts_with($max, '0')) {
269269
$max = \intval($max, 8);
270270
} else {
271271
$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
@@ -72,7 +72,7 @@ public static function checkIp4($requestIp, $ip)
7272
return self::$checkedIps[$cacheKey] = false;
7373
}
7474

75-
if (false !== strpos($ip, '/')) {
75+
if (str_contains($ip, '/')) {
7676
[$address, $netmask] = explode('/', $ip, 2);
7777

7878
if ('0' === $netmask) {
@@ -120,7 +120,7 @@ public static function checkIp6($requestIp, $ip)
120120
throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
121121
}
122122

123-
if (false !== strpos($ip, '/')) {
123+
if (str_contains($ip, '/')) {
124124
[$address, $netmask] = explode('/', $ip, 2);
125125

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

JsonResponse.php

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

Request.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static function createFromGlobals()
292292
{
293293
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
294294

295-
if (0 === strpos($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
295+
if (str_starts_with($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
296296
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
297297
) {
298298
parse_str($request->getContent(), $data);
@@ -1647,7 +1647,7 @@ public function getLanguages()
16471647
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
16481648
$this->languages = [];
16491649
foreach ($languages as $lang => $acceptHeaderItem) {
1650-
if (false !== strpos($lang, '-')) {
1650+
if (str_contains($lang, '-')) {
16511651
$codes = explode('-', $lang);
16521652
if ('i' === $codes[0]) {
16531653
// Language not listed in ISO 639 that are not variants
@@ -1949,7 +1949,7 @@ private function setPhpDefaultLocale(string $locale): void
19491949
*/
19501950
private function getUrlencodedPrefix(string $string, string $prefix): ?string
19511951
{
1952-
if (0 !== strpos(rawurldecode($string), $prefix)) {
1952+
if (!str_starts_with(rawurldecode($string), $prefix)) {
19531953
return null;
19541954
}
19551955

@@ -2057,7 +2057,7 @@ private function normalizeAndFilterClientIps(array $clientIps, string $ip): arra
20572057
if ($i) {
20582058
$clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
20592059
}
2060-
} elseif (0 === strpos($clientIp, '[')) {
2060+
} elseif (str_starts_with($clientIp, '[')) {
20612061
// Strip brackets and :port from IPv6 addresses.
20622062
$i = strpos($clientIp, ']', 1);
20632063
$clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);

Response.php

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

313313
// Check if we need to send extra expire info headers
314-
if ('1.0' == $this->getProtocolVersion() && false !== strpos($headers->get('Cache-Control'), 'no-cache')) {
314+
if ('1.0' == $this->getProtocolVersion() && str_contains($headers->get('Cache-Control'), 'no-cache')) {
315315
$headers->set('pragma', 'no-cache');
316316
$headers->set('expires', -1);
317317
}
@@ -921,7 +921,7 @@ public function setEtag(string $etag = null, bool $weak = false)
921921
if (null === $etag) {
922922
$this->headers->remove('Etag');
923923
} else {
924-
if (0 !== strpos($etag, '"')) {
924+
if (!str_starts_with($etag, '"')) {
925925
$etag = '"'.$etag.'"';
926926
}
927927

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
@@ -102,7 +102,7 @@ public function remove($name)
102102
protected function &resolveAttributePath($name, $writeContext = false)
103103
{
104104
$array = &$this->attributes;
105-
$name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
105+
$name = (str_starts_with($name, $this->namespaceCharacter)) ? substr($name, 1) : $name;
106106

107107
// Check if there is anything to do, else return
108108
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($sessionId, $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

@@ -661,7 +661,7 @@ protected function doRead($sessionId)
661661
} catch (\PDOException $e) {
662662
// Catch duplicate key error because other connection created the session already.
663663
// It would only not be the case when the other connection destroyed the session.
664-
if (0 === strpos($e->getCode(), '23')) {
664+
if (str_starts_with($e->getCode(), '23')) {
665665
// Retrieve finished session data written by concurrent connection by restarting the loop.
666666
// We have to start a new transaction as a failed query will mark the current transaction as
667667
// 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_class($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

0 commit comments

Comments
 (0)