hiphop-php /hphp/tools/docskel/base.php

Language PHP Lines 233
MD5 Hash b57c2f19d25a581ccc3bb5fd444e8a3b Estimated Cost $5,326 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?hh

class HHVMDocExtension {
  protected bool $verbose = false;
  protected bool $parsed = false;
  protected array $classes = [];
  protected array $functions = [];

  protected function svnScandir(string $path): array {
    $ret = array('dirs' => [], 'files' => []);
    $entries = scandir($path);
    foreach ($entries as $entry) {
      if ($entry[0] == '.') continue;
      if (is_dir("$path/$entry")) {
        $ret['dirs'][$entry] = [
          'name' => $entry,
          'path' => "$path/$entry",
        ];
      } else if (pathinfo($entry, PATHINFO_EXTENSION) === 'xml') {
        $ret['files'][$entry] = [
          'name' => $entry,
          'path' => "$path/$entry",
        ];
      }
    }
    return $ret;
  }

  protected static function GetDescriptionDOM(DOMNode $d): string {
    $ret = '';
    for ($node = $d->firstChild; $node; $node = $node->nextSibling) {
      if ($node->nodeType == XML_TEXT_NODE) {
        $ret .= preg_replace('/\s+/s', ' ', (string)$node->nodeValue);
        continue;
      }
      switch (strtolower($node->nodeName)) {
        case 'function':
          $ret .= self::GetDescriptionDOM($node) . '()';
          break;

        case 'para':
        default:
          $ret .= self::GetDescriptionDOM($node);
      }
    }
    return $ret;
  }

  protected static function GetDescription(SimpleXMLElement $e): string {
    return trim(self::GetDescriptionDOM(dom_import_simplexml($e)));
  }

  protected function getXml(string $path): SimpleXMLElement {
    $xml = file_get_contents($path);
    $xml = preg_replace_callback('@&([a-zA-Z0-9_\.-]*);@s',
    function (array<string> $ents) {
      $ent = $ents[1];
      switch (strtolower($ent)) {
        case 'true':
        case 'false':
        case 'null':
          return strtoupper($ent);
        default:
          return '';
      }
    }, $xml);
    return simplexml_load_string($xml, "SimpleXMLElement",
                                 LIBXML_NOENT|LIBXML_NONET);
  }

  protected function parseFunction(SimpleXMLElement $sxe,
                                   bool $oop): array {
    $f = [];
    // UNSAFE: typechecker doesn't understand __get on $sxe
    foreach ($sxe->refsect1 as $sect) {
      $attr = $sect->attributes();
      if ($attr->role == "description") {
        foreach($sect->methodsynopsis as $synopsis) {
          $fname = (string)$synopsis->methodname;
          $colon = strpos($fname, '::');
          if ($colon === false) {
            if ($oop) { $f['alias'] = $fname; continue; }
            $f['name'] = $fname;
          } else {
            if (!$oop) { $f['alias'] = $fname; continue; }
            $f['class'] = substr($fname, 0, $colon);
            $f['name'] = substr($fname, $colon + 2);
          }
          $f['return']['type'] = (string)$synopsis->type;
          $modifiers = [];
          foreach ($synopsis->modifier as $m) {
            $modifiers[] = (string)$m;
          }
          $f['modifiers'] = $modifiers;
          $f['args'] = [];
          foreach ($synopsis->methodparam as $param) {
            $pn = trim((string)$param->parameter);
            $pa = $param->parameter->attributes();
            $prole = (string)$pa->role;
            if (empty($pn)) continue;
            $arg = [
              'type' => strtolower((string)$param->type),
              'name' => $pn,
              'reference' => ($prole == 'reference'),
            ];
            if (isset($param->initializer)) {
              $arg['default'] = (string)$param->initializer;
            }
            $f['args'][strtolower($pn)] = $arg;
          }
        }
      } elseif ($attr->role == "parameters") {
        foreach ($sect->para->variablelist->varlistentry as $entry) {
          $name = trim((string)$entry->term->parameter);
          if (empty($f['args'][strtolower($name)])) continue;
          $f['args'][strtolower($name)]['desc'] =
                     self::GetDescription($entry->listitem);
        }
      } elseif ($attr->role == "returnvalues") {
        $f['return']['desc'] = self::GetDescription($sect);
      }
    }
    if (!empty($f['name'])) {
      $f['desc'] = (string)$sxe->refnamediv->refpurpose;
      if (empty($f['class'])) {
        $this->functions[strtolower($f['name'])] = $f;
      } else {
        $this->classes[strtolower($f['class'])]
                      ['functions'][strtolower($f['name'])] = $f;
      }
    }
  }

