Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PHP 8.0 functions with polyfill #3884

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/polyfill-php80": "^1.22",
"symfony/polyfill-mbstring": "^1.3",
"symfony/polyfill-ctype": "^1.8"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ private function updateRepr(): void
}

$dot = false;
if ('.' === substr($this->message, -1)) {
if (str_ends_with($this->message, '.')) {
$this->message = substr($this->message, 0, -1);
$dot = true;
}

$questionMark = false;
if ('?' === substr($this->message, -1)) {
if (str_ends_with($this->message, '?')) {
$this->message = substr($this->message, 0, -1);
$questionMark = true;
}
Expand Down 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
2 changes: 1 addition & 1 deletion src/FileExtensionEscapingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function guess(string $name)
return 'html'; // return html for directories
}

if ('.twig' === substr($name, -5)) {
if (str_ends_with($name, '.twig')) {
$name = substr($name, 0, -5);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,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
6 changes: 3 additions & 3 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,13 +297,13 @@ 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'];
}

if ($object = $r->getClosureThis()) {
$callable = [$object, $r->name];
$callableName = (\function_exists('get_debug_type') ? get_debug_type($object) : \get_class($object)).'::'.$r->name;
$callableName = get_debug_type($object).'::'.$r->name;
} elseif (\PHP_VERSION_ID >= 80111 && $class = $r->getClosureCalledClass()) {
$callableName = $class->name.'::'.$r->name;
} elseif (\PHP_VERSION_ID < 80111 && $class = $r->getClosureScopeClass()) {
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