public static function getNextAvailableVariableName(string $variable, array $usedVariables): string
    {
        $variable = self::toVariableName($variable);
        while (true) {
            // check that the name is not reserved
            if (!in_array($variable, $usedVariables, true)) {
                break;
            }

            $numbers = '';
            while (true) {
                $lastCharacter = substr($variable, strlen($variable) - 1);
                if ($lastCharacter >= '0' && $lastCharacter <= '9') {
                    $numbers = $lastCharacter.$numbers;
                    $variable = substr($variable, 0, strlen($variable) - 1);
                } else {
                    break;
                }
            }

            if ($numbers === '') {
                $numbers = 0;
            } else {
                $numbers = (int) $numbers;
            }
            ++$numbers;

            $variable = $variable.$numbers;
        }

        return $variable;
    }