minor #3944 Add type hints to private properties (fabpot)

This PR was merged into the 4.x branch.

Discussion
----------

Add type hints to private properties

Commits
-------

35640b6d Add type hints to private properties
This commit is contained in:
Fabien Potencier
2023-12-14 20:45:59 +01:00
49 changed files with 342 additions and 186 deletions
+2 -2
View File
@@ -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)
{
+9 -8
View File
@@ -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
+31 -19
View File
@@ -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<string, mixed> */
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<string, mixed>|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<string, object>
*/
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;
+5 -5
View File
@@ -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.
+4 -4
View File
@@ -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<string, array{precedence: int, class: class-string<AbstractUnary>}> */
private $unaryOperators;
private array $unaryOperators;
/** @var array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: self::OPERATOR_*}> */
private $binaryOperators;
private array $binaryOperators;
public function __construct(Parser $parser, Environment $env)
{
+3 -3
View File
@@ -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.
+5 -2
View File
@@ -24,7 +24,10 @@ use Twig\TwigFilter;
final class EscaperExtension extends AbstractExtension
{
private $defaultStrategy;
private $escapers = [];
/**
* @var array<string, callable(Environment, string, string): string>
*/
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<string, callable> An array of escapers
*/
public function getEscapers()
{
+1 -1
View File
@@ -15,7 +15,7 @@ use Twig\NodeVisitor\OptimizerNodeVisitor;
final class OptimizerExtension extends AbstractExtension
{
private $optimizers;
private int $optimizers;
public function __construct(int $optimizers = -1)
{
+4 -1
View File
@@ -16,7 +16,10 @@ use Twig\Profiler\Profile;
class ProfilerExtension extends AbstractExtension
{
private $actives = [];
/**
* @var Profile[]
*/
private array $actives = [];
public function __construct(Profile $profile)
{
+3 -3
View File
@@ -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)
{
+20 -5
View File
@@ -26,11 +26,26 @@ use Twig\TwigTest;
*/
final class StagingExtension extends AbstractExtension
{
private $functions = [];
private $filters = [];
private $visitors = [];
private $tokenParsers = [];
private $tests = [];
/**
* @var array<string, TwigFunction>
*/
private array $functions = [];
/**
* @var array<string, TwigFilter>
*/
private array $filters = [];
/**
* @var list<NodeVisitorInterface>
*/
private array $visitors = [];
/**
* @var array<string, TokenParserInterface>
*/
private array $tokenParsers = [];
/**
* @var array<string, TwigTest>
*/
private array $tests = [];
public function addFunction(TwigFunction $function): void
{
+42 -21
View File
@@ -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<string, TwigFilter> */
private $filters;
/** @var array<string, TwigTest> */
private $tests;
/** @var array<string, TwigFunction> */
private $functions;
/** @var array<string, array{precedence: int, class: class-string<AbstractUnary>}> */
private $unaryOperators;
/** @var array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}> */
private $binaryOperators;
/** @var array<string, mixed> */
private $globals;
private $functionCallbacks = [];
private $filterCallbacks = [];
private $parserCallbacks = [];
private $lastModified = 0;
/**
* @var TokenParserInterface[]
*/
private array $parsers;
/**
* @var NodeVisitorInterface[]
*/
private array $visitors;
/**
* @var array<string, TwigFilter>
*/
private array $filters;
/**
* @var array<string, TwigTest>
*/
private array $tests;
/**
* @var array<string, TwigFunction>
*/
private array $functions;
/**
* @var array<string, array{precedence: int, class: class-string<AbstractUnary>}>
*/
private array $unaryOperators;
/**
* @var array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
*/
private array $binaryOperators;
/**
* @var array<string, mixed>|null
*/
private ?array $globals = null;
private array $functionCallbacks = [];
private array $filterCallbacks = [];
private array $parserCallbacks = [];
private int $lastModified = 0;
public function __construct()
{
+31 -16
View File
@@ -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<Token>
*/
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<int, array{0: string, 1: int}>
*/
private array $brackets;
private Environment $env;
private Source $source;
/**
* @var array<string, string>
*/
private array $options;
/**
* @var array<string, string>
*/
private array $regexes;
private int $position;
private array $positions;
private int $currentVarBlockLine;
public const STATE_DATA = 0;
public const STATE_BLOCK = 1;
+5 -2
View File
@@ -28,10 +28,13 @@ use Twig\Source;
*/
final class ArrayLoader implements LoaderInterface
{
private $templates = [];
/**
* @var array<string, string>
*/
private array $templates = [];
/**
* @param array $templates An array of templates (keys are the names, and values are the source code)
* @param array<string, string> $templates An array of templates (keys are the names, and values are the source code)
*/
public function __construct(array $templates = [])
{
+9 -2
View File
@@ -21,8 +21,15 @@ use Twig\Source;
*/
final class ChainLoader implements LoaderInterface
{
private $hasSourceCache = [];
private $loaders = [];
/**
* @var array<string, bool>
*/
private array $hasSourceCache = [];
/**
* @var LoaderInterface[]
*/
private array $loaders = [];
/**
* @param LoaderInterface[] $loaders
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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)
{
+3 -3
View File
@@ -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)
{
+1 -1
View File
@@ -15,7 +15,7 @@ use Twig\Compiler;
class ArrayExpression extends AbstractExpression
{
private $index;
private int $index;
public function __construct(array $elements, int $lineno)
{
+1 -1
View File
@@ -18,7 +18,7 @@ use Twig\Node\Node;
abstract class CallExpression extends AbstractExpression
{
private $reflector;
private ?array $reflector = null;
protected function compileCallable(Compiler $compiler)
{
+1 -1
View File
@@ -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()',
+1 -1
View File
@@ -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)
{
+2 -2
View File
@@ -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")
;
}
+1 -1
View File
@@ -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
+5 -2
View File
@@ -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
+12 -6
View File
@@ -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<string, bool>
*/
private array $blocks = [];
private SafeAnalysisNodeVisitor $safeAnalysis;
private ?NodeTraverser $traverser = null;
private string|false $defaultStrategy = false;
/**
* @var array<string>
*/
private array $safeVars = [];
public function __construct()
{
@@ -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
{
+9 -3
View File
@@ -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
+5 -5
View File
@@ -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
{
+32 -13
View File
@@ -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)
{
+1 -1
View File
@@ -18,7 +18,7 @@ use Twig\Profiler\Profile;
*/
abstract class BaseDumper
{
private $root;
private float $root;
public function dump(Profile $profile): string
{
@@ -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)
{
+9 -6
View File
@@ -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')
{
+1 -1
View File
@@ -23,7 +23,7 @@ use Psr\Container\ContainerInterface;
*/
class ContainerRuntimeLoader implements RuntimeLoaderInterface
{
private $container;
private ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
+5 -2
View File
@@ -18,10 +18,13 @@ namespace Twig\RuntimeLoader;
*/
class FactoryRuntimeLoader implements RuntimeLoaderInterface
{
private $map;
/**
* @var array<string, callable(): object>
*/
private array $map;
/**
* @param array $map An array where keys are class names and values factory callables
* @param array<string, callable(): object> $map An array where keys are class names and values factory callables
*/
public function __construct(array $map = [])
{
@@ -18,7 +18,7 @@ namespace Twig\Sandbox;
*/
final class SecurityNotAllowedFilterError extends SecurityError
{
private $filterName;
private string $filterName;
public function __construct(string $message, string $functionName)
{
@@ -18,7 +18,7 @@ namespace Twig\Sandbox;
*/
final class SecurityNotAllowedFunctionError extends SecurityError
{
private $functionName;
private string $functionName;
public function __construct(string $message, string $functionName)
{
@@ -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)
{
@@ -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)
{
+1 -1
View File
@@ -18,7 +18,7 @@ namespace Twig\Sandbox;
*/
final class SecurityNotAllowedTagError extends SecurityError
{
private $tagName;
private string $tagName;
public function __construct(string $message, string $tagName)
{
+20 -5
View File
@@ -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<string, string[]>
*/
private array $allowedMethods;
/**
* @var array<string, string|string[]>
*/
private array $allowedProperties;
/**
* @var string[]
*/
private array $allowedFunctions;
public function __construct(array $allowedTags = [], array $allowedFilters = [], array $allowedMethods = [], array $allowedProperties = [], array $allowedFunctions = [])
{
+3 -3
View File
@@ -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
+2 -2
View File
@@ -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
+6 -3
View File
@@ -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)
{
+9 -3
View File
@@ -23,10 +23,16 @@ use Twig\Node\Node;
*/
final class TwigFilter
{
private $name;
private string $name;
private $callable;
private $options;
private $arguments = [];
/**
* @var array<string, mixed>
*/
private array $options;
/**
* @var array<int, mixed>
*/
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.
+9 -3
View File
@@ -23,10 +23,16 @@ use Twig\Node\Node;
*/
final class TwigFunction
{
private $name;
private string $name;
private $callable;
private $options;
private $arguments = [];
/**
* @var array<string, mixed>
*/
private array $options;
/**
* @var array<int, mixed>
*/
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.
+9 -3
View File
@@ -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<string, mixed>
*/
private array $options;
/**
* @var array<int, mixed>
*/
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.
+1 -1
View File
@@ -20,7 +20,7 @@ use Twig\Source;
*/
final class DeprecationCollector
{
private $twig;
private Environment $twig;
public function __construct(Environment $twig)
{
+6 -6
View File
@@ -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)
{