Skip to content

Commit

Permalink
minor twigphp#3933 Remove obsolete code (fabpot)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Remove obsolete code

Commits
-------

b9cf29d Remove obsolete code
583d7b2 Remove hack to inline throwing an exception
3d5ad9a Remove obsolete code
3472ae1 Simplify code
  • Loading branch information
fabpot committed Dec 9, 2023
2 parents 15dadf9 + b9cf29d commit 67c8798
Show file tree
Hide file tree
Showing 23 changed files with 44 additions and 221 deletions.
6 changes: 6 additions & 0 deletions extra/intl-extra/Tests/Fixtures/format_date.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
{{ '2019-08-07 23:39:12'|format_date }}
{{ '2019-08-07 23:39:12'|format_date(locale='fr') }}
{{ '2019-08-07 23:39:12'|format_time }}

{{ 'today 23:39:12'|format_datetime('relative_short', 'none', locale='fr') }}
{{ 'today 23:39:12'|format_datetime('relative_full', 'full', locale='fr') }}
--DATA--
return [];
--EXPECT--
Expand All @@ -24,3 +27,6 @@ mercredi 7 août 2019 à 23:39:12 temps universel coordonné
Aug 7, 2019
7 août 2019
11:39:12 PM

aujourd’hui
aujourd’hui à 23:39:12 temps universel coordonné
12 changes: 0 additions & 12 deletions extra/intl-extra/Tests/Fixtures/format_date_php8.test

This file was deleted.

1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</testsuites>
<php>
<ini name="error_reporting" value="-1" />
<ini name="xdebug.mode" value="off" />
<ini name="xdebug.overload_var_dump" value="0" />
</php>
<listeners>
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(string $directory, int $options = 0)

public function generateKey(string $name, string $className): string
{
$hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $className);
$hash = hash('xxh128', $className);

return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function getTemplateClass(string $name, int $index = null): string
{
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;

return $this->templateClassPrefix.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $key).(null === $index ? '' : '___'.$index);
return $this->templateClassPrefix.hash('xxh128', $key).(null === $index ? '' : '___'.$index);
}

/**
Expand Down Expand Up @@ -384,7 +384,7 @@ public function loadTemplate(string $cls, string $name, int $index = null): Temp
*/
public function createTemplate(string $template, string $name = null): TemplateWrapper
{
$hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $template, false);
$hash = hash('xxh128', $template, false);
if (null !== $name) {
$name = sprintf('%s (string template %s)', $name, $hash);
} else {
Expand Down
49 changes: 24 additions & 25 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,45 @@ private function initialize()

$this->isInitialized = true;

// when PHP 7.3 is the min version, we will be able to remove the '#' part in preg_quote as it's part of the default
$this->regexes = [
// }}
'lex_var' => '{
\s*
(?:'.
preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1], '#').'\s*'. // -}}\s*
preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1]).'\s*'. // -}}\s*
'|'.
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_variable'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~}}[ \t\0\x0B]*
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_variable'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~}}[ \t\0\x0B]*
'|'.
preg_quote($this->options['tag_variable'][1], '#'). // }}
preg_quote($this->options['tag_variable'][1]). // }}
')
}Ax',

// %}
'lex_block' => '{
\s*
(?:'.
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*\n?'. // -%}\s*\n?
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*\n?'. // -%}\s*\n?
'|'.
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
'|'.
preg_quote($this->options['tag_block'][1], '#').'\n?'. // %}\n?
preg_quote($this->options['tag_block'][1]).'\n?'. // %}\n?
')
}Ax',

// {% endverbatim %}
'lex_raw_data' => '{'.
preg_quote($this->options['tag_block'][0], '#'). // {%
preg_quote($this->options['tag_block'][0]). // {%
'('.
$this->options['whitespace_trim']. // -
'|'.
$this->options['whitespace_line_trim']. // ~
')?\s*endverbatim\s*'.
'(?:'.
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'. // -%}
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*'. // -%}
'|'.
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
'|'.
preg_quote($this->options['tag_block'][1], '#'). // %}
preg_quote($this->options['tag_block'][1]). // %}
')
}sx',

Expand All @@ -121,44 +120,44 @@ private function initialize()
// #}
'lex_comment' => '{
(?:'.
preg_quote($this->options['whitespace_trim'].$this->options['tag_comment'][1], '#').'\s*\n?'. // -#}\s*\n?
preg_quote($this->options['whitespace_trim'].$this->options['tag_comment'][1]).'\s*\n?'. // -#}\s*\n?
'|'.
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_comment'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~#}[ \t\0\x0B]*
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_comment'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~#}[ \t\0\x0B]*
'|'.
preg_quote($this->options['tag_comment'][1], '#').'\n?'. // #}\n?
preg_quote($this->options['tag_comment'][1]).'\n?'. // #}\n?
')
}sx',

// verbatim %}
'lex_block_raw' => '{
\s*verbatim\s*
(?:'.
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'. // -%}\s*
preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*'. // -%}\s*
'|'.
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]*
'|'.
preg_quote($this->options['tag_block'][1], '#'). // %}
preg_quote($this->options['tag_block'][1]). // %}
')
}Asx',

