diff --git a/.php_cs.dist b/.php_cs.dist index ff8a55dbb..1b31c0a3d 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -11,6 +11,7 @@ return PhpCsFixer\Config::create() 'heredoc_to_nowdoc' => false, 'ordered_imports' => true, 'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'], + 'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'all'], ]) ->setRiskyAllowed(true) ->setFinder(PhpCsFixer\Finder::create()->in(__DIR__)) diff --git a/lib/Twig/Cache/Filesystem.php b/lib/Twig/Cache/Filesystem.php index b2704dd6b..73b0220eb 100644 --- a/lib/Twig/Cache/Filesystem.php +++ b/lib/Twig/Cache/Filesystem.php @@ -47,7 +47,7 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface public function write($key, $content) { - $dir = dirname($key); + $dir = \dirname($key); if (!is_dir($dir)) { if (false === @mkdir($dir, 0777, true)) { clearstatcache(true, $dir); @@ -65,9 +65,9 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) { // Compile cached file into bytecode cache - if (function_exists('opcache_invalidate')) { + if (\function_exists('opcache_invalidate')) { opcache_invalidate($key, true); - } elseif (function_exists('apc_compile_file')) { + } elseif (\function_exists('apc_compile_file')) { apc_compile_file($key); } } diff --git a/lib/Twig/Error/Syntax.php b/lib/Twig/Error/Syntax.php index c59b03577..dca85d62f 100644 --- a/lib/Twig/Error/Syntax.php +++ b/lib/Twig/Error/Syntax.php @@ -28,7 +28,7 @@ class Twig_Error_Syntax extends Twig_Error $alternatives = []; foreach ($items as $item) { $lev = levenshtein($name, $item); - if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) { + if ($lev <= \strlen($name) / 3 || false !== strpos($item, $name)) { $alternatives[$item] = $lev; } } diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php index d4808f6d8..304753caa 100644 --- a/lib/Twig/Extension/Escaper.php +++ b/lib/Twig/Extension/Escaper.php @@ -68,8 +68,8 @@ final class Twig_Extension_Escaper extends Twig_Extension { // disable string callables to avoid calling a function named html or js, // or any other upcoming escaping strategy - if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) { - return call_user_func($this->defaultStrategy, $name); + if (!\is_string($this->defaultStrategy) && false !== $this->defaultStrategy) { + return \call_user_func($this->defaultStrategy, $name); } return $this->defaultStrategy; diff --git a/lib/Twig/Extension/Profiler.php b/lib/Twig/Extension/Profiler.php index 065b221e8..1ed7fe288 100644 --- a/lib/Twig/Extension/Profiler.php +++ b/lib/Twig/Extension/Profiler.php @@ -29,14 +29,14 @@ class Twig_Extension_Profiler extends Twig_Extension $profile->leave(); array_shift($this->actives); - if (1 === count($this->actives)) { + if (1 === \count($this->actives)) { $this->actives[0]->leave(); } } public function getNodeVisitors() { - return [new Twig_Profiler_NodeVisitor_Profiler(get_class($this))]; + return [new Twig_Profiler_NodeVisitor_Profiler(\get_class($this))]; } } diff --git a/lib/Twig/Extension/Sandbox.php b/lib/Twig/Extension/Sandbox.php index 77b23c57d..e59c77e1e 100644 --- a/lib/Twig/Extension/Sandbox.php +++ b/lib/Twig/Extension/Sandbox.php @@ -84,7 +84,7 @@ final class Twig_Extension_Sandbox extends Twig_Extension public function ensureToStringAllowed($obj) { - if ($this->isSandboxed() && is_object($obj)) { + if ($this->isSandboxed() && \is_object($obj)) { $this->policy->checkMethodAllowed($obj, '__toString'); } diff --git a/lib/Twig/FileExtensionEscapingStrategy.php b/lib/Twig/FileExtensionEscapingStrategy.php index 6b13c7213..7854f7691 100644 --- a/lib/Twig/FileExtensionEscapingStrategy.php +++ b/lib/Twig/FileExtensionEscapingStrategy.php @@ -31,7 +31,7 @@ class Twig_FileExtensionEscapingStrategy */ public static function guess($name) { - if (in_array(substr($name, -1), ['/', '\\'])) { + if (\in_array(substr($name, -1), ['/', '\\'])) { return 'html'; // return html for directories } diff --git a/lib/Twig/Node/Expression/Filter/Default.php b/lib/Twig/Node/Expression/Filter/Default.php index 2aeb742d7..9bac0e162 100644 --- a/lib/Twig/Node/Expression/Filter/Default.php +++ b/lib/Twig/Node/Expression/Filter/Default.php @@ -24,7 +24,7 @@ class Twig_Node_Expression_Filter_Default extends Twig_Node_Expression_Filter if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getTemplateLine()); - $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getTemplateLine()); + $false = \count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getTemplateLine()); $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getTemplateLine()); } else { diff --git a/lib/Twig/Node/If.php b/lib/Twig/Node/If.php index 2980a673c..094720ad3 100644 --- a/lib/Twig/Node/If.php +++ b/lib/Twig/Node/If.php @@ -30,7 +30,7 @@ class Twig_Node_If extends Twig_Node public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); - for ($i = 0, $count = count($this->getNode('tests')); $i < $count; $i += 2) { + for ($i = 0, $count = \count($this->getNode('tests')); $i < $count; $i += 2) { if ($i > 0) { $compiler ->outdent() diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 1f0d46823..33a9e57b7 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -36,7 +36,7 @@ class Twig_Node_Macro extends Twig_Node ->write(sprintf('public function macro_%s(', $this->getAttribute('name'))) ; - $count = count($this->getNode('arguments')); + $count = \count($this->getNode('arguments')); $pos = 0; foreach ($this->getNode('arguments') as $name => $default) { $compiler diff --git a/lib/Twig/Node/Set.php b/lib/Twig/Node/Set.php index 0db73093a..815bbe41d 100644 --- a/lib/Twig/Node/Set.php +++ b/lib/Twig/Node/Set.php @@ -40,7 +40,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeCaptureInterface { $compiler->addDebugInfo($this); - if (count($this->getNode('names')) > 1) { + if (\count($this->getNode('names')) > 1) { $compiler->write('list('); foreach ($this->getNode('names') as $idx => $node) { if ($idx) { @@ -68,7 +68,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeCaptureInterface if (!$this->getAttribute('capture')) { $compiler->raw(' = '); - if (count($this->getNode('names')) > 1) { + if (\count($this->getNode('names')) > 1) { $compiler->write('['); foreach ($this->getNode('values') as $idx => $value) { if ($idx) { diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index 628a1954c..d9228d70c 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -80,7 +80,7 @@ final class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor return $node; } - $class = get_class($node); + $class = \get_class($node); return new $class( $this->getEscaperFilter($type, $expression), @@ -122,13 +122,13 @@ final class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor $safe = $this->safeAnalysis->getSafe($expression); } - return in_array($type, $safe) || in_array('all', $safe); + return \in_array($type, $safe) || \in_array('all', $safe); } private function needEscaping(Twig_Environment $env) { - if (count($this->statusStack)) { - return $this->statusStack[count($this->statusStack) - 1]; + if (\count($this->statusStack)) { + return $this->statusStack[\count($this->statusStack) - 1]; } return $this->defaultStrategy ? $this->defaultStrategy : false; diff --git a/lib/Twig/Profiler/Dumper/Base.php b/lib/Twig/Profiler/Dumper/Base.php index 913afd4f0..8aeeeb773 100644 --- a/lib/Twig/Profiler/Dumper/Base.php +++ b/lib/Twig/Profiler/Dumper/Base.php @@ -49,7 +49,7 @@ abstract class Twig_Profiler_Dumper_Base $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent)); } - $nCount = count($profile->getProfiles()); + $nCount = \count($profile->getProfiles()); foreach ($profile as $i => $p) { $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount); } diff --git a/lib/Twig/Sandbox/SecurityPolicy.php b/lib/Twig/Sandbox/SecurityPolicy.php index 9c0d35229..b9433c0b7 100644 --- a/lib/Twig/Sandbox/SecurityPolicy.php +++ b/lib/Twig/Sandbox/SecurityPolicy.php @@ -45,7 +45,7 @@ final class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyIn { $this->allowedMethods = []; foreach ($methods as $class => $m) { - $this->allowedMethods[$class] = array_map('strtolower', is_array($m) ? $m : [$m]); + $this->allowedMethods[$class] = array_map('strtolower', \is_array($m) ? $m : [$m]); } } @@ -62,19 +62,19 @@ final class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyIn public function checkSecurity($tags, $filters, $functions) { foreach ($tags as $tag) { - if (!in_array($tag, $this->allowedTags)) { + if (!\in_array($tag, $this->allowedTags)) { throw new Twig_Sandbox_SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag); } } foreach ($filters as $filter) { - if (!in_array($filter, $this->allowedFilters)) { + if (!\in_array($filter, $this->allowedFilters)) { throw new Twig_Sandbox_SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter); } } foreach ($functions as $function) { - if (!in_array($function, $this->allowedFunctions)) { + if (!\in_array($function, $this->allowedFunctions)) { throw new Twig_Sandbox_SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function); } } @@ -90,14 +90,14 @@ final class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyIn $method = strtolower($method); foreach ($this->allowedMethods as $class => $methods) { if ($obj instanceof $class) { - $allowed = in_array($method, $methods); + $allowed = \in_array($method, $methods); break; } } if (!$allowed) { - $class = get_class($obj); + $class = \get_class($obj); throw new Twig_Sandbox_SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method); } } @@ -107,14 +107,14 @@ final class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyIn $allowed = false; foreach ($this->allowedProperties as $class => $properties) { if ($obj instanceof $class) { - $allowed = in_array($property, is_array($properties) ? $properties : [$properties]); + $allowed = \in_array($property, \is_array($properties) ? $properties : [$properties]); break; } } if (!$allowed) { - $class = get_class($obj); + $class = \get_class($obj); throw new Twig_Sandbox_SecurityNotAllowedPropertyError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property); } } diff --git a/lib/Twig/Token.php b/lib/Twig/Token.php index c054c7e77..4244e857d 100644 --- a/lib/Twig/Token.php +++ b/lib/Twig/Token.php @@ -67,14 +67,14 @@ final class Twig_Token */ public function test($type, $values = null) { - if (null === $values && !is_int($type)) { + if (null === $values && !\is_int($type)) { $values = $type; $type = self::NAME_TYPE; } return ($this->type === $type) && ( null === $values || - (is_array($values) && in_array($this->value, $values)) || + (\is_array($values) && \in_array($this->value, $values)) || $this->value == $values ); } diff --git a/lib/Twig/TokenParser/For.php b/lib/Twig/TokenParser/For.php index b41340579..0177a2889 100644 --- a/lib/Twig/TokenParser/For.php +++ b/lib/Twig/TokenParser/For.php @@ -44,7 +44,7 @@ final class Twig_TokenParser_For extends Twig_TokenParser } $stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3); - if (count($targets) > 1) { + if (\count($targets) > 1) { $keyTarget = $targets->getNode(0); $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine()); $valueTarget = $targets->getNode(1); @@ -95,7 +95,7 @@ final class Twig_TokenParser_For extends Twig_TokenParser { if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) { $attribute = $node->getNode('attribute'); - if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), ['length', 'revindex0', 'revindex', 'last'])) { + if ($attribute instanceof Twig_Node_Expression_Constant && \in_array($attribute->getAttribute('value'), ['length', 'revindex0', 'revindex', 'last'])) { throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext()); } } diff --git a/lib/Twig/TokenParser/Set.php b/lib/Twig/TokenParser/Set.php index 6c5bb5274..a7b79d52f 100644 --- a/lib/Twig/TokenParser/Set.php +++ b/lib/Twig/TokenParser/Set.php @@ -33,13 +33,13 @@ final class Twig_TokenParser_Set extends Twig_TokenParser $stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3); - if (count($names) !== count($values)) { + if (\count($names) !== \count($values)) { throw new Twig_Error_Syntax('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); } } else { $capture = true; - if (count($names) > 1) { + if (\count($names) > 1) { throw new Twig_Error_Syntax('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); } diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index d3ea2663d..cce9a9747 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -34,7 +34,7 @@ final class Twig_TokenStream public function injectTokens(array $tokens) { - $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current)); + $this->tokens = array_merge(\array_slice($this->tokens, 0, $this->current), $tokens, \array_slice($this->tokens, $this->current)); } /** diff --git a/test/Twig/Tests/Cache/FilesystemTest.php b/test/Twig/Tests/Cache/FilesystemTest.php index 818e3ece0..806143eeb 100644 --- a/test/Twig/Tests/Cache/FilesystemTest.php +++ b/test/Twig/Tests/Cache/FilesystemTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__DIR__).'/FilesystemHelper.php'; +require_once \dirname(__DIR__).'/FilesystemHelper.php'; class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase { @@ -36,7 +36,7 @@ class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase { $key = $this->directory.'/cache/cachefile.php'; - $dir = dirname($key); + $dir = \dirname($key); @mkdir($dir, 0777, true); $this->assertTrue(is_dir($dir)); $this->assertFalse(class_exists($this->classname, false)); @@ -81,7 +81,7 @@ class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase */ public function testWriteFailMkdir() { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (\defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Read-only directories not possible on Windows.'); } @@ -103,7 +103,7 @@ class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase */ public function testWriteFailDirWritable() { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (\defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Read-only directories not possible on Windows.'); } @@ -143,7 +143,7 @@ class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase { $key = $this->directory.'/cache/cachefile.php'; - $dir = dirname($key); + $dir = \dirname($key); @mkdir($dir, 0777, true); $this->assertTrue(is_dir($dir)); @@ -167,7 +167,7 @@ class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase public function testGenerateKey($expected, $input) { $cache = new Twig_Cache_Filesystem($input); - $this->assertRegExp($expected, $cache->generateKey('_test_', get_class($this))); + $this->assertRegExp($expected, $cache->generateKey('_test_', \get_class($this))); } public function provideDirectories() diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 8c66b0e76..2302dd2a3 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -285,7 +285,7 @@ class Twig_Tests_EnvironmentTest extends \PHPUnit\Framework\TestCase $twig = new Twig_Environment($loader); $twig->addExtension($extension); - $this->assertInstanceOf('Twig_ExtensionInterface', $twig->getExtension(get_class($extension))); + $this->assertInstanceOf('Twig_ExtensionInterface', $twig->getExtension(\get_class($extension))); $this->assertTrue($twig->isTemplateFresh('page', time())); } diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index 20955c9ec..dadcf090f 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -26,7 +26,7 @@ class Twig_Tests_Node_ForTest extends Twig_Test_NodeTestCase $this->assertEquals($valueTarget, $node->getNode('value_target')); $this->assertEquals($seq, $node->getNode('seq')); $this->assertTrue($node->getAttribute('ifexpr')); - $this->assertEquals('Twig_Node_If', get_class($node->getNode('body'))); + $this->assertEquals('Twig_Node_If', \get_class($node->getNode('body'))); $this->assertEquals($body, $node->getNode('body')->getNode('tests')->getNode(1)->getNode(0)); $this->assertFalse($node->hasNode('else')); diff --git a/test/Twig/Tests/escapingTest.php b/test/Twig/Tests/escapingTest.php index 66e3b7d11..7bbd92cfe 100644 --- a/test/Twig/Tests/escapingTest.php +++ b/test/Twig/Tests/escapingTest.php @@ -238,22 +238,22 @@ class Twig_Test_EscapingTest extends \PHPUnit\Framework\TestCase protected function codepointToUtf8($codepoint) { if ($codepoint < 0x80) { - return chr($codepoint); + return \chr($codepoint); } if ($codepoint < 0x800) { - return chr($codepoint >> 6 & 0x3f | 0xc0) - .chr($codepoint & 0x3f | 0x80); + return \chr($codepoint >> 6 & 0x3f | 0xc0) + .\chr($codepoint & 0x3f | 0x80); } if ($codepoint < 0x10000) { - return chr($codepoint >> 12 & 0x0f | 0xe0) - .chr($codepoint >> 6 & 0x3f | 0x80) - .chr($codepoint & 0x3f | 0x80); + return \chr($codepoint >> 12 & 0x0f | 0xe0) + .\chr($codepoint >> 6 & 0x3f | 0x80) + .\chr($codepoint & 0x3f | 0x80); } if ($codepoint < 0x110000) { - return chr($codepoint >> 18 & 0x07 | 0xf0) - .chr($codepoint >> 12 & 0x3f | 0x80) - .chr($codepoint >> 6 & 0x3f | 0x80) - .chr($codepoint & 0x3f | 0x80); + return \chr($codepoint >> 18 & 0x07 | 0xf0) + .\chr($codepoint >> 12 & 0x3f | 0x80) + .\chr($codepoint >> 6 & 0x3f | 0x80) + .\chr($codepoint & 0x3f | 0x80); } throw new \Exception('Codepoint requested outside of Unicode range.'); } @@ -269,7 +269,7 @@ class Twig_Test_EscapingTest extends \PHPUnit\Framework\TestCase $this->assertEquals($literal, twig_escape_filter($this->env, $literal, 'js')); } else { $literal = $this->codepointToUtf8($chr); - if (in_array($literal, $immune)) { + if (\in_array($literal, $immune)) { $this->assertEquals($literal, twig_escape_filter($this->env, $literal, 'js')); } else { $this->assertNotEquals( @@ -292,7 +292,7 @@ class Twig_Test_EscapingTest extends \PHPUnit\Framework\TestCase $this->assertEquals($literal, twig_escape_filter($this->env, $literal, 'html_attr')); } else { $literal = $this->codepointToUtf8($chr); - if (in_array($literal, $immune)) { + if (\in_array($literal, $immune)) { $this->assertEquals($literal, twig_escape_filter($this->env, $literal, 'html_attr')); } else { $this->assertNotEquals(