Created
June 6, 2017 05:26
-
-
Save timelsass/f09eba3b613cd0df8305c36ba1e4f2ba to your computer and use it in GitHub Desktop.
basic php class documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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