Google\Cloud\Dev\Snippet\Parser\Parser::examples PHP Method

examples() public method

Example: Yeah, this example is pretty useless. $examples = $parser->examples($docBlock);
public examples ( DocBlock $docBlock, $fullyQualifiedName, string $file, integer $line, array $magicMethods = [] ) : array
$docBlock phpDocumentor\Reflection\DocBlock The DocBlock to parse
$file string The filename the docblock is in
$line integer The line where the tested method or class is declared.
$magicMethods array
return array
    public function examples(DocBlock $docBlock, $fullyQualifiedName, $file, $line, array $magicMethods = [])
    {
        $text = $docBlock->getText();
        // $return = $docBlock->getTagsByName('return');
        // if (!empty($return)) {
        //     print_r($return[0]->getTypes());exit;
        // }
        $parts = explode('Example:', $text);
        if (strpos($text, 'Example:') === false) {
            return [];
        }
        $converter = new Parsedown();
        $document = new DOMDocument();
        $parsedText = $converter->text($parts[1]);
        $document->loadHTML($parsedText);
        $examples = $document->getElementsByTagName('code');
        $index = 0;
        $res = [];
        foreach ($examples as $example) {
            $name = $this->extractSnippetName($example->textContent);
            $indexOrName = $name;
            if (!$name) {
                $indexOrName = $index;
            }
            $identifier = $this->createIdentifier($fullyQualifiedName, $indexOrName);
            $snippet = new Snippet($identifier, ['content' => $example->textContent, 'fqn' => $fullyQualifiedName, 'index' => $index, 'name' => $name, 'file' => $file, 'line' => $line]);
            $res[$identifier] = $snippet;
            $index++;
        }
        $res = array_merge($res, $magicMethods);
        return $res;
    }