code: 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;
      }
docs: Returns the next available variable name to use.
  
  @param string $variable      The variable name we wish to use
  @param array  $usedVariables The list of variable names already used.
  
  @return string