public function withParameter(string $key, $value): self
    {
        if ($this->lastOperation === Node::class) {
            $element = $this->elements->last();
        } else {
            $element = $this->elements->dropEnd(1)->last();
        }

        $element = $element->withParameter($key, $value);
        $path = new self;
        $path->lastOperation = $this->lastOperation;

        if ($this->lastOperation === Node::class) {
            $path->elements = $this
                ->elements
                ->dropEnd(1)
                ->add($element);
        } else {
            $path->elements = $this
                ->elements
                ->dropEnd(2)
                ->add($element)
                ->add($this->elements->last());
        }

        return $path;
    }