Skip to content

Commit 882946c

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 21336db commit 882946c

17 files changed

+29
-29
lines changed

Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function getScript($request)
8585

8686
$requires = '';
8787
foreach (get_declared_classes() as $class) {
88-
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
88+
if (str_starts_with($class, 'ComposerAutoloaderInit')) {
8989
$r = new \ReflectionClass($class);
9090
$file = \dirname($r->getFileName(), 2).'/autoload.php';
9191
if (file_exists($file)) {

Config/FileLocator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function locate($file, $currentPath = null, $first = true)
7070
// no need to trigger deprecations when the loaded file is given as absolute path
7171
foreach ($this->paths as $deprecatedPath) {
7272
foreach ((array) $locations as $location) {
73-
if (null !== $currentPath && 0 === strpos($location, $currentPath)) {
73+
if (null !== $currentPath && str_starts_with($location, $currentPath)) {
7474
return $locations;
7575
}
7676

77-
if (0 === strpos($location, $deprecatedPath) && (null === $currentPath || false === strpos($location, $currentPath))) {
77+
if (str_starts_with($location, $deprecatedPath) && (null === $currentPath || !str_contains($location, $currentPath))) {
7878
$deprecation = sprintf('Loading the file "%s" from the global resource directory "%s" is deprecated since Symfony 4.4 and will be removed in 5.0.', $file, $deprecatedPath);
7979
}
8080
}

Controller/ControllerResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function getController(Request $request)
106106
*/
107107
protected function createController($controller)
108108
{
109-
if (false === strpos($controller, '::')) {
109+
if (!str_contains($controller, '::')) {
110110
$controller = $this->instantiateController($controller);
111111

112112
if (!\is_callable($controller)) {
@@ -154,7 +154,7 @@ protected function instantiateController($class)
154154
private function getControllerError($callable): string
155155
{
156156
if (\is_string($callable)) {
157-
if (false !== strpos($callable, '::')) {
157+
if (str_contains($callable, '::')) {
158158
$callable = explode('::', $callable, 2);
159159
} else {
160160
return sprintf('Function "%s" does not exist.', $callable);
@@ -195,7 +195,7 @@ private function getControllerError($callable): string
195195
foreach ($collection as $item) {
196196
$lev = levenshtein($method, $item);
197197

198-
if ($lev <= \strlen($method) / 3 || false !== strpos($item, $method)) {
198+
if ($lev <= \strlen($method) / 3 || str_contains($item, $method)) {
199199
$alternatives[] = $item;
200200
}
201201
}

DataCollector/DumpDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ public function collect(Request $request, Response $response/*, \Throwable $exce
120120
if (!$this->requestStack
121121
|| !$response->headers->has('X-Debug-Token')
122122
|| $response->isRedirection()
123-
|| ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
123+
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
124124
|| 'html' !== $request->getRequestFormat()
125125
|| false === strripos($response->getContent(), '</body>')
126126
) {
127-
if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
127+
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
128128
$dumper = new HtmlDumper('php://output', $this->charset);
129129
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
130130
} else {

DataCollector/MemoryDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ private function convertToBytes(string $memoryLimit)
104104

105105
$memoryLimit = strtolower($memoryLimit);
106106
$max = strtolower(ltrim($memoryLimit, '+'));
107-
if (0 === strpos($max, '0x')) {
107+
if (str_starts_with($max, '0x')) {
108108
$max = \intval($max, 16);
109-
} elseif (0 === strpos($max, '0')) {
109+
} elseif (str_starts_with($max, '0')) {
110110
$max = \intval($max, 8);
111111
} else {
112112
$max = (int) $max;

DataCollector/RequestDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function getName()
394394
*/
395395
protected function parseController($controller)
396396
{
397-
if (\is_string($controller) && false !== strpos($controller, '::')) {
397+
if (\is_string($controller) && str_contains($controller, '::')) {
398398
$controller = explode('::', $controller);
399399
}
400400

@@ -431,7 +431,7 @@ protected function parseController($controller)
431431
'line' => $r->getStartLine(),
432432
];
433433

434-
if (false !== strpos($r->name, '{closure}')) {
434+
if (str_contains($r->name, '{closure}')) {
435435
return $controller;
436436
}
437437
$controller['method'] = $r->name;

Debug/FileLinkFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function format($file, $line)
5050
{
5151
if ($fmt = $this->getFileLinkFormat()) {
5252
for ($i = 1; isset($fmt[$i]); ++$i) {
53-
if (0 === strpos($file, $k = $fmt[$i++])) {
53+
if (str_starts_with($file, $k = $fmt[$i++])) {
5454
$file = substr_replace($file, $fmt[$i], 0, \strlen($k));
5555
break;
5656
}

DependencyInjection/AddAnnotatedClassesToCachePass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private function expandClasses(array $patterns, array $classes): array
6262

6363
// Explicit classes declared in the patterns are returned directly
6464
foreach ($patterns as $key => $pattern) {
65-
if (!str_ends_with($pattern, '\\') && false === strpos($pattern, '*')) {
65+
if (!str_ends_with($pattern, '\\') && !str_contains($pattern, '*')) {
6666
unset($patterns[$key]);
6767
$expanded[] = ltrim($pattern, '\\');
6868
}
@@ -127,10 +127,10 @@ private function patternsToRegexps(array $patterns): array
127127

128128
private function matchAnyRegexps(string $class, array $regexps): bool
129129
{
130-
$isTest = false !== strpos($class, 'Test');
130+
$isTest = str_contains($class, 'Test');
131131

132132
foreach ($regexps as $regex) {
133-
if ($isTest && false === strpos($regex, 'Test')) {
133+
if ($isTest && !str_contains($regex, 'Test')) {
134134
continue;
135135
}
136136

EventListener/AbstractTestSessionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function onKernelResponse(FilterResponseEvent $event)
8181
if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
8282
$params = session_get_cookie_params() + ['samesite' => null];
8383
foreach ($this->sessionOptions as $k => $v) {
84-
if (0 === strpos($k, 'cookie_')) {
84+
if (str_starts_with($k, 'cookie_')) {
8585
$params[substr($k, 7)] = $v;
8686
}
8787
}

Exception/ControllerDoesNotReturnResponseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(string $message, callable $controller, string $file,
3838

3939
private function parseControllerDefinition(callable $controller): ?array
4040
{
41-
if (\is_string($controller) && false !== strpos($controller, '::')) {
41+
if (\is_string($controller) && str_contains($controller, '::')) {
4242
$controller = explode('::', $controller);
4343
}
4444

HttpCache/AbstractSurrogate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function hasSurrogateCapability(Request $request)
5757
return false;
5858
}
5959

60-
return false !== strpos($value, sprintf('%s/1.0', strtoupper($this->getName())));
60+
return str_contains($value, sprintf('%s/1.0', strtoupper($this->getName())));
6161
}
6262

6363
/**

HttpCache/Esi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getName()
3737
*/
3838
public function addSurrogateControl(Response $response)
3939
{
40-
if (false !== strpos($response->getContent(), '<esi:include')) {
40+
if (str_contains($response->getContent(), '<esi:include')) {
4141
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
4242
}
4343
}

HttpCache/Ssi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getName()
3434
*/
3535
public function addSurrogateControl(Response $response)
3636
{
37-
if (false !== strpos($response->getContent(), '<!--#include')) {
37+
if (str_contains($response->getContent(), '<!--#include')) {
3838
$response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
3939
}
4040
}

Kernel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,17 @@ public function locateResource($name/*, $dir = null, $first = true, $triggerDepr
255255
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
256256
}
257257

258-
if (false !== strpos($name, '..')) {
258+
if (str_contains($name, '..')) {
259259
throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
260260
}
261261

262262
$bundleName = substr($name, 1);
263263
$path = '';
264-
if (false !== strpos($bundleName, '/')) {
264+
if (str_contains($bundleName, '/')) {
265265
[$bundleName, $path] = explode('/', $bundleName, 2);
266266
}
267267

268-
$isResource = 0 === strpos($path, 'Resources') && null !== $dir;
268+
$isResource = str_starts_with($path, 'Resources') && null !== $dir;
269269
$overridePath = substr($path, 9);
270270
$bundle = $this->getBundle($bundleName);
271271
$files = [];
@@ -471,7 +471,7 @@ protected function build(ContainerBuilder $container)
471471
protected function getContainerClass()
472472
{
473473
$class = static::class;
474-
$class = false !== strpos($class, "@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
474+
$class = str_contains($class, "@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
475475
$class = $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
476476

477477
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {

Log/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function log($level, $message, array $context = [])
8888

8989
private function format(string $level, string $message, array $context, bool $prefixDate = true): string
9090
{
91-
if (false !== strpos($message, '{')) {
91+
if (str_contains($message, '{')) {
9292
$replacements = [];
9393
foreach ($context as $key => $val) {
9494
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {

Profiler/FileProfilerStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
3434
*/
3535
public function __construct(string $dsn)
3636
{
37-
if (0 !== strpos($dsn, 'file:')) {
37+
if (!str_starts_with($dsn, 'file:')) {
3838
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
3939
}
4040
$this->folder = substr($dsn, 5);
@@ -64,7 +64,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null, $st
6464
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
6565
$csvTime = (int) $csvTime;
6666

67-
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) {
67+
if ($ip && !str_contains($csvIp, $ip) || $url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
6868
continue;
6969
}
7070

Tests/EventListener/RouterListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHtt
6060

6161
$this->assertEquals($expectedHttpPort, $context->getHttpPort());
6262
$this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
63-
$this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
63+
$this->assertEquals(str_starts_with($uri, 'https') ? 'https' : 'http', $context->getScheme());
6464
}
6565

6666
public function getPortData()

0 commit comments

Comments
 (0)