diff --git a/src/Cache/ChainCache.php b/src/Cache/ChainCache.php index 18c66f35f..c94afdb43 100644 --- a/src/Cache/ChainCache.php +++ b/src/Cache/ChainCache.php @@ -21,14 +21,12 @@ namespace Twig\Cache; */ final class ChainCache implements CacheInterface { - private $caches; - /** * @param iterable $caches The ordered list of caches used to store and fetch cached items */ - public function __construct(iterable $caches) - { - $this->caches = $caches; + public function __construct( + private iterable $caches, + ) { } public function generateKey(string $name, string $className): string diff --git a/src/Compiler.php b/src/Compiler.php index 1e7ed04c6..1a43aa7f6 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -22,7 +22,6 @@ class Compiler private $lastLine; private $source; private $indentation; - private $env; private $debugInfo = []; private $sourceOffset; private $sourceLine; @@ -30,9 +29,9 @@ class Compiler private $didUseEcho = false; private $didUseEchoStack = []; - public function __construct(Environment $env) - { - $this->env = $env; + public function __construct( + private Environment $env, + ) { } public function getEnvironment(): Environment diff --git a/src/Environment.php b/src/Environment.php index 237d59803..bb4bc09ae 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -107,7 +107,7 @@ class Environment * false (default): allows templates to use a mix of "yield" and "echo" calls to allow for a progressive migration * Switch to "true" when possible as this will be the only supported mode in Twig 4.0 */ - public function __construct(LoaderInterface $loader, $options = []) + public function __construct(LoaderInterface $loader, array $options = []) { $this->setLoader($loader); diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 28b556ccc..bfc1a1d04 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -47,18 +47,16 @@ class ExpressionParser public const OPERATOR_LEFT = 1; public const OPERATOR_RIGHT = 2; - private $parser; - private $env; /** @var array}> */ private $unaryOperators; /** @var array, associativity: self::OPERATOR_*}> */ private $binaryOperators; private $readyNodes = []; - public function __construct(Parser $parser, Environment $env) - { - $this->parser = $parser; - $this->env = $env; + public function __construct( + private Parser $parser, + private Environment $env, + ) { $this->unaryOperators = $env->getUnaryOperators(); $this->binaryOperators = $env->getBinaryOperators(); } diff --git a/src/Extension/OptimizerExtension.php b/src/Extension/OptimizerExtension.php index 965bfdb04..d3fe46a67 100644 --- a/src/Extension/OptimizerExtension.php +++ b/src/Extension/OptimizerExtension.php @@ -15,11 +15,9 @@ use Twig\NodeVisitor\OptimizerNodeVisitor; final class OptimizerExtension extends AbstractExtension { - private $optimizers; - - public function __construct(int $optimizers = -1) - { - $this->optimizers = $optimizers; + public function __construct( + private int $optimizers = -1, + ) { } public function getNodeVisitors(): array diff --git a/src/Extension/YieldNotReadyExtension.php b/src/Extension/YieldNotReadyExtension.php index 2503c8d81..49dfb8085 100644 --- a/src/Extension/YieldNotReadyExtension.php +++ b/src/Extension/YieldNotReadyExtension.php @@ -18,11 +18,9 @@ use Twig\NodeVisitor\YieldNotReadyNodeVisitor; */ final class YieldNotReadyExtension extends AbstractExtension { - private $useYield; - - public function __construct(bool $useYield) - { - $this->useYield = $useYield; + public function __construct( + private bool $useYield, + ) { } public function getNodeVisitors(): array diff --git a/src/Loader/ArrayLoader.php b/src/Loader/ArrayLoader.php index ce613c9cc..2bb54b7a8 100644 --- a/src/Loader/ArrayLoader.php +++ b/src/Loader/ArrayLoader.php @@ -28,14 +28,12 @@ use Twig\Source; */ final class ArrayLoader implements LoaderInterface { - private $templates = []; - /** * @param array $templates An array of templates (keys are the names, and values are the source code) */ - public function __construct(array $templates = []) - { - $this->templates = $templates; + public function __construct( + private array $templates = [], + ) { } public function setTemplate(string $name, string $template): void diff --git a/src/Loader/ChainLoader.php b/src/Loader/ChainLoader.php index 90f798db3..6e4f9511c 100644 --- a/src/Loader/ChainLoader.php +++ b/src/Loader/ChainLoader.php @@ -21,11 +21,6 @@ use Twig\Source; */ final class ChainLoader implements LoaderInterface { - /** - * @var \Traversable|LoaderInterface[] - */ - private $loaders; - /** * @var array */ @@ -34,9 +29,9 @@ final class ChainLoader implements LoaderInterface /** * @param iterable $loaders */ - public function __construct(iterable $loaders = []) - { - $this->loaders = $loaders; + public function __construct( + private iterable $loaders = [], + ) { } public function addLoader(LoaderInterface $loader): void diff --git a/src/Markup.php b/src/Markup.php index 1788acc4f..3020c60c4 100644 --- a/src/Markup.php +++ b/src/Markup.php @@ -19,7 +19,7 @@ namespace Twig; class Markup implements \Countable, \JsonSerializable { private $content; - private $charset; + private ?string $charset; public function __construct($content, $charset) { diff --git a/src/NodeVisitor/OptimizerNodeVisitor.php b/src/NodeVisitor/OptimizerNodeVisitor.php index 0d2dc02d5..a943f45c3 100644 --- a/src/NodeVisitor/OptimizerNodeVisitor.php +++ b/src/NodeVisitor/OptimizerNodeVisitor.php @@ -47,13 +47,13 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface private $loops = []; private $loopsTargets = []; - private $optimizers; /** * @param int $optimizers The optimizer mode */ - public function __construct(int $optimizers = -1) - { + public function __construct( + private int $optimizers = -1, + ) { if ($optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_TEXT_NODES)) { throw new \InvalidArgumentException(\sprintf('Optimizer mode "%s" is not valid.', $optimizers)); } @@ -65,8 +65,6 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface if (-1 !== $optimizers && self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $optimizers)) { trigger_deprecation('twig/twig', '3.12', 'The "Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES" option is deprecated and does nothing.'); } - - $this->optimizers = $optimizers; } public function enterNode(Node $node, Environment $env): Node diff --git a/src/NodeVisitor/YieldNotReadyNodeVisitor.php b/src/NodeVisitor/YieldNotReadyNodeVisitor.php index 6470bdabc..4b190b414 100644 --- a/src/NodeVisitor/YieldNotReadyNodeVisitor.php +++ b/src/NodeVisitor/YieldNotReadyNodeVisitor.php @@ -21,12 +21,11 @@ use Twig\Node\Node; */ final class YieldNotReadyNodeVisitor implements NodeVisitorInterface { - private $useYield; private $yieldReadyNodes = []; - public function __construct(bool $useYield) - { - $this->useYield = $useYield; + public function __construct( + private bool $useYield, + ) { } public function enterNode(Node $node, Environment $env): Node diff --git a/src/Parser.php b/src/Parser.php index 28cc8a0e1..cd8da2b8e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -40,15 +40,14 @@ class Parser private $blocks; private $blockStack; private $macros; - private $env; private $importedSymbols; private $traits; private $embeddedTemplates = []; private $varNameSalt = 0; - public function __construct(Environment $env) - { - $this->env = $env; + public function __construct( + private Environment $env, + ) { } public function getVarName(): string diff --git a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php index 4d2a58105..1458bc5fc 100644 --- a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php +++ b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php @@ -27,12 +27,11 @@ use Twig\Profiler\Profile; */ final class ProfilerNodeVisitor implements NodeVisitorInterface { - private $extensionName; private $varName; - public function __construct(string $extensionName) - { - $this->extensionName = $extensionName; + public function __construct( + private string $extensionName, + ) { $this->varName = \sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $extensionName)); } diff --git a/src/Profiler/Profile.php b/src/Profiler/Profile.php index 72506b7c8..2928e1646 100644 --- a/src/Profiler/Profile.php +++ b/src/Profiler/Profile.php @@ -20,18 +20,15 @@ final class Profile implements \IteratorAggregate, \Serializable public const BLOCK = 'block'; public const TEMPLATE = 'template'; public const MACRO = 'macro'; - - private $template; - private $name; - private $type; private $starts = []; private $ends = []; private $profiles = []; - public function __construct(string $template = 'main', string $type = self::ROOT, string $name = 'main') - { - $this->template = $template; - $this->type = $type; + public function __construct( + private string $template = 'main', + private string $type = self::ROOT, + private string $name = 'main', + ) { $this->name = str_starts_with($name, '__internal_') ? 'INTERNAL' : $name; $this->enter(); } diff --git a/src/Runtime/EscaperRuntime.php b/src/Runtime/EscaperRuntime.php index b1dac9640..e4aee629a 100644 --- a/src/Runtime/EscaperRuntime.php +++ b/src/Runtime/EscaperRuntime.php @@ -26,11 +26,9 @@ final class EscaperRuntime implements RuntimeExtensionInterface /** @internal */ public $safeLookup = []; - private $charset; - - public function __construct($charset = 'UTF-8') - { - $this->charset = $charset; + public function __construct( + private $charset = 'UTF-8', + ) { } /** diff --git a/src/RuntimeLoader/ContainerRuntimeLoader.php b/src/RuntimeLoader/ContainerRuntimeLoader.php index b360d7bea..05106680c 100644 --- a/src/RuntimeLoader/ContainerRuntimeLoader.php +++ b/src/RuntimeLoader/ContainerRuntimeLoader.php @@ -23,11 +23,9 @@ use Psr\Container\ContainerInterface; */ class ContainerRuntimeLoader implements RuntimeLoaderInterface { - private $container; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; + public function __construct( + private ContainerInterface $container, + ) { } public function load(string $class) diff --git a/src/RuntimeLoader/FactoryRuntimeLoader.php b/src/RuntimeLoader/FactoryRuntimeLoader.php index 130648392..5d4e70b92 100644 --- a/src/RuntimeLoader/FactoryRuntimeLoader.php +++ b/src/RuntimeLoader/FactoryRuntimeLoader.php @@ -18,14 +18,12 @@ namespace Twig\RuntimeLoader; */ class FactoryRuntimeLoader implements RuntimeLoaderInterface { - private $map; - /** * @param array $map An array where keys are class names and values factory callables */ - public function __construct(array $map = []) - { - $this->map = $map; + public function __construct( + private array $map = [], + ) { } public function load(string $class) diff --git a/src/Source.php b/src/Source.php index 3cb02403c..0f626b62d 100644 --- a/src/Source.php +++ b/src/Source.php @@ -18,20 +18,16 @@ namespace Twig; */ final class Source { - private $code; - private $name; - private $path; - /** * @param string $code The template source code * @param string $name The template logical name * @param string $path The filesystem path of the template if any */ - public function __construct(string $code, string $name, string $path = '') - { - $this->code = $code; - $this->name = $name; - $this->path = $path; + public function __construct( + private string $code, + private string $name, + private string $path = '', + ) { } public function getCode(): string diff --git a/src/Template.php b/src/Template.php index d3c0c229d..498c35a6d 100644 --- a/src/Template.php +++ b/src/Template.php @@ -35,7 +35,6 @@ abstract class Template protected $parent; protected $parents = []; - protected $env; protected $blocks = []; protected $traits = []; protected $extensions = []; @@ -43,9 +42,9 @@ abstract class Template private $useYield; - public function __construct(Environment $env) - { - $this->env = $env; + public function __construct( + protected Environment $env, + ) { $this->useYield = $env->useYield(); $this->extensions = $env->getExtensions(); } diff --git a/src/TemplateWrapper.php b/src/TemplateWrapper.php index fcfb070c7..c31f50161 100644 --- a/src/TemplateWrapper.php +++ b/src/TemplateWrapper.php @@ -18,19 +18,16 @@ namespace Twig; */ final class TemplateWrapper { - private $env; - private $template; - /** * This method is for internal use only and should never be called * directly (use Twig\Environment::load() instead). * * @internal */ - public function __construct(Environment $env, Template $template) - { - $this->env = $env; - $this->template = $template; + public function __construct( + private Environment $env, + private Template $template, + ) { } public function render(array $context = []): string diff --git a/src/Token.php b/src/Token.php index 5be39bdc7..237634ad1 100644 --- a/src/Token.php +++ b/src/Token.php @@ -17,10 +17,6 @@ namespace Twig; */ final class Token { - private $value; - private $type; - private $lineno; - public const EOF_TYPE = -1; public const TEXT_TYPE = 0; public const BLOCK_START_TYPE = 1; @@ -37,11 +33,11 @@ final class Token public const ARROW_TYPE = 12; public const SPREAD_TYPE = 13; - public function __construct(int $type, $value, int $lineno) - { - $this->type = $type; - $this->value = $value; - $this->lineno = $lineno; + public function __construct( + private int $type, + private $value, + private int $lineno, + ) { } public function __toString() diff --git a/src/TokenStream.php b/src/TokenStream.php index 32357f931..c91701bfe 100644 --- a/src/TokenStream.php +++ b/src/TokenStream.php @@ -21,13 +21,12 @@ use Twig\Error\SyntaxError; */ final class TokenStream { - private $tokens; private $current = 0; - private $source; - public function __construct(array $tokens, ?Source $source = null) - { - $this->tokens = $tokens; + public function __construct( + private array $tokens, + private ?Source $source = null, + ) { $this->source = $source ?: new Source('', ''); } diff --git a/src/Util/DeprecationCollector.php b/src/Util/DeprecationCollector.php index ad5310617..0ea26ed4b 100644 --- a/src/Util/DeprecationCollector.php +++ b/src/Util/DeprecationCollector.php @@ -20,11 +20,9 @@ use Twig\Source; */ final class DeprecationCollector { - private $twig; - - public function __construct(Environment $twig) - { - $this->twig = $twig; + public function __construct( + private Environment $twig, + ) { } /**