Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiline {define} block #459

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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}
- Functions handling with FunctionExecutor in new Latte
- Removed unformatPresenterClass of new PresenterFactory

Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
}

$parameters = [];
$pattern = '/(?<define>{define (?<block_name>.*?),? (?<parameters>.*)}) on line (?<line>\d+)/';
$pattern = '/(?<define>{define\s+(?<block_name>.*?)\s*,?\s+(?<parameters>.*)})\s+on line (?<line>\d+)/s';
preg_match($pattern, $comment->getText(), $match);
if (isset($match['parameters'])) {
$define = $match['define'];

Check failure on line 62 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 7.4

Offset 'define' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 62 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.0

Offset 'define' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 62 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.3

Offset 'define' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 62 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.1

Offset 'define' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 62 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.2

Offset 'define' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

$typesAndVariablesPattern = '/(?<type>[\?\\\[\]\<\>[:alnum:]]*)[ ]*\$(?<variable>[[:alnum:]]+)/';
$typesAndVariablesPattern = '/(?<type>[\?\\\[\]\<\>[:alnum:]]*)[ ]*\$(?<variable>[[:alnum:]]+)/s';
preg_match_all($typesAndVariablesPattern, $match['parameters'], $typesAndVariables);

$variableTypes = array_combine($typesAndVariables['variable'], $typesAndVariables['type']) ?: [];
Expand All @@ -79,7 +79,7 @@
}

// create php code from latte code of define
$phpContent = '<?php ' . preg_replace(['/^{define /', '/' . $match['block_name'] . ',? /', '/}$/'], ['function ', $methodName . '(', ') {}'], $define);

Check failure on line 82 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 7.4

Offset 'block_name' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 82 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.0

Offset 'block_name' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 82 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.3

Offset 'block_name' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 82 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.1

Offset 'block_name' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

Check failure on line 82 in src/Compiler/NodeVisitor/AddParametersForBlockNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan - PHP 8.2

Offset 'block_name' might not exist on array{0?: string, define?: non-falsy-string, 1?: non-falsy-string, block_name?: string, 2?: string, parameters: string, 3?: string, line?: numeric-string, ...}.

try {
// parse php code
Expand All @@ -102,7 +102,7 @@
}
} else {
// process default blocks content etc.
$pattern = '/{block (?<block_name>.*?)} on line (?<line>\d+)/';
$pattern = '/{block\s+(?<block_name>.*?)}\s+on line (?<line>\d+)/s';
preg_match($pattern, $comment->getText(), $match);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);
Expand Down
Loading