function.fetch_docblock.php
Simple function to fetch a functions docblock and any tags
- Author: Aidan Lister <aidan@php.net>
- Version: 1.0.0
- Views: 0
- Downloads: 0
Source
<?php
/**
* PHP 5 added a set of new constants which need to be declared in this file for
* effective PHP 5 parsing. It also removed constants, which need to be
* included for PHP 4 parsing.
*
* The following file will define constants for PHP 4 / PHP 5 compatability
*
* The source of this file can be found at:
* http://tinyurl.com/plmlo
*
* It is part of the PEAR PHP_Compat package:
* http://pear.php.net/package/PHP_Compat
*/
require_once 'PHP/Compat/Constant/T.php';
/**
* Simple function to fetch a functions docblock and any tags
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.0
* @param string $path The path to the file
*/
function fetch_docblock($path)
{
// Load and tokenize
$source = file_get_contents($path);
$tokens = token_get_all($source);
// Get the docblock comment
$score = 0;
foreach ($tokens as $token) {
++$score;
if (is_string($token)) {
continue;
}
// Ensure only the first function or class docblock is used
list($id, $text) = $token;
switch ($id):
case T_COMMENT:
case T_DOC_COMMENT:
$synopsis = $text;
$score = -10;
break;
case T_FUNCTION:
case T_CLASS:
if ($score == -8) {
break 2;
}
break;
default:
break;
endswitch;
}
if (!isset($synopsis)) {
return false;
}
// Line ending
$synopsis = str_replace("\r", '', $synopsis);
$lines = explode("\n", $synopsis);
unset($lines[0]);
// Grab the lines and bangs
$description = array();
$tags = array();
foreach ($lines as $line) {
if (substr($line, 0, 4) === ' * @') {
$tag = explode(' ', substr($line, 4), 2);
if ($tag[0] !== 'param') {
$tags[] = $tag;
}
} else {
$description[] = substr($line, 3);
}
}
// Format
$synopsis = trim(implode("\n", $description)) . "\n\n";
$synopsis = preg_replace("#([^\n])\n([^\n])#", '\1 \2', $synopsis);
$synopsis = preg_replace("#([^\n\n]+)\n\n#", "<p>\\1</p>\n", $synopsis);
return array($synopsis, $tags);
}
?>
Comments
No comments.