Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ file/folder - the file or folder you want to docblock (php files)
target function - to docblock only a specific method/function name

Example:
php docblock.php target.php targetFunction
bin/docblock target.php targetFunction
or
php docblock.php target/dir -r targetFunction
bin/docblock target/dir -r targetFunction

Credit to Sean Coates for the getProtos function, modified a little.
http://seancoates.com/fun-with-the-tokenizer
http://seancoates.com/fun-with-the-tokenizer
Binary file added bin/docblock
Binary file not shown.
Binary file added bin/docblock.phar
Binary file not shown.
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "vicebas/php-docblock-generator",
"description": "Generate PHPDoc Blocks in files and Folders",
"require": {},
"autoload": {
"psr-0": {
"DocBlockGenerator\\": "src"
}
},
"bin": [
"bin/docblock"
]
}
13 changes: 13 additions & 0 deletions composer.json~
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "agentile/php-docblock-generator",
"description": "Generate PHPDoc Blocks in files and Folders",
"require": {},
"autoload": {
"psr-0": {
"DocBlockGenerator\\": "src"
}
},
"bin": [
"bin/docblock"
],
}
10 changes: 10 additions & 0 deletions docblock.phar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$app = new Phar("bin/docblock.phar", 0, "docblock.phar");
$app->addFile('src/docblock.php');
$app->addFile('src/DocBlockGenerator.class.php');
$defaultStub = $app->createDefaultStub("src/docblock.php");
$stub = "#!/usr/bin/env php \n".$defaultStub;
$app->setStub($stub);
$app->stopBuffering();
?>

173 changes: 82 additions & 91 deletions docblock.php → src/DocBlockGenerator.class.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
<?php
/**
* DocBlockGenerator
*
* This class will generate docblock outline for files/folders.
*
* Use from command line - params:
* file/folder - the file or folder you want to docblock (php files)
* -r - to have it recursively go through a folder
* target function - to docblock only a specific method/function name
*
* Example:
* php docblock.php target.php targetFunction
* or
* php docblock.php target/dir -r targetFunction
*
* Credit to Sean Coates for the getProtos function, modified a little.
* http://seancoates.com/fun-with-the-tokenizer
*
* TODOs :
* 1. add all proper docblock properties
* 2. better checking for if docblock already exists
* 3. docblocking for class properties
* 4. try to gather more data for automatic insertion such as for @access
*
* @author Anthony Gentile
* @version 0.85
* @link http://agentile.com/docblock/
*/
namespace DocBlockGenerator;

class DocBlockGenerator {

public $exts = array('.php', '.php4', '.php5', '.phps', '.inc');
Expand Down Expand Up @@ -92,7 +66,7 @@ public function start()
if (is_file($this->target)) {
$valid_file = $this->fileCheck($this->target);
if ($valid_file == false) {
return;
return false;
}
$this->fileDocBlock();
} elseif (is_dir($this->target)) {
Expand All @@ -107,7 +81,7 @@ public function start()
}
} else {
$this->log[] = 'This is not a file or folder.';
return;
return false;
}
}

