首页 > 解决方案 > PhpDoc 生成警告

问题描述

以下文档中生成的警告消息是在文档内所有方法中看到的消息。

警告:count():参数必须是在 C:\php72\pear\phpDocumentor\vendor\twig\twig\lib\Twig\Extension\Core.php 中实现 Countable 的数组或对象,第 1198 行警告:count() : 参数必须是在 C:\php72\pear\phpDocumentor\vendor\twig\twig\lib\Twig\Extension\Core.php 中实现 Countable 的数组或对象,第 1198 行在此处输入图像描述

标签: warningsmessagesphpdoc

解决方案


我为纠正同样的错误付出了很多努力,所以我希望在这里分享完整的解决方案:要更改什么以及如何编辑 phar 文件:

将此脚本复制到您的 phar 文件夹中的文件中,并使用 php 命令行运行它,并在代码中指定选项:

<?php
//to be run with php -d phar.readonly=Off -f thisFileName 
if (ini_get('phar.readonly')==1){
    die("\n\nThis script must be run with option -d phar.readonly=Off so that it can write the .phar file\n\n"); 
}

//the file you want to change
$file = 'vendor/twig/twig/lib/Twig/Extension/Core.php';

//the function in the file you want to change
$oldFunction = '#function twig_length_filter[^{]*[^}]*}#'; //assuming it's still the actual one line function, regex might need updates in the furure

//the replacement function
$newFunction = <<<'funct'
function twig_length_filter(Twig_Environment $env, $thing)
    {
        if (null === $thing) {
        return 0;
        }
        if (is_scalar($thing)) {
            return mb_strlen($thing, $env->getCharset());
        }
        if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
            return mb_strlen((string) $thing, $env->getCharset());
        }
        if ($thing instanceof \Countable || is_array($thing)) {
            return count($thing);
        }
        return 1;
    }
funct;

//access the phar
$p = new Phar('phpDocumentor.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME);
//extract the file
$p->extractTo('./', $file, true);

//return here if you want to check the file first
//return;

//change the function 
file_put_contents($file,
    $newFile = preg_replace(
            $oldFunction, 
            $newFunction, 
            file_get_contents($file)
        )
    );

//update the file
$p[$file] = $file;

//done
echo 'Done. Don\'t forget to delete the "vendor" folder extracted from the phar !';

推荐阅读