如何理解此PHPSTAN错误(具有参数$ asttreedata,而没有在Itable类型阵列中指定的值类型。)

发布于 2025-02-13 09:06:34 字数 2346 浏览 0 评论 0原文

假设我具有这样的格式函数:

function prepareValue(mixed $value): string
{
    ...
    return "{$value}";
}

/**
 * @param array<int, array> $astTreeData
 * @return string
 */
function makeFormattedDiff(array $astTreeData): string
{
    $statusTree = [
        'added' => function (string $path, array $node) {
            {some code...}
            return "Property '{$path}' was added with value: {$value}";
        },
        'deleted' => fn($path) => "Property '{$path}' was removed",
        'nested' => function (string $path, array $node, callable $iterRender) {
            {some code...}
            return $iterRender($children, $path);
        },
        'changed' => function (string $path, array $node) {
            {some code...}
            return "Property '{$path}' was updated. From {$valueBefore} to {$valueAfter}";
        },
        'unchanged' => fn() => [],
    ];

    /**
     * @param array<int, array> $diff
     * @return string
     */
    $renderPlainDiff = function (array $diff, string|bool $pathComposition) use (&$renderPlainDiff, $statusTree) {
        $diffCopy = $diff;
        $lines = array_reduce(
            $diffCopy,
            /**
             * @param array<int, string> $acc
             * @param array<string, array> $node
             * @param array|null $initial
             * @return array
             */
            function (array $acc, array $node, array|null $initial = []) use (
                $renderPlainDiff,
                $pathComposition,
                $statusTree
            ) {
                {some code...}
                $diffTypeHandler = $statusTree[$status];
                return flatten([$acc, $diffTypeHandler($newPath, $node, $renderPlainDiff)]);
            },
            []
        );

        return implode("\n", $lines);
    };

    return $renderPlainDiff($astTreeData, false);
}

所有参数和返回值均用phpdoc详细指定,键入提示语法,但是phpstan给出了一个错误消息phpstan:函数差异\ formatters \ plainformatter \ plainformatter \ plainformatter \ make formatteddiff()具有parameter $ asttreedata with with parameter $ asttreedata with paramtreet $ asttreedata。在Iterable类型数组中没有指定的值类型

需要描述哪些迭代类型数组值?

在此处输入图像描述

Say I have a formatter function like this:

function prepareValue(mixed $value): string
{
    ...
    return "{$value}";
}

/**
 * @param array<int, array> $astTreeData
 * @return string
 */
function makeFormattedDiff(array $astTreeData): string
{
    $statusTree = [
        'added' => function (string $path, array $node) {
            {some code...}
            return "Property '{$path}' was added with value: {$value}";
        },
        'deleted' => fn($path) => "Property '{$path}' was removed",
        'nested' => function (string $path, array $node, callable $iterRender) {
            {some code...}
            return $iterRender($children, $path);
        },
        'changed' => function (string $path, array $node) {
            {some code...}
            return "Property '{$path}' was updated. From {$valueBefore} to {$valueAfter}";
        },
        'unchanged' => fn() => [],
    ];

    /**
     * @param array<int, array> $diff
     * @return string
     */
    $renderPlainDiff = function (array $diff, string|bool $pathComposition) use (&$renderPlainDiff, $statusTree) {
        $diffCopy = $diff;
        $lines = array_reduce(
            $diffCopy,
            /**
             * @param array<int, string> $acc
             * @param array<string, array> $node
             * @param array|null $initial
             * @return array
             */
            function (array $acc, array $node, array|null $initial = []) use (
                $renderPlainDiff,
                $pathComposition,
                $statusTree
            ) {
                {some code...}
                $diffTypeHandler = $statusTree[$status];
                return flatten([$acc, $diffTypeHandler($newPath, $node, $renderPlainDiff)]);
            },
            []
        );

        return implode("\n", $lines);
    };

    return $renderPlainDiff($astTreeData, false);
}

All parameters and return values are specified in detail with phpdoc and type hinting syntax but phpStan gives an error message phpstan: Function Differ\Formatters\plainFormatter\makeFormattedDiff() has parameter $astTreeData with no value type specified in iterable type array

What iterable type array values need to be described?

enter image description here

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文