From 35640b6d2d0805530ecc738efcc055fe988ed749 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 13 Dec 2023 18:52:03 +0100 Subject: [PATCH] Add type hints to private properties --- src/Cache/FilesystemCache.php | 4 +- src/Compiler.php | 17 ++--- src/Environment.php | 50 +++++++++------ src/Error/Error.php | 10 +-- src/ExpressionParser.php | 8 +-- src/Extension/CoreExtension.php | 6 +- src/Extension/EscaperExtension.php | 7 ++- src/Extension/OptimizerExtension.php | 2 +- src/Extension/ProfilerExtension.php | 5 +- src/Extension/SandboxExtension.php | 6 +- src/Extension/StagingExtension.php | 25 ++++++-- src/ExtensionSet.php | 63 ++++++++++++------- src/Lexer.php | 47 +++++++++----- src/Loader/ArrayLoader.php | 7 ++- src/Loader/ChainLoader.php | 11 +++- src/Loader/FilesystemLoader.php | 2 +- src/Markup.php | 4 +- src/Node/CheckSecurityNode.php | 6 +- src/Node/Expression/ArrayExpression.php | 2 +- src/Node/Expression/CallExpression.php | 2 +- src/Node/Expression/NameExpression.php | 2 +- src/Node/ForNode.php | 2 +- src/Node/ModuleNode.php | 4 +- src/Node/Node.php | 2 +- src/NodeTraverser.php | 7 ++- src/NodeVisitor/EscaperNodeVisitor.php | 18 ++++-- .../MacroAutoImportNodeVisitor.php | 4 +- src/NodeVisitor/OptimizerNodeVisitor.php | 12 +++- src/NodeVisitor/SandboxNodeVisitor.php | 10 +-- src/Parser.php | 45 +++++++++---- src/Profiler/Dumper/BaseDumper.php | 2 +- .../NodeVisitor/ProfilerNodeVisitor.php | 4 +- src/Profiler/Profile.php | 15 +++-- src/RuntimeLoader/ContainerRuntimeLoader.php | 2 +- src/RuntimeLoader/FactoryRuntimeLoader.php | 7 ++- src/Sandbox/SecurityNotAllowedFilterError.php | 2 +- .../SecurityNotAllowedFunctionError.php | 2 +- src/Sandbox/SecurityNotAllowedMethodError.php | 4 +- .../SecurityNotAllowedPropertyError.php | 4 +- src/Sandbox/SecurityNotAllowedTagError.php | 2 +- src/Sandbox/SecurityPolicy.php | 25 ++++++-- src/Source.php | 6 +- src/TemplateWrapper.php | 4 +- src/TokenStream.php | 9 ++- src/TwigFilter.php | 12 +++- src/TwigFunction.php | 12 +++- src/TwigTest.php | 12 +++- src/Util/DeprecationCollector.php | 2 +- tests/Node/ModuleTest.php | 12 ++-- 49 files changed, 342 insertions(+), 186 deletions(-) diff --git a/src/Cache/FilesystemCache.php b/src/Cache/FilesystemCache.php index f6bc7afc1..d1bd0ec8c 100644 --- a/src/Cache/FilesystemCache.php +++ b/src/Cache/FilesystemCache.php @@ -20,8 +20,8 @@ class FilesystemCache implements CacheInterface { public const FORCE_BYTECODE_INVALIDATION = 1; - private $directory; - private $options; + private string $directory; + private int $options; public function __construct(string $directory, int $options = 0) { diff --git a/src/Compiler.php b/src/Compiler.php index eb652c61a..f1d2047e6 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -19,18 +19,19 @@ use Twig\Node\Node; */ class Compiler { - private $lastLine; - private $source; - private $indentation; - private $env; - private $debugInfo = []; - private $sourceOffset; - private $sourceLine; - private $varNameSalt = 0; + private ?int $lastLine; + private string $source; + private int $indentation; + private Environment $env; + private array $debugInfo; + private int $sourceOffset; + private int $sourceLine; + private int $varNameSalt; public function __construct(Environment $env) { $this->env = $env; + $this->reset(); } public function getEnvironment(): Environment diff --git a/src/Environment.php b/src/Environment.php index fa7771333..cd95edd2b 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -47,25 +47,37 @@ class Environment public const RELEASE_VERSION = 0; public const EXTRA_VERSION = 'DEV'; - private $charset; - private $loader; - private $debug; - private $autoReload; - private $cache; - private $lexer; - private $parser; - private $compiler; + private string $charset; + private LoaderInterface $loader; + private bool $debug; + private bool $autoReload; + private CacheInterface|string|false $cache; + private ?Lexer $lexer = null; + private ?Parser $parser = null; + private ?Compiler $compiler = null; /** @var array */ - private $globals = []; - private $resolvedGlobals; - private $loadedTemplates; - private $strictVariables; - private $templateClassPrefix = '__TwigTemplate_'; - private $originalCache; - private $extensionSet; - private $runtimeLoaders = []; - private $runtimes = []; - private $optionsHash; + private array $globals = []; + /** + * @var array|null + */ + private ?array $resolvedGlobals = null; + /** + * @var Template[] + */ + private array $loadedTemplates; + private bool $strictVariables; + private string $templateClassPrefix = '__TwigTemplate_'; + private CacheInterface|string|false $originalCache; + private ExtensionSet $extensionSet; + /** + * @var RuntimeLoaderInterface[] + */ + private array $runtimeLoaders = []; + /** + * @var array + */ + private array $runtimes = []; + private string $optionsHash; /** * Constructor. @@ -438,7 +450,7 @@ class Environment $count = \count($names); foreach ($names as $name) { if ($name instanceof Template) { - return $name; + return new TemplateWrapper($this, $name); } if ($name instanceof TemplateWrapper) { return $name; diff --git a/src/Error/Error.php b/src/Error/Error.php index 6df67ef2b..0c63cd340 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -38,11 +38,11 @@ use Twig\Template; */ class Error extends \Exception { - private $lineno; - private $name; - private $rawMessage; - private $sourcePath; - private $sourceCode; + private int $lineno; + private ?string $name; + private string $rawMessage; + private ?string $sourcePath = null; + private ?string $sourceCode = null; /** * Constructor. diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 09f4e537b..a8e899032 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -48,12 +48,12 @@ class ExpressionParser public const OPERATOR_LEFT = 1; public const OPERATOR_RIGHT = 2; - private $parser; - private $env; + private Parser $parser; + private Environment $env; /** @var array}> */ - private $unaryOperators; + private array $unaryOperators; /** @var array, associativity: self::OPERATOR_*}> */ - private $binaryOperators; + private array $binaryOperators; public function __construct(Parser $parser, Environment $env) { diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index a9e38436a..d35f1a34f 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -83,9 +83,9 @@ use Twig\TwigTest; final class CoreExtension extends AbstractExtension { - private $dateFormats = ['F j, Y H:i', '%d days']; - private $numberFormat = [0, '.', ',']; - private $timezone = null; + private array $dateFormats = ['F j, Y H:i', '%d days']; + private array $numberFormat = [0, '.', ',']; + private ?\DateTimeZone $timezone = null; /** * Sets the default format to be used by the date filter. diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index 8134bddbd..bd1e70df2 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -24,7 +24,10 @@ use Twig\TwigFilter; final class EscaperExtension extends AbstractExtension { private $defaultStrategy; - private $escapers = []; + /** + * @var array + */ + private array $escapers = []; /** @internal */ public $safeClasses = []; @@ -110,7 +113,7 @@ final class EscaperExtension extends AbstractExtension /** * Gets all defined escapers. * - * @return callable[] An array of escapers + * @return array An array of escapers */ public function getEscapers() { diff --git a/src/Extension/OptimizerExtension.php b/src/Extension/OptimizerExtension.php index 965bfdb04..bf2cd7bf7 100644 --- a/src/Extension/OptimizerExtension.php +++ b/src/Extension/OptimizerExtension.php @@ -15,7 +15,7 @@ use Twig\NodeVisitor\OptimizerNodeVisitor; final class OptimizerExtension extends AbstractExtension { - private $optimizers; + private int $optimizers; public function __construct(int $optimizers = -1) { diff --git a/src/Extension/ProfilerExtension.php b/src/Extension/ProfilerExtension.php index 43e4a449e..206992816 100644 --- a/src/Extension/ProfilerExtension.php +++ b/src/Extension/ProfilerExtension.php @@ -16,7 +16,10 @@ use Twig\Profiler\Profile; class ProfilerExtension extends AbstractExtension { - private $actives = []; + /** + * @var Profile[] + */ + private array $actives = []; public function __construct(Profile $profile) { diff --git a/src/Extension/SandboxExtension.php b/src/Extension/SandboxExtension.php index c861159b6..120fece4d 100644 --- a/src/Extension/SandboxExtension.php +++ b/src/Extension/SandboxExtension.php @@ -20,9 +20,9 @@ use Twig\TokenParser\SandboxTokenParser; final class SandboxExtension extends AbstractExtension { - private $sandboxedGlobally; - private $sandboxed; - private $policy; + private bool $sandboxedGlobally; + private bool $sandboxed = false; + private SecurityPolicyInterface $policy; public function __construct(SecurityPolicyInterface $policy, $sandboxed = false) { diff --git a/src/Extension/StagingExtension.php b/src/Extension/StagingExtension.php index 0ea47f90c..f32738912 100644 --- a/src/Extension/StagingExtension.php +++ b/src/Extension/StagingExtension.php @@ -26,11 +26,26 @@ use Twig\TwigTest; */ final class StagingExtension extends AbstractExtension { - private $functions = []; - private $filters = []; - private $visitors = []; - private $tokenParsers = []; - private $tests = []; + /** + * @var array + */ + private array $functions = []; + /** + * @var array + */ + private array $filters = []; + /** + * @var list + */ + private array $visitors = []; + /** + * @var array + */ + private array $tokenParsers = []; + /** + * @var array + */ + private array $tests = []; public function addFunction(TwigFunction $function): void { diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index f7ecd0746..9d81786cc 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -27,28 +27,49 @@ use Twig\TokenParser\TokenParserInterface; */ final class ExtensionSet { - private $extensions; - private $initialized = false; - private $runtimeInitialized = false; + /** + * @var ExtensionInterface[] + */ + private array $extensions; + private bool $initialized = false; + private bool $runtimeInitialized = false; private $staging; - private $parsers; - private $visitors; - /** @var array */ - private $filters; - /** @var array */ - private $tests; - /** @var array */ - private $functions; - /** @var array}> */ - private $unaryOperators; - /** @var array, associativity: ExpressionParser::OPERATOR_*}> */ - private $binaryOperators; - /** @var array */ - private $globals; - private $functionCallbacks = []; - private $filterCallbacks = []; - private $parserCallbacks = []; - private $lastModified = 0; + /** + * @var TokenParserInterface[] + */ + private array $parsers; + /** + * @var NodeVisitorInterface[] + */ + private array $visitors; + /** + * @var array + */ + private array $filters; + /** + * @var array + */ + private array $tests; + /** + * @var array + */ + private array $functions; + /** + * @var array}> + */ + private array $unaryOperators; + /** + * @var array, associativity: ExpressionParser::OPERATOR_*}> + */ + private array $binaryOperators; + /** + * @var array|null + */ + private ?array $globals = null; + private array $functionCallbacks = []; + private array $filterCallbacks = []; + private array $parserCallbacks = []; + private int $lastModified = 0; public function __construct() { diff --git a/src/Lexer.php b/src/Lexer.php index 98ffbac0f..073084c6a 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -19,23 +19,38 @@ use Twig\Error\SyntaxError; */ class Lexer { - private $isInitialized = false; + private bool $isInitialized = false; - private $tokens; - private $code; - private $cursor; - private $lineno; - private $end; - private $state; - private $states; - private $brackets; - private $env; - private $source; - private $options; - private $regexes; - private $position; - private $positions; - private $currentVarBlockLine; + /** + * @var list + */ + private array $tokens; + private string $code; + private int $cursor; + private int $lineno; + private int $end; + private int $state; + /** + * @var int[] + */ + private array $states; + /** + * @var array + */ + private array $brackets; + private Environment $env; + private Source $source; + /** + * @var array + */ + private array $options; + /** + * @var array + */ + private array $regexes; + private int $position; + private array $positions; + private int $currentVarBlockLine; public const STATE_DATA = 0; public const STATE_BLOCK = 1; diff --git a/src/Loader/ArrayLoader.php b/src/Loader/ArrayLoader.php index 5d726c35a..9b7aa4787 100644 --- a/src/Loader/ArrayLoader.php +++ b/src/Loader/ArrayLoader.php @@ -28,10 +28,13 @@ use Twig\Source; */ final class ArrayLoader implements LoaderInterface { - private $templates = []; + /** + * @var array + */ + private array $templates = []; /** - * @param array $templates An array of templates (keys are the names, and values are the source code) + * @param array $templates An array of templates (keys are the names, and values are the source code) */ public function __construct(array $templates = []) { diff --git a/src/Loader/ChainLoader.php b/src/Loader/ChainLoader.php index 210ec174b..5625d8d3e 100644 --- a/src/Loader/ChainLoader.php +++ b/src/Loader/ChainLoader.php @@ -21,8 +21,15 @@ use Twig\Source; */ final class ChainLoader implements LoaderInterface { - private $hasSourceCache = []; - private $loaders = []; + /** + * @var array + */ + private array $hasSourceCache = []; + + /** + * @var LoaderInterface[] + */ + private array $loaders = []; /** * @param LoaderInterface[] $loaders diff --git a/src/Loader/FilesystemLoader.php b/src/Loader/FilesystemLoader.php index 1b277fe2f..0854ea642 100644 --- a/src/Loader/FilesystemLoader.php +++ b/src/Loader/FilesystemLoader.php @@ -28,7 +28,7 @@ class FilesystemLoader implements LoaderInterface protected $cache = []; protected $errorCache = []; - private $rootPath; + private string $rootPath; /** * @param string|array $paths A path or an array of paths where to look for templates diff --git a/src/Markup.php b/src/Markup.php index 1788acc4f..10e4f3e5c 100644 --- a/src/Markup.php +++ b/src/Markup.php @@ -18,8 +18,8 @@ namespace Twig; */ class Markup implements \Countable, \JsonSerializable { - private $content; - private $charset; + private string $content; + private string $charset; public function __construct($content, $charset) { diff --git a/src/Node/CheckSecurityNode.php b/src/Node/CheckSecurityNode.php index 472732796..811ecb0d3 100644 --- a/src/Node/CheckSecurityNode.php +++ b/src/Node/CheckSecurityNode.php @@ -18,9 +18,9 @@ use Twig\Compiler; */ class CheckSecurityNode extends Node { - private $usedFilters; - private $usedTags; - private $usedFunctions; + private array $usedFilters; + private array $usedTags; + private array $usedFunctions; public function __construct(array $usedFilters, array $usedTags, array $usedFunctions) { diff --git a/src/Node/Expression/ArrayExpression.php b/src/Node/Expression/ArrayExpression.php index a9674e28b..c5a5ac7cc 100644 --- a/src/Node/Expression/ArrayExpression.php +++ b/src/Node/Expression/ArrayExpression.php @@ -15,7 +15,7 @@ use Twig\Compiler; class ArrayExpression extends AbstractExpression { - private $index; + private int $index; public function __construct(array $elements, int $lineno) { diff --git a/src/Node/Expression/CallExpression.php b/src/Node/Expression/CallExpression.php index 6482cbff2..d9ca5818b 100644 --- a/src/Node/Expression/CallExpression.php +++ b/src/Node/Expression/CallExpression.php @@ -18,7 +18,7 @@ use Twig\Node\Node; abstract class CallExpression extends AbstractExpression { - private $reflector; + private ?array $reflector = null; protected function compileCallable(Compiler $compiler) { diff --git a/src/Node/Expression/NameExpression.php b/src/Node/Expression/NameExpression.php index c69513745..14697e84d 100644 --- a/src/Node/Expression/NameExpression.php +++ b/src/Node/Expression/NameExpression.php @@ -16,7 +16,7 @@ use Twig\Compiler; class NameExpression extends AbstractExpression { - private $specialVars = [ + private array $specialVars = [ '_self' => '$this->getTemplateName()', '_context' => '$context', '_charset' => '$this->env->getCharset()', diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index 78b361d8a..47ef98a9a 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -23,7 +23,7 @@ use Twig\Node\Expression\AssignNameExpression; */ class ForNode extends Node { - private $loop; + private ForLoopNode $loop; public function __construct(AssignNameExpression $keyTarget, AssignNameExpression $valueTarget, AbstractExpression $seq, ?Node $ifexpr, Node $body, ?Node $else, int $lineno, string $tag = null) { diff --git a/src/Node/ModuleNode.php b/src/Node/ModuleNode.php index dce335c63..6735c819f 100644 --- a/src/Node/ModuleNode.php +++ b/src/Node/ModuleNode.php @@ -161,8 +161,8 @@ final class ModuleNode extends Node ->raw(" extends Template\n") ->write("{\n") ->indent() - ->write("private \$source;\n") - ->write("private \$macros = [];\n\n") + ->write("private Source \$source;\n") + ->write("private array \$macros = [];\n\n") ; } diff --git a/src/Node/Node.php b/src/Node/Node.php index fcba26a49..f709726f0 100644 --- a/src/Node/Node.php +++ b/src/Node/Node.php @@ -27,7 +27,7 @@ class Node implements \Countable, \IteratorAggregate protected $lineno; protected $tag; - private $sourceContext; + private ?Source $sourceContext = null; /** * @param array $nodes An array of named nodes diff --git a/src/NodeTraverser.php b/src/NodeTraverser.php index 47a2d5ca3..3086e4696 100644 --- a/src/NodeTraverser.php +++ b/src/NodeTraverser.php @@ -23,8 +23,11 @@ use Twig\NodeVisitor\NodeVisitorInterface; */ final class NodeTraverser { - private $env; - private $visitors = []; + private Environment $env; + /** + * @var NodeVisitorInterface[][] + */ + private array $visitors = []; /** * @param NodeVisitorInterface[] $visitors diff --git a/src/NodeVisitor/EscaperNodeVisitor.php b/src/NodeVisitor/EscaperNodeVisitor.php index 8f79fde06..3c31cedcf 100644 --- a/src/NodeVisitor/EscaperNodeVisitor.php +++ b/src/NodeVisitor/EscaperNodeVisitor.php @@ -34,12 +34,18 @@ use Twig\NodeTraverser; */ final class EscaperNodeVisitor implements NodeVisitorInterface { - private $statusStack = []; - private $blocks = []; - private $safeAnalysis; - private $traverser; - private $defaultStrategy = false; - private $safeVars = []; + private array $statusStack = []; + /** + * @var array + */ + private array $blocks = []; + private SafeAnalysisNodeVisitor $safeAnalysis; + private ?NodeTraverser $traverser = null; + private string|false $defaultStrategy = false; + /** + * @var array + */ + private array $safeVars = []; public function __construct() { diff --git a/src/NodeVisitor/MacroAutoImportNodeVisitor.php b/src/NodeVisitor/MacroAutoImportNodeVisitor.php index d6a7781ba..ac5196110 100644 --- a/src/NodeVisitor/MacroAutoImportNodeVisitor.php +++ b/src/NodeVisitor/MacroAutoImportNodeVisitor.php @@ -28,8 +28,8 @@ use Twig\Node\Node; */ final class MacroAutoImportNodeVisitor implements NodeVisitorInterface { - private $inAModule = false; - private $hasMacroCalls = false; + private bool $inAModule = false; + private bool $hasMacroCalls = false; public function enterNode(Node $node, Environment $env): Node { diff --git a/src/NodeVisitor/OptimizerNodeVisitor.php b/src/NodeVisitor/OptimizerNodeVisitor.php index 6b39f0094..e1e860d37 100644 --- a/src/NodeVisitor/OptimizerNodeVisitor.php +++ b/src/NodeVisitor/OptimizerNodeVisitor.php @@ -44,9 +44,15 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface public const OPTIMIZE_FOR = 2; public const OPTIMIZE_RAW_FILTER = 4; - private $loops = []; - private $loopsTargets = []; - private $optimizers; + /** + * @var ForNode[] + */ + private array $loops = []; + /** + * @var string[] + */ + private array $loopsTargets = []; + private int $optimizers; /** * @param int $optimizers The optimizer mode diff --git a/src/NodeVisitor/SandboxNodeVisitor.php b/src/NodeVisitor/SandboxNodeVisitor.php index 1446cee6b..04431168c 100644 --- a/src/NodeVisitor/SandboxNodeVisitor.php +++ b/src/NodeVisitor/SandboxNodeVisitor.php @@ -33,11 +33,11 @@ use Twig\Node\SetNode; */ final class SandboxNodeVisitor implements NodeVisitorInterface { - private $inAModule = false; - private $tags; - private $filters; - private $functions; - private $needsToStringWrap = false; + private bool $inAModule = false; + private array $tags; + private array $filters; + private array $functions; + private bool $needsToStringWrap = false; public function enterNode(Node $node, Environment $env): Node { diff --git a/src/Parser.php b/src/Parser.php index 3e2dc1eda..bd48f7bc1 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -24,6 +24,7 @@ use Twig\Node\NodeCaptureInterface; use Twig\Node\NodeOutputInterface; use Twig\Node\PrintNode; use Twig\Node\TextNode; +use Twig\NodeVisitor\NodeVisitorInterface; use Twig\TokenParser\TokenParserInterface; /** @@ -31,19 +32,37 @@ use Twig\TokenParser\TokenParserInterface; */ class Parser { - private $stack = []; - private $stream; - private $parent; - private $visitors; - private $expressionParser; - private $blocks; - private $blockStack; - private $macros; - private $env; - private $importedSymbols; - private $traits; - private $embeddedTemplates = []; - private $varNameSalt = 0; + private array $stack = []; + private TokenStream $stream; + private ?Node $parent; + /** + * @var NodeVisitorInterface[]|null + */ + private ?array $visitors = null; + private ?ExpressionParser $expressionParser = null; + /** + * @var Node[] + */ + private array $blocks; + /** + * @var string[] + */ + private array $blockStack; + /** + * @var MacroNode[] + */ + private array $macros; + private Environment $env; + private array $importedSymbols; + /** + * @var Node[] + */ + private array $traits; + /** + * @var ModuleNode[] + */ + private array $embeddedTemplates = []; + private int $varNameSalt = 0; public function __construct(Environment $env) { diff --git a/src/Profiler/Dumper/BaseDumper.php b/src/Profiler/Dumper/BaseDumper.php index 4da43e475..807a0b8a0 100644 --- a/src/Profiler/Dumper/BaseDumper.php +++ b/src/Profiler/Dumper/BaseDumper.php @@ -18,7 +18,7 @@ use Twig\Profiler\Profile; */ abstract class BaseDumper { - private $root; + private float $root; public function dump(Profile $profile): string { diff --git a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php index e4df09f1d..933766763 100644 --- a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php +++ b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php @@ -27,8 +27,8 @@ use Twig\Profiler\Profile; */ final class ProfilerNodeVisitor implements NodeVisitorInterface { - private $extensionName; - private $varName; + private string $extensionName; + private string $varName; public function __construct(string $extensionName) { diff --git a/src/Profiler/Profile.php b/src/Profiler/Profile.php index 72506b7c8..042baaff6 100644 --- a/src/Profiler/Profile.php +++ b/src/Profiler/Profile.php @@ -21,12 +21,15 @@ final class Profile implements \IteratorAggregate, \Serializable public const TEMPLATE = 'template'; public const MACRO = 'macro'; - private $template; - private $name; - private $type; - private $starts = []; - private $ends = []; - private $profiles = []; + private string $template; + private string $name; + private string $type; + private array $starts = []; + private array $ends = []; + /** + * @var Profile[] + */ + private array $profiles = []; public function __construct(string $template = 'main', string $type = self::ROOT, string $name = 'main') { diff --git a/src/RuntimeLoader/ContainerRuntimeLoader.php b/src/RuntimeLoader/ContainerRuntimeLoader.php index b360d7bea..36435ea2d 100644 --- a/src/RuntimeLoader/ContainerRuntimeLoader.php +++ b/src/RuntimeLoader/ContainerRuntimeLoader.php @@ -23,7 +23,7 @@ use Psr\Container\ContainerInterface; */ class ContainerRuntimeLoader implements RuntimeLoaderInterface { - private $container; + private ContainerInterface $container; public function __construct(ContainerInterface $container) { diff --git a/src/RuntimeLoader/FactoryRuntimeLoader.php b/src/RuntimeLoader/FactoryRuntimeLoader.php index 130648392..6aad95caf 100644 --- a/src/RuntimeLoader/FactoryRuntimeLoader.php +++ b/src/RuntimeLoader/FactoryRuntimeLoader.php @@ -18,10 +18,13 @@ namespace Twig\RuntimeLoader; */ class FactoryRuntimeLoader implements RuntimeLoaderInterface { - private $map; + /** + * @var array + */ + private array $map; /** - * @param array $map An array where keys are class names and values factory callables + * @param array $map An array where keys are class names and values factory callables */ public function __construct(array $map = []) { diff --git a/src/Sandbox/SecurityNotAllowedFilterError.php b/src/Sandbox/SecurityNotAllowedFilterError.php index 02d306360..9293a3f0b 100644 --- a/src/Sandbox/SecurityNotAllowedFilterError.php +++ b/src/Sandbox/SecurityNotAllowedFilterError.php @@ -18,7 +18,7 @@ namespace Twig\Sandbox; */ final class SecurityNotAllowedFilterError extends SecurityError { - private $filterName; + private string $filterName; public function __construct(string $message, string $functionName) { diff --git a/src/Sandbox/SecurityNotAllowedFunctionError.php b/src/Sandbox/SecurityNotAllowedFunctionError.php index 4f76dc6ec..71c9f02bc 100644 --- a/src/Sandbox/SecurityNotAllowedFunctionError.php +++ b/src/Sandbox/SecurityNotAllowedFunctionError.php @@ -18,7 +18,7 @@ namespace Twig\Sandbox; */ final class SecurityNotAllowedFunctionError extends SecurityError { - private $functionName; + private string $functionName; public function __construct(string $message, string $functionName) { diff --git a/src/Sandbox/SecurityNotAllowedMethodError.php b/src/Sandbox/SecurityNotAllowedMethodError.php index 8df9d0baa..733f8dd71 100644 --- a/src/Sandbox/SecurityNotAllowedMethodError.php +++ b/src/Sandbox/SecurityNotAllowedMethodError.php @@ -18,8 +18,8 @@ namespace Twig\Sandbox; */ final class SecurityNotAllowedMethodError extends SecurityError { - private $className; - private $methodName; + private string $className; + private string $methodName; public function __construct(string $message, string $className, string $methodName) { diff --git a/src/Sandbox/SecurityNotAllowedPropertyError.php b/src/Sandbox/SecurityNotAllowedPropertyError.php index 42ec4f386..15ece2fbb 100644 --- a/src/Sandbox/SecurityNotAllowedPropertyError.php +++ b/src/Sandbox/SecurityNotAllowedPropertyError.php @@ -18,8 +18,8 @@ namespace Twig\Sandbox; */ final class SecurityNotAllowedPropertyError extends SecurityError { - private $className; - private $propertyName; + private string $className; + private string $propertyName; public function __construct(string $message, string $className, string $propertyName) { diff --git a/src/Sandbox/SecurityNotAllowedTagError.php b/src/Sandbox/SecurityNotAllowedTagError.php index 4522150e1..f9cd625b4 100644 --- a/src/Sandbox/SecurityNotAllowedTagError.php +++ b/src/Sandbox/SecurityNotAllowedTagError.php @@ -18,7 +18,7 @@ namespace Twig\Sandbox; */ final class SecurityNotAllowedTagError extends SecurityError { - private $tagName; + private string $tagName; public function __construct(string $message, string $tagName) { diff --git a/src/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index 8db241135..f376fb4e8 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -21,11 +21,26 @@ use Twig\Template; */ final class SecurityPolicy implements SecurityPolicyInterface { - private $allowedTags; - private $allowedFilters; - private $allowedMethods; - private $allowedProperties; - private $allowedFunctions; + /** + * @var string[] + */ + private array $allowedTags; + /** + * @var string[] + */ + private array $allowedFilters; + /** + * @var array + */ + private array $allowedMethods; + /** + * @var array + */ + private array $allowedProperties; + /** + * @var string[] + */ + private array $allowedFunctions; public function __construct(array $allowedTags = [], array $allowedFilters = [], array $allowedMethods = [], array $allowedProperties = [], array $allowedFunctions = []) { diff --git a/src/Source.php b/src/Source.php index 3cb02403c..87427da94 100644 --- a/src/Source.php +++ b/src/Source.php @@ -18,9 +18,9 @@ namespace Twig; */ final class Source { - private $code; - private $name; - private $path; + private string $code; + private string $name; + private string $path; /** * @param string $code The template source code diff --git a/src/TemplateWrapper.php b/src/TemplateWrapper.php index 7e71492e8..2518f133d 100644 --- a/src/TemplateWrapper.php +++ b/src/TemplateWrapper.php @@ -18,8 +18,8 @@ namespace Twig; */ final class TemplateWrapper { - private $env; - private $template; + private Environment $env; + private Template $template; /** * This method is for internal use only and should never be called diff --git a/src/TokenStream.php b/src/TokenStream.php index 1eac11a02..445768f92 100644 --- a/src/TokenStream.php +++ b/src/TokenStream.php @@ -21,9 +21,12 @@ use Twig\Error\SyntaxError; */ final class TokenStream { - private $tokens; - private $current = 0; - private $source; + /** + * @var Token[] + */ + private array $tokens; + private int $current = 0; + private Source $source; public function __construct(array $tokens, Source $source = null) { diff --git a/src/TwigFilter.php b/src/TwigFilter.php index 8993026c8..2e9de3d8c 100644 --- a/src/TwigFilter.php +++ b/src/TwigFilter.php @@ -23,10 +23,16 @@ use Twig\Node\Node; */ final class TwigFilter { - private $name; + private string $name; private $callable; - private $options; - private $arguments = []; + /** + * @var array + */ + private array $options; + /** + * @var array + */ + private array $arguments = []; /** * @param callable|array{class-string, string}|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation. diff --git a/src/TwigFunction.php b/src/TwigFunction.php index d910d1fd5..ee7bc6100 100644 --- a/src/TwigFunction.php +++ b/src/TwigFunction.php @@ -23,10 +23,16 @@ use Twig\Node\Node; */ final class TwigFunction { - private $name; + private string $name; private $callable; - private $options; - private $arguments = []; + /** + * @var array + */ + private array $options; + /** + * @var array + */ + private array $arguments = []; /** * @param callable|array{class-string, string}|null $callable A callable implementing the function. If null, you need to overwrite the "node_class" option to customize compilation. diff --git a/src/TwigTest.php b/src/TwigTest.php index 3769ec162..1f878e6ea 100644 --- a/src/TwigTest.php +++ b/src/TwigTest.php @@ -22,10 +22,16 @@ use Twig\Node\Expression\TestExpression; */ final class TwigTest { - private $name; + private string $name; private $callable; - private $options; - private $arguments = []; + /** + * @var array + */ + private array $options; + /** + * @var array + */ + private array $arguments = []; /** * @param callable|array{class-string, string}|null $callable A callable implementing the test. If null, you need to overwrite the "node_class" option to customize compilation. diff --git a/src/Util/DeprecationCollector.php b/src/Util/DeprecationCollector.php index 378b666bd..925c02398 100644 --- a/src/Util/DeprecationCollector.php +++ b/src/Util/DeprecationCollector.php @@ -20,7 +20,7 @@ use Twig\Source; */ final class DeprecationCollector { - private $twig; + private Environment $twig; public function __construct(Environment $twig) { diff --git a/tests/Node/ModuleTest.php b/tests/Node/ModuleTest.php index d6b378ad5..c9a95c81d 100644 --- a/tests/Node/ModuleTest.php +++ b/tests/Node/ModuleTest.php @@ -76,8 +76,8 @@ use Twig\Template; /* foo.twig */ class __TwigTemplate_%x extends Template { - private \$source; - private \$macros = []; + private Source \$source; + private array \$macros = []; public function __construct(Environment \$env) { @@ -147,8 +147,8 @@ use Twig\Template; /* foo.twig */ class __TwigTemplate_%x extends Template { - private \$source; - private \$macros = []; + private Source \$source; + private array \$macros = []; public function __construct(Environment \$env) { @@ -238,8 +238,8 @@ use Twig\Template; /* foo.twig */ class __TwigTemplate_%x extends Template { - private \$source; - private \$macros = []; + private Source \$source; + private array \$macros = []; public function __construct(Environment \$env) {