diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index ed740b47187..e2680cd6b92 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -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); diff --git a/src/Error/Error.php b/src/Error/Error.php index bca1fa64c5b..6df67ef2ba5 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -171,11 +171,11 @@ private function guessTemplateInfo(): void $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT); foreach ($backtrace as $trace) { if (isset($trace['object']) && $trace['object'] instanceof Template) { - $currentClass = \get_class($trace['object']); + $currentClass = $trace['object']::class; $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']); + $templateClass = $trace['object']::class; } } } diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 13e0f0876ed..09f4e537b9e 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -623,7 +623,7 @@ public function parseArguments($namedArguments = false, $definition = false, $al $name = null; if ($namedArguments && $token = $stream->nextIf(/* Token::OPERATOR_TYPE */ 8, '=')) { if (!$value instanceof NameExpression) { - throw new SyntaxError(sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext()); + throw new SyntaxError(sprintf('A parameter name must be a string, "%s" given.', $value::class), $token->getLine(), $stream->getSourceContext()); } $name = $value->getAttribute('name'); @@ -704,7 +704,7 @@ private function parseNotTestExpression(Node $node): NotUnary private function parseTestExpression(Node $node): TestExpression { $stream = $this->parser->getStream(); - list($name, $test) = $this->getTest($node->getTemplateLine()); + [$name, $test] = $this->getTest($node->getTemplateLine()); $class = $this->getTestNodeClass($test); $arguments = null; diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 36aa8f10a7f..2c9312e4d51 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -529,7 +529,7 @@ function twig_date_converter(Environment $env, $date = null, $timezone = null) function twig_replace_filter($str, $from) { if (!is_iterable($from)) { - throw new RuntimeError(sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".', \is_object($from) ? \get_class($from) : \gettype($from))); + throw new RuntimeError(sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".', get_debug_type($from))); } return strtr($str ?? '', twig_to_array($from)); @@ -1054,16 +1054,12 @@ function twig_trim_filter($string, $characterMask = null, $side = 'both') $characterMask = " \t\n\r\0\x0B"; } - switch ($side) { - case 'both': - return trim($string ?? '', $characterMask); - case 'left': - return ltrim($string ?? '', $characterMask); - case 'right': - return rtrim($string ?? '', $characterMask); - default: - throw new RuntimeError('Trimming side must be "left", "right" or "both".'); - } + return match ($side) { + 'both' => trim($string ?? '', $characterMask), + 'left' => ltrim($string ?? '', $characterMask), + 'right' => rtrim($string ?? '', $characterMask), + default => throw new RuntimeError('Trimming side must be "left", "right" or "both".'), + }; } /** @@ -1123,7 +1119,7 @@ function twig_length_filter(Environment $env, $thing) return mb_strlen($thing, $env->getCharset()); } - if ($thing instanceof \Countable || \is_array($thing) || $thing instanceof \SimpleXMLElement) { + if (is_countable($thing) || $thing instanceof \SimpleXMLElement) { return \count($thing); } @@ -1384,10 +1380,10 @@ function twig_constant($constant, $object = null) { if (null !== $object) { if ('class' === $constant) { - return \get_class($object); + return $object::class; } - $constant = \get_class($object).'::'.$constant; + $constant = $object::class.'::'.$constant; } if (!\defined($constant)) { @@ -1412,7 +1408,7 @@ function twig_constant_is_defined($constant, $object = null) return true; } - $constant = \get_class($object).'::'.$constant; + $constant = $object::class.'::'.$constant; } return \defined($constant); @@ -1430,7 +1426,7 @@ function twig_constant_is_defined($constant, $object = null) function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) { if (!is_iterable($items)) { - throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', \is_object($items) ? \get_class($items) : \gettype($items))); + throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', get_debug_type($items))); } $size = ceil($size); @@ -1492,9 +1488,9 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar } if ($object instanceof ArrayAccess) { - $message = sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist.', $arrayItem, \get_class($object)); + $message = sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist.', $arrayItem, $object::class); } elseif (\is_object($object)) { - $message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface.', $item, \get_class($object)); + $message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface.', $item, $object::class); } elseif (\is_array($object)) { if (empty($object)) { $message = sprintf('Key "%s" does not exist as the array is empty.', $arrayItem); @@ -1558,14 +1554,14 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar static $cache = []; - $class = \get_class($object); + $class = $object::class; // object method // precedence: getXxx() > isXxx() > hasXxx() if (!isset($cache[$class])) { $methods = get_class_methods($object); sort($methods); - $lcMethods = array_map(function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, $methods); + $lcMethods = array_map(fn($value) => strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $methods); $classCache = []; foreach ($methods as $i => $method) { $classCache[$method] = $method; @@ -1674,7 +1670,7 @@ function twig_array_column($array, $name, $index = null): array function twig_array_filter(Environment $env, $array, $arrow) { if (!is_iterable($array)) { - throw new RuntimeError(sprintf('The "filter" filter expects an array or "Traversable", got "%s".', \is_object($array) ? \get_class($array) : \gettype($array))); + throw new RuntimeError(sprintf('The "filter" filter expects an array or "Traversable", got "%s".', get_debug_type($array))); } twig_check_arrow_in_sandbox($env, $arrow, 'filter', 'filter'); @@ -1743,7 +1739,7 @@ function twig_array_every(Environment $env, $array, $arrow) function twig_check_arrow_in_sandbox(Environment $env, $arrow, $thing, $type) { - if (!$arrow instanceof Closure && $env->hasExtension('\Twig\Extension\SandboxExtension') && $env->getExtension('\Twig\Extension\SandboxExtension')->isSandboxed()) { + if (!$arrow instanceof Closure && $env->hasExtension(\Twig\Extension\SandboxExtension::class) && $env->getExtension(\Twig\Extension\SandboxExtension::class)->isSandboxed()) { throw new RuntimeError(sprintf('The callable passed to the "%s" %s must be a Closure in sandbox mode.', $thing, $type)); } } diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index ef8879dbdc6..90dd5023ad1 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -172,7 +172,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char if (!\is_string($string)) { if (\is_object($string) && method_exists($string, '__toString')) { if ($autoescape) { - $c = \get_class($string); + $c = $string::class; $ext = $env->getExtension(EscaperExtension::class); if (!isset($ext->safeClasses[$c])) { $ext->safeClasses[$c] = []; diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index d32200ceb51..f7ecd074651 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -122,7 +122,7 @@ public function getLastModified(): int public function addExtension(ExtensionInterface $extension): void { - $class = \get_class($extension); + $class = $extension::class; if ($this->initialized) { throw new \LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $class)); @@ -330,7 +330,7 @@ public function getGlobals(): array $extGlobals = $extension->getGlobals(); if (!\is_array($extGlobals)) { - throw new \UnexpectedValueException(sprintf('"%s::getGlobals()" must return an array of globals.', \get_class($extension))); + throw new \UnexpectedValueException(sprintf('"%s::getGlobals()" must return an array of globals.', $extension::class)); } $globals = array_merge($globals, $extGlobals); @@ -466,11 +466,11 @@ private function initExtension(ExtensionInterface $extension): void // operators if ($operators = $extension->getOperators()) { if (!\is_array($operators)) { - throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array with operators, got "%s".', \get_class($extension), \is_object($operators) ? \get_class($operators) : \gettype($operators).(\is_resource($operators) ? '' : '#'.$operators))); + throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array with operators, got "%s".', $extension::class, \is_object($operators) ? $operators::class : \gettype($operators).(\is_resource($operators) ? '' : '#'.$operators))); } if (2 !== \count($operators)) { - throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array of 2 elements, got %d.', \get_class($extension), \count($operators))); + throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array of 2 elements, got %d.', $extension::class, \count($operators))); } $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]); diff --git a/src/FileExtensionEscapingStrategy.php b/src/FileExtensionEscapingStrategy.php index 812071bf971..9dbc6356e08 100644 --- a/src/FileExtensionEscapingStrategy.php +++ b/src/FileExtensionEscapingStrategy.php @@ -43,18 +43,11 @@ public static function guess(string $name) $extension = pathinfo($name, \PATHINFO_EXTENSION); - switch ($extension) { - case 'js': - return 'js'; - - case 'css': - return 'css'; - - case 'txt': - return false; - - default: - return 'html'; - } + return match ($extension) { + 'js' => 'js', + 'css' => 'css', + 'txt' => false, + default => 'html', + }; } } diff --git a/src/Lexer.php b/src/Lexer.php index 690b3d7375c..98ffbac0f6e 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -209,7 +209,7 @@ public function tokenize(Source $source): TokenStream $this->pushToken(/* Token::EOF_TYPE */ -1); if (!empty($this->brackets)) { - list($expect, $lineno) = array_pop($this->brackets); + [$expect, $lineno] = array_pop($this->brackets); throw new SyntaxError(sprintf('Unclosed "%s".', $expect), $lineno, $this->source); } @@ -355,7 +355,7 @@ private function lexExpression(): void throw new SyntaxError(sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source); } - list($expect, $lineno) = array_pop($this->brackets); + [$expect, $lineno] = array_pop($this->brackets); if ($this->code[$this->cursor] != strtr($expect, '([{', ')]}')) { throw new SyntaxError(sprintf('Unclosed "%s".', $expect), $lineno, $this->source); } @@ -425,7 +425,7 @@ private function lexString(): void $this->pushToken(/* Token::STRING_TYPE */ 7, stripcslashes($match[0])); $this->moveCursor($match[0]); } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { - list($expect, $lineno) = array_pop($this->brackets); + [$expect, $lineno] = array_pop($this->brackets); if ('"' != $this->code[$this->cursor]) { throw new SyntaxError(sprintf('Unclosed "%s".', $expect), $lineno, $this->source); } diff --git a/src/Loader/ChainLoader.php b/src/Loader/ChainLoader.php index fbf4f3a0654..210ec174b95 100644 --- a/src/Loader/ChainLoader.php +++ b/src/Loader/ChainLoader.php @@ -92,7 +92,7 @@ public function getCacheKey(string $name): string try { return $loader->getCacheKey($name); } catch (LoaderError $e) { - $exceptions[] = \get_class($loader).': '.$e->getMessage(); + $exceptions[] = $loader::class.': '.$e->getMessage(); } } @@ -110,7 +110,7 @@ public function isFresh(string $name, int $time): bool try { return $loader->isFresh($name, $time); } catch (LoaderError $e) { - $exceptions[] = \get_class($loader).': '.$e->getMessage(); + $exceptions[] = $loader::class.': '.$e->getMessage(); } } diff --git a/src/Loader/FilesystemLoader.php b/src/Loader/FilesystemLoader.php index 1073a406a06..1b277fe2fc4 100644 --- a/src/Loader/FilesystemLoader.php +++ b/src/Loader/FilesystemLoader.php @@ -183,7 +183,7 @@ protected function findTemplate(string $name, bool $throw = true) } try { - list($namespace, $shortname) = $this->parseName($name); + [$namespace, $shortname] = $this->parseName($name); $this->validateName($shortname); } catch (LoaderError $e) { diff --git a/src/Node/Expression/CallExpression.php b/src/Node/Expression/CallExpression.php index 2ecc031dc9e..6482cbff254 100644 --- a/src/Node/Expression/CallExpression.php +++ b/src/Node/Expression/CallExpression.php @@ -38,7 +38,7 @@ protected function compileCallable(Compiler $compiler) $compiler->raw(sprintf('$this->env->getRuntime(\'%s\')->%s', $callable[0], $callable[1])); } } elseif (\is_array($callable) && $callable[0] instanceof ExtensionInterface) { - $class = \get_class($callable[0]); + $class = $callable[0]::class; if (!$compiler->getEnvironment()->hasExtension($class)) { // Compile a non-optimized call to trigger a \Twig\Error\RuntimeError, which cannot be a compile-time error $compiler->raw(sprintf('$this->env->getExtension(\'%s\')', $class)); @@ -140,7 +140,7 @@ protected function getArguments($callable, $arguments) throw new \LogicException($message); } - list($callableParameters, $isPhpVariadic) = $this->getCallableParameters($callable, $isVariadic); + [$callableParameters, $isPhpVariadic] = $this->getCallableParameters($callable, $isVariadic); $arguments = []; $names = []; $missingArguments = []; diff --git a/src/Node/Node.php b/src/Node/Node.php index 30659ae0fd1..fcba26a49ec 100644 --- a/src/Node/Node.php +++ b/src/Node/Node.php @@ -39,7 +39,7 @@ public function __construct(array $nodes = [], array $attributes = [], int $line { foreach ($nodes as $name => $node) { if (!$node instanceof self) { - throw new \InvalidArgumentException(sprintf('Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.', \is_object($node) ? \get_class($node) : (null === $node ? 'null' : \gettype($node)), $name, static::class)); + throw new \InvalidArgumentException(sprintf('Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.', \is_object($node) ? $node::class : (null === $node ? 'null' : \gettype($node)), $name, static::class)); } } $this->nodes = $nodes; diff --git a/src/NodeVisitor/EscaperNodeVisitor.php b/src/NodeVisitor/EscaperNodeVisitor.php index c390d7cc71b..8f79fde0663 100644 --- a/src/NodeVisitor/EscaperNodeVisitor.php +++ b/src/NodeVisitor/EscaperNodeVisitor.php @@ -141,7 +141,7 @@ private function escapePrintNode(PrintNode $node, Environment $env, string $type return $node; } - $class = \get_class($node); + $class = $node::class; return new $class($this->getEscaperFilter($type, $expression), $node->getTemplateLine()); } diff --git a/src/Parser.php b/src/Parser.php index 4016a5f39ab..3e2dc1eda38 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -335,7 +335,7 @@ private function filterBodyNodes(Node $node, bool $nested = false): ?Node // here, $nested means "being at the root level of a child template" // we need to discard the wrapping "Node" for the "body" node - $nested = $nested || Node::class !== \get_class($node); + $nested = $nested || Node::class !== $node::class; foreach ($node as $k => $n) { if (null !== $n && null === $this->filterBodyNodes($n, $nested)) { $node->removeNode($k); diff --git a/src/Profiler/Profile.php b/src/Profiler/Profile.php index 7979a23c67a..72506b7c8da 100644 --- a/src/Profiler/Profile.php +++ b/src/Profiler/Profile.php @@ -176,6 +176,6 @@ public function __serialize(): array */ public function __unserialize(array $data): void { - list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = $data; + [$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles] = $data; } } diff --git a/src/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index a725aa4f104..8db24113596 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -50,7 +50,7 @@ public function setAllowedMethods(array $methods): void { $this->allowedMethods = []; foreach ($methods as $class => $m) { - $this->allowedMethods[$class] = array_map(function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, \is_array($m) ? $m : [$m]); + $this->allowedMethods[$class] = array_map(fn($value) => strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), \is_array($m) ? $m : [$m]); } } @@ -101,7 +101,7 @@ public function checkMethodAllowed($obj, $method): void } if (!$allowed) { - $class = \get_class($obj); + $class = $obj::class; throw new SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method); } } @@ -117,7 +117,7 @@ public function checkPropertyAllowed($obj, $property): void } if (!$allowed) { - $class = \get_class($obj); + $class = $obj::class; throw new SecurityNotAllowedPropertyError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property); } } diff --git a/src/Template.php b/src/Template.php index ffbaae1ea1c..cc47088f559 100644 --- a/src/Template.php +++ b/src/Template.php @@ -213,7 +213,7 @@ public function renderParentBlock($name, array $context, array $blocks = []) if ($this->env->isDebug()) { ob_start(); } else { - ob_start(function () { return ''; }); + ob_start(fn() => ''); } $this->displayParentBlock($name, $context, $blocks); @@ -238,7 +238,7 @@ public function renderBlock($name, array $context, array $blocks = [], $useBlock if ($this->env->isDebug()) { ob_start(); } else { - ob_start(function () { return ''; }); + ob_start(fn() => ''); } $this->displayBlock($name, $context, $blocks, $useBlocks); @@ -373,7 +373,7 @@ public function render(array $context) if ($this->env->isDebug()) { ob_start(); } else { - ob_start(function () { return ''; }); + ob_start(fn() => ''); } try { $this->display($context); diff --git a/src/TemplateWrapper.php b/src/TemplateWrapper.php index 1ecd82251f3..7e71492e88c 100644 --- a/src/TemplateWrapper.php +++ b/src/TemplateWrapper.php @@ -65,7 +65,7 @@ public function renderBlock(string $name, array $context = []): string if ($this->env->isDebug()) { ob_start(); } else { - ob_start(function () { return ''; }); + ob_start(fn() => ''); } try { $this->template->displayBlock($name, $context); diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 3830403dbe3..2e4268d22f8 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -205,14 +205,14 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e } catch (\Exception $e) { if (false !== $exception) { $message = $e->getMessage(); - $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $message))); + $this->assertSame(trim($exception), trim(sprintf('%s: %s', $e::class, $message))); $last = substr($message, \strlen($message) - 1); $this->assertTrue('.' === $last || '?' === $last, 'Exception message must end with a dot or a question mark.'); return; } - throw new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e); + throw new Error(sprintf('%s: %s', $e::class, $e->getMessage()), -1, null, $e); } finally { restore_error_handler(); } @@ -223,18 +223,18 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e $output = trim($template->render(eval($match[1].';')), "\n "); } catch (\Exception $e) { if (false !== $exception) { - $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $e->getMessage()))); + $this->assertSame(trim($exception), trim(sprintf('%s: %s', $e::class, $e->getMessage()))); return; } - $e = new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e); + $e = new Error(sprintf('%s: %s', $e::class, $e->getMessage()), -1, null, $e); - $output = trim(sprintf('%s: %s', \get_class($e), $e->getMessage())); + $output = trim(sprintf('%s: %s', $e::class, $e->getMessage())); } if (false !== $exception) { - list($class) = explode(':', $exception); + [$class] = explode(':', $exception); $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception'; $this->assertThat(null, new $constraintClass($class)); } diff --git a/src/Token.php b/src/Token.php index 59279b8fe7c..860f4c9d157 100644 --- a/src/Token.php +++ b/src/Token.php @@ -91,94 +91,47 @@ public function getValue() public static function typeToString(int $type, bool $short = false): string { - switch ($type) { - case self::EOF_TYPE: - $name = 'EOF_TYPE'; - break; - case self::TEXT_TYPE: - $name = 'TEXT_TYPE'; - break; - case self::BLOCK_START_TYPE: - $name = 'BLOCK_START_TYPE'; - break; - case self::VAR_START_TYPE: - $name = 'VAR_START_TYPE'; - break; - case self::BLOCK_END_TYPE: - $name = 'BLOCK_END_TYPE'; - break; - case self::VAR_END_TYPE: - $name = 'VAR_END_TYPE'; - break; - case self::NAME_TYPE: - $name = 'NAME_TYPE'; - break; - case self::NUMBER_TYPE: - $name = 'NUMBER_TYPE'; - break; - case self::STRING_TYPE: - $name = 'STRING_TYPE'; - break; - case self::OPERATOR_TYPE: - $name = 'OPERATOR_TYPE'; - break; - case self::PUNCTUATION_TYPE: - $name = 'PUNCTUATION_TYPE'; - break; - case self::INTERPOLATION_START_TYPE: - $name = 'INTERPOLATION_START_TYPE'; - break; - case self::INTERPOLATION_END_TYPE: - $name = 'INTERPOLATION_END_TYPE'; - break; - case self::ARROW_TYPE: - $name = 'ARROW_TYPE'; - break; - case self::SPREAD_TYPE: - $name = 'SPREAD_TYPE'; - break; - default: - throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type)); - } + $name = match ($type) { + self::EOF_TYPE => 'EOF_TYPE', + self::TEXT_TYPE => 'TEXT_TYPE', + self::BLOCK_START_TYPE => 'BLOCK_START_TYPE', + self::VAR_START_TYPE => 'VAR_START_TYPE', + self::BLOCK_END_TYPE => 'BLOCK_END_TYPE', + self::VAR_END_TYPE => 'VAR_END_TYPE', + self::NAME_TYPE => 'NAME_TYPE', + self::NUMBER_TYPE => 'NUMBER_TYPE', + self::STRING_TYPE => 'STRING_TYPE', + self::OPERATOR_TYPE => 'OPERATOR_TYPE', + self::PUNCTUATION_TYPE => 'PUNCTUATION_TYPE', + self::INTERPOLATION_START_TYPE => 'INTERPOLATION_START_TYPE', + self::INTERPOLATION_END_TYPE => 'INTERPOLATION_END_TYPE', + self::ARROW_TYPE => 'ARROW_TYPE', + self::SPREAD_TYPE => 'SPREAD_TYPE', + default => throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type)), + }; return $short ? $name : 'Twig\Token::'.$name; } public static function typeToEnglish(int $type): string { - switch ($type) { - case self::EOF_TYPE: - return 'end of template'; - case self::TEXT_TYPE: - return 'text'; - case self::BLOCK_START_TYPE: - return 'begin of statement block'; - case self::VAR_START_TYPE: - return 'begin of print statement'; - case self::BLOCK_END_TYPE: - return 'end of statement block'; - case self::VAR_END_TYPE: - return 'end of print statement'; - case self::NAME_TYPE: - return 'name'; - case self::NUMBER_TYPE: - return 'number'; - case self::STRING_TYPE: - return 'string'; - case self::OPERATOR_TYPE: - return 'operator'; - case self::PUNCTUATION_TYPE: - return 'punctuation'; - case self::INTERPOLATION_START_TYPE: - return 'begin of string interpolation'; - case self::INTERPOLATION_END_TYPE: - return 'end of string interpolation'; - case self::ARROW_TYPE: - return 'arrow function'; - case self::SPREAD_TYPE: - return 'spread operator'; - default: - throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type)); - } + return match ($type) { + self::EOF_TYPE => 'end of template', + self::TEXT_TYPE => 'text', + self::BLOCK_START_TYPE => 'begin of statement block', + self::VAR_START_TYPE => 'begin of print statement', + self::BLOCK_END_TYPE => 'end of statement block', + self::VAR_END_TYPE => 'end of print statement', + self::NAME_TYPE => 'name', + self::NUMBER_TYPE => 'number', + self::STRING_TYPE => 'string', + self::OPERATOR_TYPE => 'operator', + self::PUNCTUATION_TYPE => 'punctuation', + self::INTERPOLATION_START_TYPE => 'begin of string interpolation', + self::INTERPOLATION_END_TYPE => 'end of string interpolation', + self::ARROW_TYPE => 'arrow function', + self::SPREAD_TYPE => 'spread operator', + default => throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type)), + }; } } diff --git a/src/TokenParser/EmbedTokenParser.php b/src/TokenParser/EmbedTokenParser.php index 64b4f296f27..adf683cc19e 100644 --- a/src/TokenParser/EmbedTokenParser.php +++ b/src/TokenParser/EmbedTokenParser.php @@ -30,7 +30,7 @@ public function parse(Token $token): Node $parent = $this->parser->getExpressionParser()->parseExpression(); - list($variables, $only, $ignoreMissing) = $this->parseArguments(); + [$variables, $only, $ignoreMissing] = $this->parseArguments(); $parentToken = $fakeParentToken = new Token(/* Token::STRING_TYPE */ 7, '__parent__', $token->getLine()); if ($parent instanceof ConstantExpression) { diff --git a/src/TokenParser/IncludeTokenParser.php b/src/TokenParser/IncludeTokenParser.php index 28beb8ae477..fda5bfd8c0a 100644 --- a/src/TokenParser/IncludeTokenParser.php +++ b/src/TokenParser/IncludeTokenParser.php @@ -31,7 +31,7 @@ public function parse(Token $token): Node { $expr = $this->parser->getExpressionParser()->parseExpression(); - list($variables, $only, $ignoreMissing) = $this->parseArguments(); + [$variables, $only, $ignoreMissing] = $this->parseArguments(); return new IncludeNode($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); } diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index b22fdf42450..549b40e41e2 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -301,7 +301,7 @@ public function testAddMockExtension() $twig = new Environment($loader); $twig->addExtension($extension); - $this->assertInstanceOf(ExtensionInterface::class, $twig->getExtension(\get_class($extension))); + $this->assertInstanceOf(ExtensionInterface::class, $twig->getExtension($extension::class)); $this->assertTrue($twig->isTemplateFresh('page', time())); } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index aa24d5fbe8f..a4db042ed7b 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -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) diff --git a/tests/Node/Expression/FilterTest.php b/tests/Node/Expression/FilterTest.php index b8cc48fab16..04b7d2f4253 100644 --- a/tests/Node/Expression/FilterTest.php +++ b/tests/Node/Expression/FilterTest.php @@ -131,7 +131,7 @@ protected function foobar() // from extension $node = $this->createFilter($string, 'foo'); - $tests[] = [$node, sprintf('$this->extensions[\'%s\']->foo("abc")', \get_class($extension)), $environment]; + $tests[] = [$node, sprintf('$this->extensions[\'%s\']->foo("abc")', $extension::class), $environment]; $node = $this->createFilter($string, 'foobar'); $tests[] = [$node, '$this->env->getFilter(\'foobar\')->getCallable()("abc")', $environment];