Skip to content

Commit b3724b7

Browse files
committed
[TwigBridge] Add types to private properties
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent 48859fe commit b3724b7

31 files changed

+89
-117
lines changed

AppVariable.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
*/
2626
class AppVariable
2727
{
28-
private $tokenStorage;
29-
private $requestStack;
30-
private $environment;
31-
private $debug;
28+
private TokenStorageInterface $tokenStorage;
29+
private RequestStack $requestStack;
30+
private string $environment;
31+
private bool $debug;
3232

3333
public function setTokenStorage(TokenStorageInterface $tokenStorage)
3434
{
@@ -57,11 +57,11 @@ public function setDebug(bool $debug)
5757
*/
5858
public function getToken(): ?TokenInterface
5959
{
60-
if (null === $tokenStorage = $this->tokenStorage) {
60+
if (!isset($this->tokenStorage)) {
6161
throw new \RuntimeException('The "app.token" variable is not available.');
6262
}
6363

64-
return $tokenStorage->getToken();
64+
return $this->tokenStorage->getToken();
6565
}
6666

6767
/**
@@ -71,23 +71,19 @@ public function getToken(): ?TokenInterface
7171
*/
7272
public function getUser(): ?UserInterface
7373
{
74-
if (null === $tokenStorage = $this->tokenStorage) {
74+
if (!isset($this->tokenStorage)) {
7575
throw new \RuntimeException('The "app.user" variable is not available.');
7676
}
7777

78-
if (!$token = $tokenStorage->getToken()) {
79-
return null;
80-
}
81-
82-
return $token->getUser();
78+
return $this->tokenStorage->getToken()?->getUser();
8379
}
8480

8581
/**
8682
* Returns the current request.
8783
*/
8884
public function getRequest(): ?Request
8985
{
90-
if (null === $this->requestStack) {
86+
if (!isset($this->requestStack)) {
9187
throw new \RuntimeException('The "app.request" variable is not available.');
9288
}
9389

@@ -99,7 +95,7 @@ public function getRequest(): ?Request
9995
*/
10096
public function getSession(): ?Session
10197
{
102-
if (null === $this->requestStack) {
98+
if (!isset($this->requestStack)) {
10399
throw new \RuntimeException('The "app.session" variable is not available.');
104100
}
105101
$request = $this->getRequest();
@@ -112,7 +108,7 @@ public function getSession(): ?Session
112108
*/
113109
public function getEnvironment(): string
114110
{
115-
if (null === $this->environment) {
111+
if (!isset($this->environment)) {
116112
throw new \RuntimeException('The "app.environment" variable is not available.');
117113
}
118114

@@ -124,7 +120,7 @@ public function getEnvironment(): string
124120
*/
125121
public function getDebug(): bool
126122
{
127-
if (null === $this->debug) {
123+
if (!isset($this->debug)) {
128124
throw new \RuntimeException('The "app.debug" variable is not available.');
129125
}
130126

Command/DebugCommand.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ class DebugCommand extends Command
3535
protected static $defaultName = 'debug:twig';
3636
protected static $defaultDescription = 'Show a list of twig functions, filters, globals and tests';
3737

38-
private $twig;
39-
private $projectDir;
40-
private $bundlesMetadata;
41-
private $twigDefaultPath;
42-
private $filesystemLoaders;
43-
private $fileLinkFormatter;
38+
private Environment $twig;
39+
private ?string $projectDir;
40+
private array $bundlesMetadata;
41+
private ?string $twigDefaultPath;
42+
43+
/**
44+
* @var FilesystemLoader[]
45+
*/
46+
private array $filesystemLoaders;
47+
48+
private ?FileLinkFormatter $fileLinkFormatter;
4449

4550
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, FileLinkFormatter $fileLinkFormatter = null)
4651
{
@@ -557,7 +562,7 @@ private function isAbsolutePath(string $file): bool
557562
*/
558563
private function getFilesystemLoaders(): array
559564
{
560-
if (null !== $this->filesystemLoaders) {
565+
if (isset($this->filesystemLoaders)) {
561566
return $this->filesystemLoaders;
562567
}
563568
$this->filesystemLoaders = [];

Command/LintCommand.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ class LintCommand extends Command
3838
protected static $defaultName = 'lint:twig';
3939
protected static $defaultDescription = 'Lint a Twig template and outputs encountered errors';
4040

41-
private $twig;
42-
43-
/**
44-
* @var string|null
45-
*/
46-
private $format;
41+
private Environment $twig;
42+
private string $format;
4743

4844
public function __construct(Environment $twig)
4945
{
@@ -86,11 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8682
$io = new SymfonyStyle($input, $output);
8783
$filenames = $input->getArgument('filename');
8884
$showDeprecations = $input->getOption('show-deprecations');
89-
$this->format = $input->getOption('format');
90-
91-
if (null === $this->format) {
92-
$this->format = GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
93-
}
85+
$this->format = $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt');
9486

9587
if (['-'] === $filenames) {
9688
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), uniqid('sf_', true))]);

DataCollector/TwigDataCollector.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
*/
2929
class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
3030
{
31-
private $profile;
32-
private $twig;
33-
private $computed;
31+
private Profile $profile;
32+
private ?Environment $twig;
33+
private array $computed;
3434

3535
public function __construct(Profile $profile, Environment $twig = null)
3636
{
@@ -51,7 +51,7 @@ public function collect(Request $request, Response $response, \Throwable $except
5151
public function reset()
5252
{
5353
$this->profile->reset();
54-
$this->computed = null;
54+
unset($this->computed);
5555
$this->data = [];
5656
}
5757

@@ -140,18 +140,12 @@ public function getHtmlCallGraph()
140140

141141
public function getProfile()
142142
{
143-
if (null === $this->profile) {
144-
$this->profile = unserialize($this->data['profile'], ['allowed_classes' => ['Twig_Profiler_Profile', 'Twig\Profiler\Profile']]);
145-
}
146-
147-
return $this->profile;
143+
return $this->profile ??= unserialize($this->data['profile'], ['allowed_classes' => ['Twig_Profiler_Profile', 'Twig\Profiler\Profile']]);
148144
}
149145

150146
private function getComputedData(string $index)
151147
{
152-
if (null === $this->computed) {
153-
$this->computed = $this->computeData($this->getProfile());
154-
}
148+
$this->computed ??= $this->computeData($this->getProfile());
155149

156150
return $this->computed[$index];
157151
}

ErrorRenderer/TwigErrorRenderer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
*/
2626
class TwigErrorRenderer implements ErrorRendererInterface
2727
{
28-
private $twig;
29-
private $fallbackErrorRenderer;
30-
private $debug;
28+
private Environment $twig;
29+
private HtmlErrorRenderer $fallbackErrorRenderer;
30+
private \Closure|bool $debug;
3131

3232
/**
3333
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
@@ -36,7 +36,7 @@ public function __construct(Environment $twig, HtmlErrorRenderer $fallbackErrorR
3636
{
3737
$this->twig = $twig;
3838
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
39-
$this->debug = $debug;
39+
$this->debug = !\is_callable($debug) || $debug instanceof \Closure ? $debug : \Closure::fromCallable($debug);
4040
}
4141

4242
/**

Extension/AssetExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
final class AssetExtension extends AbstractExtension
2424
{
25-
private $packages;
25+
private Packages $packages;
2626

2727
public function __construct(Packages $packages)
2828
{

Extension/CodeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
final class CodeExtension extends AbstractExtension
2424
{
25-
private $fileLinkFormat;
26-
private $charset;
27-
private $projectDir;
25+
private string|FileLinkFormatter|array|false $fileLinkFormat;
26+
private string $charset;
27+
private string $projectDir;
2828

2929
public function __construct(string|FileLinkFormatter $fileLinkFormat, string $projectDir, string $charset)
3030
{

Extension/CsrfRuntime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
final class CsrfRuntime
2121
{
22-
private $csrfTokenManager;
22+
private CsrfTokenManagerInterface $csrfTokenManager;
2323

2424
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
2525
{

Extension/DumpExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
*/
2727
final class DumpExtension extends AbstractExtension
2828
{
29-
private $cloner;
30-
private $dumper;
29+
private ClonerInterface $cloner;
30+
private ?HtmlDumper $dumper;
3131

3232
public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
3333
{

Extension/FormExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
final class FormExtension extends AbstractExtension
3232
{
33-
private $translator;
33+
private ?TranslatorInterface $translator;
3434

3535
public function __construct(TranslatorInterface $translator = null)
3636
{

0 commit comments

Comments
 (0)