添加和完成 PHP 源代码文档的工具 [关闭]

2024-05-27

我有几个已完成的较旧的 PHP 项目,其中有很多内容,我想以 javadoc/phpDocumentor 风格记录它们。

虽然手动处理每个文件并被迫在记录的同时进行代码审查是最好的事情,但我只是出于时间限制,对帮助我尽可能自动化任务的工具感兴趣。

我正在考虑的工具理想情况下应具有以下功能:

  • 解析 PHP 项目树并告诉我哪里有未记录的文件、类和函数/方法(即缺少适当文档块注释的元素)

  • 提供一种方法来轻松添加丢失的文档块创建空结构并且,理想情况下,在编辑器中打开文件(内部或外部我不在乎),以便我可以输入描述。

选修的:

  • 自动识别参数类型、返回值等。但这并不是真正需要的。

所讨论的语言是 PHP,尽管我可以想象 C/Java 工具经过一些调整后可能能够处理 PHP 文件。

感谢您的宝贵意见!


I think PHP_Codesniffer http://pear.php.net/manual/en/package.php.php-codesniffer.php可以指示何时没有文档块 - 请参阅报告示例这一页 http://pear.php.net/manual/en/package.php.php-codesniffer.usage.php (引用其中之一) :

--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR   | Missing file doc comment
 20 | ERROR   | PHP keywords must be lowercase; expected "false" but found
    |         | "FALSE"
 47 | ERROR   | Line not indented correctly; expected 4 spaces but found 1
 47 | WARNING | Equals sign not aligned with surrounding assignments
 51 | ERROR   | Missing function doc comment
 88 | ERROR   | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

我想你可以使用 PHP_Codesniffer 至少获得所有没有文档的文件/类/方法的列表;据我所知,它可以生成 XML 作为输出,这将更容易使用一些自动化工具进行解析——这可能是某些文档块生成器的第一步;-)


