如何在 Eclipse 中使用 PHPdoc
我们目前正处于一个新项目的开始阶段,希望(这一次)从一开始就尽可能多地发表评论,以帮助未来的发展。
我试图找出在 Eclipse 中使用 phpDoc 的最佳实践,但结果非常有限。
您能分享一下在 Eclipse 中使用 phpDoc 注释内容的最佳实践和技巧吗?
We are currently in the beginning of a new project, and would like to (for once) comment as much as possible from the start to help future development.
I have tried to find out what best practices are of using phpDoc in Eclipse, but with pretty slim results.
Can you share your best practices and tricks of using phpDoc to comment stuff in Eclipse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于应该注释什么以及如何注释,没有“真正的标准”,但是几乎任何注释其代码的人都会使用一些标签。
例如,我通常至少使用:
@param类型名称描述
:用于函数/方法的参数@returns类型
:用于函数/方法的返回值@throws ExceptionType
:如果函数/方法在某些情况下抛出异常@see ..
。 :当我想引用另一个文件或@package
和@subpackage
@property type $name
:它允许 Eclipse PDT 进行自动完成,甚至在魔法属性上也是如此——例如,Doctrine 就使用了它。其中大多数由 Eclipse PDT 使用来帮助您编码(尤其是
@param
);但请随意添加一些 Eclipse PDT 未使用的内容:如果您从代码生成文档,它总是有用的;-)我能给您的最好建议是查看一些大型应用程序和/或框架的源代码(Zend Framework、Doctrine,...),看看它们的代码是如何的评论说——他们很可能正在使用一些被广泛接受的东西。
例如,如果您查看 Zend Framework 代码,您可以找到类的类似内容:
方法的类似内容:
无论如何,最重要的是保持一致:团队中的每个成员都应该以相同的方式发表评论,遵循相同的约定。
There is no "real standard" about what should be commented and how, but some tags are used by pretty much anyone who comments his code.
For instance, I generally use at least :
@param type name description
: for the parameters of functions/methods@returns type
: for the return value of functions/methods@throws ExceptionType
: if the functions/methods throws an Exception under some circumstances@see ..
. : when I want to make a reference to either another file, or an URL that gives more informations@package
and@subpackage
@property type $name
: it allows Eclipse PDT to do auto-completion, even on magic properties -- Doctrine uses this, for instance.Most of those are used by Eclipse PDT to help you when coding (especially
@param
) ; but feel free to add some that are not used by Eclipse PDT : if you generate the documentation from your code, it can always be useful ;-)The best advice I can give you would be to take a look at the source-code of some big applications and/or frameworks (Zend Framework, Doctrine, ...), to see how their code is commented -- chances are they are using something that's well accepted.
For instance, if you take a look at Zend Framework code, you can find stuff like this for a class :
And like this for a method :
Anyway, the most important thing is to be consistent : every member of your team should comment the same way, follow the same conventions.
至少,我至少会坚持 Eclipse 根据您的代码自动插入的最小 phpdoc 标签。
我要努力实现的第二个最低水平是让 PhpDocumentor 本身满意。针对代码运行 PhpDocumentor 后,请在文档根目录中查找 error.html 页面。这将列出 PhpDocumentor 不喜欢的任何内容,例如没有文件级文档块。您可以努力将错误列表减少到零。
您可以努力达到的第三个级别是满足 PEAR [1] 的 PHP_CodeSniffer 应用程序中包含的任何一项编码标准。这里的一个缺点是这些标准更具体地关注代码本身,但所有标准都包含有关代码文档的规则。
[1] -- http://pear.php.net/package/PHP_CodeSniffer
At a bare minimum, I'd at least stick with what minimal phpdoc tags are automatically inserted by Eclipse based on your code.
The second minimal level I'd strive for would be to keep PhpDocumentor itself happy. Once you run PhpDocumentor against your code, look for the errors.html page in the root of your docs. This will list anything PhpDocumentor doesn't like, such as not having file-level docblocks. You could strive to bring that list of errors down to zero.
The third level you could strive to reach would be satisfying any one of the coding standards included in the PHP_CodeSniffer application at PEAR [1]. A drawback here is that these standards more specifically focus on the code itself, but all of the standards do include rules regarding code documentation.
[1] -- http://pear.php.net/package/PHP_CodeSniffer