请求参数和 phpdoc

发布于 2024-07-30 19:48:16 字数 779 浏览 6 评论 0原文

可能的重复:
是否有记录 GET/POST 参数的标准?

尝试找出通过 phpdoc 以有意义的方式记录请求参数的最佳方法。 具体来说,我有一些 Zend Framework 控制器操作,它们通过 GET/POST 接收参数,但不是功能参数。 这有道理吗?

/**
 * Display a pagination/grid list of rows.
 *
 * @param string $_GET['order']  What field to sort on
 * @param string $_GET['dir']    Direction to sort; either ASC|DESC
 * 
 * @return void
 */
public function listAction()
{
    $order = $this->_request->order;
    ...

如果我为此方法生成文档,则不会表明“order”和“dir”可以通过 url 字符串传递给此方法。 这样做更有意义吗?

@param string $order

我应该使用 @var 吗?

欢迎想法。

Possible Duplicate:
Is there a standard for documenting GET/POST parameters?

Trying to figure out the best way to document request parameters via phpdoc in a manner that makes sense. Specifically, I've got some Zend Framework controller actions that receive parameters via GET/POST, but aren't functional params. Does this make sense?

/**
 * Display a pagination/grid list of rows.
 *
 * @param string $_GET['order']  What field to sort on
 * @param string $_GET['dir']    Direction to sort; either ASC|DESC
 * 
 * @return void
 */
public function listAction()
{
    $order = $this->_request->order;
    ...

If I generated docs for this method, there wouldn't be an indication that "order" and "dir" can be passed via a url string to this method. Would it make more sense to just do

@param string $order

Should I use @var instead?

Thoughts welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-08-06 19:48:16

我会避免与@param搞混。

您还可以创建一个 _validate() 方法以使其在代码中显而易见。 然后您可以使用 _validate() 创建一个用于单元测试的接缝

/**
 * Display a pagination/grid list of rows.
 *
 * Requires $_REQUEST['order'] and $_REQUEST['dir']
 * 
 * @return void
 */
public function listAction()
{
    list($order, $dir) = $this->_validate($this->_request);
    ...

private function _validate($request) {
    if (!$request->order)
         throw new InvalidArgumentException('"Order" must be in request');

    if (!$request->dir)
         throw new InvalidArgumentException('"Dir" must be in request');

    // maybe clean vars???
    //Zend_Filter_Numeric.....

    return array($request->order, $request->dir);
} 

I would avoid mucking with @param.

Also you could make a _validate() method to make it obvious in the code. Then you could use _validate() to create a seam for unit testing.

/**
 * Display a pagination/grid list of rows.
 *
 * Requires $_REQUEST['order'] and $_REQUEST['dir']
 * 
 * @return void
 */
public function listAction()
{
    list($order, $dir) = $this->_validate($this->_request);
    ...

private function _validate($request) {
    if (!$request->order)
         throw new InvalidArgumentException('"Order" must be in request');

    if (!$request->dir)
         throw new InvalidArgumentException('"Dir" must be in request');

    // maybe clean vars???
    //Zend_Filter_Numeric.....

    return array($request->order, $request->dir);
} 
深陷 2024-08-06 19:48:16

我通常要么使用你建议的内容,要么在代码太长时添加一个简单的非 phpdoc 注释,或者什么都不做。

我相信,在这三者之间,您的解决方案是最好的。

您只需检查一件事:生成 phpdoc 时渲染效果是否良好?

理论上,由于 phpdoc 使用您在文档块中给出的名称,我想它应该......

如果是的话......那么,我没有看到更好的方法; 不需要更好的方法:我认为你不能做任何比这更干净/可读/可理解的事情。

我不喜欢这个

@param string $order

想法:没有任何内容表明 $order 应该在 $_GET 中给出,并且不是“真正的方法参数”; 所以我宁愿避免这种语法。

我从不使用 @var 作为参数,顺便说一句:仅用于变量,当我觉得需要记录它们时(这并不常见;至少对于短方法/代码部分)

I generally either use what you proposed, or put a simple non-phpdoc comment when the code is too long, or just do nothing.

Between those three, your solution is the best, I believe.

Only one thing that you should check : does this render nicely when you are generating the phpdoc ?

In theory, as phpdoc uses the names you give in the doc-block, I suppose it should...

If yes... well, I don't see a better way ; not the need for a better way : I don't think you could do anything more clean/readable/understandable than this.

I do not like the

@param string $order

idea : nothing show the $order should be given in $_GET and is not a "real method parameter" ; so I'd rather avoid this syntax.

I never user @var for parameters, btw : only for variables, when I feel the need of documenting them (which is not often ; at least for short methods / parts of code)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文