Skip to content

Commit

Permalink
Use str_* when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 8, 2023
1 parent 45b215a commit 0cc8699
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function dataUri(string $data, string $mime = null, array $parameters = [
$repr .= ';'.$key.'='.rawurlencode($value);
}

if (0 === strpos($mime, 'text/')) {
if (str_starts_with($mime, 'text/')) {
$repr .= ','.rawurlencode($data);
} else {
$repr .= ';base64,'.base64_encode($data);
Expand Down
2 changes: 1 addition & 1 deletion src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function guessTemplateInfo(): void
foreach ($backtrace as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Template) {
$currentClass = \get_class($trace['object']);
$isEmbedContainer = null === $templateClass ? false : 0 === strpos($templateClass, $currentClass);
$isEmbedContainer = null === $templateClass ? false : str_starts_with($templateClass, $currentClass);
if (null === $this->name || ($this->name == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
$template = $trace['object'];
$templateClass = \get_class($trace['object']);
Expand Down
2 changes: 1 addition & 1 deletion src/Error/SyntaxError.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function addSuggestions(string $name, array $items): void
$alternatives = [];
foreach ($items 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] = $lev;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ function twig_in_filter($value, $compare)

if (\is_string($compare)) {
if (\is_string($value) || \is_int($value) || \is_float($value)) {
return '' === $value || false !== strpos($compare, (string) $value);
return '' === $value || str_contains($compare, (string) $value);
}

return false;
Expand Down Expand Up @@ -1570,13 +1570,13 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar
$classCache[$method] = $method;
$classCache[$lcName = $lcMethods[$i]] = $method;

if ('g' === $lcName[0] && 0 === strpos($lcName, 'get')) {
if ('g' === $lcName[0] && str_starts_with($lcName, 'get')) {
$name = substr($method, 3);
$lcName = substr($lcName, 3);
} elseif ('i' === $lcName[0] && 0 === strpos($lcName, 'is')) {
} elseif ('i' === $lcName[0] && str_starts_with($lcName, 'is')) {
$name = substr($method, 2);
$lcName = substr($lcName, 2);
} elseif ('h' === $lcName[0] && 0 === strpos($lcName, 'has')) {
} elseif ('h' === $lcName[0] && str_starts_with($lcName, 'has')) {
$name = substr($method, 3);
$lcName = substr($lcName, 3);
if (\in_array('is'.$lcName, $lcMethods)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ private function lexExpression(): void
$this->moveCursor($match[0]);
}
// punctuation
elseif (false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) {
elseif (str_contains(self::PUNCTUATION, $this->code[$this->cursor])) {
// opening bracket
if (false !== strpos('([{', $this->code[$this->cursor])) {
if (str_contains('([{', $this->code[$this->cursor])) {
$this->brackets[] = [$this->code[$this->cursor], $this->lineno];
}
// closing bracket
elseif (false !== strpos(')]}', $this->code[$this->cursor])) {
elseif (str_contains(')]}', $this->code[$this->cursor])) {
if (empty($this->brackets)) {
throw new SyntaxError(sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private function parseName(string $name, string $default = self::MAIN_NAMESPACE)

private function validateName(string $name): void
{
if (false !== strpos($name, "\0")) {
if (str_contains($name, "\0")) {
throw new LoaderError('A template name cannot contain NUL bytes.');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Node/Expression/CallExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function compileCallable(Compiler $compiler)
{
$callable = $this->getAttribute('callable');

if (\is_string($callable) && false === strpos($callable, '::')) {
if (\is_string($callable) && !str_contains($callable, '::')) {
$compiler->raw($callable);
} else {
[$r, $callable] = $this->reflectCallable($callable);
Expand Down Expand Up @@ -297,7 +297,7 @@ private function reflectCallable($callable)
}
$r = new \ReflectionFunction($closure);

if (false !== strpos($r->name, '{closure}')) {
if (str_contains($r->name, '{closure}')) {
return $this->reflector = [$r, $callable, 'Closure'];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private function filterBodyNodes(Node $node, bool $nested = false): ?Node
($node instanceof TextNode && !ctype_space($node->getAttribute('data')))
|| (!$node instanceof TextNode && !$node instanceof BlockReferenceNode && $node instanceof NodeOutputInterface)
) {
if (false !== strpos((string) $node, \chr(0xEF).\chr(0xBB).\chr(0xBF))) {
if (str_contains((string) $node, \chr(0xEF).\chr(0xBB).\chr(0xBF))) {
$t = substr($node->getAttribute('data'), 3);
if ('' === $t || ctype_space($t)) {
// bypass empty nodes starting with a BOM
Expand Down
2 changes: 1 addition & 1 deletion src/Profiler/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $template = 'main', string $type = self::ROOT
{
$this->template = $template;
$this->type = $type;
$this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name;
$this->name = str_starts_with($name, '__internal_') ? 'INTERNAL' : $name;
$this->enter();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Test/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getTests($name, $legacyTests = false)
continue;
}

if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
if ($legacyTests xor str_contains($file->getRealpath(), '.legacy.test')) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function br()

public function is_multi_word($value)
{
return false !== strpos($value, ' ');
return str_contains($value, ' ');
}

public function __call($method, $arguments)
Expand Down

0 comments on commit 0cc8699

Please sign in to comment.