'lex_block_line' => '{\s*line\s+(\d+)\s*'.preg_quote($this->options['tag_block'][1], '#').'}As',
'lex_block_line' => '{\s*line\s+(\d+)\s*'.preg_quote($this->options['tag_block'][1]).'}As',

// {{ or {% or {#
'lex_tokens_start' => '{
('.
preg_quote($this->options['tag_variable'][0], '#'). // {{
preg_quote($this->options['tag_variable'][0]). // {{
'|'.
preg_quote($this->options['tag_block'][0], '#'). // {%
preg_quote($this->options['tag_block'][0]). // {%
'|'.
preg_quote($this->options['tag_comment'][0], '#'). // {#
preg_quote($this->options['tag_comment'][0]). // {#
')('.
preg_quote($this->options['whitespace_trim'], '#'). // -
preg_quote($this->options['whitespace_trim']). // -
'|'.
preg_quote($this->options['whitespace_line_trim'], '#'). // ~
preg_quote($this->options['whitespace_line_trim']). // ~
')?
}sx',
'interpolation_start' => '{'.preg_quote($this->options['interpolation'][0], '#').'\s*}A',
'interpolation_end' => '{\s*'.preg_quote($this->options['interpolation'][1], '#').'}A',
'interpolation_start' => '{'.preg_quote($this->options['interpolation'][0]).'\s*}A',
'interpolation_end' => '{\s*'.preg_quote($this->options['interpolation'][1]).'}A',
];
}

Expand Down
27 changes: 1 addition & 26 deletions src/Node/Expression/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public function addElement(AbstractExpression $value, AbstractExpression $key =
public function compile(Compiler $compiler): void
{
$keyValuePairs = $this->getKeyValuePairs();
$needsArrayMergeSpread = \PHP_VERSION_ID < 80100 && $this->hasSpreadItem($keyValuePairs);

if ($needsArrayMergeSpread) {
$compiler->raw('twig_array_merge(');
}
$compiler->raw('[');
$first = true;
$reopenAfterMergeSpread = false;
Expand All @@ -82,18 +77,12 @@ public function compile(Compiler $compiler): void
$reopenAfterMergeSpread = false;
}

if ($needsArrayMergeSpread && $pair['value']->hasAttribute('spread')) {
$compiler->raw('], ')->subcompile($pair['value']);
$first = true;
$reopenAfterMergeSpread = true;
continue;
}
if (!$first) {
$compiler->raw(', ');
}
$first = false;

if ($pair['value']->hasAttribute('spread') && !$needsArrayMergeSpread) {
if ($pair['value']->hasAttribute('spread')) {
$compiler->raw('...')->subcompile($pair['value']);
++$nextIndex;
} else {
Expand All @@ -117,19 +106,5 @@ public function compile(Compiler $compiler): void
if (!$reopenAfterMergeSpread) {
$compiler->raw(']');
}
if ($needsArrayMergeSpread) {
$compiler->raw(')');
}
}

private function hasSpreadItem(array $pairs): bool
{
foreach ($pairs as $pair) {
if ($pair['value']->hasAttribute('spread')) {
return true;
}
}

return false;
}
}
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/EqualBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class EqualBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(0 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('==');
Expand Down
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/GreaterBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class GreaterBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('>');
Expand Down
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/GreaterEqualBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class GreaterEqualBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(0 <= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('>=');
Expand Down
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/LessBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class LessBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(-1 === twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('<');
Expand Down
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/LessEqualBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class LessEqualBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(0 >= twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('<=');
Expand Down
17 changes: 0 additions & 17 deletions src/Node/Expression/Binary/NotEqualBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@

class NotEqualBinary extends AbstractBinary
{
public function compile(Compiler $compiler): void
{
if (\PHP_VERSION_ID >= 80000) {
parent::compile($compiler);

return;
}

$compiler
->raw('(0 !== twig_compare(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw('))')
;
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('!=');
Expand Down
6 changes: 2 additions & 4 deletions src/Node/Expression/CallExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function getArguments($callable, $arguments)
$pos = 0;
foreach ($callableParameters as $callableParameter) {
$name = $this->normalizeName($callableParameter->name);
if (\PHP_VERSION_ID >= 80000 && 'range' === $callable) {
if ('range' === $callable) {
if ('start' === $name) {
$name = 'low';
} elseif ('end' === $name) {
Expand Down Expand Up @@ -304,10 +304,8 @@ private function reflectCallable($callable)
if ($object = $r->getClosureThis()) {
$callable = [$object, $r->name];
$callableName = get_debug_type($object).'::'.$r->name;
} elseif (\PHP_VERSION_ID >= 80111 && $class = $r->getClosureCalledClass()) {
} elseif ($class = $r->getClosureCalledClass()) {
$callableName = $class->name.'::'.$r->name;
} elseif (\PHP_VERSION_ID < 80111 && $class = $r->getClosureScopeClass()) {
$callableName = (\is_array($callable) ? $callable[0] : $class->name).'::'.$r->name;
} else {
$callable = $callableName = $r->name;
}
Expand Down
Loading

0 comments on commit 67c8798

Please sign in to comment.