-
Notifications
You must be signed in to change notification settings - Fork 0
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
POC write output for varType and templateType macros #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
use Latte\MacroNode; | ||
use Latte\PhpHelpers; | ||
use Latte\PhpWriter; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
|
||
|
||
/** | ||
|
@@ -838,7 +840,7 @@ public function macroParameters(MacroNode $node, PhpWriter $writer): void | |
/** | ||
* {varType type $var} | ||
*/ | ||
public function macroVarType(MacroNode $node): void | ||
public function macroVarType(MacroNode $node, PhpWriter $writer): string | ||
{ | ||
if ($node->modifiers) { | ||
$node->setArgs($node->args . $node->modifiers); | ||
|
@@ -851,6 +853,10 @@ public function macroVarType(MacroNode $node): void | |
if (!$type || !$variable) { | ||
throw new CompileException('Unexpected content, expecting {varType type $var}.'); | ||
} | ||
|
||
$varName = $variable[0]; | ||
$paramName = ltrim($varName, '$'); | ||
return $writer->write("/** @var $type */\n$varName = \$this->params['$paramName']"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing semicolon at the end of this write |
||
} | ||
|
||
|
||
|
@@ -869,12 +875,32 @@ public function macroVarPrint(MacroNode $node): string | |
/** | ||
* {templateType ClassName} | ||
*/ | ||
public function macroTemplateType(MacroNode $node): void | ||
public function macroTemplateType(MacroNode $node, PhpWriter $writer): string | ||
{ | ||
if (!$this->getCompiler()->isInHead()) { | ||
throw new CompileException($node->getNotation() . ' is allowed only in template header.'); | ||
} | ||
$node->validate('class name'); | ||
|
||
$templateTypes = []; | ||
try { | ||
$reflectionClass = new ReflectionClass($node->args); | ||
foreach ($reflectionClass->getProperties() as $property) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getProperties returns only real properties, it is not working for |
||
$propertyName = $property->getName(); | ||
$type = $property->getType(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this works only for php >= 7.4 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and only for native typed properties (php doc is not read) |
||
|
||
$templateType = ''; | ||
if ($type) { | ||
$templateType .= "/** @var {$type->getName()} */\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getName() can be called only on ReflectionNamedType, it could be also ReflectionUnionType here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO handle nullable |
||
} | ||
$templateType .= "\$$propertyName = \$this->params['$propertyName'];"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO handle nullable with |
||
$templateTypes[] = $templateType; | ||
} | ||
} catch (ReflectionException $e) { | ||
|
||
} | ||
|
||
return $writer->write(implode("\n", $templateTypes)); | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO handle nullable with
$this->params['$paramName'] ?? null