From e90fb1fa968c8a304c95810de0461436699841e5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 21 Nov 2023 17:25:49 +0100 Subject: [PATCH] Fix CS --- src/ExpressionParser.php | 2 +- src/ExtensionSet.php | 8 ++++---- src/Loader/ChainLoader.php | 4 ++-- src/Node/Node.php | 2 +- src/NodeVisitor/EscaperNodeVisitor.php | 2 +- src/Parser.php | 2 +- src/Sandbox/SecurityPolicy.php | 4 ++-- src/Test/IntegrationTestCase.php | 10 +++++----- tests/EnvironmentTest.php | 2 +- tests/Extension/SandboxTest.php | 2 +- tests/IntegrationTest.php | 2 +- tests/Node/Expression/FilterTest.php | 2 +- 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 13e0f0876e..6e355d3cf5 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'); diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index d32200ceb5..f7ecd07465 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/Loader/ChainLoader.php b/src/Loader/ChainLoader.php index fbf4f3a065..210ec174b9 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/Node/Node.php b/src/Node/Node.php index 30659ae0fd..fcba26a49e 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 c390d7cc71..8f79fde066 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 4016a5f39a..3e2dc1eda3 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/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index a725aa4f10..f4f8d40f0d 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -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/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index e97ad41706..a3f12b786f 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,14 +223,14 @@ 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) { diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index b22fdf4245..549b40e41e 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/Extension/SandboxTest.php b/tests/Extension/SandboxTest.php index 2e40a825da..ba29cbd9ac 100644 --- a/tests/Extension/SandboxTest.php +++ b/tests/Extension/SandboxTest.php @@ -440,7 +440,7 @@ public function testMultipleClassMatchesViaInheritanceInAllowedMethods() $twig_parent_first->load('1_childobj_childmethod')->render(self::$params); } catch (SecurityError $e) { $this->fail('checkMethodAllowed is exiting prematurely after matching a parent class and not seeing a method allowed on a child class later in the list'); - } + } try { $twig_child_first->load('1_childobj_parentmethod')->render(self::$params); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index aa24d5fbe8..a4db042ed7 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 b8cc48fab1..04b7d2f425 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];