Swagger\Context::phpdocContent PHP Method

phpdocContent() public method

The text contents of the phpdoc comment (excl. tags)
public phpdocContent ( ) : string | null
return string | null
    public function phpdocContent()
    {
        $comment = explode("\n", $this->comment);
        $comment[0] = preg_replace('/[ \\t]*\\/\\*\\*/', '', $comment[0]);
        // strip '/**'
        $i = count($comment) - 1;
        $comment[$i] = preg_replace('/\\*\\/[ \\t]*$/', '', $comment[$i]);
        // strip '*/'
        $lines = [];
        $append = false;
        foreach ($comment as $line) {
            $line = ltrim($line, "\t *");
            if (substr($line, 0, 1) === '@') {
                break;
            }
            if ($append) {
                $i = count($lines) - 1;
                $lines[$i] = substr($lines[$i], 0, -1) . $line;
            } else {
                $lines[] = $line;
            }
            $append = substr($line, -1) === '\\';
        }
        $description = trim(implode("\n", $lines));
        if ($description === '') {
            return null;
        }
        return $description;
    }

Usage Example

Example #1
0
    /**
     * https://phpdoc.org/docs/latest/guides/docblocks.html
     */
    public function testPhpdocSummaryAndDescription()
    {
        $single = new Context(['comment' => '/** This is a single line DocComment. */']);
        $this->assertEquals('This is a single line DocComment.', $single->phpdocContent());
        $multi = new Context(['comment' => "/**\n * This is a multi-line DocComment.\n */"]);
        $this->assertEquals('This is a multi-line DocComment.', $multi->phpdocContent());
        $emptyWhiteline = new Context(['comment' => <<<END
/**
 * This is a summary
 *
 * This is a description
 */
END
]);
        $this->assertEquals('This is a summary', $emptyWhiteline->phpdocSummary());
        $periodNewline = new Context(['comment' => <<<END
     /**
     * This is a summary.
     * This is a description
     */
END
]);
        $this->assertEquals('This is a summary.', $periodNewline->phpdocSummary());
        $multilineSummary = new Context(['comment' => <<<END
     /**
     * This is a summary
     * but this is part of the summary
     */
END
]);
    }