Expand Down Expand Up @@ -195,9 +169,13 @@ public function getProtos()
$funcs = array();
$classes = array();
$curr_class = '';
$curr_func = '';
$class_depth = 0;
$count = count($tokens);
for ($i = 0; $i < $count; $i++) {
if (is_array($tokens[$i]) && $tokens[$i][0] == T_RETURN) {
$funcs[$curr_func]['return'] = 'returns';
}
if (is_array($tokens[$i]) && $tokens[$i][0] == T_CLASS) {
$line = $tokens[$i][2];
++$i; // whitespace;
Expand All @@ -212,16 +190,36 @@ public function getProtos()
} elseif (is_array($tokens[$i]) && $tokens[$i][0] == T_FUNCTION) {
$next_by_ref = FALSE;
$this_func = array();
$func_status = array();

if ($tokens[$i-2][1] == 'static') {
$func_status['static'] = true;
} else {
$func_status['static'] = false;
}

if ($tokens[$i-2][1] != 'static') {
if ($tokens[$i-2][1] == 'public' || $tokens[$i-2][1] == 'private'|| $tokens[$i-2][1] == 'protected') {
$func_status['access'] = $tokens[$i-2][1];
}
}
if ($tokens[$i-2][1] == 'static') {
if ($tokens[$i-4][1] == 'public' || $tokens[$i-4][1] == 'private'|| $tokens[$i-4][1] == 'protected') {
$func_status['access'] = $tokens[$i-4][1];
}

}

while ($tokens[++$i] != ')') {
while ($tokens[++$i] != '{') {
if (is_array($tokens[$i]) && $tokens[$i][0] != T_WHITESPACE) {
if (!$this_func) {
$curr_func = $tokens[$i][1];
$this_func = array(
'name' => $tokens[$i][1],
'class' => $curr_class,
'line' => $tokens[$i][2],
);
} else {
} elseif ($tokens[$i][0] == T_VARIABLE) {
$this_func['params'][] = array(
'byRef' => $next_by_ref,
'name' => $tokens[$i][1],
Expand All @@ -239,7 +237,8 @@ public function getProtos()
$this_func['params'][count($this_func['params']) - 1]['default'] = $tokens[$i][1];
}
}
$funcs[] = $this_func;

$funcs[$curr_func] = $this_func + $func_status;
} elseif ($tokens[$i] == '{' || $tokens[$i] == 'T_CURLY_OPEN' || $tokens[$i] == 'T_DOLLAR_OPEN_CURLY_BRACES') {
++$class_depth;
} elseif ($tokens[$i] == '}') {
Expand Down Expand Up @@ -445,21 +444,53 @@ public function functionDocBlock($indent, $data)
$doc_block .= "{$indent} *\n";
if (isset($data['params'])) {
foreach($data['params'] as $func_param) {
$doc_block .= "{$indent} * @param {$func_param['name']}\n";
$doc_block .= "{$indent} * @param ". (isset($func_param['default'])?$this->decodeType($func_param['default']):'type') . " {$func_param['name']}\n";
}
$doc_block .= "{$indent} *\n";
}
$doc_block .= "{$indent} *\n";
$doc_block .= "{$indent} * @return\n";
$doc_block .= "{$indent} *\n";
$doc_block .= "{$indent} * @access\n";
$doc_block .= "{$indent} * @static\n";
$doc_block .= "{$indent} * @see\n";
$doc_block .= "{$indent} * @since\n";
if (isset($data['return'])) {
$doc_block .= "{$indent} * @return type\n";
}
if (!empty($data['access'])) {
$doc_block .= "{$indent} * @access {$data['access']}\n";
}
if ($data['static']) {
$doc_block .= "{$indent} * @static\n";
}
// We can't determine these automatically yet.
//$doc_block .= "{$indent} * @see\n";
//$doc_block .= "{$indent} * @since\n";
$doc_block .= "{$indent} */\n";

return $doc_block;
}

/**
* Decode the parameter type
* @param type $type
* @return string
*/
public function decodeType($type) {
$typeToReturn = $type;

if ($type == "''") {
$typeToReturn = 'string';
}

if (is_int($type)) {
$typeToReturn = 'int';
}

if ($type === false) {
$typeToReturn = 'bool';
}

if ($type === true) {
$typeToReturn = 'bool';
}
return $typeToReturn;
}

/**
* classDocBlock
* Docblock for class
Expand All @@ -479,15 +510,15 @@ public function classDocBlock($indent, $data)
$doc_block .= "{$indent} * {$data['name']}\n";
$doc_block .= "{$indent} * Insert description here\n";
$doc_block .= "{$indent} *\n";
$doc_block .= "{$indent} * @category\n";
$doc_block .= "{$indent} * @package\n";
$doc_block .= "{$indent} * @author\n";
$doc_block .= "{$indent} * @copyright\n";
$doc_block .= "{$indent} * @license\n";
$doc_block .= "{$indent} * @version\n";
$doc_block .= "{$indent} * @link\n";
$doc_block .= "{$indent} * @see\n";
$doc_block .= "{$indent} * @since\n";
//$doc_block .= "{$indent} * @category\n";
//$doc_block .= "{$indent} * @package\n";
//$doc_block .= "{$indent} * @author\n";
//$doc_block .= "{$indent} * @copyright\n";
//$doc_block .= "{$indent} * @license\n";
//$doc_block .= "{$indent} * @version\n";
//$doc_block .= "{$indent} * @link\n";
//$doc_block .= "{$indent} * @see\n";
//$doc_block .= "{$indent} * @since\n";
$doc_block .= "{$indent} */\n";

return $doc_block;
Expand Down Expand Up @@ -516,44 +547,4 @@ public function getStrIndent($str, $count = 0)
}

}

$argv = empty($_SERVER['argv']) ? array(0 => '') : $_SERVER['argv'];

$current_dir = getcwd();

$options = array(
'file_folder' => '',
'target_function' => '',
'recursive' => false
);

foreach ($argv as $k => $arg) {
if ($k !== 0) {
if (strtolower($arg) === '-r') {
$options['recursive'] = true;
} elseif (is_file($arg)) {
$options['file_folder'] = $arg;
} elseif (is_file($current_dir . '/' . $arg)) {
$options['file_folder'] = $current_dir . '/' . $arg;
} elseif (is_dir($arg)) {
$options['file_folder'] = $arg;
} elseif (is_dir($current_dir . '/' . $arg)) {
$options['file_folder'] = $current_dir . '/' . $arg;
} else {
$options['target_function'] = $arg;
}
}
}

if (isset($argv[1])) {
if (is_file($options['file_folder']) || is_dir($options['file_folder'])) {
$doc_block_generator = new DocBlockGenerator($options['file_folder'], $options['target_function'], $options['recursive']);
$doc_block_generator->start();
$doc_block_generator->result();
} else {
die("\nThis is not a valid file or directory\n");
}

} else {
die("\nPlease provide a file or directory as a parameter\n");
}
?>
74 changes: 74 additions & 0 deletions src/docblock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* DocBlockGenerator
*
* This class will generate docblock outline for files/folders.
*
* Use from command line - params:
* file/folder - the file or folder you want to docblock (php files)
* -r - to have it recursively go through a folder
* target function - to docblock only a specific method/function name
*
* Example:
* php docblock.php target.php targetFunction
* or
* php docblock.php target/dir -r targetFunction
*
* Credit to Sean Coates for the getProtos function, modified a little.
* http://seancoates.com/fun-with-the-tokenizer
*
* TODOs :
* 1. add all proper docblock properties
* 2. better checking for if docblock already exists
* 3. docblocking for class properties
* 4. try to gather more data for automatic insertion such as for @access
*
* @author Anthony Gentile
* @version 0.85
* @link http://agentile.com/docblock/
*/

include("DocBlockGenerator.class.php");

use DocBlockGenerator\DocBlockGenerator ;

$argv = empty($_SERVER['argv']) ? array(0 => '') : $_SERVER['argv'];

$current_dir = getcwd();

$options = array(
'file_folder' => '',
'target_function' => '',
'recursive' => false
);

foreach ($argv as $k => $arg) {
if ($k !== 0) {
if (strtolower($arg) === '-r') {
$options['recursive'] = true;
} elseif (is_file($arg)) {
$options['file_folder'] = $arg;
} elseif (is_file($current_dir . '/' . $arg)) {
$options['file_folder'] = $current_dir . '/' . $arg;
} elseif (is_dir($arg)) {
$options['file_folder'] = $arg;
} elseif (is_dir($current_dir . '/' . $arg)) {
$options['file_folder'] = $current_dir . '/' . $arg;
} else {
$options['target_function'] = $arg;
}
}
}

if (isset($argv[1])) {
if (is_file($options['file_folder']) || is_dir($options['file_folder'])) {
$doc_block_generator = new DocBlockGenerator($options['file_folder'], $options['target_function'], $options['recursive']);
$doc_block_generator->start();
$doc_block_generator->result();
} else {
die("\nThis is not a valid file or directory\n");
}

} else {
die("\nPlease provide a file or directory as a parameter\n");
}