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

POC write output for varType and templateType macros #1

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 28 additions & 2 deletions src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Latte\MacroNode;
use Latte\PhpHelpers;
use Latte\PhpWriter;
use ReflectionClass;
use ReflectionException;


/**
Expand Down Expand Up @@ -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);
Expand All @@ -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']");
Copy link
Owner Author

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing semicolon at the end of this write

}


Expand All @@ -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) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getProperties returns only real properties, it is not working for @property / @property-read annotation

$propertyName = $property->getName();
$type = $property->getType();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works only for php >= 7.4

Copy link
Owner Author

Choose a reason for hiding this comment

The 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";
Copy link
Owner Author

Choose a reason for hiding this comment

The 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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO handle nullable

}
$templateType .= "\$$propertyName = \$this->params['$propertyName'];";
Copy link
Owner Author

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['$propertyName'] ?? null

$templateTypes[] = $templateType;
}
} catch (ReflectionException $e) {

}

return $writer->write(implode("\n", $templateTypes));
}


Expand Down