<?php

/**
 * PHP-IR print statement.
 *
 * PHP `print` is expression-like and returns 1. When it appears as a plain
 * statement, the return value is ignored, but the IR still keeps it distinct
 * from `echo`.
 */
final class PhpIrPrint
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $expr;

    /**
     * Create a print statement.
     */
    public function __construct(int $startLine, int $endLine, array $comments, object $expr)
    {
        $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' => 'Print',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (PhpIrComment $comment): array => $comment->toArray(), $this->comments),
            'expr' => method_exists($this->expr, 'toArray') ? $this->expr->toArray() : ['kind' => get_debug_type($this->expr)],
        ];
    }
}
