diff --git a/README b/README index c63f592..5ab3036 100644 --- a/README +++ b/README @@ -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 diff --git a/bin/docblock b/bin/docblock new file mode 100755 index 0000000..2157f9a Binary files /dev/null and b/bin/docblock differ diff --git a/bin/docblock.phar b/bin/docblock.phar new file mode 100755 index 0000000..2157f9a Binary files /dev/null and b/bin/docblock.phar differ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6a87d2e --- /dev/null +++ b/composer.json @@ -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" + ] +} diff --git a/composer.json~ b/composer.json~ new file mode 100644 index 0000000..a93d5c9 --- /dev/null +++ b/composer.json~ @@ -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" + ], +} diff --git a/docblock.phar.php b/docblock.phar.php new file mode 100644 index 0000000..a6847b3 --- /dev/null +++ b/docblock.phar.php @@ -0,0 +1,10 @@ +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(); +?> + diff --git a/docblock.php b/src/DocBlockGenerator.class.php similarity index 81% rename from docblock.php rename to src/DocBlockGenerator.class.php index ec6e263..38b2f52 100644 --- a/docblock.php +++ b/src/DocBlockGenerator.class.php @@ -1,32 +1,6 @@ target)) { $valid_file = $this->fileCheck($this->target); if ($valid_file == false) { - return; + return false; } $this->fileDocBlock(); } elseif (is_dir($this->target)) { @@ -107,7 +81,7 @@ public function start() } } else { $this->log[] = 'This is not a file or folder.'; - return; + return false; } } @@ -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; @@ -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], @@ -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] == '}') { @@ -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 @@ -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; @@ -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"); -} \ No newline at end of file +?> diff --git a/src/docblock.php b/src/docblock.php new file mode 100644 index 0000000..8cde822 --- /dev/null +++ b/src/docblock.php @@ -0,0 +1,74 @@ + '') : $_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"); +}