We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
我认为
PHP_Codesniffer
可以指示何时没有 docblock - 请参阅 此页面 (引用其中之一):我想您可以使用 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
选项,例如:在输出中间给出这个:
但是,在这里,即使它很有用作为一个报告工具,它在生成丢失的文档块时并没有那么有帮助......
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 提供了为方法预先生成文档块的功能)。
除此之外,我真的不知道有任何全自动工具来生成文档块......但我很确定我们可以使用以下任一方法创建一个有趣的工具:
token_get_all
解析 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"*
这个想法实际上还不错:
PHP_Beautifier
工具非常好当涉及到格式化一些格式不正确的 PHP 代码时,它非常强大PHP_Beautifier_Filter
了解提供的过滤器列表我链接到的博客文章中使用的想法是:
T_CLASS
T_FUNCTION
T_INTERFACE
To run the tool on some `MyClass.php` file, I've had to first install `PHP_Beautifier` :
然后,将过滤器下载到我正在工作的目录(当然可以将其放在默认目录中):
之后,我创建了一个新的
beautifier-1.php
脚本 >(再次基于我链接到的博客文章中的建议),这将:MyClass.php
文件的内容PHP_Beautifier
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)*
请注意,我还必须在 phpDoc.filter.php 中设置两个小东西的路径,以避免出现警告和通知...
相应的补丁可以在那里下载:http://extern .pascal-martin.fr/so/phpDoc.filter-pmn.patch
Now, if we run that `beautifier-1.php` script :
使用初始包含此代码的
MyClass.php
文件:这是我们得到的结果 - 一旦我们文件被美化了:
我们可以注意到:
MyClass
类上添加的docblock__construct
方法doSomething
上的 docblock 已经存在于我们的代码中:它尚未被删除。@todo
标签 ^^Now, it's not perfect, of course :
受保护的 $_myVar
But I'm pretty sure that this idea could be used as a starting point to something a lot more interesting :
I think
PHP_Codesniffer
can indicate when there is no docblock -- see the examples of reports on this page (quoting one of those) :I suppose you could use PHP_Codesniffer to at least get a list of all files/classes/methods that don't have a documentation; from what I remember, it can generate XML as output, which would be easier to parse using some automated tool -- that could be the first step of some docblock-generator ;-)
Also, if you are using [phpDocumentor](http://www.phpdoc.org/) to generate the documentation, can this one not report errors for missing blocks ?
After a couple of tests, it can -- for instance, running it on a class-file with not much documentation, with the
--undocumentedelements
option, such as this :Gives this in the middle of the output :
But, here, too, even if it's useful as a reporting tool, it's not that helpful when it comes to generating the missing docblocks...
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...
... Which works pretty fine : there is generally not more than a couple of missing docblocks every day, so the task can be done by hand (and Eclipse PDT provides a feature to pre-generate the docblock for a method, when you are editing a specific file/method).
Appart from that, I don't really know any fully-automated tool to generate docblocks... But I'm pretty sure we could manage to create an interesting tool, using either :
token_get_all
to parse the source of a PHP file.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 idea is actually not bad :
PHP_Beautifier
tool is pretty nice and powerful, when it comes to formating some PHP code that's not well formatedPHP_Beautifier_Filter
for a list of provided filtersThe idea that's used in the blog-post I linked to is to :
T_CLASS
T_FUNCTION
T_INTERFACE
To run the tool on some `MyClass.php` file, I've had to first install `PHP_Beautifier` :
Then, download the filter to the directory I was working in (could have put it in the default directory, of course) :
And, after that, I created a new
beautifier-1.php
script (Based on what's proposed in the blog-post I linked to, once again), which will :MyClass.php
filePHP_Beautifier
phpDoc
filter we just downloadedThe 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)*
Note that I also had to path two small things in
phpDoc.filter.php
, to avoid a warning and a notice...The corresponding patch can be downloaded there : http://extern.pascal-martin.fr/so/phpDoc.filter-pmn.patch
Now, if we run that `beautifier-1.php` script :
With a
MyClass.php
file that initialy contains this code :Here's the kind of result we get -- once our file is Beautified :
We can note :
MyClass
class__construct
methoddoSomething
was already present in our code : it's not been removed.@todo
tags ^^Now, it's not perfect, of course :
protected $_myVar
But I'm pretty sure that this idea could be used as a starting point to something a lot more interesting :
由于已经提到了 PHPCS,我引入了 Reflection API 来检查是否缺少 DocBlock。下面链接的文章是关于如何解决您的问题的简短教程:
还有一个 PEAR 包
PHP_DocBlockGenerator< /code>
可以为包含、全局变量、函数、参数、类、常量、属性和方法(以及其他事物)创建文件页面块和文档块。
Since PHPCS was already mentioned, I throw in the Reflection API to check for missing DocBlocks. The article linked below is a short tutorial on how you could approach your problem:
There is also a PEAR Package
PHP_DocBlockGenerator
that can Create the file Page block and the DocBlocks for includes, global variables, functions, parameters, classes, constants, properties and methods (and other things).php-tracer-weaver 可以检测代码并生成具有参数类型的文档块,通过推导运行时分析。
php-tracer-weaver can instrument code and generate docblocks with the parameter types, deducted through runtime analysis.
您可以使用 PHP 的 Code Sniffer 根据一组预定义的编码准则来测试您的代码。它还将检查丢失的文档块并生成可用于识别文件的报告。
You can use the Code Sniffer for PHP to test your code against a predefined set of coding guidelines. It will also check for missing docblocks and generate a report you can use to identify the files.
phpDocumentor 的 1.4.x 版本具有 -ue 选项(--undocumentedelements)[1],这将导致未记录的元素在其文档运行期间生成的 error.html 页面上列为警告。
此外,PEAR 的 PHP_DocBlockGenerator [2] 看起来可以为您生成缺失的文档块。
[1] -- http ://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#using.command-line.undocumentedelements
[2] -- http://pear.php.net/package/PHP_DocBlockGenerator
The 1.4.x versions of phpDocumentor have the -ue option (--undocumentedelements) [1], which will cause undocumented elements to be listed as warnings on the errors.html page that it generates during its doc run.
Further, PHP_DocBlockGenerator [2] from PEAR looks like it can generate missing docblocks for you.
[1] -- http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#using.command-line.undocumentedelements
[2] -- http://pear.php.net/package/PHP_DocBlockGenerator
我们在工作中使用 codeniffer 来实现此功能,并使用标准 PEAR 或 Zend 标准。它不允许您即时编辑文件,但肯定会给您一个列表,其中包含缺少哪种文档块的行和描述。
哈特哈,
Jc
We use codesniffer for this functionality at work, using standard PEAR or Zend standards. It will not allow you to edit the files on the fly, but will definitely give you a list, with lines and description of what kind of docblock is missing.
HTH,
Jc
不知道这是否有帮助,但如果 Codesniffer 可以指出函数/方法,那么一个像样的 PHP IDE(我提供 PHPEd)可以轻松检查和构建每个函数的 PHPDoc 注释。
只需在每个函数上方输入
/**
并按 ENTER,PHPEd 就会自动用@param1
、@param1
、@return
等正确填写,准备好您的额外描述。这是我为了提供示例而尝试的第一个:这很容易调整为:
不完全是一个自动化解决方案,但对于我这个懒惰的开发人员来说足够快了:)
No idea if it's any help, but if Codesniffer can point out the functions/methods, then a decent PHP IDE (I offer PHPEd) can easily inspect and scaffold the PHPDoc comments for each function.
Simply type
/**
above each function and press ENTER, and PHPEd will auto-complete the code with@param1
,@param1
,@return
, etc. filled out correctly, ready for your extra descriptions. Here's the first one I tried in order to provide an example:This is easily tweaked to:
Not exactly an automated solution, but quick enough for me as a lazy developer :)
您想真正自动化填写“javadoc”类型数据的问题吗?
可以配置 DMS 软件重新工程工具包来执行此操作。
它像编译器一样解析源文本,构建内部编译器结构,让您实现任意分析,对这些结构进行修改,然后重新生成(“prettyprint”)根据结构更改更改的源文本。它甚至保留了原始文本的注释和格式;您当然可以插入其他评论,它们就会出现,这似乎是您的主要目标。 DMS 为许多语言执行此操作,包括 PHP
您想要做的是解析每个 PHP 文件,找到每个类/方法,生成应该是该实体的“javadoc”注释(类和方法的区别,对吧?),然后检查编译器结构中是否实际存在相应的注释。如果没有,只需插入它们即可。 PrettyPrint 最终结果。
因为它可以访问表示代码的编译器结构,所以按照您的建议生成参数和返回信息应该不难。当然,它不能生成有关预期目的的注释;但它可以生成一个占位符供您稍后填写。
You want to actually automate the problem of filling in the "javadoc" type data?
The DMS Software Reengineering Toolkit could be configured to do this.
It parses source text just like compilers do, builds internal compiler structures, lets you implement arbitrary analyses, make modification to those structures, and then regenerate ("prettyprint") the source text changed according to the structure changes. It even preserves comments and formatting of the original text; you can of course insert additional comments and they will appear and this seems to be your primary goal. DMS does this for many languages, including PHP
What you would want to do is parse each PHP file, locate every class/method, generate the "javadoc" comments that should be that entity (difference for classes and methods, right?) and then check that corresponding comments were actually present in the compiler structures. If not, simply insert them. PrettyPrint the final result.
Because it has access to the compiler structures that represent the code, it shouldn't be difficult to generate parameter and return info, as you suggested. What it can't do, of course, is generate comments about intendend purpose; but it could generate a placeholder for you to fill in later.
最近,我必须对文档块修复进行大量自动化,主要基于上面的正确答案以及一些特定于上下文的更改。这是一个 hack,但我会链接回此处,以防将来对其他人有用。本质上,它对 PHP Beautifier 中的注释块标记进行基本解析。
https://gist.github.com/israelshirk/408f2656100196e73367
I had to do a large batch of automation of docblock fixing recently, mostly based on the correct answer above kwith some context-specific changes. It's a hack, but I'm linking back here in case it's useful to anyone else in the future. Essentially, it does basic parsing on comment block tokens within PHP Beautifier.
https://gist.github.com/israelshirk/408f2656100196e73367