Skip to content

Commit

Permalink
minor #3884 Use PHP 8.0 functions with polyfill (GromNaN)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Use PHP 8.0 functions with polyfill

Backport some changes from #3881 into 3.x using `symfony/polyfill-php80`.

- `str_starts_with`
- `str_ends_with`
- `str_contains`
- `get_debug_type`

Benefits:
- Less conflicts when both 3.x and 4.x branches will be maintained.
- Performance gain for PHP 8.0+ ([83,7% of installs](https://packagist.org/packages/twig/twig/php-stats#3))

Commits
-------

1d5c092 Use PHP 8.0 functions with polyfill
  • Loading branch information
fabpot committed Oct 20, 2023
2 parents 6f62291 + 1d5c092 commit f1b5227
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 19 deletions.
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 @@ -925,7 +925,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 @@ -1571,13 +1571,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

0 comments on commit f1b5227

Please sign in to comment.