【问题标题】:How to create new phpDoc annotations in PhpStorm 8如何在 PhpStorm 8 中创建新的 phpDoc 注释
【发布时间】:2015-01-16 00:31:49
【问题描述】:

我通过 Google 进行了搜索,但找不到此问题的答案。 PhpStorm 有许多用于代码完成的内置注释,但也缺少许多注释。我很确定有一种方法可以创建新注释,但在设置中的任何地方都找不到它。或者它可能是某个地方的 XML 文件。 NetBeans 支持此功能。

换句话说,我如何在 phpStorm 8 中创建新的 phpDoc 注释以完成,例如 phpUnit 的 @usesDefaultClass

【问题讨论】:

  • @usesDefaultClass 是什么?
  • @uses 是 phpUnit 中的一个新注解,用于指示严格模式下的代码覆盖率收集器将调用此方法,但未在测试中。 @usesDefaultClass 是为所有@uses 注解所引用的类指定完整的类名(除非它们有完整的命名空间)。
  • 我不明白的是,“为完成创建新的 phpDoc 注释” 到底是什么意思? -- 帮助 IDE 中的 @tag 完成 .. 或将该标记后面的文本作为类名,以便 IDE 帮助完成类名 .. 还是什么?在任何情况下:标签列表和特定位置都不能从外部来源告诉 IDE——这种“支持”必须是硬编码的。所以请在问题跟踪器中提交一张新票,地址为youtrack.jetbrains.com/issues/WI
  • 两者都很好,而且 NetBeans 支持此功能,因此我认为 PhpStorm 也有此功能。我将为它创建一张票,因为它需要编码。发表您的评论作为答案,我会接受它作为正确的答案。
  • 它到底支持多少?有什么例子吗? NetBeans 是如何知道新的@tag 以及在它之后应该有类名的?

标签: phpstorm phpdoc


【解决方案1】:

我在这里找到了这个答案:http://blog.jetbrains.com/webide/2013/02/phpdoc_and_code_templates/ 希望对你有帮助

PhpDoc 模板

PhpDoc 模板位于 Settings |文件模板 |包括。目前有 PHP 类文档注释、PHP 函数文档注释和 PHP 字段文档注释三个模板。这些中的任何一个都可以通过#parse 指令包含到其他模板中。比如我们可以修改原来的类模板(Templates | PHP Class),在生成类的时候也包含类PHP Doc:

<?php
#parse("PHP File Header.php")
...
#parse("PHP Class Doc Comment")
class ${NAME} {
}

在以下情况下使用相同的模板:

我们在类、函数(方法)或类字段前输入 /** 并按 Enter 后会生成一个新的 PHP Doc 注释。 我们调用代码 |生成 | PHPDoc 阻止或使用 Add PHP Doc 快速修复来检查 Missing PHP Doc。 下面是可以在 PHP Doc 模板中使用的变量列表:

${NAME}

The element (class, function, field) name.
${NAMESPACE}

The name of the namespace the element belongs to without any leading or trailing backslashes, for example Core\Widgets. The variable value is empty if the element doesn’t belong to any namespace. A check like `#if (${NAMESPACE})` ... is possible.
${CLASS_NAME}

Contains a class name for class methods and fields. Will be empty for functions that do not belong to any class.
${TYPE_HINT}

For functions (methods), contains the return type of the function (method). For fields, evaluates to the field’s type if it can be found, for example, from a default value. Will be empty if the type cannot be retrieved from the code.
${STATIC}

Takes the value of “static” if the function or field is static, otherwise, an empty string. We can use this variable with the condition `#if (${STATIC})` ... to generate something specific for static class members.
${CARET}

Marks the position where the editor caret should be placed after the comment is added. Note: Works only if the comment is added after we type “/**” and hit Enter. Should be placed inside the comment, not on the first line /** or on the last line */. In all other cases the caret marker will be ignored.
${PARAM_DOC}

A generated PHP Doc fragment containing function (method) parameters in the form: “* @param type $name“. For example, if the function signature is foo ($x, $y), it will evaluate to:
  • @param $x
  • @param $y

    ${THROWS_DOC}

    生成的 PHP 文档片段包含从函数(方法)主体抛出的异常,格式为 * @throws ExceptionName。每行/@throws 标记一个例外。例如:

    • @throws DOMException
    • @throws HttpException

覆盖/实现方法的代码模板

以下模板可以在设置 | 下找到文件模板 |代码:PHP 实现的方法体和 PHP 覆盖的方法体。考虑到在大多数情况下,我们需要对父方法进行简单调用或只需要我们自己的注释(可能是 TODO 的某些版本),因此参数很少:

${NAME}

Method name.
${PARAM_LIST}

A comma-separated list of parameters. For example, if the original method signature is foo(Bar $bar, $y), the variable will take the value “$bar, $x” which can be used in a call to the parent method as `${NAME}(${PARAM_LIST})`”
${RETURN}

Either “return” or an empty string.

美元符号变量:${DS}

解决了将美元符号 $ 放在模板中的任何位置的问题。实际上,美元符号在 PHP 和 Velocity 模板引擎中都使用,负责幕后的代码生成。因此,每当我们需要美元符号时,只需使用 ${DS} 作为它的等价物。例如,如果我们要生成$this-&gt;foo(),则需要放入${DS}this-&gt;foo()。这可能看起来并不完美,但保证不会发生冲突。

【讨论】:

  • 感谢您的广泛回答,但问题是关于 phpDoc 注释/标签完成,而不是关于 PhpStorm 中的模板。
  • 我认为你可以使用自定义模板来做到这一点,但我认为你可以使用这个插件来做到这一点:plugins.jetbrains.com/plugin/7320
  • 自定义注释是一个 TODO,看起来在 phpStorm 中根本不可能。 :(
猜你喜欢
  • 2010-10-05
  • 2011-06-21
  • 1970-01-01
  • 2010-12-17
  • 2011-11-16
  • 2016-12-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多