<?php

/**
 * PHP-IR array dimension fetch expression, e.g. $array[$key].
 */
final class PhpIrArrayDimFetch
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $var;
    private object $dim;

    /**
     * Create an array dimension fetch expression.
     */
    public function __construct(int $startLine, int $endLine, array $comments, object $var, object $dim)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->var = $var;
        $this->dim = $dim;
    }

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