diff --git a/src/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index 1406e8061a9..d617c381f56 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -102,7 +102,7 @@ public function checkMethodAllowed($obj, $method) } 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); } } @@ -119,7 +119,7 @@ public function checkPropertyAllowed($obj, $property) } 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/Sandbox/SourcePolicyInterface.php b/src/Sandbox/SourcePolicyInterface.php index b78ac50afd0..96d62386ed4 100644 --- a/src/Sandbox/SourcePolicyInterface.php +++ b/src/Sandbox/SourcePolicyInterface.php @@ -24,5 +24,5 @@ interface SourcePolicyInterface * @param Source $source * */ - public function enableSandbox(Source $source = null) : bool; + public function enableSandbox(Source $source = null): bool; } diff --git a/tests/Extension/SandboxTest.php b/tests/Extension/SandboxTest.php index c5159ea43fa..1a4771b408d 100644 --- a/tests/Extension/SandboxTest.php +++ b/tests/Extension/SandboxTest.php @@ -107,7 +107,7 @@ public function testSandboxGloballyFalseUnallowedFilterWithIncludeTemplateFromSt $twig->load('1_basic2_include_template_from_string_sandboxed')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called'); } catch (SecurityError $e) { - $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); + $this->assertInstanceOf(SecurityNotAllowedFilterError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); $this->assertEquals('upper', $e->getFilterName(), 'Exception should be raised on the "upper" filter'); } } @@ -120,7 +120,7 @@ public function testSandboxGloballyTrueUnallowedFilterWithIncludeTemplateFromStr $twig->load('1_basic2_include_template_from_string_sandboxed')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called'); } catch (SecurityError $e) { - $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); + $this->assertInstanceOf(SecurityNotAllowedFilterError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); $this->assertEquals('upper', $e->getFilterName(), 'Exception should be raised on the "upper" filter'); } } @@ -140,7 +140,7 @@ public function testSandboxGloballyTrueUnallowedFilterWithIncludeTemplateFromStr $twig->load('1_basic2_include_template_from_string')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called'); } catch (SecurityError $e) { - $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); + $this->assertInstanceOf(SecurityNotAllowedFilterError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); $this->assertEquals('upper', $e->getFilterName(), 'Exception should be raised on the "upper" filter'); } } @@ -389,7 +389,7 @@ public function testSandboxDisabledAfterIncludeFunctionError() public function testSandboxWithNoClosureFilter() { - $this->expectException('\Twig\Error\RuntimeError'); + $this->expectException(\Twig\Error\RuntimeError::class); $this->expectExceptionMessage('The callable passed to the "filter" filter must be a Closure in sandbox mode in "index" at line 1.'); $twig = $this->getEnvironment(true, ['autoescape' => 'html'], ['index' => <<getEnvironment(false, [], self::$templates, [], [], [], [], [], new class() implements \Twig\Sandbox\SourcePolicyInterface { public function enableSandbox($source): bool { - return $source->getName() != '1_basic'; - }}); + return '1_basic' != $source->getName(); + } + }); $this->assertEquals('FOO', $twig->load('1_basic')->render(self::$params)); } @@ -435,8 +436,9 @@ public function testSandboxSourcePolicyEnableReturningTrue() $twig = $this->getEnvironment(false, [], self::$templates, [], [], [], [], [], new class() implements \Twig\Sandbox\SourcePolicyInterface { public function enableSandbox($source): bool { - return $source->getName() === '1_basic'; - }}); + return '1_basic' === $source->getName(); + } + }); $this->expectException(SecurityError::class); $twig->load('1_basic')->render([]); } @@ -447,9 +449,10 @@ public function testSandboxSourcePolicyFalseDoesntOverrideOtherEnables() public function enableSandbox($source): bool { return false; - }}); - $this->expectException(SecurityError::class); - $twig->load('1_basic')->render([]); + } + }); + $this->expectException(SecurityError::class); + $twig->load('1_basic')->render([]); } }