Also, if you are using [phpDocumentor](http://www.phpdoc.org/) to generate the documentation, can this one not report errors for missing blocks ?

经过几次测试后,它可以 - 例如,在没有太多文档的类文件上运行它,使用--undocumentedelements选项,例如:

phpdoc --filename MyClass.php --target doc --undocumentedelements

在输出的中间给出这个:

Reading file /home/squale/developpement/tests/temp/test-phpdoc/MyClass.php -- Parsing file
WARNING in MyClass.php on line 2: Class "MyClass" has no Class-level DocBlock.
WARNING in MyClass.php on line 2: no @package tag was used in a DocBlock for class MyClass
WARNING in MyClass.php on line 5: Method "__construct" has no method-level DocBlock.
WARNING in MyClass.php on line 16: File "/home/squale/developpement/tests/temp/test-phpdoc/MyClass.php" has no page-level DocBlock, use @package in the first DocBlock to create one
done

但是,在这里,即使它作为报告工具很有用,但在生成丢失的文档块时却没有那么有用......


Now, I don't know of any tool that will pre-generate the missing docblocks for you : I generally use PHP_Codesniffer and/or phpDocumentor in my continuous integration mecanism, it reports missing docblocks, and, then, each developper adds what is missing, from his IDE...

...这工作得很好:每天通常不会有超过几个丢失的文档块,因此该任务可以手动完成(当您编辑特定文件/方法时,Eclipse PDT 提供了为方法预先生成文档块的功能).

除此之外,我真的不知道有什么全自动工具来生成文档块......但我很确定我们可以使用以下任一方法来创建一个有趣的工具:

  • 反射 API https://www.php.net/reflection
  • token_get_all https://www.php.net/manual/en/function.token-get-all.php解析 PHP 文件的源代码。

After a bit more searching, though, I found this blog-post *(it's in french -- maybe some people here will be able to understand)* : [Ajout automatique de Tags phpDoc à l'aide de PHP_Beautifier](http://fxnion.free.fr/articles/phpdoc-tags-php-beautifier.php).
*Possible translation of the title : "Automatically adding phpDoc tags, using PHP_Beautifier"*

这个想法其实还不错:

  • The PHP_Beautifier http://pear.php.net/package/PHP_Beautifier tool is pretty nice and powerful, when it comes to formating some PHP code that's not well formated
    • 我已经多次使用它来编写我什至无法阅读的代码^^
  • And it can be extended, using what it calls "filters".
    • see PHP_Beautifier_Filter http://beautifyphp.sourceforge.net/docs/PHP_Beautifier/Filter/PHP_Beautifier_Filter.html获取提供的过滤器列表

我链接到的博客文章中使用的想法是:

  • create a new PHP_Beautifier filter, that will detect the following tokens :
    • T_CLASS
    • T_FUNCTION
    • T_INTERFACE
  • 并在它们之前添加一个“草稿”文档块(如果还没有)

To run the tool on some `MyClass.php` file, I've had to first install `PHP_Beautifier` :
pear install --alldeps Php_Beautifier-beta

然后,将过滤器下载到我正在工作的目录中(当然可以将其放在默认目录中) :

wget http://fxnion.free.fr/downloads/phpDoc.filter.phpcs
cp phpDoc.filter.phpcs phpDoc.filter.php

之后,我创建了一个新的beautifier-1.php script (再次基于我链接到的博客文章中的建议),这将:

  • 加载我的内容MyClass.php file
  • 实例化PHP_Beautifier
  • 添加一些过滤器来美化代码
  • Add the phpDoc我们刚刚下载的过滤器
  • 美化文件的源代码,并将其回显到标准输出。

The code of the `beautifier-1.php` script will like this :
*(Once again, the biggest part is a copy-paste from the blog-post ; I only translated the comments, and changed a couple of small things)*
require_once 'PHP/Beautifier.php';

// Load the content of my source-file, with missing docblocks
$sourcecode = file_get_contents('MyClass.php');

$oToken = new PHP_Beautifier(); 

// The phpDoc.filter.php file is not in the default directory,
// but in the "current" one => we need to add it to the list of
// directories that PHP_Beautifier will search in for filters
$oToken->addFilterDirectory(dirname(__FILE__));

// Adding some nice filters, to format the code
$oToken->addFilter('ArrayNested');  
$oToken->addFilter('Lowercase');        
$oToken->addFilter('IndentStyles', array('style'=>'k&r'));

// Adding the phpDoc filter, asking it to add a license
// at the beginning of the file
$oToken->addFilter('phpDoc', array('license'=>'php'));

// The code is in $sourceCode
// We could also have used the setInputFile method,
// instead of having the code in a variable
$oToken->setInputString($sourcecode);        
$oToken->process();

// And here we get the result, all clean !              
echo $oToken->get();

请注意,我还必须在其中设置两个小东西phpDoc.filter.php,以避免警告和通知...
可以在那里下载相应的补丁:http://extern.pascal-martin.fr/so/phpDoc.filter-pmn.patch http://extern.pascal-martin.fr/so/phpDoc.filter-pmn.patch


Now, if we run that `beautifier-1.php` script :
$ php ./beautifier-1.php

With a MyClass.php最初包含此代码的文件:

class MyClass {
    public function __construct($myString, $myInt) {
        // 
    }
    
    /**
     * Method with some comment
     * @param array $params blah blah
     */
    public function doSomething(array $params = array()) {
        // ...
    }
    
    protected $_myVar;
}

这是我们得到的结果 - 一旦我们的文件被美化:

<?php
/**
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to [email protected] /cdn-cgi/l/email-protection so we can mail you a copy immediately.
 * @category   PHP
 * @package
 * @subpackage Filter
 * @author FirstName LastName <mail>
 * @copyright 2009 FirstName LastName
 * @link
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version    CVS: $Id:$
 */


/**
 * @todo Description of class MyClass
 * @author 
 * @version 
 * @package 
 * @subpackage 
 * @category 
 * @link 
 */
class MyClass {
    
    /**
     * @todo Description of function __construct
     * @param  $myString 
     * @param  $myInt
     * @return 
     */
    public function __construct($myString, $myInt) {
        //
        
    }
    /**
     * Method with some comment
     * @param array $params blah blah
     */
    public function doSomething(array $params = array()) {
        // ...
        
    }
    
    protected $_myVar;
}

我们可以注意到:

  • 文件开头的许可证块
  • 已添加的文档块MyClass class
  • 已添加的文档块__construct method
  • 文档块上doSomething已经存在于我们的代码中:它尚未被删除。
  • 有一些@todo tags ^^

Now, it's not perfect, of course :
  • It doesn't document all the stuff we could want it too
    • 例如,在这里,它没有记录protected $_myVar
  • 它不会增强现有的文档块
  • And it doesn't open the file in any graphical editor
    • 但我想这会更困难......

But I'm pretty sure that this idea could be used as a starting point to something a lot more interesting :
  • About the stuff that doesn't get documented : adding new tags that will be recognized should not be too hard
    • 您只需将它们添加到过滤器开头的列表中
  • 我不得不承认,增强现有的文档块可能会更困难
  • 一件好事是这可以完全自动化
  • 使用 Eclipse PDT,也许可以将其设置为外部工具,所以我们至少可以从我们的 IDE 启动它?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加和完成 PHP 源代码文档的工具 [关闭] 的相关文章

  • PHP 编码风格回归;在开关/外壳中

    我们正在尝试为我们的团队实施新的编码风格指南 当未找到 break 时 php codeniffer 会在 switch case 语句上打印警告 如下所示 switch foo case 1 return 1 case 2 return
  • 我可以使用 HSQLDB 进行 junit 测试克隆 mySQL 数据库吗

    我正在开发一个 spring webflow 项目 我想我可以使用 HSQLDB 而不是 mysql 进行 junit 测试吗 如何将我的 mysql 数据库克隆到 HSQLDB 如果您使用 spring 3 1 或更高版本 您可以使用 s
  • 一次从多个表中删除行

    我正在尝试将 2 个查询合并为一个这样的查询 result db gt query DELETE FROM menu WHERE name new or die db gt error result db gt query DELETE F
  • 从 Laravel 4 输入生成新数组

    我使用 Input all 从动态生成的表单中获取一些输入 我使用 jQuery 来允许用户添加字段 字段名称为 first names last names 和 emails input 变量现在看起来像这样 array size 4 t
  • 如何处理 REST api 中的 php 通知、警告和错误?

    在 REST API 中 200 响应表明操作成功 PHP 默认情况下直接在响应正文中输出错误消息 而不更改响应代码 在 SPA 中 用户无法直接看到响应文本 因此 当应用程序未按预期工作时 我通过 FireBug 检查响应正文 以检查可能
  • 使用PHP从doc、xls文件中读取数据

    我想知道是否可以从 doc 和 xls 文件中读取数据并将 将内容读取到图像文件中 创建文档的页面样本 例如 我有一些文件希望我的客户购买 所以我需要自动创建小图像 例如我的文档样本 我们将不胜感激您的帮助 对于读取 xls 文件 我真的推
  • PHP 检查当前日期是在设定日期之前还是之后

    我从数据库中提取一个日期 其格式为 dd mm YYYY 我想做的是检查当前日期 如果当前日期早于数据库中的日期 则需要打印数据库日期 如果是在之后 则需要打印 继续 有人能指出我正确的方向吗 if strtotime database d
  • 雄辩的第一个 where 子句

    我想知道 Laravel 如何实现雄辩的语法 以便可以静态调用第一个 where 子句User where User where id 23 gt where email email gt first 他们有吗public static f
  • Jquery一键提交多个同名表单

    我有动态创建的循环表单 我需要一键提交所有表单 我正在遵循下面的代码 你能建议我怎么做吗 谢谢
  • Java执行器服务线程池[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 如果我使用 Executor 框架在
  • 如何从终端运行处理应用程序

    我目前正在使用加工 http processing org对于一个小项目 但是我不喜欢它附带的文本编辑器 我使用 vim 编写所有代码 我找到了 pde 文件的位置 并且我一直在从 vim 中编辑它们 然后重新打开它们并运行它们 重新加载脚
  • PHP 表单 - 带验证蜜罐

    我有以下内容 效果很好 但对垃圾邮件机器人开放 我想放入蜜罐 而不是验证码 下面的代码适用于验证姓名 电子邮件 消息 但我无法让它与蜜罐一起工作 任何人都可以查看 蜜罐 代码并告诉我如何修复它吗 我希望表单给出 success2 不允许垃圾
  • Java列表的线程安全

    我有一个列表 它将在线程安全上下文或非线程安全上下文中使用 究竟会是哪一个 无法提前确定 在这种特殊情况下 每当列表进入非线程安全上下文时 我都会使用它来包装它 Collections synchronizedList 但如果不进入非线程安
  • 如何在桌面浏览器上使用 webdriver 移动网络

    我正在使用 selenium webdriver 进行 AUT 被测应用程序 的功能测试自动化 AUT 是响应式网络 我几乎完成了桌面浏览器的不同测试用例 现在 相同的测试用例也适用于移动浏览器 因为可以从移动浏览器访问 AUT 由于它是响
  • simpleframework,将空元素反序列化为空字符串而不是 null

    我使用简单框架 http simple sourceforge net http simple sourceforge net 在一个项目中满足我的序列化 反序列化需求 但在处理空 空字符串值时它不能按预期工作 好吧 至少不是我所期望的 如
  • Firebase 添加新节点

    如何将这些节点放入用户节点中 并创建另一个节点来存储帖子 我的数据库参考 databaseReference child user getUid setValue userInformations 您需要使用以下代码 databaseRef
  • 捕获的图像分辨率太大

    我在做什么 我允许用户捕获图像 将其存储到 SD 卡中并上传到服务器 但捕获图像的分辨率为宽度 4608 像素和高度 2592 像素 现在我想要什么 如何在不影响质量的情况下获得小分辨率图像 例如我可以获取或设置捕获的图像分辨率为原始图像分
  • 当我从 Netbeans 创建 Derby 数据库时,它存储在哪里?

    当我从 netbeans 创建 Derby 数据库时 它存储在哪里 如何将它与项目的其余部分合并到一个文件夹中 右键单击Databases gt JavaDB in the Service查看并选择Properties This will
  • java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置 - Similiar 不回答

    尝试学习 Selenium 我打开了类似的问题 但似乎没有任何帮助 我的代码 package seleniumPractice import org openqa selenium WebDriver import org openqa s
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两

随机推荐

  • 评级栏更改星星颜色而不使用自定义图像

    有什么办法可以改变星星的颜色吗 我不想使用自定义图像来实现它 您可以将这些行添加到创建方法中 RatingBar ratingBar RatingBar findViewById R id ratingBar LayerDrawable s
  • Lua 的标准(或最好支持的)大数(任意精度)库是什么?

    我正在处理大量无法四舍五入的数字 使用 Lua 的标准数学库 似乎没有方便的方法来保持精度超过某些内部限制 我还看到有几个库可以加载以处理大数字 http oss digirati com br luabignum http oss dig
  • EasyMock : java.lang.IllegalStateException: 1 个匹配器预期,2 个记录

    我在使用 EasyMock 2 5 2 和 JUnit 4 8 2 通过 Eclipse 运行 时遇到问题 我已阅读此处所有类似的帖子 但尚未找到答案 我有一个包含两个测试的类 它们测试相同的方法 我正在使用匹配器 每个测试单独运行时都会通
  • MAMP Python-MySQLdb 问题:调用 Python 文件后 libssl.1.0.0.dylib 的路径发生变化

    我正在尝试使用 python MySQLdb 访问 MAMP 服务器上的 MySQL 数据库 当我最初尝试使用 python sql 调用 Python 文件来访问 MAMP 上的数据库时 我得到了image not found关于错误li
  • 如何重命名 bash 函数?

    我正在围绕另一个定义 bash 函数的软件包开发一些方便的包装器 我想用我自己的同名函数替换他们的 bash 函数 同时仍然能够从我的函数中运行他们的函数 换句话说 我需要重命名它们的函数 或者为其创建某种持久别名 当我创建同名函数时 该别
  • 两列宽度可变且它们之间的间隙固定

    我需要动态设置两列的样式 它们各自的宽度应为 50 但它们之间的固定间隙为 10px 当我折叠菜单时 列应加宽至可用空间 并且间隙应保持 10 像素 因此 列不能采用固定宽度 我试过这个 container background red w
  • 如何使用 VB.NET 或 C#.NET 代码从 yahoo 邮件 ID 发送邮件

    我想从我的 yahoomail Id 发送邮件 如何使用 VB NET 或 C NET 代码从 yahoo mail Id 发送邮件 需要善意的帮助 提前谢谢 西瓦库马尔 以下是一些制作基本 html 电子邮件消息的示例 http help
  • 在 NLTK Python 的朴素贝叶斯分类器中使用文档长度

    我正在使用 Python 中的 NLTK 构建垃圾邮件过滤器 现在 我检查单词的出现情况并使用 NaiveBayesClassifier 其准确度为 0 98 垃圾邮件的 F 测量值为 0 92 非垃圾邮件的 F 测量值为 0 98 然而
  • send() 使我的程序崩溃

    我正在运行服务器和客户端 我正在我的计算机上测试我的程序 这是服务器中向客户端发送数据的函数 int sendToClient int fd string msg cout lt lt sending to client lt lt fd
  • SQL 选择与带有通配符的 URL 匹配的行

    我在数据库中有一个表 其中一列包含一个 URL 例如http example com users http example com users 轮廓 我得到了一个 URL 例如http example com users 234 profi
  • coreplot 栏点击不工作

    我从 Github 下载了这段代码 https github com gilthonweapps CorePlotBarChartExample https github com gilthonweapps CorePlotBarChart
  • 如何在开头时解析 json 文件

    我想解析以下 JSON 文件 但以 向我表明这是一个数组 然后继续 对象 我当前的解析器返回一个 JSON 对象 我的问题是 如何修改解析器来解析这个文件 这样解析器将为我提供其他 JSON 文件 从对象或排列开始 JSON 文件 codi
  • 如何使用PHP在服务器端缩小图像?

    我有一些从服务器提取的图像 imgUrl保存图像的路径 现在我用 img src width 100 height 200 或 CSS 来缩小图像 但我想在 PHP 中执行此操作 以便将已缩放的图像提供给 DOM 有任何想法吗 Thanks
  • 检查url图片是否存在

    我正在尝试使用 if 语句检查 url 图像是否存在 然而 当尝试通过错误的图像网址测试它时 它会不断返回 致命错误 在解包可选值时意外发现 nil code var httpUrl subJson image url stringValu
  • BeautifulSoup - 抓取论坛页面

    我正在尝试抓取论坛讨论并将其导出为 csv 文件 其中包含 线程标题 用户 和 帖子 等行 其中后者是每个人的实际论坛帖子 我是 Python 和 BeautifulSoup 的初学者 所以我对此感到非常困难 我当前的问题是 csv 文件中
  • 如何在 iPhone 上缩小 UIPickerView?

    我想降低一个高度UIPickerView在我的 iPhone 应用程序中 使其仅显示一行和一列 选择器视图的高度应等于行的高度 我正在使用 Interface Builder 来构建UIPickerView 但我找不到调整此控件大小的简单方
  • 在 Node js 应用程序中加载backbone.js

    我正在使用node js 和backbone 来构建网络应用程序 Backbone 是我的软件包要求的一部分 我之前使用过 Rails 和 Backbone 辅助 gem 非常适合将需要到达客户端的所有资产 js 文件 拼凑在一起 话虽如此
  • 如何使用 JQuery 提取嵌套 HTML 中的文本?

    我这里有 HTML 代码 div class actResult style border solid table tbody tr td Order Number td td 1 td tr tr td Customer Number t
  • 如何从oracle存储过程中提取out变量?

    我有存储过程 其中有很多输出变量 所以我这样调用存储过程 export const infoHR3 async gt try const sql Declare ln order qty NUMBER ln in proc qty hr N
  • 添加和完成 PHP 源代码文档的工具 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我有几个已完成的较旧的 PHP 项目 其中有很多内容 我想以 javadoc phpDocumentor