0

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

3
  • The $astTreeData parameter is an array, but you're not saying an array of what. I'd say it's that, but I can't reproduce it on my end Commented Jul 6, 2022 at 7:01
  • OK. Maybe you mean something like `/** * @param array<int, array<int, array>> $astTreeData */ Commented Jul 6, 2022 at 7:09
  • Yeah or array<mixed> or something, I don't know what's in there Commented Jul 6, 2022 at 7:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.