  protected function parseFunctions(string $path): void {
    $dirinfo = $this->svnScandir($path);
    foreach($dirinfo['files'] as $file) {
      $xml = $this->getXml($file['path']);
      $this->parseFunction($xml, true);
      $this->parseFunction($xml, false);
    }
  }

  protected function parseClass(string $path): void {
    $sxe = $this->getXml($path);
    // UNSAFE: typechecker doesn't understand __get on $sxe
    foreach ($sxe->partintro->section as $sect) {
      $xmlattrs = $sect->attributes('xml', true);
      if (substr($xmlattrs->id, -6) == '.intro') {
        $name = substr($xmlattrs->id, 0, -6);
        $this->classes[strtolower($name)]['intro'] =
               self::GetDescription($sect);
      } elseif (substr($xmlattrs->id, -9) == '.synopsis' &&
                !empty($sect->classsynopsis)) {
        $name = substr($xmlattrs->id, 0, -9);
        $synopsis = $sect->classsynopsis;
        $this->classes[strtolower($name)]['name'] = ucfirst($name);
        foreach($synopsis->ooclass as $ooclass) {
          $modifier = (string)$ooclass->modifier;
          $cn = (string)$ooclass->classname;
          if (empty($modifier)) {
            $this->classes[strtolower($name)]['name'] = $cn;
          } elseif ($modifier == 'extends') {
            $this->classes[strtolower($name)]['extends'] = $cn;
          } elseif ($modifier == 'implements') {
            $this->classes[strtolower($name)]['implements'][] = $cn;
          }
        }
      } elseif (substr($xmlattrs->id, -6) == '.props' &&
                !empty($sect->variablelist)) {
        $name = substr($xmlattrs->id, 0, -6);
        foreach ($sect->variablelist->varlistentry as $field) {
          $propname = (string)$field->term->varname;
          $this->classes[strtolower($name)]['props'][strtolower($propname)] = [
            'name' => $propname,
            'desc' => self::GetDescription($field->listitem),
          ];
        }
      }
    }
  }

  protected function fixupClasses(): void {
    foreach ($this->classes as &$class) {
      if (!empty($class['functions']) &&
           empty($class['name'])) {
        foreach($class['functions'] as $func) {
          $class['name'] = $func['class'];
          break;
        }
      }
    }
  }

  protected function parse(): this {
    if ($this->parsed) return $this;

    $url = $this->root.
           '/en/reference/'.
           urlencode($this->name);
    $dirinfo = $this->svnScandir($url);
    foreach($dirinfo['files'] as $file) {
      $this->parseClass($file['path']);
    }

    foreach ($dirinfo['dirs'] as $dir) {
      $this->parseFunctions($dir['path']);
    }
    $this->fixupClasses();

    $this->parsed = true;
    return $this;
  }

  public function __construct(protected string $name,
                              protected string $root):
                  void {
    if (!file_exists($this->root.'/en/reference') ||
        !is_dir($this->root.'/en/reference')) {
      throw new Exception("{$this->root} does not appear to be ".
                          "a phpdoc checkout.  See http://php.net/git.php ".
                          "for instructions on how to checkout phpdoc-en.");
    }
  }

  public function getFunctions(): array { return $this->parse()->functions; }
  public function getClasses(): array { return $this->parse()->classes; }
  public function setVerbose(bool $verbose): this {
    $this->verbose = $verbose;
    return $this;
  }
}
Back to Top