mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 20:06:31 +00:00
Merge branch '1.x' into 2.x
* 1.x: tweaked PHP-CS-Fixer config
This commit is contained in:
@@ -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__))
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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))];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
|
||||
@@ -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'));
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user