From 9979144038a91c3a998192997529233f803e783f Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 7 Jun 2021 01:15:42 +0200 Subject: [PATCH] Leverage str_contains/str_starts_with Signed-off-by: Alexander M. Turek --- Application.php | 6 +++--- Command/Command.php | 2 +- Formatter/OutputFormatter.php | 2 +- Helper/QuestionHelper.php | 6 +++--- Input/ArgvInput.php | 12 ++++++------ Input/ArrayInput.php | 4 ++-- Input/InputOption.php | 2 +- Logger/ConsoleLogger.php | 2 +- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Application.php b/Application.php index ed6d00190..15d537dac 100644 --- a/Application.php +++ b/Application.php @@ -877,7 +877,7 @@ private function doActuallyRenderThrowable(\Throwable $e, OutputInterface $outpu $len = 0; } - if (false !== strpos($message, "@anonymous\0")) { + if (str_contains($message, "@anonymous\0")) { $message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) { return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0]; }, $message); @@ -1155,7 +1155,7 @@ private function findAlternatives(string $name, iterable $collection): array } $lev = levenshtein($subname, $parts[$i]); - if ($lev <= \strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) { + if ($lev <= \strlen($subname) / 3 || '' !== $subname && str_contains($parts[$i], $subname)) { $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev; } elseif ($exists) { $alternatives[$collectionName] += $threshold; @@ -1165,7 +1165,7 @@ private function findAlternatives(string $name, iterable $collection): array foreach ($collection as $item) { $lev = levenshtein($name, $item); - if ($lev <= \strlen($name) / 3 || false !== strpos($item, $name)) { + if ($lev <= \strlen($name) / 3 || str_contains($item, $name)) { $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev; } } diff --git a/Command/Command.php b/Command/Command.php index 1e109b194..da9b9f6af 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -613,7 +613,7 @@ public function getSynopsis($short = false) */ public function addUsage($usage) { - if (0 !== strpos($usage, $this->name)) { + if (!str_starts_with($usage, $this->name)) { $usage = sprintf('%s %s', $this->name, $usage); } diff --git a/Formatter/OutputFormatter.php b/Formatter/OutputFormatter.php index b0355d811..0f969c7ad 100644 --- a/Formatter/OutputFormatter.php +++ b/Formatter/OutputFormatter.php @@ -180,7 +180,7 @@ public function formatAndWrap(string $message, int $width) $output .= $this->applyCurrentStyle(substr($message, $offset), $output, $width, $currentLineLength); - if (false !== strpos($output, "\0")) { + if (str_contains($output, "\0")) { return strtr($output, ["\0" => '\\', '\\<' => '<']); } diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index 5c3ecc4e5..089de76bd 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -311,7 +311,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu $matches = array_filter( $autocomplete($ret), function ($match) use ($ret) { - return '' === $ret || 0 === strpos($match, $ret); + return '' === $ret || str_starts_with($match, $ret); } ); $numMatches = \count($matches); @@ -348,7 +348,7 @@ function ($match) use ($ret) { foreach ($autocomplete($ret) as $value) { // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle) - if (0 === strpos($value, $tempRet)) { + if (str_starts_with($value, $tempRet)) { $matches[$numMatches++] = $value; } } @@ -377,7 +377,7 @@ function ($match) use ($ret) { private function mostRecentlyEnteredValue(string $entered): string { // Determine the most recent value that the user entered - if (false === strpos($entered, ',')) { + if (!str_contains($entered, ',')) { return $entered; } diff --git a/Input/ArgvInput.php b/Input/ArgvInput.php index 6d2946926..b63529509 100644 --- a/Input/ArgvInput.php +++ b/Input/ArgvInput.php @@ -75,7 +75,7 @@ protected function parse() $this->parseArgument($token); } elseif ($parseOptions && '--' == $token) { $parseOptions = false; - } elseif ($parseOptions && 0 === strpos($token, '--')) { + } elseif ($parseOptions && str_starts_with($token, '--')) { $this->parseLongOption($token); } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { $this->parseShortOption($token); @@ -243,7 +243,7 @@ public function getFirstArgument() $isOption = false; foreach ($this->tokens as $i => $token) { if ($token && '-' === $token[0]) { - if (false !== strpos($token, '=') || !isset($this->tokens[$i + 1])) { + if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { continue; } @@ -285,8 +285,8 @@ public function hasParameterOption($values, $onlyParams = false) // Options with values: // For long options, test for '--option=' at beginning // For short options, test for '-o' at beginning - $leading = 0 === strpos($value, '--') ? $value.'=' : $value; - if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) { + $leading = str_starts_with($value, '--') ? $value.'=' : $value; + if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { return true; } } @@ -316,8 +316,8 @@ public function getParameterOption($values, $default = false, $onlyParams = fals // Options with values: // For long options, test for '--option=' at beginning // For short options, test for '-o' at beginning - $leading = 0 === strpos($value, '--') ? $value.'=' : $value; - if ('' !== $leading && 0 === strpos($token, $leading)) { + $leading = str_starts_with($value, '--') ? $value.'=' : $value; + if ('' !== $leading && str_starts_with($token, $leading)) { return substr($token, \strlen($leading)); } } diff --git a/Input/ArrayInput.php b/Input/ArrayInput.php index bf9a8a455..30bd2054a 100644 --- a/Input/ArrayInput.php +++ b/Input/ArrayInput.php @@ -133,9 +133,9 @@ protected function parse() if ('--' === $key) { return; } - if (0 === strpos($key, '--')) { + if (str_starts_with($key, '--')) { $this->addLongOption(substr($key, 2), $value); - } elseif (0 === strpos($key, '-')) { + } elseif (str_starts_with($key, '-')) { $this->addShortOption(substr($key, 1), $value); } else { $this->addArgument($key, $value); diff --git a/Input/InputOption.php b/Input/InputOption.php index 710e9a809..c40e0da34 100644 --- a/Input/InputOption.php +++ b/Input/InputOption.php @@ -56,7 +56,7 @@ class InputOption */ public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) { - if (0 === strpos($name, '--')) { + if (str_starts_with($name, '--')) { $name = substr($name, 2); } diff --git a/Logger/ConsoleLogger.php b/Logger/ConsoleLogger.php index 4a0315656..c9ee03561 100644 --- a/Logger/ConsoleLogger.php +++ b/Logger/ConsoleLogger.php @@ -104,7 +104,7 @@ public function hasErrored() */ private function interpolate(string $message, array $context): string { - if (false === strpos($message, '{')) { + if (!str_contains($message, '{')) { return $message; }