<?php

/**
 * PHP-IR return statement.
 *
 * A null expression represents PHP `return;`. Non-null expressions carry the
 * returned PHP value and are lowered to the existing Zval runtime path.
 */
final class PhpIrReturn
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private ?object $expr;

    /** Create a return statement. */
    public function __construct(int $startLine, int $endLine, array $comments, ?object $expr = null)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->expr = $expr;
    }

    /** Convert to a plain array for debugging/export. */
    public function toArray(): array
    {
        return [
            'kind' => 'Return',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (PhpIrComment $comment): array => $comment->toArray(), $this->comments),
            'expr' => $this->expr !== null && method_exists($this->expr, 'toArray') ? $this->expr->toArray() : null,
        ];
    }
}
