diff --git a/src/Error/Error.php b/src/Error/Error.php index bca1fa64c..6df67ef2b 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -171,11 +171,11 @@ class Error extends \Exception $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 13e0f0876..6e355d3cf 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -623,7 +623,7 @@ class ExpressionParser $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'); diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 36aa8f10a..8cf43ba2f 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".', \is_object($from) ? $from::class : \gettype($from))); } return strtr($str ?? '', twig_to_array($from)); @@ -1384,10 +1384,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 +1412,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 +1430,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".', \is_object($items) ? $items::class : \gettype($items))); } $size = ceil($size); @@ -1492,9 +1492,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,7 +1558,7 @@ 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() @@ -1674,7 +1674,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".', \is_object($array) ? $array::class : \gettype($array))); } twig_check_arrow_in_sandbox($env, $arrow, 'filter', 'filter'); @@ -1743,7 +1743,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 ef8879dbd..90dd5023a 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 d32200ceb..f7ecd0746 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -122,7 +122,7 @@ final class ExtensionSet 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 @@ final class ExtensionSet $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 @@ final class ExtensionSet // 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/Loader/ChainLoader.php b/src/Loader/ChainLoader.php index fbf4f3a06..210ec174b 100644 --- a/src/Loader/ChainLoader.php +++ b/src/Loader/ChainLoader.php @@ -92,7 +92,7 @@ final class ChainLoader implements LoaderInterface try { return $loader->getCacheKey($name); } catch (LoaderError $e) { - $exceptions[] = \get_class($loader).': '.$e->getMessage(); + $exceptions[] = $loader::class.': '.$e->getMessage(); } } @@ -110,7 +110,7 @@ final class ChainLoader implements LoaderInterface try { return $loader->isFresh($name, $time); } catch (LoaderError $e) { - $exceptions[] = \get_class($loader).': '.$e->getMessage(); + $exceptions[] = $loader::class.': '.$e->getMessage(); } } diff --git a/src/Node/Expression/CallExpression.php b/src/Node/Expression/CallExpression.php index 2ecc031dc..92e6530c2 100644 --- a/src/Node/Expression/CallExpression.php +++ b/src/Node/Expression/CallExpression.php @@ -38,7 +38,7 @@ abstract class CallExpression extends AbstractExpression $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)); diff --git a/src/Node/Node.php b/src/Node/Node.php index 30659ae0f..fcba26a49 100644 --- a/src/Node/Node.php +++ b/src/Node/Node.php @@ -39,7 +39,7 @@ class Node implements \Countable, \IteratorAggregate { 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 c390d7cc7..8f79fde06 100644 --- a/src/NodeVisitor/EscaperNodeVisitor.php +++ b/src/NodeVisitor/EscaperNodeVisitor.php @@ -141,7 +141,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface 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 4016a5f39..3e2dc1eda 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -335,7 +335,7 @@ class Parser // 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/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index a725aa4f1..f4f8d40f0 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -101,7 +101,7 @@ final class SecurityPolicy implements SecurityPolicyInterface } 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 @@ final class SecurityPolicy implements SecurityPolicyInterface } 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/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 3830403db..20dc4f371 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -205,14 +205,14 @@ abstract class IntegrationTestCase extends TestCase } 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,14 +223,14 @@ abstract class IntegrationTestCase extends TestCase $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) { diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index b22fdf424..549b40e41 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -301,7 +301,7 @@ class EnvironmentTest extends TestCase $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/Node/Expression/FilterTest.php b/tests/Node/Expression/FilterTest.php index b8cc48fab..04b7d2f42 100644 --- a/tests/Node/Expression/FilterTest.php +++ b/tests/Node/Expression/FilterTest.php @@ -131,7 +131,7 @@ class FilterTest extends NodeTestCase // 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];