Skip to content

Commit

Permalink
Leverage str_contains/str_starts_with
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
derrabus authored and nicolas-grekas committed Jul 21, 2021
1 parent 21336db commit 882946c
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function getScript($request)

$requires = '';
foreach (get_declared_classes() as $class) {
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
if (str_starts_with($class, 'ComposerAutoloaderInit')) {
$r = new \ReflectionClass($class);
$file = \dirname($r->getFileName(), 2).'/autoload.php';
if (file_exists($file)) {
Expand Down
4 changes: 2 additions & 2 deletions Config/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public function locate($file, $currentPath = null, $first = true)
// no need to trigger deprecations when the loaded file is given as absolute path
foreach ($this->paths as $deprecatedPath) {
foreach ((array) $locations as $location) {
if (null !== $currentPath && 0 === strpos($location, $currentPath)) {
if (null !== $currentPath && str_starts_with($location, $currentPath)) {
return $locations;
}

if (0 === strpos($location, $deprecatedPath) && (null === $currentPath || false === strpos($location, $currentPath))) {
if (str_starts_with($location, $deprecatedPath) && (null === $currentPath || !str_contains($location, $currentPath))) {
$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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Controller/ControllerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getController(Request $request)
*/
protected function createController($controller)
{
if (false === strpos($controller, '::')) {
if (!str_contains($controller, '::')) {
$controller = $this->instantiateController($controller);

if (!\is_callable($controller)) {
Expand Down Expand Up @@ -154,7 +154,7 @@ protected function instantiateController($class)
private function getControllerError($callable): string
{
if (\is_string($callable)) {
if (false !== strpos($callable, '::')) {
if (str_contains($callable, '::')) {
$callable = explode('::', $callable, 2);
} else {
return sprintf('Function "%s" does not exist.', $callable);
Expand Down Expand Up @@ -195,7 +195,7 @@ private function getControllerError($callable): string
foreach ($collection as $item) {
$lev = levenshtein($method, $item);

if ($lev <= \strlen($method) / 3 || false !== strpos($item, $method)) {
if ($lev <= \strlen($method) / 3 || str_contains($item, $method)) {
$alternatives[] = $item;
}
}
Expand Down
4 changes: 2 additions & 2 deletions DataCollector/DumpDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ public function collect(Request $request, Response $response/*, \Throwable $exce
if (!$this->requestStack
|| !$response->headers->has('X-Debug-Token')
|| $response->isRedirection()
|| ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
|| 'html' !== $request->getRequestFormat()
|| false === strripos($response->getContent(), '</body>')
) {
if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
$dumper = new HtmlDumper('php://output', $this->charset);
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
} else {
Expand Down
4 changes: 2 additions & 2 deletions DataCollector/MemoryDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ private function convertToBytes(string $memoryLimit)

$memoryLimit = strtolower($memoryLimit);
$max = strtolower(ltrim($memoryLimit, '+'));
if (0 === strpos($max, '0x')) {
if (str_starts_with($max, '0x')) {
$max = \intval($max, 16);
} elseif (0 === strpos($max, '0')) {
} elseif (str_starts_with($max, '0')) {
$max = \intval($max, 8);
} else {
$max = (int) $max;
Expand Down
4 changes: 2 additions & 2 deletions DataCollector/RequestDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function getName()
*/
protected function parseController($controller)
{
if (\is_string($controller) && false !== strpos($controller, '::')) {
if (\is_string($controller) && str_contains($controller, '::')) {
$controller = explode('::', $controller);
}

Expand Down Expand Up @@ -431,7 +431,7 @@ protected function parseController($controller)
'line' => $r->getStartLine(),
];

if (false !== strpos($r->name, '{closure}')) {
if (str_contains($r->name, '{closure}')) {
return $controller;
}
$controller['method'] = $r->name;
Expand Down
2 changes: 1 addition & 1 deletion Debug/FileLinkFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function format($file, $line)
{
if ($fmt = $this->getFileLinkFormat()) {
for ($i = 1; isset($fmt[$i]); ++$i) {
if (0 === strpos($file, $k = $fmt[$i++])) {
if (str_starts_with($file, $k = $fmt[$i++])) {
$file = substr_replace($file, $fmt[$i], 0, \strlen($k));
break;
}
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/AddAnnotatedClassesToCachePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function expandClasses(array $patterns, array $classes): array

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

private function matchAnyRegexps(string $class, array $regexps): bool
{
$isTest = false !== strpos($class, 'Test');
$isTest = str_contains($class, 'Test');

foreach ($regexps as $regex) {
if ($isTest && false === strpos($regex, 'Test')) {
if ($isTest && !str_contains($regex, 'Test')) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion EventListener/AbstractTestSessionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function onKernelResponse(FilterResponseEvent $event)
if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
$params = session_get_cookie_params() + ['samesite' => null];
foreach ($this->sessionOptions as $k => $v) {
if (0 === strpos($k, 'cookie_')) {
if (str_starts_with($k, 'cookie_')) {
$params[substr($k, 7)] = $v;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Exception/ControllerDoesNotReturnResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(string $message, callable $controller, string $file,

private function parseControllerDefinition(callable $controller): ?array
{
if (\is_string($controller) && false !== strpos($controller, '::')) {
if (\is_string($controller) && str_contains($controller, '::')) {
$controller = explode('::', $controller);
}

Expand Down
2 changes: 1 addition & 1 deletion HttpCache/AbstractSurrogate.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function hasSurrogateCapability(Request $request)
return false;
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion HttpCache/Esi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getName()
*/
public function addSurrogateControl(Response $response)
{
if (false !== strpos($response->getContent(), '<esi:include')) {
if (str_contains($response->getContent(), '<esi:include')) {
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
}
}
Expand Down
2 changes: 1 addition & 1 deletion HttpCache/Ssi.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getName()
*/
public function addSurrogateControl(Response $response)
{
if (false !== strpos($response->getContent(), '<!--#include')) {
if (str_contains($response->getContent(), '<!--#include')) {
$response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
}
}
Expand Down
8 changes: 4 additions & 4 deletions Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,17 @@ public function locateResource($name/*, $dir = null, $first = true, $triggerDepr
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
}

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

$bundleName = substr($name, 1);
$path = '';
if (false !== strpos($bundleName, '/')) {
if (str_contains($bundleName, '/')) {
[$bundleName, $path] = explode('/', $bundleName, 2);
}

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

if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
Expand Down
2 changes: 1 addition & 1 deletion Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function log($level, $message, array $context = [])

private function format(string $level, string $message, array $context, bool $prefixDate = true): string
{
if (false !== strpos($message, '{')) {
if (str_contains($message, '{')) {
$replacements = [];
foreach ($context as $key => $val) {
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
Expand Down
4 changes: 2 additions & 2 deletions Profiler/FileProfilerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FileProfilerStorage implements ProfilerStorageInterface
*/
public function __construct(string $dsn)
{
if (0 !== strpos($dsn, 'file:')) {
if (!str_starts_with($dsn, 'file:')) {
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));
}
$this->folder = substr($dsn, 5);
Expand Down Expand Up @@ -64,7 +64,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null, $st
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
$csvTime = (int) $csvTime;

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

Expand Down
2 changes: 1 addition & 1 deletion Tests/EventListener/RouterListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHtt

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

public function getPortData()
Expand Down

0 comments on commit 882946c

Please sign in to comment.