From 17083ad646928a3ed9da900bc3cd2ad6aec0012e Mon Sep 17 00:00:00 2001 From: Martin Jonas Date: Thu, 26 Sep 2024 18:57:27 +0200 Subject: [PATCH 1/2] Support for multiline {define} block --- .../NodeVisitor/AddParametersForBlockNodeVisitor.php | 6 +++--- .../SimpleControl/Fixtures/Blocks/define.latte | 7 +++++++ .../LatteTemplatesRuleForSimpleControlTest.php | 12 +++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php b/src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php index e80e3937..8464cecf 100644 --- a/src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php +++ b/src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php @@ -56,12 +56,12 @@ public function leaveNode(Node $node): ?array } $parameters = []; - $pattern = '/(?{define (?.*?),? (?.*)}) on line (?\d+)/'; + $pattern = '/(?{define\s+(?.*?)\s*,?\s+(?.*)})\s+on line (?\d+)/s'; preg_match($pattern, $comment->getText(), $match); if (isset($match['parameters'])) { $define = $match['define']; - $typesAndVariablesPattern = '/(?[\?\\\[\]\<\>[:alnum:]]*)[ ]*\$(?[[:alnum:]]+)/'; + $typesAndVariablesPattern = '/(?[\?\\\[\]\<\>[:alnum:]]*)[ ]*\$(?[[:alnum:]]+)/s'; preg_match_all($typesAndVariablesPattern, $match['parameters'], $typesAndVariables); $variableTypes = array_combine($typesAndVariables['variable'], $typesAndVariables['type']) ?: []; @@ -102,7 +102,7 @@ public function leaveNode(Node $node): ?array } } else { // process default blocks content etc. - $pattern = '/{block (?.*?)} on line (?\d+)/'; + $pattern = '/{block\s+(?.*?)}\s+on line (?\d+)/s'; preg_match($pattern, $comment->getText(), $match); } diff --git a/tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Blocks/define.latte b/tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Blocks/define.latte index a93fb10d..f303caea 100644 --- a/tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Blocks/define.latte +++ b/tests/Rule/LatteTemplatesRule/SimpleControl/Fixtures/Blocks/define.latte @@ -29,4 +29,11 @@ {include no-comma-block, $knownInteger} {include no-comma-block} +{define multiline-block, + int $count, + string $name} + {php \PHPStan\dumpType($count)} + {php \PHPStan\dumpType($name)} +{/define} + {php \PHPStan\dumpType($paramString)} diff --git a/tests/Rule/LatteTemplatesRule/SimpleControl/LatteTemplatesRuleForSimpleControlTest.php b/tests/Rule/LatteTemplatesRule/SimpleControl/LatteTemplatesRuleForSimpleControlTest.php index 19974676..3645d96a 100644 --- a/tests/Rule/LatteTemplatesRule/SimpleControl/LatteTemplatesRuleForSimpleControlTest.php +++ b/tests/Rule/LatteTemplatesRule/SimpleControl/LatteTemplatesRuleForSimpleControlTest.php @@ -378,9 +378,19 @@ public function testBlocks(): void 30, 'define.latte', ], + [ + 'Dumped type: int', + 35, + 'define.latte', + ], + [ + 'Dumped type: string', + 36, + 'define.latte', + ], [ 'Dumped type: \'some string\'', - 32, + 39, 'define.latte', ], ]); From 4c059a4896a49ba312eed6771afc11c0845afa6a Mon Sep 17 00:00:00 2001 From: Martin Jonas Date: Fri, 27 Sep 2024 09:13:53 +0200 Subject: [PATCH 2/2] Changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91929de4..0d113171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Updated coding standard (Possible **BC break** - added `final` or `abstract` to (almost) all classes) - Bleeding edge changes - updated typehints +- Fixed parameters parsing for multiline {define} ## [0.16.3] - 2023-11-26 ### Added