PHPDoc 应该如何回调?

发布于 2024-10-16 02:36:57 字数 1298 浏览 3 评论 0原文

我有一种方法,可以从数据库中提取修改后的预序树横向树,并使用回调函数对其进行过滤。例如:

/**
 * Recursive function for building the Cas_Template_TreeNode.
 *
 * @static
 * @param array $rows
 * @param callback $filter A function to filter the tree by (return a value convertible to false to remove the item from the tree)
 * @return array
 */
private static function MakeTreeGivenDbRows($rows, $filter = null)
{
    if ($filter === null)
    {
        $filter = function($unused)
        {
            return true;
        };
    }
    $result = array();
    $childrenCount = 0;
    for ($idx = 0; $idx < count($rows); $idx += $childrenCount + 1)
    {
        $current = $rows[$idx];
        $childrenCount = self::ChildrenCountFromRow($current);
        if (!$filter($current))
        {
            continue;
        }
        $childrenStartAt = $idx + 1;
        $childRows = array_slice($rows, $childrenStartAt, $childrenCount);
        $children = self::MakeTreeGivenDbRows($childRows, $filter);
        $result[] = new Cas_Template_TreeNode(self::MakeNodeGivenDbRow($current), $children);
    }
    if (empty($result))
    {
        return null;
    }
    return $result;
}

我不确定变量 $filter 的 PHPDoc 应该是什么——它是一个回调,这就是我所指出的,但我不确定这是否正确。

另外,如果对此代码中的质量(或缺乏质量)有任何其他评论,我们将不胜感激:)

I have a method which extracts a modified preorder tree transversal tree from the database, and filters that using a callback function. For example:

/**
 * Recursive function for building the Cas_Template_TreeNode.
 *
 * @static
 * @param array $rows
 * @param callback $filter A function to filter the tree by (return a value convertible to false to remove the item from the tree)
 * @return array
 */
private static function MakeTreeGivenDbRows($rows, $filter = null)
{
    if ($filter === null)
    {
        $filter = function($unused)
        {
            return true;
        };
    }
    $result = array();
    $childrenCount = 0;
    for ($idx = 0; $idx < count($rows); $idx += $childrenCount + 1)
    {
        $current = $rows[$idx];
        $childrenCount = self::ChildrenCountFromRow($current);
        if (!$filter($current))
        {
            continue;
        }
        $childrenStartAt = $idx + 1;
        $childRows = array_slice($rows, $childrenStartAt, $childrenCount);
        $children = self::MakeTreeGivenDbRows($childRows, $filter);
        $result[] = new Cas_Template_TreeNode(self::MakeNodeGivenDbRow($current), $children);
    }
    if (empty($result))
    {
        return null;
    }
    return $result;
}

I'm not sure what the PHPDoc should be for the variable $filter -- it's a callback, which is what I've indicated, but I'm not sure if that's correct.

Also, any other comments on the quality (or lack thereof) in this code would be appreciated :)

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

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

发布评论

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

评论(4

停顿的约定 2024-10-23 02:36:57

正确的类型提示是callable,它在例如PhpStorm中已经可用很长时间了,并且是PSR-5 目前正在规范中。

(我很惊讶没有其他人提到callable,它不是什么新鲜事,多年来一直在各地使用 - 据我所知,callback 不是也从来不是定义的 PHP 伪类型

请注意可调用不仅包括闭包,还包括“老派”PHP 回调,例如 array($object, 'methodName')array('ClassName', 'methodName') 以及甚至 'function_name' - 为了最大限度地发挥 API 的实用性,您应该涵盖所有这些用例,这很容易,因为 call_user_funccall_user_func_array 支持所有四种类型可调用对象:字符串形式的函数名称、对象/方法名称、类/方法名称和闭包。

The correct type-hint is callable, which has been available in e.g PhpStorm for a long time, and is part of PSR-5 which is currently under specification.

(I'm surprised nobody else mentioned callable, it's nothing new and has been used everywhere for years - to my knowledge, callback is not and never was a defined PHP pseudo-type)

Note that callable includes not just closures, but also "old school" PHP callbacks, e.g. array($object, 'methodName') or array('ClassName', 'methodName') and even 'function_name'- to maximize the usefulness of your API, you should cover all of those use-cases, which is quite easy, since both call_user_func and call_user_func_array support all four varieties of callables: function-name as string, object/method-name, class/method-name and closure.

如此安好 2024-10-23 02:36:57

“回调”确实可以作为 phpDocumentor 中的有效数据类型...我刚刚验证了它...至少使用 PHP 5.2.4。 “有效数据类型”可能取决于您运行 phpDocumentor 的 PHP 版本。

"callback" does work as a valid datatype in phpDocumentor... I just verified it... using PHP 5.2.4, at least. It's feasible that "valid datatypes" depend on the PHP version you run phpDocumentor with.

烟若柳尘 2024-10-23 02:36:57

phpDoc 并没有真正指定接受的变量类型是什么,只是它们应该是有效的 php 变量类型的名称。在这种情况下,“回调”是正确的。这是 PHP 中伪类型的名称。

phpDoc doesn't really specify what the accepted variable types are, just that they should be the names of valid php variable types. In this case "callback" would be correct. That is the name of the pseudo-type in PHP.

你げ笑在眉眼 2024-10-23 02:36:57

@param 标记的文档似乎已经很久没有更新了,因此它没有说明任何有关闭包甚至使用 create_function() 进行的回调的内容。

如果 phpDocumentor 拒绝将callback识别为数据类型,则必须使用mixed

The documentation for the @param tag doesn't seem to have been updated in eons, so it doesn't say anything about closures or even callbacks made with create_function().

If phpDocumentor refuses to recognize callback as a data type, you'll have to go with mixed.

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