*/ #[YieldReady] class PrintNode extends Node implements NodeOutputInterface, CoercesChildrenToStringInterface { public function __construct(AbstractExpression $expr, int $lineno) { parent::__construct(['expr' => $expr], [], $lineno); } public function compile(Compiler $compiler): void { /** @var AbstractExpression */ $expr = $this->getNode('expr'); $compiler->addDebugInfo($this); if ($expr->isGenerator()) { $compiler->write('yield from '); } else { $compiler->write('yield '); if (!$this->isString($expr)) { $compiler->raw('(string) '); } } $compiler ->subcompile($expr) ->raw(";\n") ; } public function getStringCoercedChildNames(): array { return ['expr']; } private function isString(AbstractExpression $expr): bool { if ($expr instanceof ReturnStringInterface) { return true; } return $expr instanceof ConstantExpression && !$expr->isDefinedTestEnabled() && \is_string($expr->getAttribute('value')); } }