Skip to content

Commit 72c91f6

Browse files
committed
minor twigphp#4534 Add return type annotations on Twig 3 to prepare Twig 4 (smnandre)
This PR was squashed before being merged into the 3.x branch. Discussion ---------- Add return type annotations on Twig 3 to prepare Twig 4 Started to add missing type annotation following `@stof` comment [here](twigphp#4523 (comment)). Update: I checked here every class (but one) in the `src` directory. But you should ~~probably~~ **certainely** have another look. Remains: - src/ExpressionParser.php, where i'm not confident enough about the best return types (AbstractExpression|null works almost everytime, but lack some precision) - extra directory - tests directory I'm happy to let them to someone else.... this was less fun than I expected 😅 Commits ------- 82be134 Add return type annotations on Twig 3 to prepare Twig 4
2 parents 03f6351 + 82be134 commit 72c91f6

31 files changed

+224
-19
lines changed

src/AbstractTwigCallable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function getDynamicName(): string
7979
return $this->dynamicName;
8080
}
8181

82+
/**
83+
* @return callable|array{class-string, string}|null
84+
*/
8285
public function getCallable()
8386
{
8487
return $this->callable;

src/Environment.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public function useYield(): bool
155155

156156
/**
157157
* Enables debugging mode.
158+
*
159+
* @return void
158160
*/
159161
public function enableDebug()
160162
{
@@ -164,6 +166,8 @@ public function enableDebug()
164166

165167
/**
166168
* Disables debugging mode.
169+
*
170+
* @return void
167171
*/
168172
public function disableDebug()
169173
{
@@ -183,6 +187,8 @@ public function isDebug()
183187

184188
/**
185189
* Enables the auto_reload option.
190+
*
191+
* @return void
186192
*/
187193
public function enableAutoReload()
188194
{
@@ -191,6 +197,8 @@ public function enableAutoReload()
191197

192198
/**
193199
* Disables the auto_reload option.
200+
*
201+
* @return void
194202
*/
195203
public function disableAutoReload()
196204
{
@@ -209,6 +217,8 @@ public function isAutoReload()
209217

210218
/**
211219
* Enables the strict_variables option.
220+
*
221+
* @return void
212222
*/
213223
public function enableStrictVariables()
214224
{
@@ -218,6 +228,8 @@ public function enableStrictVariables()
218228

219229
/**
220230
* Disables the strict_variables option.
231+
*
232+
* @return void
221233
*/
222234
public function disableStrictVariables()
223235
{
@@ -267,6 +279,8 @@ public function getCache($original = true)
267279
* @param CacheInterface|string|false $cache A Twig\Cache\CacheInterface implementation,
268280
* an absolute path to the compiled templates,
269281
* or false to disable cache
282+
*
283+
* @return void
270284
*/
271285
public function setCache($cache)
272286
{
@@ -503,6 +517,9 @@ public function resolveTemplate($names): TemplateWrapper
503517
throw new LoaderError(\sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));
504518
}
505519

520+
/**
521+
* @return void
522+
*/
506523
public function setLexer(Lexer $lexer)
507524
{
508525
$this->lexer = $lexer;
@@ -520,6 +537,9 @@ public function tokenize(Source $source): TokenStream
520537
return $this->lexer->tokenize($source);
521538
}
522539

540+
/**
541+
* @return void
542+
*/
523543
public function setParser(Parser $parser)
524544
{
525545
$this->parser = $parser;
@@ -539,6 +559,9 @@ public function parse(TokenStream $stream): ModuleNode
539559
return $this->parser->parse($stream);
540560
}
541561

562+
/**
563+
* @return void
564+
*/
542565
public function setCompiler(Compiler $compiler)
543566
{
544567
$this->compiler = $compiler;
@@ -573,6 +596,9 @@ public function compileSource(Source $source): string
573596
}
574597
}
575598

599+
/**
600+
* @return void
601+
*/
576602
public function setLoader(LoaderInterface $loader)
577603
{
578604
$this->loader = $loader;
@@ -583,6 +609,9 @@ public function getLoader(): LoaderInterface
583609
return $this->loader;
584610
}
585611

612+
/**
613+
* @return void
614+
*/
586615
public function setCharset(string $charset)
587616
{
588617
if ('UTF8' === $charset = strtoupper($charset ?: '')) {
@@ -603,6 +632,9 @@ public function hasExtension(string $class): bool
603632
return $this->extensionSet->hasExtension($class);
604633
}
605634

635+
/**
636+
* @return void
637+
*/
606638
public function addRuntimeLoader(RuntimeLoaderInterface $loader)
607639
{
608640
$this->runtimeLoaders[] = $loader;
@@ -650,6 +682,9 @@ public function getRuntime(string $class)
650682
throw new RuntimeError(\sprintf('Unable to load the "%s" runtime.', $class));
651683
}
652684

685+
/**
686+
* @return void
687+
*/
653688
public function addExtension(ExtensionInterface $extension)
654689
{
655690
$this->extensionSet->addExtension($extension);
@@ -658,6 +693,8 @@ public function addExtension(ExtensionInterface $extension)
658693

659694
/**
660695
* @param ExtensionInterface[] $extensions An array of extensions
696+
*
697+
* @return void
661698
*/
662699
public function setExtensions(array $extensions)
663700
{
@@ -673,6 +710,9 @@ public function getExtensions(): array
673710
return $this->extensionSet->getExtensions();
674711
}
675712

713+
/**
714+
* @return void
715+
*/
676716
public function addTokenParser(TokenParserInterface $parser)
677717
{
678718
$this->extensionSet->addTokenParser($parser);
@@ -701,6 +741,9 @@ public function registerUndefinedTokenParserCallback(callable $callable): void
701741
$this->extensionSet->registerUndefinedTokenParserCallback($callable);
702742
}
703743

744+
/**
745+
* @return void
746+
*/
704747
public function addNodeVisitor(NodeVisitorInterface $visitor)
705748
{
706749
$this->extensionSet->addNodeVisitor($visitor);
@@ -716,6 +759,9 @@ public function getNodeVisitors(): array
716759
return $this->extensionSet->getNodeVisitors();
717760
}
718761

762+
/**
763+
* @return void
764+
*/
719765
public function addFilter(TwigFilter $filter)
720766
{
721767
$this->extensionSet->addFilter($filter);
@@ -750,6 +796,9 @@ public function getFilters(): array
750796
return $this->extensionSet->getFilters();
751797
}
752798

799+
/**
800+
* @return void
801+
*/
753802
public function addTest(TwigTest $test)
754803
{
755804
$this->extensionSet->addTest($test);
@@ -773,6 +822,9 @@ public function getTest(string $name): ?TwigTest
773822
return $this->extensionSet->getTest($name);
774823
}
775824

825+
/**
826+
* @return void
827+
*/
776828
public function addFunction(TwigFunction $function)
777829
{
778830
$this->extensionSet->addFunction($function);
@@ -814,6 +866,8 @@ public function getFunctions(): array
814866
* but after, you can only update existing globals.
815867
*
816868
* @param mixed $value The global value
869+
*
870+
* @return void
817871
*/
818872
public function addGlobal(string $name, $value)
819873
{

src/Extension/EscaperExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function setEnvironment(Environment $environment): void
8080
}
8181

8282
/**
83+
* @return void
84+
*
8385
* @deprecated since Twig 3.10
8486
*/
8587
public function setEscaperRuntime(EscaperRuntime $escaper)
@@ -130,6 +132,8 @@ public function getDefaultStrategy(string $name)
130132
* @param string $strategy The strategy name that should be used as a strategy in the escape call
131133
* @param callable(Environment, string, string): string $callable A valid PHP callable
132134
*
135+
* @return void
136+
*
133137
* @deprecated since Twig 3.10
134138
*/
135139
public function setEscaper($strategy, callable $callable)
@@ -163,6 +167,8 @@ public function getEscapers()
163167
}
164168

165169
/**
170+
* @return void
171+
*
166172
* @deprecated since Twig 3.10
167173
*/
168174
public function setSafeClasses(array $safeClasses = [])
@@ -177,6 +183,8 @@ public function setSafeClasses(array $safeClasses = [])
177183
}
178184

179185
/**
186+
* @return void
187+
*
180188
* @deprecated since Twig 3.10
181189
*/
182190
public function addSafeClass(string $class, array $strategies)
@@ -192,6 +200,8 @@ public function addSafeClass(string $class, array $strategies)
192200

193201
/**
194202
* @internal
203+
*
204+
* @return array<string>
195205
*/
196206
public static function escapeFilterIsSafe(Node $filterArgs)
197207
{

src/Extension/SandboxExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function isSourceSandboxed(?Source $source): bool
7272
return $this->sourcePolicy->enableSandbox($source);
7373
}
7474

75-
public function setSecurityPolicy(SecurityPolicyInterface $policy)
75+
public function setSecurityPolicy(SecurityPolicyInterface $policy): void
7676
{
7777
$this->policy = $policy;
7878
}
@@ -117,6 +117,13 @@ public function checkPropertyAllowed($obj, $property, int $lineno = -1, ?Source
117117
}
118118
}
119119

120+
/**
121+
* @param mixed $obj
122+
*
123+
* @return mixed
124+
*
125+
* @throws SecurityNotAllowedMethodError
126+
*/
120127
public function ensureToStringAllowed($obj, int $lineno = -1, ?Source $source = null)
121128
{
122129
if (\is_array($obj)) {

src/ExtensionSet.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function __construct()
6262
$this->staging = new StagingExtension();
6363
}
6464

65+
/**
66+
* @return void
67+
*/
6568
public function initRuntime()
6669
{
6770
$this->runtimeInitialized = true;

src/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(Environment $env, array $options = [])
8282
], $options);
8383
}
8484

85-
private function initialize()
85+
private function initialize(): void
8686
{
8787
if ($this->isInitialized) {
8888
return;

src/Markup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct($content, $charset)
2727
$this->charset = $charset;
2828
}
2929

30-
public function __toString()
30+
public function __toString(): string
3131
{
3232
return $this->content;
3333
}

src/Node/CheckSecurityCallNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#[YieldReady]
2121
class CheckSecurityCallNode extends Node
2222
{
23+
/**
24+
* @return void
25+
*/
2326
public function compile(Compiler $compiler)
2427
{
2528
$compiler

src/Node/Expression/CallExpression.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ abstract class CallExpression extends AbstractExpression
2626
{
2727
private $reflector = null;
2828

29+
/**
30+
* @return void
31+
*/
2932
protected function compileCallable(Compiler $compiler)
3033
{
3134
$twigCallable = $this->getTwigCallable();

src/Node/Expression/FunctionExpression.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function __construct(TwigFunction|string $function, Node $arguments, int
4444
$this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12'));
4545
}
4646

47+
/**
48+
* @return void
49+
*/
4750
public function compile(Compiler $compiler)
4851
{
4952
$name = $this->getAttribute('name');

0 commit comments

Comments
 (0)