Skip to content

Instantly share code, notes, and snippets.

@timelsass
Created June 6, 2017 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelsass/f09eba3b613cd0df8305c36ba1e4f2ba to your computer and use it in GitHub Desktop.
Save timelsass/f09eba3b613cd0df8305c36ba1e4f2ba to your computer and use it in GitHub Desktop.
basic php class documentation
<?php
function getDocumentation( $class ) {
$reflection = new \ReflectionClass( $class );
$comments = array();
foreach ( $reflection->getMethods() as $method ) {
if ( $method->isPublic() && ! $method->isConstructor() ) {
$methodDetails = array(
'class' => $method->class,
'method' => $method->name,
);
$comment = $method->getDocComment();
if ( preg_match_all( '/@(\w+)\s+(.*)\r?\n/m', $comment, $matches ) ) {
$methodDetails = array_merge( $methodDetails, array_combine( $matches[1], $matches[2] ) );
}
if ( preg_match_all( '/\*(.)((?!@).)+/m', $comment, $matches ) ) {
$methodDetails['shortDescription'] = trim( str_replace( '*', '', $matches[0][0] ) );
if ( count( $matches[0] ) > 1 ) {
$l = array_shift( $matches[0] );
$methodDetails['longDescription'] = implode( ' ', array_map( 'trim', str_replace( '*', '', $matches[0] ) ) );
}
}
$comments[] = $methodDetails;
}
}
return $comments;
}
function documentationHtml( $parsed ) {
$html = '';
$html .= '<div class="class">' . $parsed[0]['class'] . '</div>';
foreach( $parsed as $comment ) {
$html .= "<div class=\"method-block {$comment['method']}\">";
foreach( $comment as $k => $v ) {
if ( ! empty( $k ) ) {
$html .= "<div class=\"{$k}\"><p><span>{$k}</span><span>{$v}</span></p></div>";
}
}
$html .= '</div>';
}
return $html;
}
// Class to get documentation for.
$doc = getDocumentation( 'ClassName' );
// Create HTML for documentation.
$html = documentationHtml( $doc );
// Echo documentation.
echo( $html );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment