Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MartinMystikJonas/05a11cbbb404184493b2353f4041a6bc to your computer and use it in GitHub Desktop.
Save MartinMystikJonas/05a11cbbb404184493b2353f4041a6bc to your computer and use it in GitHub Desktop.
diff --git a/src/Latte/Compiler/TemplateGenerator.php b/src/Latte/Compiler/TemplateGenerator.php
index c522437b..b7fe0e91 100644
--- a/src/Latte/Compiler/TemplateGenerator.php
+++ b/src/Latte/Compiler/TemplateGenerator.php
@@ -197,7 +197,7 @@ private static function buildParams(
? implode('', $res) . 'unset($ʟ_args);'
: "extract($cont);" . (str_contains($cont, '$this') ? '' : "unset($cont);");
- return $extract . "\n"
+ return $extract
. $scope->extractTypes() . "\n\n"
. $body;
}
diff --git a/src/Latte/Compiler/VariableScope.php b/src/Latte/Compiler/VariableScope.php
index 0d77ec94..a6c1fd84 100644
--- a/src/Latte/Compiler/VariableScope.php
+++ b/src/Latte/Compiler/VariableScope.php
@@ -46,6 +46,6 @@ public static function printComment(string $name, ?string $type): string
public function extractTypes(): string
{
- return implode('', $this->types) . "\n";
+ return implode("\n", array_filter($this->types));
}
}
diff --git a/src/Latte/Essential/Nodes/VarNode.php b/src/Latte/Essential/Nodes/VarNode.php
index 8b7561a3..b9e6dab2 100644
--- a/src/Latte/Essential/Nodes/VarNode.php
+++ b/src/Latte/Essential/Nodes/VarNode.php
@@ -76,7 +76,7 @@ public function print(PrintContext $context): string
? $assign->var->name->print($context)
: $context->encodeString($assign->var->name);
$res[] = $var . ' => ' . $assign->expr->print($context);
- $types[] = $scope->addExpression($var, $type);
+ $types[] = $scope->addVariable($var, $type);
}
return $context->format(
diff --git a/tests/common/expected/Compiler.unquoted.attrs.phtml b/tests/common/expected/Compiler.unquoted.attrs.phtml
index 7896aa03..e0f285d7 100644
--- a/tests/common/expected/Compiler.unquoted.attrs.phtml
+++ b/tests/common/expected/Compiler.unquoted.attrs.phtml
@@ -40,3 +40,4 @@ final class Template%a% extends Latte\Runtime\Template
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/common/expected/contentType.ical.phtml b/tests/common/expected/contentType.ical.phtml
index 47fc9154..b45c44dd 100644
--- a/tests/common/expected/contentType.ical.phtml
+++ b/tests/common/expected/contentType.ical.phtml
@@ -49,3 +49,4 @@ World' /* line %d% */;
return get_defined_vars();
}
}
+%A%
\ No newline at end of file
diff --git a/tests/common/expected/contentType.xml.phtml b/tests/common/expected/contentType.xml.phtml
index 656b9de6..498ec529 100644
--- a/tests/common/expected/contentType.xml.phtml
+++ b/tests/common/expected/contentType.xml.phtml
@@ -147,3 +147,4 @@ var html = ';
return get_defined_vars();
}
}
+%A%
\ No newline at end of file
diff --git a/tests/filters/expected/general.phtml b/tests/filters/expected/general.phtml
index 9c18078b..23a7c060 100644
--- a/tests/filters/expected/general.phtml
+++ b/tests/filters/expected/general.phtml
@@ -95,3 +95,4 @@ bar')) /* line %d% */;
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/block.dynamic.phtml b/tests/tags/expected/block.dynamic.phtml
index 80ead1d2..6cf2e82a 100644
--- a/tests/tags/expected/block.dynamic.phtml
+++ b/tests/tags/expected/block.dynamic.phtml
@@ -109,3 +109,4 @@ final class Template%a% extends Latte\Runtime\Template
echo ' expression ';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/block.local.dynamic.phtml b/tests/tags/expected/block.local.dynamic.phtml
index 19c44503..940c90c4 100644
--- a/tests/tags/expected/block.local.dynamic.phtml
+++ b/tests/tags/expected/block.local.dynamic.phtml
@@ -98,3 +98,4 @@ final class Template%a% extends Latte\Runtime\Template
echo '<span>hello</span>';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.args1.phtml b/tests/tags/expected/define.args1.phtml
index 66958965..4e62bdad 100644
--- a/tests/tags/expected/define.args1.phtml
+++ b/tests/tags/expected/define.args1.phtml
@@ -62,3 +62,4 @@ d) ';
$this->renderBlock('test', ['hello'] + [], 'html') /* line %d% */;
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.args2.phtml b/tests/tags/expected/define.args2.phtml
index e623f7f9..64778be3 100644
--- a/tests/tags/expected/define.args2.phtml
+++ b/tests/tags/expected/define.args2.phtml
@@ -51,3 +51,4 @@ d) ';
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.args3.phtml b/tests/tags/expected/define.args3.phtml
index 7a91196f..a2b0882d 100644
--- a/tests/tags/expected/define.args3.phtml
+++ b/tests/tags/expected/define.args3.phtml
@@ -48,3 +48,4 @@ c) ';
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.args4.phtml b/tests/tags/expected/define.args4.phtml
index ab1fc96a..02e4d2ae 100644
--- a/tests/tags/expected/define.args4.phtml
+++ b/tests/tags/expected/define.args4.phtml
@@ -26,3 +26,4 @@ c) ';
$this->renderBlock('test', ['hello' => 1] + [], 'html') /* line %d% */;
}
}
+%A%
diff --git a/tests/tags/expected/define.args5.phtml b/tests/tags/expected/define.args5.phtml
index abeded28..6177bb48 100644
--- a/tests/tags/expected/define.args5.phtml
+++ b/tests/tags/expected/define.args5.phtml
@@ -42,3 +42,4 @@ b) ';
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.phtml b/tests/tags/expected/define.phtml
index 51d63a48..05080202 100644
--- a/tests/tags/expected/define.phtml
+++ b/tests/tags/expected/define.phtml
@@ -51,3 +51,4 @@ final class Template%a% extends Latte\Runtime\Template
echo 'true';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/define.typehints.phtml b/tests/tags/expected/define.typehints.phtml
index 49c699d6..ed93986a 100644
--- a/tests/tags/expected/define.typehints.phtml
+++ b/tests/tags/expected/define.typehints.phtml
@@ -22,3 +22,4 @@ final class Template%a% extends Latte\Runtime\Template
{
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/embed.block.phtml b/tests/tags/expected/embed.block.phtml
index 974eaee4..bbb491ff 100644
--- a/tests/tags/expected/embed.block.phtml
+++ b/tests/tags/expected/embed.block.phtml
@@ -72,3 +72,4 @@ final class Template%a% extends Latte\Runtime\Template
echo 'embed1-A';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/embed.file.phtml b/tests/tags/expected/embed.file.phtml
index d2c655a8..2ce33038 100644
--- a/tests/tags/expected/embed.file.phtml
+++ b/tests/tags/expected/embed.file.phtml
@@ -46,3 +46,4 @@ final class Template%a% extends Latte\Runtime\Template
echo 'nested embeds A';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/extends.1.phtml b/tests/tags/expected/extends.1.phtml
index 1c29e833..1677c945 100644
--- a/tests/tags/expected/extends.1.phtml
+++ b/tests/tags/expected/extends.1.phtml
@@ -19,3 +19,4 @@ final class Template%a% extends Latte\Runtime\Template
return get_defined_vars();
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/extends.4.phtml b/tests/tags/expected/extends.4.phtml
index 6e7e0789..0df86d5f 100644
--- a/tests/tags/expected/extends.4.phtml
+++ b/tests/tags/expected/extends.4.phtml
@@ -30,3 +30,4 @@ final class Template%a% extends Latte\Runtime\Template
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/general.n-attributes.phtml b/tests/tags/expected/general.n-attributes.phtml
index b0484ae2..d63e3a59 100644
--- a/tests/tags/expected/general.n-attributes.phtml
+++ b/tests/tags/expected/general.n-attributes.phtml
@@ -497,3 +497,4 @@ final class Template%a% extends Latte\Runtime\Template
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/general.phtml b/tests/tags/expected/general.phtml
index 0ba35330..df0b87e7 100644
--- a/tests/tags/expected/general.phtml
+++ b/tests/tags/expected/general.phtml
@@ -2,7 +2,10 @@
use Latte\Runtime as LR;
-/** source: %A% */
+/**
+* source: %a%
+* @property FiltersTemplate%a%
+*/
final class Template%a% extends Latte\Runtime\Template
{
public const Blocks = [
@@ -108,3 +111,4 @@ final class Template%a% extends Latte\Runtime\Template
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/import.phtml b/tests/tags/expected/import.phtml
index efbfc743..5dd11170 100644
--- a/tests/tags/expected/import.phtml
+++ b/tests/tags/expected/import.phtml
@@ -18,3 +18,8 @@ final class Template%a% extends Latte\Runtime\Template
return get_defined_vars();
}
}
+
+
+class FiltersTemplate%a%
+{
+}
diff --git a/tests/tags/expected/include.block.from.phtml b/tests/tags/expected/include.block.from.phtml
index a335ee03..043fab4d 100644
--- a/tests/tags/expected/include.block.from.phtml
+++ b/tests/tags/expected/include.block.from.phtml
@@ -8,3 +8,4 @@ final class Template%a% extends Latte\Runtime\Template
echo ' after';
%A%
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.inc1.phtml b/tests/tags/expected/include.inc1.phtml
index 68d3d47b..118de124 100644
--- a/tests/tags/expected/include.inc1.phtml
+++ b/tests/tags/expected/include.inc1.phtml
@@ -24,3 +24,4 @@ Parent: ';
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.inc2.phtml b/tests/tags/expected/include.inc2.phtml
index 5810d2df..18dcffbd 100644
--- a/tests/tags/expected/include.inc2.phtml
+++ b/tests/tags/expected/include.inc2.phtml
@@ -19,3 +19,4 @@ Parent: ';
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.inc3.phtml b/tests/tags/expected/include.inc3.phtml
index 24290bf9..7ac596aa 100644
--- a/tests/tags/expected/include.inc3.phtml
+++ b/tests/tags/expected/include.inc3.phtml
@@ -14,3 +14,4 @@ final class Template%a% extends Latte\Runtime\Template
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.phtml b/tests/tags/expected/include.phtml
index a71b9f82..083614c9 100644
--- a/tests/tags/expected/include.phtml
+++ b/tests/tags/expected/include.phtml
@@ -12,3 +12,4 @@ final class Template%a% extends Latte\Runtime\Template
}) /* line %d% */;
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.with-blocks.inc.phtml b/tests/tags/expected/include.with-blocks.inc.phtml
index c8e566e3..a578ba46 100644
--- a/tests/tags/expected/include.with-blocks.inc.phtml
+++ b/tests/tags/expected/include.with-blocks.inc.phtml
@@ -27,3 +27,4 @@ final class Template%a% extends Latte\Runtime\Template
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/include.with-blocks.phtml b/tests/tags/expected/include.with-blocks.phtml
index f10a80e1..a06e8f64 100644
--- a/tests/tags/expected/include.with-blocks.phtml
+++ b/tests/tags/expected/include.with-blocks.phtml
@@ -11,3 +11,4 @@ final class Template%a% extends Latte\Runtime\Template
$this->renderBlock('test', [], 'html') /* line %d% */;
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/inheritance.1.parent.phtml b/tests/tags/expected/inheritance.1.parent.phtml
index 4478de06..58b23e3d 100644
--- a/tests/tags/expected/inheritance.1.parent.phtml
+++ b/tests/tags/expected/inheritance.1.parent.phtml
@@ -72,3 +72,4 @@ Parent: ';
';
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/inheritance.1.phtml b/tests/tags/expected/inheritance.1.phtml
index 3d113a0b..e791014a 100644
--- a/tests/tags/expected/inheritance.1.phtml
+++ b/tests/tags/expected/inheritance.1.phtml
@@ -71,3 +71,4 @@ final class Template%a% extends Latte\Runtime\Template
echo "\n";
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/inheritance.2.phtml b/tests/tags/expected/inheritance.2.phtml
index 2da62733..85fc8ae5 100644
--- a/tests/tags/expected/inheritance.2.phtml
+++ b/tests/tags/expected/inheritance.2.phtml
@@ -69,3 +69,4 @@ final class Template%a% extends Latte\Runtime\Template
{
}
}
+%A%
\ No newline at end of file
diff --git a/tests/tags/expected/varType.phtml b/tests/tags/expected/varType.phtml
index e69de29b..b255ee82 100644
--- a/tests/tags/expected/varType.phtml
+++ b/tests/tags/expected/varType.phtml
@@ -0,0 +1,15 @@
+<?php
+%A%
+ public function main(): array
+ {
+ extract($this->params);
+ /** @var string $a */
+%A%
+ public function blockTest(array $ʟ_args): void
+ {
+ extract($this->params);
+ /** @var string $a */
+%A%
+ /** @var int $b */
+ $b = 5%a%;
+%A%
\ No newline at end of file
diff --git a/tests/tags/templateType.phpt b/tests/tags/templateType.phpt
index 5b7789ab..2b1a3ee6 100644
--- a/tests/tags/templateType.phpt
+++ b/tests/tags/templateType.phpt
@@ -44,6 +44,6 @@ class TemplateClass
);
Assert::contains(
- '/** @var int $intType *//** @var int|bool $intBoolType *//** @var array<int, string> $arrayType */',
+ '/** @var int $intType */' . "\n " . '/** @var int|bool $intBoolType */'. "\n " . '/** @var array<int, string> $arrayType */',
$latte->compile('{templateType TemplateClass}'),
);
diff --git a/tests/tags/var.default.nodes.phpt b/tests/tags/var.default.nodes.phpt
index 6f1794a9..911c420b 100644
--- a/tests/tags/var.default.nodes.phpt
+++ b/tests/tags/var.default.nodes.phpt
@@ -20,5 +20,7 @@
name: var2
Integer:
value: 3
+ SuperiorType:
+ 'int|array'
Fragment:
XX, exportTraversing('{var $var, int|array $var2 = 3}'));
diff --git a/tests/tags/var.default.phpt b/tests/tags/var.default.phpt
index 648813f0..cabd215d 100644
--- a/tests/tags/var.default.phpt
+++ b/tests/tags/var.default.phpt
@@ -25,7 +25,7 @@ function ws(string $s): string
// types
Assert::contains('$temp->var1 = 123 /*', $latte->compile('{var int $temp->var1 = 123}'));
Assert::contains('$temp->var1 = 123 /*', $latte->compile('{var null|int|string[] $temp->var1 = 123}'));
- Assert::contains('/** @var int|string[] $var1 */ $var1 = 123; /** @var ?class $var2 */ $var2 = \'nette framework\' /* line 1 */;', ws($latte->compile('{var int|string[] $var1 = 123, ?class $var2 = "nette framework"}')));
+ Assert::contains('/** @var int|string[] $var1 */$var1 = 123; /** @var ?class $var2 */$var2 = \'nette framework\' /* line 1 */;', ws($latte->compile('{var int|string[] $var1 = 123, ?class $var2 = "nette framework"}')));
Assert::contains('$var1 = 123; $var2 = 456 /*', ws($latte->compile('{var A\B $var1 = 123, ?A\B $var2 = 456}')));
Assert::contains('$var1 = 123; $var2 = 456 /*', ws($latte->compile('{var \A\B $var1 = 123, ?\A\B $var2 = 456}')));
diff --git a/tests/tags/varType.nodes.phpt b/tests/tags/varType.nodes.phpt
index e20e9d6b..8db16099 100644
--- a/tests/tags/varType.nodes.phpt
+++ b/tests/tags/varType.nodes.phpt
@@ -15,5 +15,9 @@
Template:
Fragment:
VarType:
+ Variable:
+ name: int
+ SuperiorType:
+ 'int'
Fragment:
XX, exportTraversing('{varType int $int}'));
diff --git a/tests/tags/varType.phpt b/tests/tags/varType.phpt
index 8c32741c..c18d87d2 100644
--- a/tests/tags/varType.phpt
+++ b/tests/tags/varType.phpt
@@ -57,3 +57,27 @@
'/** @var array{0:int,1:int} $var */',
$latte->compile('{varType array{0: int, 1: int} $var}'),
);
+
+$template = <<<'XX'
+
+{varType string $a}
+
+{$a}
+
+{varType string $c}
+{var $c = 10}
+
+{include test}
+
+{define test}
+ {varType int $b}
+ {var $b = 5}
+ {$a}{$b}
+{/define}
+
+XX;
+
+Assert::matchFile(
+ __DIR__ . '/expected/varType.phtml',
+ $latte->compile($template)
+);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment