protected function getFunctionDeclarationBase()

in src/CodegenFunctionish.hack [174:219]


  protected function getFunctionDeclarationBase(
    string $keywords,
    bool $is_abstract = false,
  ): string {
    $builder = (new HackBuilder($this->config))
      ->add($keywords)
      ->addf('%s(%s)', $this->name, Str\join($this->parameters, ', '))
      ->addIf($this->contexts !== null, '[' . Str\join($this->contexts ?? keyset[], ', ') . ']')
      ->addIf($this->returnType !== null, ': '.($this->returnType ?? ''));

    $code = $builder->getCode();

    // If the total length is longer than max len, try to break it. Otherwise
    // return Total length = 2 (indent) + codelength + 2 or 1 (" {" or ";")
    // If the function/method is abstract, the ";" will be appended later
    // Therefore it has one char less than non-abstract functions, which has "{"
    if (
      Str\length($code) <=
        $this->config->getMaxLineLength() - 4 + (int)$is_abstract ||
      $this->fixme !== null
    ) {
      return (new HackBuilder($this->config))->add($code)->getCode();
    } else {
      $parameter_lines = Vec\map(
        $this->parameters,
        $line ==> {
          if (Str\search($line, '...$') !== null) {
            return $line;
          }
          return $line.',';
        },
      );

      $multi_line_builder = (new HackBuilder($this->config))
        ->add($keywords)
        ->addLine($this->name.'(')
        ->indent()
        ->addLines($parameter_lines)
        ->unindent()
        ->add(')')
        ->addIf($this->contexts !== null, '[' . Str\join($this->contexts ?? keyset[], ', ') . ']')
        ->addIf($this->returnType !== null, ': '.($this->returnType ?? ''));

      return $multi_line_builder->getCode();
    }
  }