<?php

/**
 * PHP-IR object creation expression: new ClassName(...).
 */
final class PhpIrNew
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private string $className;
    private array $args;

    /**
     * Create this PHP-IR node.
     */
    public function __construct(int $startLine, int $endLine, array $comments, string $className, array $args)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->className = $className;
        $this->args = $args;
    }

    /**
     * Convert to a plain array for debugging/export.
     */
    public function toArray(): array
    {
        return [
            'kind' => 'New',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (PhpIrComment $comment): array => $comment->toArray(), $this->comments),
            'className' => $this->className,
            'args' => array_map(
                static fn (object $arg): array => method_exists($arg, 'toArray') ? $arg->toArray() : ['kind' => get_debug_type($arg)],
                $this->args,
            ),
        ];
    }
}
