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 e72a900 commit 9979144
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Formatter/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" => '\\', '\\<' => '<']);
}

Expand Down
6 changes: 3 additions & 3 deletions Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Input/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Logger/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 9979144

Please sign in to comment.